Minor refactoring

This commit is contained in:
Junegunn Choi 2014-01-30 02:51:06 +09:00
parent aad335475c
commit b0eca20dc2

52
fzf
View File

@ -174,7 +174,8 @@ class FZF
Environment variables
FZF_DEFAULT_COMMAND Default command to use when input is tty
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
FZF_MOUSE_ENABLED Set to 0 to disable mouse] + $/ + $/
exit x
end
@ -471,7 +472,7 @@ class FZF
def init_screen
C.init_screen
if ENV.fetch('FZF_MOUSE_ENABLED', '1') == '1'
unless ENV.fetch('FZF_MOUSE_ENABLED', '1') == '0'
C.mouseinterval 0
C.mousemask C::ALL_MOUSE_EVENTS
end
@ -715,6 +716,10 @@ class FZF
end
end
def test_mouse st, *states
states.any? { |s| s & st > 0 }
end
def start_loop
got = nil
begin
@ -765,13 +770,13 @@ class FZF
nil
},
}
actions[C::KEY_UP] = actions[ctrl(:p)] = actions[ctrl(:k)]
actions[C::KEY_DOWN] = actions[ctrl(:n)] = actions[ctrl(:j)]
actions[C::KEY_LEFT] = actions[ctrl(:b)]
actions[C::KEY_RIGHT] = actions[ctrl(:f)]
actions[127] = actions[ctrl(:h)]
actions[C::KEY_DOWN] = actions[ctrl(:n)] = actions[ctrl(:j)]
actions[C::KEY_UP] = actions[ctrl(:p)] = actions[ctrl(:k)]
actions[C::KEY_BTAB] = actions[:select] = actions[ctrl(:i)]
actions[C::KEY_BACKSPACE] = actions[127] = actions[ctrl(:h)]
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
actions[:select] = actions[C::KEY_BTAB] = actions[ctrl(:i)]
emit(:key) { [@query.get, cursor] } unless @query.empty?
pmv = nil
@ -786,15 +791,15 @@ class FZF
when C::KEY_MOUSE
if m = C.getmouse
st = m.bstate
if ((st & C::BUTTON1_PRESSED) | (st & C::BUTTON1_RELEASED)) > 0
if test_mouse(st, C::BUTTON1_PRESSED, C::BUTTON1_RELEASED)
if m.y == cursor_y
# TODO Wide-characters
cursor = [0, [input.length, m.x - 2].min].max
elsif m.x > 1 && m.y <= max_items
vselect { |v|
tv = max_items - m.y - 1
if st & C::BUTTON1_RELEASED > 0
if (st & C::BUTTON_SHIFT) > 0
if test_mouse(st, C::BUTTON1_RELEASED)
if test_mouse(st, C::BUTTON_SHIFT)
ch = :select
elsif pmv == tv
ch = ctrl(:m)
@ -804,9 +809,9 @@ class FZF
tv
}
end
elsif ((st & 0x8000000) | (st & C::BUTTON2_PRESSED)) > 0
elsif test_mouse(st, 0x8000000, C::BUTTON2_PRESSED)
ch = C::KEY_DOWN
elsif (st & C::BUTTON4_PRESSED) > 0
elsif test_mouse(st, C::BUTTON4_PRESSED)
ch = C::KEY_UP
end
end
@ -820,26 +825,25 @@ class FZF
else
ch2
end
when Fixnum
# Ruby 1.8
if (ch.chr rescue nil) && ch.chr =~ /[[:print:]]/
ch = ch.chr
else
chs = [ch]
(num_unicode_bytes(ch) - 1).times do |i|
chs << C.getch
end
upd = actions.fetch(ch, proc { |ch|
if ch.is_a?(Fixnum)
# Ruby 1.8
if (ch.chr rescue '') =~ /[[:print:]]/
ch = ch.chr
elsif (nch = num_unicode_bytes(ch)) > 1
chs = [ch]
(nch - 1).times do |i|
chs << C.getch
end
# UTF-8 TODO Ruby 1.8
if chs.length > 1
ch = chs.pack('C*').force_encoding('UTF-8')
end
end
end
upd = actions.fetch(ch, proc { |chr|
if chr =~ /[[:print:]]/
input.insert cursor, chr
if ch.is_a?(String) && ch =~ /[[:print:]]/
input.insert cursor, ch
cursor += 1
end
}).call(ch)