Performance optimization: batch application of input chars

This commit is contained in:
Junegunn Choi 2014-03-05 11:21:20 +09:00
parent 2dbca00bfb
commit b3182c3304

71
fzf
View File

@ -7,7 +7,7 @@
# / __/ / /_/ __/ # / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell # /_/ /___/_/ Fuzzy finder for your shell
# #
# Version: 0.7.3 (March 4, 2014) # Version: 0.7.3 (March 5, 2014)
# #
# Author: Junegunn Choi # Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf # URL: https://github.com/junegunn/fzf
@ -81,6 +81,7 @@ class FZF
@extended = nil @extended = nil
@mouse = true @mouse = true
@filter = nil @filter = nil
@pending = nil
argv = argv =
if opts = ENV['FZF_DEFAULT_OPTS'] if opts = ENV['FZF_DEFAULT_OPTS']
@ -755,6 +756,49 @@ class FZF
states.any? { |s| s & st > 0 } states.any? { |s| s & st > 0 }
end end
def to_printable 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
ch = chs.pack('C*').force_encoding('UTF-8')
end
end
ch.is_a?(String) && ch =~ /[[:print:]]/ && ch
end
def getch
if pending = @pending
@pending = nil
return pending
end
C.stdscr.timeout = -1
c = C.getch
if ch = to_printable(c)
chs = [ch]
C.stdscr.timeout = 0
while c = C.getch
if ch = to_printable(c)
chs << ch
else
@pending = c
break
end
end
chs
else
c
end
end
def start_loop def start_loop
got = nil got = nil
begin begin
@ -820,10 +864,7 @@ class FZF
@cursor_x.set cursor @cursor_x.set cursor
render { print_input } render { print_input }
C.stdscr.timeout = -1 case ch = getch
ch = C.getch
case ch
when C::KEY_MOUSE when C::KEY_MOUSE
if m = C.getmouse if m = C.getmouse
st = m.bstate st = m.bstate
@ -871,23 +912,9 @@ class FZF
end end
upd = actions.fetch(ch, proc { |ch| upd = actions.fetch(ch, proc { |ch|
if ch.is_a?(Fixnum) if ch.is_a? Array
# Ruby 1.8 input.insert cursor, ch.join
if (ch.chr rescue '') =~ /[[:print:]]/ cursor += ch.length
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')
end
end
if ch.is_a?(String) && ch =~ /[[:print:]]/
input.insert cursor, ch
cursor += 1
end end
}).call(ch) }).call(ch)