Minor refactoring

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

62
fzf
View File

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