Allow bare put action in transform output
Before adoption: this commit came in with the history the project adopted, so no landing records it.
transform/bg-transform now permit bare `put`, inserting the key that triggered the action (`a:transform:echo put` puts `a`).
authored by Junegunn Choi · committed by Junegunn Choi · parent 7d647c7 · Browse files · 6 files·+33 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+33 added−9 removed
- tests:+18 added−0 removed
- other:+15 added−9 removed
Provenance
- Commit
ae78a5c- Tree
43ae939- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7d647c7
6 files changed+33 added−9 removed
modified:src/tui/tui.go+7 added
2 unmodified lines3 import (4 "strconv"5 "strings"6 "time"7+ "unicode"8 9 "github.com/junegunn/fzf/src/util"10 "github.com/rivo/uniseg"11 )240 unmodified lines252 // Ignore MouseEvent pointer253 return Event{e.Type, e.Char, nil}254 }255 256+// Printable returns true if the event is a printable character that can be257+// inserted into the query (e.g. via the 'put' action).258+func (e Event) Printable() bool {259+ return e.Type == Rune && unicode.IsGraphic(e.Char)260+}261+262 func (e Event) KeyName() string {263 if me := e.MouseEvent; me != nil {264 return me.Name()265 }1256 unmodified linesmodified:src/options_test.go+2 added-2 removed
570 unmodified lines571 }572 }573 574 func TestParseSingleActionList(t *testing.T) {575- actions, _ := parseSingleActionList("Execute@foo+bar,baz@+up+up+reload:down+down")575+ actions, _ := parseSingleActionList("Execute@foo+bar,baz@+up+up+reload:down+down", false)576 if len(actions) != 4 {577 t.Errorf("Invalid number of actions parsed:%d", len(actions))578 }579 if actions[0].t != actExecute || actions[0].a != "foo+bar,baz" {7 unmodified lines587 }588 }589 590 func TestParseSingleActionListError(t *testing.T) {591- _, err := parseSingleActionList("change-query(foobar)baz")591+ _, err := parseSingleActionList("change-query(foobar)baz", false)592 if err == nil {593 t.Errorf("Failed to detect error")594 }595 }9 unmodified linesmodified:src/options.go+3 added-5 removed
8 unmodified lines9 "regexp"10 "strconv"11 "strings"12 "time"13- "unicode"13 14 "github.com/junegunn/fzf/src/algo"15 "github.com/junegunn/fzf/src/tui"16 "github.com/junegunn/fzf/src/util"1715 unmodified lines1732 masked = strings.ReplaceAll(masked, "+:", string([]rune{escapedPlus, ':'}))1733 return masked1734 }1735 1737-func parseSingleActionList(str string) ([]*action, error) {1736+func parseSingleActionList(str string, putAllowed bool) ([]*action, error) {1737 // We prepend a colon to satisfy argActionRegexp and remove it later1738 masked := maskActionContents(":" + str)[1:]1740- return parseActionList(masked, str, []*action{}, false)1739+ return parseActionList(masked, str, []*action{}, putAllowed)1740 }1741 1742 func parseActionList(masked string, original string, prevActions []*action, putAllowed bool) ([]*action, error) {1743 maskedStrings := strings.Split(masked, "+")297 unmodified lines2041 return err2042 }2043 key = firstKey(keys)2044 }2046- putAllowed := key.Type == tui.Rune && unicode.IsGraphic(key.Char)2047- keymap[key], err = parseActionList(pair[1], origPairStr[len(pair[0])+1:], keymap[key], putAllowed)2045+ keymap[key], err = parseActionList(pair[1], origPairStr[len(pair[0])+1:], keymap[key], key.Printable())2046 if err != nil {2047 return err2048 }2049 }1941 unmodified linesmodified:src/server.go+1 added-1 removed
238 unmodified lines239 return bad("incomplete request")240 }241 body = body[:contentLength]242 243- actions, err := parseSingleActionList(strings.Trim(string(body), "\r\n"))243+ actions, err := parseSingleActionList(strings.Trim(string(body), "\r\n"), false)244 if err != nil {245 return bad(err.Error())246 }247 if len(actions) == 0 {30 unmodified linesmodified:src/terminal.go+2 added-1 removed
6971 unmodified lines6972 }6973 })6974 case actTransform, actBgTransform:6975 capture(false, func(body string) {6976+ // Allow 'put' if the triggering key is a printable character6976- if actions, err := parseSingleActionList(strings.Trim(body, "\r\n")); err == nil {6977+ if actions, err := parseSingleActionList(strings.Trim(body, "\r\n"), event.Printable()); err == nil {6978 // NOTE: We're not properly passing the return value here6979 doActions(actions)6980 }6981 })1385 unmodified linesmodified:test/test_core.rb+18 added
969 unmodified lines970 tmux.send_keys :b971 tmux.until { |lines| assert_includes lines[1], ' aabravo/aabravo' }972 end973 974+ def test_transform_put975+ tmux.send_keys %(seq 1000 | #{FZF} --bind 'a:transform:echo put'), :Enter976+ tmux.until { |lines| assert_equal 1000, lines.match_count }977+ tmux.send_keys :a978+ tmux.until { |lines| assert_equal '> a', lines.last }979+ tmux.send_keys :b980+ tmux.until { |lines| assert_equal '> ab', lines.last }981+ end982+983+ # The async callback runs in a later iteration, but 'put' must still insert984+ # the key that triggered the bg-transform (snapshot of the scheduling event).985+ def test_bg_transform_put986+ tmux.send_keys %(seq 1000 | #{FZF} --bind 'a:bg-transform:sleep 0.5; echo put'), :Enter987+ tmux.until { |lines| assert_equal 1000, lines.match_count }988+ tmux.send_keys 'ab'989+ tmux.until { |lines| assert_equal '> ba', lines.last }990+ end991+992 def test_accept_non_empty993 tmux.send_keys %(seq 1000 | #{fzf('--print-query --bind enter:accept-non-empty')}), :Enter994 tmux.until { |lines| assert_equal 1000, lines.match_count }995 tmux.send_keys 'foo'1807 unmodified lines