Support old Ruby (even runs on 1.8.5)

This commit is contained in:
Junegunn Choi 2013-10-24 12:16:32 +09:00
parent b54fd30edb
commit 0165dc87a7

23
fzf
View File

@ -105,6 +105,12 @@ else
self.unpack('c').first self.unpack('c').first
end end
end end
class Fixnum
def ord
self
end
end
end end
C.init_screen C.init_screen
@ -179,7 +185,7 @@ searcher = Thread.new {
new_search = new_items || query_changed new_search = new_items || query_changed
if new_search if new_search
regexp = pquery.empty? ? nil : regexp = pquery.empty? ? nil :
Regexp.new(pquery.each_char.inject('') { |sum, e| Regexp.new(pquery.split(//).inject('') { |sum, e|
e = Regexp.escape e e = Regexp.escape e
sum << "#{e}[^#{e}]*?" sum << "#{e}[^#{e}]*?"
}, Regexp::IGNORECASE) }, Regexp::IGNORECASE)
@ -208,7 +214,7 @@ searcher = Thread.new {
end end
end end
cache[pquery] ||= (prefix_cache ? prefix_cache.map(&:first) : list).map { |line| cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|
if regexp if regexp
md = line.match regexp md = line.match regexp
md ? [line, md.offset(0)] : nil md ? [line, md.offset(0)] : nil
@ -217,7 +223,7 @@ searcher = Thread.new {
end end
}.compact }.compact
end end
}.flatten(1) }.inject([]) { |all, e| all.concat e }
@stat.search += 1 @stat.search += 1
new_length = matches.length new_length = matches.length
@ -327,13 +333,12 @@ begin
127 => proc { input[cursor -= 1] = '' if cursor > 0 }, 127 => proc { input[cursor -= 1] = '' if cursor > 0 },
:left => proc { cursor = [0, cursor - 1].max }, :left => proc { cursor = [0, cursor - 1].max },
:right => proc { cursor = [input.length, cursor + 1].min }, :right => proc { cursor = [input.length, cursor + 1].min },
}.tap { |actions|
actions[ctrl :b] = actions[:left]
actions[ctrl :f] = actions[:right]
actions[ctrl :h] = actions[127]
actions[ctrl :n] = actions[ctrl :j]
actions[ctrl :p] = actions[ctrl :k]
} }
actions[ctrl(:b)] = actions[:left]
actions[ctrl(:f)] = actions[:right]
actions[ctrl(:h)] = actions[127]
actions[ctrl(:n)] = actions[ctrl(:j)]
actions[ctrl(:p)] = actions[ctrl(:k)]
while true while true
ord = tty.getc.ord ord = tty.getc.ord