Add execute-silent action
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #823
authored by Junegunn Choi · committed by Junegunn Choi · parent ed57dcb · Browse files · 5 files·+93 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+93 added−55 removed
- other:+93 added−55 removed
Provenance
- Commit
421b9b2- Tree
a79c584- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ed57dcb
5 files changed+93 added−55 removed
modified:man/man1/fzf.1+5 added-4 removed
467 unmodified lines468 \fBdeselect-all\fR469 \fBdown\fR \fIctrl-j ctrl-n down\fR470 \fBend-of-line\fR \fIctrl-e end\fR471 \fBexecute(...)\fR (see below for the details)472+ \fBexecute-silent(...)\fR (see below for the details)473 \fRexecute-multi(...)\fR (deprecated in favor of \fB{+}\fR expression)474 \fBforward-char\fR \fIctrl-f right\fR475 \fBforward-word\fR \fIalt-f shift-right\fR476 \fBignore\fR61 unmodified lines538 the closing character. The catch is that it should be the last one in the539 comma-separated list of key-action pairs.540 .RE541 541-\fBexecute-multi(...)\fR is an alternative action that executes the command542-with the selected entries when multi-select is enabled (\fB--multi\fR). With543-this action, \fB{}\fR is replaced with the quoted strings of the selected544-entries separated by spaces.542+fzf switches to the alternate screen when executing a command. However, if the543+process is expected to complete quickly, and you are not interested in its544+output, you might want to use \fBexecute-silent\fR instead, which silently545+executes the command without switching.546 547 .SH AUTHOR548 Junegunn Choi (\fIjunegunn.c@gmail.com\fR)549 15 unmodified linesmodified:src/options.go+33 added-25 removed
579 unmodified lines580 escapedComma = 1581 escapedPlus = 2582 )583 584+func init() {585+ // Backreferences are not supported.586+ // "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|')587+ executeRegexp = regexp.MustCompile(588+ "(?si):(execute(?:-multi|-silent)?):.+|:(execute(?:-multi|-silent)?)(\\([^)]*\\)|\\[[^\\]]*\\]|~[^~]*~|![^!]*!|@[^@]*@|\\#[^\\#]*\\#|\\$[^\\$]*\\$|%[^%]*%|\\^[^\\^]*\\^|&[^&]*&|\\*[^\\*]*\\*|;[^;]*;|/[^/]*/|\\|[^\\|]*\\|)")589+}590+591 func parseKeymap(keymap map[int][]action, str string) {585- if executeRegexp == nil {586- // Backreferences are not supported.587- // "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|')588- executeRegexp = regexp.MustCompile(589- "(?si):execute(-multi)?:.+|:execute(-multi)?(\\([^)]*\\)|\\[[^\\]]*\\]|~[^~]*~|![^!]*!|@[^@]*@|\\#[^\\#]*\\#|\\$[^\\$]*\\$|%[^%]*%|\\^[^\\^]*\\^|&[^&]*&|\\*[^\\*]*\\*|;[^;]*;|/[^/]*/|\\|[^\\|]*\\|)")590- }592 masked := executeRegexp.ReplaceAllStringFunc(str, func(src string) string {592- if src[len(":execute")] == '-' {593- return ":execute-multi(" + strings.Repeat(" ", len(src)-len(":execute-multi()")) + ")"593+ prefix := ":execute"594+ if src[len(prefix)] == '-' {595+ c := src[len(prefix)+1]596+ if c == 's' || c == 'S' {597+ prefix += "-silent"598+ } else {599+ prefix += "-multi"600+ }601 }595- return ":execute(" + strings.Repeat(" ", len(src)-len(":execute()")) + ")"602+ return prefix + "(" + strings.Repeat(" ", len(src)-len(prefix)-2) + ")"603 })604 masked = strings.Replace(masked, "::", string([]rune{escapedColon, ':'}), -1)605 masked = strings.Replace(masked, ",:", string([]rune{escapedComma, ':'}), -1)606 masked = strings.Replace(masked, "+:", string([]rune{escapedPlus, ':'}), -1)127 unmodified lines734 if t == actIgnore {735 errorExit("unknown action: " + spec)736 } else {737 var offset int731- if t == actExecuteMulti {738+ switch t {739+ case actExecuteSilent:740+ offset = len("execute-silent")741+ case actExecuteMulti:742 offset = len("execute-multi")733- } else {743+ default:744 offset = len("execute")745 }746 if spec[offset] == ':' {747 if specIndex == len(specs)-1 {13 unmodified lines761 }762 }763 764 func isExecuteAction(str string) actionType {755- t := actExecute756- if !strings.HasPrefix(str, "execute") || len(str) < len("execute(") {765+ matches := executeRegexp.FindAllStringSubmatch(":"+str, -1)766+ if matches == nil || len(matches) != 1 {767 return actIgnore768 }759-760- b := str[len("execute")]761- if strings.HasPrefix(str, "execute-multi") {762- if len(str) < len("execute-multi(") {763- return actIgnore764- }765- t = actExecuteMulti766- b = str[len("execute-multi")]769+ prefix := matches[0][1]770+ if len(prefix) == 0 {771+ prefix = matches[0][2]772 }768- e := str[len(str)-1]769- if b == ':' || b == '(' && e == ')' || b == '[' && e == ']' ||770- b == e && strings.ContainsAny(string(b), "~!@#$%^&*;/|") {771- return t773+ switch prefix {774+ case "execute":775+ return actExecute776+ case "execute-silent":777+ return actExecuteSilent778+ case "execute-multi":779+ return actExecuteMulti780 }781 return actIgnore782 }783 443 unmodified linesmodified:src/terminal_test.go+33 added-13 removed
12 unmodified lines13 return &Item{origText: &bytes, text: util.RunesToChars([]rune(trimmed))}14 }15 16 func TestReplacePlaceholder(t *testing.T) {17- items1 := []*Item{newItem(" foo'bar \x1b[31mbaz\x1b[m")}17+ item1 := newItem(" foo'bar \x1b[31mbaz\x1b[m")18+ items1 := []*Item{item1, item1}19 items2 := []*Item{20 newItem("foo'bar \x1b[31mbaz\x1b[m"),21+ newItem("foo'bar \x1b[31mbaz\x1b[m"),22 newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}23 24 var result string25 check := func(expected string) {2 unmodified lines28 }29 }30 31 // {}, preserve ansi30- result = replacePlaceholder("echo {}", false, Delimiter{}, "query", items1)32+ result = replacePlaceholder("echo {}", false, Delimiter{}, false, "query", items1)33 check("echo ' foo'\\''bar \x1b[31mbaz\x1b[m'")34 35 // {}, strip ansi34- result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items1)36+ result = replacePlaceholder("echo {}", true, Delimiter{}, false, "query", items1)37 check("echo ' foo'\\''bar baz'")38 39 // {}, with multiple items38- result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items2)39- check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ'")40+ result = replacePlaceholder("echo {}", true, Delimiter{}, false, "query", items2)41+ check("echo 'foo'\\''bar baz'")42 43 // {..}, strip leading whitespaces, preserve ansi42- result = replacePlaceholder("echo {..}", false, Delimiter{}, "query", items1)44+ result = replacePlaceholder("echo {..}", false, Delimiter{}, false, "query", items1)45 check("echo 'foo'\\''bar \x1b[31mbaz\x1b[m'")46 47 // {..}, strip leading whitespaces, strip ansi46- result = replacePlaceholder("echo {..}", true, Delimiter{}, "query", items1)48+ result = replacePlaceholder("echo {..}", true, Delimiter{}, false, "query", items1)49 check("echo 'foo'\\''bar baz'")50 51 // {q}50- result = replacePlaceholder("echo {} {q}", true, Delimiter{}, "query", items1)52+ result = replacePlaceholder("echo {} {q}", true, Delimiter{}, false, "query", items1)53 check("echo ' foo'\\''bar baz' 'query'")54 55 // {q}, multiple items54- result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, "query 'string'", items2)56+ result = replacePlaceholder("echo {+}{q}{+}", true, Delimiter{}, false, "query 'string'", items2)57 check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ''query '\\''string'\\''''foo'\\''bar baz' 'FOO'\\''BAR BAZ'")58 59+ result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, false, "query 'string'", items2)60+ check("echo 'foo'\\''bar baz''query '\\''string'\\''''foo'\\''bar baz'")61+57- result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items1)62+ result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, false, "query", items1)63 check("echo 'foo'\\''bar'/'baz'/'bazfoo'\\''bar'/'baz'/'foo'\\''bar'/' foo'\\''bar baz'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")64 60- result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items2)65+ result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, false, "query", items2)66+ check("echo 'foo'\\''bar'/'baz'/'baz'/'foo'\\''bar'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")67+68+ result = replacePlaceholder("echo {+1}/{+2}/{+-1}/{+-2}/{+..}/{n.t}/\\{}/\\{1}/\\{q}/{+3}", true, Delimiter{}, false, "query", items2)69 check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")70 71+ // forcePlus72+ result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, true, "query", items2)73+ check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")74+75+ // No match76+ result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, nil})77+ check("echo /")78+79+ // No match, but with selections80+ result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, item1})81+ check("echo /' foo'\\''bar baz'")82+83 // String delimiter84 delim := "'"65- result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, "query", items1)85+ result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, false, "query", items1)86 check("echo ' foo'\\''bar baz'/'foo'/'bar baz'")87 88 // Regex delimiter89 regex := regexp.MustCompile("[oa]+")90 // foo'bar baz71- result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, "query", items1)91+ result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, false, "query", items1)92 check("echo ' foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")93 }modified:src/terminal.go+18 added-13 removed
202 unmodified lines203 actPreviewPageDown204 actPreviousHistory205 actNextHistory206 actExecute207- actExecuteMulti207+ actExecuteSilent208+ actExecuteMulti // Deprecated209 )210 211 func toActions(types ...actionType) []action {212 actions := make([]action, len(types))913 unmodified lines1126 return strings.Join(replacements, " ")1127 })1128 }1129 1129-func (t *Terminal) executeCommand(template string, forcePlus bool) {1130+func (t *Terminal) executeCommand(template string, forcePlus bool, background bool) {1131 valid, list := t.buildPlusList(template, forcePlus)1132 if !valid {1133 return1134 }1135 command := replacePlaceholder(template, t.ansi, t.delimiter, forcePlus, string(t.input), list)1136 cmd := util.ExecCommand(command)1137+ if !background {1136- cmd.Stdin = os.Stdin1137- cmd.Stdout = os.Stdout1138- cmd.Stderr = os.Stderr1139- t.tui.Pause()1140- cmd.Run()1141- if t.tui.Resume() {1142- t.printAll()1138+ cmd.Stdin = os.Stdin1139+ cmd.Stdout = os.Stdout1140+ cmd.Stderr = os.Stderr1141+ t.tui.Pause()1142+ cmd.Run()1143+ if t.tui.Resume() {1144+ t.printAll()1145+ }1146+ t.refresh()1147+ } else {1148+ cmd.Run()1149 }1144- t.refresh()1150 }1151 1152 func (t *Terminal) hasPreviewer() bool {1153 return t.previewBox != nil240 unmodified lines1394 }1395 doAction = func(a action, mapkey int) bool {1396 switch a.t {1397 case actIgnore:1393- case actExecute:1394- t.executeCommand(a.a, false)1398+ case actExecute, actExecuteSilent:1399+ t.executeCommand(a.a, false, a.t == actExecuteSilent)1400 case actExecuteMulti:1396- t.executeCommand(a.a, true)1401+ t.executeCommand(a.a, true, false)1402 case actInvalid:1403 t.mutex.Unlock()1404 return false1405 case actTogglePreview:333 unmodified linesmodified:CHANGELOG.md+4 added
6 unmodified lines7 characters are trimmed8 - Placeholder expression used in `--preview` and `execute` action can9 optionally take `+` flag to be used with multiple selections10 - e.g. `git log --oneline | fzf --multi --preview 'git show {+1}'`11+- Added `execute-silent` action for executing a command silently without12+ switching to the alternate screen. This is useful when the process is13+ short-lived and you're not interested in its output.14+ - e.g. `fzf --bind 'ctrl-y:execute!(echo -n {} | pbcopy)'`15 16 0.16.217 ------18 - Dropped ncurses dependency548 unmodified lines