Add support for ALT-D and ALT-BS key bindings

https://github.com/junegunn/fzf/issues/111#issuecomment-67832143
This commit is contained in:
Junegunn Choi 2014-12-23 12:22:19 +09:00
parent d38f7a5eb5
commit 00190677d4
2 changed files with 48 additions and 6 deletions

29
fzf
View File

@ -7,7 +7,7 @@
# / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell
#
# Version: 0.8.8 (Nov 4, 2014)
# Version: 0.8.9 (Dec 23, 2014)
#
# Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf
@ -955,8 +955,10 @@ class FZF
get_mouse
end
when 'b', 98 then :alt_b
when 'd', 100 then :alt_d
when 'f', 102 then :alt_f
when :esc then :esc
when 127 then ctrl(:w)
else next
end if ord == 27
@ -1013,6 +1015,11 @@ class FZF
mouse_event = MouseEvent.new
backword = proc {
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
nil
}
forward = proc {
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
nil
}
actions = {
:esc => proc { exit 1 },
@ -1039,8 +1046,10 @@ class FZF
ctrl(:w) => proc {
pcursor = cursor
backword.call
yanked = input[cursor...pcursor] if pcursor > cursor
input = input[0...cursor] + input[pcursor..-1]
if pcursor > cursor
yanked = input[cursor...pcursor]
input = input[0...cursor] + input[pcursor..-1]
end
},
ctrl(:y) => proc { actions[:default].call yanked },
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
@ -1066,10 +1075,18 @@ class FZF
:del => proc { input[cursor] = '' if input.length > cursor },
:pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } },
:pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } },
:alt_b => proc { backword.call; nil },
:alt_b => proc { backword.call },
:alt_d => proc {
pcursor = cursor
forward.call
if cursor > pcursor
yanked = input[pcursor...cursor]
input = input[0...pcursor] + input[cursor..-1]
cursor = pcursor
end
},
:alt_f => proc {
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
nil
forward.call
},
:default => proc { |val|
case val

View File

@ -50,6 +50,7 @@ class MockTTY
@buffer << str
@condv.broadcast
end
self
end
end
@ -805,6 +806,30 @@ class TestFZF < MiniTest::Unit::TestCase
tty << "\e[Z\e[Z"
tty << "\r"
end
# ALT-D
assert_fzf_output %w[--print-query], "", "hello baby = world" do |tty|
tty << "hello world baby"
tty << alt(:b) << alt(:b) << alt(:d)
tty << ctrl(:e) << " = " << ctrl(:y)
tty << "\r"
end
# ALT-BACKSPACE
assert_fzf_output %w[--print-query], "", "hello baby = world " do |tty|
tty << "hello world baby"
tty << alt(:b) << alt(127.chr)
tty << ctrl(:e) << " = " << ctrl(:y)
tty << "\r"
end
end
def alt chr
"\e#{chr}"
end
def ctrl char
char.to_s.ord - 'a'.ord + 1
end
end