Allow actions to multiple keys and events at once
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #4206
authored by Junegunn Choi · committed by Junegunn Choi · parent e91f10a · Browse files · 4 files·+35 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−26 removed
- tests:+2 added−2 removed
- other:+33 added−24 removed
Provenance
- Commit
80da077- Tree
8e7b4c2- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
e91f10a
4 files changed+35 added−26 removed
modified:man/man1/fzf.1+3 added-1 removed
1289 unmodified lines1290 more \fBactions\fR. You can use it to customize key bindings or implement1291 dynamic behaviors.1292 1293 \fB\-\-bind\fR takes a comma-separated list of binding expressions. Each binding1294-expression is \fBKEY:ACTION\fR or \fBEVENT:ACTION\fR.1294+expression is \fBKEY:ACTION\fR or \fBEVENT:ACTION\fR. You can bind actions to1295+multiple keys and events by writing comma-separated list of keys and events1296+before the colon. e.g. \fBKEY1,KEY2,EVENT1,EVENT2:ACTION\fR.1297 1298 e.g.1299 \fBfzf \-\-bind=ctrl\-j:accept,ctrl\-k:kill\-line\fR1300 535 unmodified linesmodified:src/options.go+27 added-16 removed
1636 unmodified lines1637 func parseKeymap(keymap map[tui.Event][]*action, str string) error {1638 var err error1639 masked := maskActionContents(str)1640 idx := 01641+ keys := []string{}1642 for _, pairStr := range strings.Split(masked, ",") {1643 origPairStr := str[idx : idx+len(pairStr)]1644 idx += len(pairStr) + 11645 1646 pair := strings.SplitN(pairStr, ":", 2)1647+ if len(pair[0]) == 0 {1648+ return errors.New("key name required")1649+ }1650+ keys = append(keys, pair[0])1651 if len(pair) < 2 {1647- return errors.New("bind action not specified: " + origPairStr)1652+ continue1653 }1654+ for _, keyName := range keys {1649- var key tui.Event1650- if len(pair[0]) == 1 && pair[0][0] == escapedColon {1651- key = tui.Key(':')1652- } else if len(pair[0]) == 1 && pair[0][0] == escapedComma {1653- key = tui.Key(',')1654- } else if len(pair[0]) == 1 && pair[0][0] == escapedPlus {1655- key = tui.Key('+')1656- } else {1657- keys, err := parseKeyChordsImpl(pair[0], "key name required")1655+ var key tui.Event1656+ if len(keyName) == 1 && keyName[0] == escapedColon {1657+ key = tui.Key(':')1658+ } else if len(keyName) == 1 && keyName[0] == escapedComma {1659+ key = tui.Key(',')1660+ } else if len(keyName) == 1 && keyName[0] == escapedPlus {1661+ key = tui.Key('+')1662+ } else {1663+ keys, err := parseKeyChordsImpl(keyName, "key name required")1664+ if err != nil {1665+ return err1666+ }1667+ key = firstKey(keys)1668+ }1669+ putAllowed := key.Type == tui.Rune && unicode.IsGraphic(key.Char)1670+ keymap[key], err = parseActionList(pair[1], origPairStr[len(pair[0])+1:], keymap[key], putAllowed)1671 if err != nil {1672 return err1673 }1661- key = firstKey(keys)1674 }1663- putAllowed := key.Type == tui.Rune && unicode.IsGraphic(key.Char)1664- keymap[key], err = parseActionList(pair[1], origPairStr[len(pair[0])+1:], keymap[key], putAllowed)1665- if err != nil {1666- return err1667- }1675+ keys = keys[:0]1676 }1677+ if len(keys) > 0 {1678+ return errors.New("bind action not specified: " + strings.Join(keys, ", "))1679+ }1680 return nil1681 }1682 1683 func isExecuteAction(str string) actionType {1629 unmodified linesmodified:test/test_core.rb+2 added-2 removed
416 unmodified lines417 tmux.until { |lines| assert_equal 1, lines.match_count }418 end419 420 def test_bind421- tmux.send_keys "seq 1 1000 | #{fzf('-m --bind=ctrl-j:accept,u:up,T:toggle-up,t:toggle')}", :Enter421+ tmux.send_keys "seq 1 1000 | #{fzf('-m --bind=ctrl-j:accept,u,U:up,X,Y,Z:toggle-up,t:toggle')}", :Enter422 tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }423- tmux.send_keys 'uuu', 'TTT', 'tt', 'uu', 'ttt', 'C-j'423+ tmux.send_keys 'uUu', 'XYZ', 'tt', 'uu', 'ttt', 'C-j'424 assert_equal %w[4 5 6 9], fzf_output_lines425 end426 427 def test_bind_print_query1179 unmodified linesmodified:ADVANCED.md+3 added-7 removed
513 unmodified lines514 515 ```sh516 #!/usr/bin/env bash517 518-# Switch between Ripgrep mode and fzf filtering mode (CTRL-T)519-RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "518 INITIAL_QUERY="${*:-}"519 TRANSFORMER='520 words=($FZF_QUERY)521 5 unmodified lines527 # Otherwise, if the query does not end with a space,528 # restart ripgrep and reload the list529 elif ! [[ $FZF_QUERY =~ \ $ ]]; then530 pat=${words[0]}533- echo "reload:sleep 0.1; $RG_PREFIX \"$pat\" || true"531+ echo "reload:sleep 0.1; rg --column --line-number --no-heading --color=always --smart-case \"$pat\" || true"532 else533 echo search:534 fi535 '536 fzf --ansi --disabled --query "$INITIAL_QUERY" \537 --with-shell 'bash -c' \540- --bind "start:transform:$TRANSFORMER" \541- --bind "change:transform:$TRANSFORMER" \538+ --bind "start,change:transform:$TRANSFORMER" \539 --color "hl:-1:underline,hl+:-1:underline:reverse" \540 --delimiter : \541 --preview 'bat --color=always {1} --highlight-line {2}' \542 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \28 unmodified lines571 command='kubectl get pods --all-namespaces' fzf \572 --info=inline --layout=reverse --header-lines=1 \573 --prompt "$(kubectl config current-context | sed 's/-context$//')> " \574 --header $'╱ Enter (kubectl exec) ╱ CTRL-O (open log in editor) ╱ CTRL-R (reload) ╱\n\n' \578- --bind 'start:reload:$command' \579- --bind 'ctrl-r:reload:$command' \575+ --bind 'start,ctrl-r:reload:$command' \576 --bind 'ctrl-/:change-preview-window(80%,border-bottom|hidden|)' \577 --bind 'enter:execute:kubectl exec -it --namespace {1} {2} -- bash' \578 --bind 'ctrl-o:execute:${EDITOR:-vim} <(kubectl logs --all-containers --namespace {1} {2})' \579 --preview-window up:follow \139 unmodified lines