Synchronize getch calls to reduce screen glitches

This commit is contained in:
Junegunn Choi 2014-03-05 19:07:59 +09:00
parent d66b02b0cd
commit 86a66da04d

23
fzf
View File

@ -82,6 +82,7 @@ class FZF
@mouse = true @mouse = true
@filter = nil @filter = nil
@pending = nil @pending = nil
@mutex = Mutex.new
argv = argv =
if opts = ENV['FZF_DEFAULT_OPTS'] if opts = ENV['FZF_DEFAULT_OPTS']
@ -721,8 +722,10 @@ class FZF
Thread.new do Thread.new do
begin begin
while blk = @queue.shift while blk = @queue.shift
blk.call @mutex.synchronize do
refresh blk.call
refresh
end
end end
rescue Exception => e rescue Exception => e
@main.raise e @main.raise e
@ -766,7 +769,7 @@ class FZF
elsif (nch = num_unicode_bytes(ch)) > 1 elsif (nch = num_unicode_bytes(ch)) > 1
chs = [ch] chs = [ch]
(nch - 1).times do |i| (nch - 1).times do |i|
chs << C.getch chs << getch_nb
end end
# UTF-8 TODO Ruby 1.8 # UTF-8 TODO Ruby 1.8
ch = chs.pack('C*').force_encoding('UTF-8') ch = chs.pack('C*').force_encoding('UTF-8')
@ -776,6 +779,10 @@ class FZF
ch.is_a?(String) && ch =~ /[[:print:]]/ && ch ch.is_a?(String) && ch =~ /[[:print:]]/ && ch
end end
def getch_nb
@mutex.synchronize { C.getch }
end
def getch def getch
if pending = @pending if pending = @pending
@pending = nil @pending = nil
@ -784,10 +791,10 @@ class FZF
C.stdscr.timeout = -1 C.stdscr.timeout = -1
c = C.getch c = C.getch
C.stdscr.timeout = 0
if ch = to_printable(c) if ch = to_printable(c)
chs = [ch] chs = [ch]
C.stdscr.timeout = 0 if AFTER_1_9 while AFTER_1_9 && c = getch_nb
while AFTER_1_9 && c = C.getch
if ch = to_printable(c) if ch = to_printable(c)
chs << ch chs << ch
else else
@ -868,7 +875,7 @@ class FZF
case ch = getch case ch = getch
when C::KEY_MOUSE when C::KEY_MOUSE
if m = C.getmouse if m = @mutex.synchronize { C.getmouse }
st = m.bstate st = m.bstate
if test_mouse(st, C::BUTTON1_PRESSED, C::BUTTON1_RELEASED) if test_mouse(st, C::BUTTON1_PRESSED, C::BUTTON1_RELEASED)
if m.y == cursor_y if m.y == cursor_y
@ -897,9 +904,9 @@ class FZF
when 27 when 27
C.stdscr.timeout = 0 C.stdscr.timeout = 0
ch = # Typeahead arrow keys ch = # Typeahead arrow keys
case ch2 = C.getch case ch2 = getch_nb
when '[', 91 when '[', 91
case ch3 = C.getch case ch3 = getch_nb
when 'D', 68 then ctrl(:b) when 'D', 68 then ctrl(:b)
when 'C', 67 then ctrl(:f) when 'C', 67 then ctrl(:f)
when 'B', 66 then ctrl(:j) when 'B', 66 then ctrl(:j)