diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2014-01-29 02:08:18 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2014-01-29 02:08:18 +0900 |
commit | 6aa168833b71ec1394437f2d93073c7a2949efae (patch) | |
tree | fe5b6ee30b67209a8b8e68d0669d859a8a95f900 | |
parent | 0d83cae2ecf5f193b5092d0d67c9ac55794b4573 (diff) |
Ruby 1.8 compatibility
-rwxr-xr-x | fzf | 37 |
1 files changed, 24 insertions, 13 deletions
@@ -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,22 +811,19 @@ 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 - ch = chs.pack('C*').force_encoding('UTF-8') + + # UTF-8 TODO Ruby 1.8 + if chs.length > 1 + ch = chs.pack('C*').force_encoding('UTF-8') + end end end |