summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2014-03-05 11:21:20 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2014-03-05 11:21:20 +0900
commitb3182c330413d64dbdf60efdc2fbad3b5a28d12f (patch)
tree0e7efc816d9b0b2c3beb2c733c5225d527d7ff88
parent2dbca00bfb8e9f0d63514bd389f09a28bbf6e149 (diff)
Performance optimization: batch application of input chars
-rwxr-xr-xfzf71
1 files changed, 49 insertions, 22 deletions
diff --git a/fzf b/fzf
index 180a53df..8e040e14 100755
--- a/fzf
+++ b/fzf
@@ -7,7 +7,7 @@
# / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell
#
-# Version: 0.7.3 (March 4, 2014)
+# Version: 0.7.3 (March 5, 2014)
#
# Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf
@@ -81,6 +81,7 @@ class FZF
@extended = nil
@mouse = true
@filter = nil
+ @pending = nil
argv =
if opts = ENV['FZF_DEFAULT_OPTS']
@@ -755,6 +756,49 @@ class FZF
states.any? { |s| s & st > 0 }
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
got = nil
begin
@@ -820,10 +864,7 @@ class FZF
@cursor_x.set cursor
render { print_input }
- C.stdscr.timeout = -1
- ch = C.getch
-
- case ch
+ case ch = getch
when C::KEY_MOUSE
if m = C.getmouse
st = m.bstate
@@ -871,23 +912,9 @@ class FZF
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
- ch = chs.pack('C*').force_encoding('UTF-8')
- end
- end
-
- if ch.is_a?(String) && ch =~ /[[:print:]]/
- input.insert cursor, ch
- cursor += 1
+ if ch.is_a? Array
+ input.insert cursor, ch.join
+ cursor += ch.length
end
}).call(ch)