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