From 36d2bb332b87900e8178f2ec54d2cfda07f075db Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Wed, 28 Dec 2022 00:05:31 +0900 Subject: Add transform-query(...) action Test case authored by @SpicyLemon Close #1930 Close #2465 Close #2559 Close #2509 (e.g. fzf --bind 'space:transform-query:printf %s%s {q} {}') --- src/options.go | 4 +++- src/terminal.go | 26 +++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/options.go b/src/options.go index f25b6e73..1288fbe4 100644 --- a/src/options.go +++ b/src/options.go @@ -890,7 +890,7 @@ const ( func init() { executeRegexp = regexp.MustCompile( - `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put)`) + `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`) splitRegexp = regexp.MustCompile("[,:]+") actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+") } @@ -1207,6 +1207,8 @@ func isExecuteAction(str string) actionType { return actExecuteMulti case "put": return actPut + case "transform-query": + return actTransformQuery } return actIgnore } diff --git a/src/terminal.go b/src/terminal.go index 68a099cf..2d18f791 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -308,6 +308,7 @@ const ( actToggleSort actTogglePreview actTogglePreviewWrap + actTransformQuery actPreview actChangePreview actChangePreviewWindow @@ -2014,10 +2015,11 @@ func (t *Terminal) redraw(clear bool) { t.printAll() } -func (t *Terminal) executeCommand(template string, forcePlus bool, background bool) { +func (t *Terminal) executeCommand(template string, forcePlus bool, background bool, captureFirstLine bool) string { + line := "" valid, list := t.buildPlusList(template, forcePlus) if !valid { - return + return line } command := t.replacePlaceholder(template, forcePlus, string(t.input), list) cmd := util.ExecCommand(command, false) @@ -2033,11 +2035,21 @@ func (t *Terminal) executeCommand(template string, forcePlus bool, background bo t.refresh() } else { t.tui.Pause(false) - cmd.Run() + if captureFirstLine { + out, _ := cmd.StdoutPipe() + reader := bufio.NewReader(out) + cmd.Start() + line, _ = reader.ReadString('\n') + line = strings.TrimRight(line, "\r\n") + cmd.Wait() + } else { + cmd.Run() + } t.tui.Resume(false, false) } t.executing.Set(false) cleanTemporaryFiles() + return line } func (t *Terminal) hasPreviewer() bool { @@ -2610,9 +2622,9 @@ func (t *Terminal) Loop() { switch a.t { case actIgnore: case actExecute, actExecuteSilent: - t.executeCommand(a.a, false, a.t == actExecuteSilent) + t.executeCommand(a.a, false, a.t == actExecuteSilent, false) case actExecuteMulti: - t.executeCommand(a.a, true, false) + t.executeCommand(a.a, true, false, false) case actInvalid: t.mutex.Unlock() return false @@ -2635,6 +2647,10 @@ func (t *Terminal) Loop() { t.previewed.version = 0 req(reqPreviewRefresh) } + case actTransformQuery: + query := t.executeCommand(a.a, false, true, true) + t.input = []rune(query) + t.cx = len(t.input) case actToggleSort: t.sort = !t.sort changed = true -- cgit v1.2.3