Ruby 1.8 compatibility

This commit is contained in:
Junegunn Choi 2014-01-29 02:08:18 +09:00
parent 0d83cae2ec
commit 6aa168833b

35
fzf
View File

@ -701,6 +701,20 @@ class FZF
update_list false update_list false
end end
def num_unicode_bytes chr
# http://en.wikipedia.org/wiki/UTF-8
if chr & 0b10000000 > 0
bytes = 0
7.downto(2) do |shift|
break if (chr >> shift) & 0x1 == 0
bytes += 1
end
bytes
else
1
end
end
def start_loop def start_loop
got = nil got = nil
begin begin
@ -797,24 +811,21 @@ class FZF
ch2 ch2
end end
when Fixnum when Fixnum
bytes = 1 # Ruby 1.8
if (ch >> 7) == 1 if (ch.chr rescue nil) && ch.chr =~ /[[:print:]]/
6.downto(2) do |shift| ch = ch.chr
if (ch >> shift) & 0x1 == 0 else
break
end
bytes += 1
end
end
if bytes > 1
chs = [ch] chs = [ch]
(bytes - 1).times do (num_unicode_bytes(ch) - 1).times do |i|
chs << C.getch chs << C.getch
end end
# TODO: ruby 1.8
# UTF-8 TODO Ruby 1.8
if chs.length > 1
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| upd = actions.fetch(ch, proc { |chr|
if chr =~ /[[:print:]]/ if chr =~ /[[:print:]]/