Refactoring
This commit is contained in:
parent
abd07ffb9e
commit
7f2ffb9746
@ -107,7 +107,9 @@ Useful bash examples
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
# vimf - Open selected file in Vim
|
# vimf - Open selected file in Vim
|
||||||
alias vimf='vim `fzf`'
|
vimf() {
|
||||||
|
FILE=`fzf` && vim "$FILE"
|
||||||
|
}
|
||||||
|
|
||||||
# fd - cd to selected directory
|
# fd - cd to selected directory
|
||||||
fd() {
|
fd() {
|
||||||
|
145
fzf
145
fzf
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
# vim: set filetype=ruby isk=@,48-57,_,192-255:
|
# vim: set filetype=ruby isk=@,48-57,_,192-255:
|
||||||
|
#!/usr/bin/env bash
|
||||||
#
|
#
|
||||||
# ____ ____
|
# ____ ____
|
||||||
# / __/___ / __/
|
# / __/___ / __/
|
||||||
@ -40,27 +40,31 @@ exec /usr/bin/env ruby -x "$0" $* 3>&1 1>&2 2>&3
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'ostruct'
|
|
||||||
require 'curses'
|
require 'curses'
|
||||||
|
|
||||||
MAX_SORT_LEN = 500
|
MAX_SORT_LEN = 500
|
||||||
C = Curses
|
C = Curses
|
||||||
|
|
||||||
@main = Thread.current
|
|
||||||
@new = []
|
|
||||||
@lists = []
|
|
||||||
@query = ''
|
|
||||||
@mtx = Mutex.new
|
@mtx = Mutex.new
|
||||||
@smtx = Mutex.new
|
@smtx = Mutex.new
|
||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
|
@lists = []
|
||||||
|
@new = []
|
||||||
|
@query = ''
|
||||||
|
@matches = []
|
||||||
@count = 0
|
@count = 0
|
||||||
@cursor_x = 0
|
@cursor_x = 0
|
||||||
@vcursor = 0
|
@vcursor = 0
|
||||||
@matches = []
|
@events = {}
|
||||||
@loaded = false
|
|
||||||
@sort = ARGV.delete('--no-sort').nil?
|
@sort = ARGV.delete('--no-sort').nil?
|
||||||
@stat = OpenStruct.new(:hit => 0, :partial_hit => 0,
|
@stat = { :hit => 0, :partial_hit => 0, :prefix_hit => 0, :search => 0 }
|
||||||
:prefix_hit => 0, :search => 0)
|
|
||||||
|
def emit event
|
||||||
|
@mtx.synchronize do
|
||||||
|
@events[event] = yield
|
||||||
|
@cv.broadcast
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def max_items; C.lines - 2; end
|
def max_items; C.lines - 2; end
|
||||||
def cursor_y; C.lines - 1; end
|
def cursor_y; C.lines - 1; end
|
||||||
@ -148,59 +152,45 @@ C.init_pair 5, C::COLOR_CYAN, C::COLOR_BLACK
|
|||||||
|
|
||||||
reader = Thread.new {
|
reader = Thread.new {
|
||||||
while line = @read.gets
|
while line = @read.gets
|
||||||
@mtx.synchronize do
|
emit(:new) { @new << line.chomp }
|
||||||
@new << line.chomp
|
|
||||||
@cv.broadcast
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@mtx.synchronize do
|
|
||||||
@loaded = true
|
|
||||||
@cv.broadcast
|
|
||||||
end
|
end
|
||||||
|
emit(:loaded) { true }
|
||||||
@smtx.synchronize { @fan = [] }
|
@smtx.synchronize { @fan = [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
main = Thread.current
|
||||||
searcher = Thread.new {
|
searcher = Thread.new {
|
||||||
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
matches = []
|
matches = []
|
||||||
new_length = 0
|
mcount = 0 # match count
|
||||||
pquery = ''
|
plcount = 0 # prev list count
|
||||||
pvcursor = 0
|
q = ''
|
||||||
ploaded = false
|
vcursor = 0
|
||||||
plength = 0
|
|
||||||
zz = [0, 0]
|
zz = [0, 0]
|
||||||
|
|
||||||
begin
|
begin
|
||||||
while true
|
while true
|
||||||
query_changed = nil
|
|
||||||
new_items = nil
|
|
||||||
vcursor_moved = nil
|
|
||||||
wait_for_completion = nil
|
wait_for_completion = nil
|
||||||
@mtx.synchronize do
|
@mtx.synchronize do
|
||||||
while true
|
while true
|
||||||
new_items = !@new.empty?
|
events.merge! @events
|
||||||
query_changed = pquery != @query
|
wait_for_completion = !@sort && !events[:loaded]
|
||||||
vcursor_moved = pvcursor != @vcursor
|
|
||||||
loading_finished = ploaded != @loaded
|
|
||||||
wait_for_completion = !@sort && !@loaded
|
|
||||||
|
|
||||||
if !new_items && !query_changed && !vcursor_moved && !loading_finished
|
if @events.empty? # No new events
|
||||||
@cv.wait @mtx
|
@cv.wait @mtx
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
@events.clear
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if !wait_for_completion && new_items
|
if !wait_for_completion && events[:new]
|
||||||
@lists << [@new, {}]
|
@lists << [@new, {}]
|
||||||
@count += @new.length
|
@count += @new.length
|
||||||
@new = []
|
@new = []
|
||||||
fcache = {}
|
fcache = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
pquery = @query
|
|
||||||
pvcursor = @vcursor
|
|
||||||
ploaded = @loaded
|
|
||||||
end#mtx
|
end#mtx
|
||||||
|
|
||||||
if wait_for_completion
|
if wait_for_completion
|
||||||
@ -213,41 +203,45 @@ searcher = Thread.new {
|
|||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
new_search = new_items || query_changed
|
new_search = events[:key] || events[:new]
|
||||||
|
user_input = events[:key] || events[:vcursor]
|
||||||
|
|
||||||
if new_search && !@lists.empty?
|
if new_search && !@lists.empty?
|
||||||
regexp = pquery.empty? ? nil :
|
events.delete :new
|
||||||
Regexp.new(pquery.split(//).inject('') { |sum, e|
|
q = events.delete(:key) || q
|
||||||
|
regexp = q.empty? ? nil :
|
||||||
|
Regexp.new(q.split(//).inject('') { |sum, e|
|
||||||
e = Regexp.escape e
|
e = Regexp.escape e
|
||||||
sum << "#{e}[^#{e}]*?"
|
sum << "#{e}[^#{e}]*?"
|
||||||
}, Regexp::IGNORECASE)
|
}, Regexp::IGNORECASE)
|
||||||
|
|
||||||
matches =
|
matches =
|
||||||
if fcache.has_key?(pquery)
|
if fcache.has_key?(q)
|
||||||
@stat.hit += 1
|
@stat[:hit] += 1
|
||||||
fcache[pquery]
|
fcache[q]
|
||||||
else
|
else
|
||||||
@smtx.synchronize do
|
@smtx.synchronize do
|
||||||
print_info true, ' ..'
|
print_info true, ' ..'
|
||||||
refresh
|
refresh
|
||||||
end unless pquery.empty?
|
end unless q.empty?
|
||||||
|
|
||||||
found = @lists.map { |pair|
|
found = @lists.map { |pair|
|
||||||
list, cache = pair
|
list, cache = pair
|
||||||
|
|
||||||
if cache[pquery]
|
if cache[q]
|
||||||
@stat.partial_hit += 1
|
@stat[:partial_hit] += 1
|
||||||
cache[pquery]
|
cache[q]
|
||||||
else
|
else
|
||||||
prefix_cache = nil
|
prefix_cache = nil
|
||||||
(pquery.length - 1).downto(1) do |len|
|
(q.length - 1).downto(1) do |len|
|
||||||
prefix = pquery[0, len]
|
prefix = q[0, len]
|
||||||
if prefix_cache = cache[prefix]
|
if prefix_cache = cache[prefix]
|
||||||
@stat.prefix_hit += 1
|
@stat[:prefix_hit] += 1
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|
|
cache[q] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|
|
||||||
if regexp
|
if regexp
|
||||||
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
||||||
md = line.match(regexp) rescue nil
|
md = line.match(regexp) rescue nil
|
||||||
@ -258,12 +252,12 @@ searcher = Thread.new {
|
|||||||
}.compact
|
}.compact
|
||||||
end
|
end
|
||||||
}.inject([]) { |all, e| all.concat e }
|
}.inject([]) { |all, e| all.concat e }
|
||||||
fcache[pquery] = @sort ? found : found.reverse
|
fcache[q] = @sort ? found : found.reverse
|
||||||
end
|
end
|
||||||
@stat.search += 1
|
@stat[:search] += 1
|
||||||
|
|
||||||
new_length = matches.length
|
mcount = matches.length
|
||||||
if @sort && new_length <= MAX_SORT_LEN
|
if @sort && mcount <= MAX_SORT_LEN
|
||||||
matches.replace matches.sort_by { |pair|
|
matches.replace matches.sort_by { |pair|
|
||||||
line, offset = pair
|
line, offset = pair
|
||||||
[offset.last - offset.first, line.length, line]
|
[offset.last - offset.first, line.length, line]
|
||||||
@ -272,21 +266,21 @@ searcher = Thread.new {
|
|||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
# This small delay reduces the number of partial lists
|
# This small delay reduces the number of partial lists
|
||||||
sleep 0.2 if !query_changed && !vcursor_moved
|
sleep 0.2 unless user_input
|
||||||
|
|
||||||
if vcursor_moved || new_search
|
if events.delete(:vcursor) || new_search
|
||||||
@mtx.synchronize do
|
@mtx.synchronize do
|
||||||
plength = [@matches.length, max_items].min
|
plcount = [@matches.length, max_items].min
|
||||||
@matches = matches
|
@matches = matches
|
||||||
pvcursor = @vcursor = [0, [@vcursor, new_length - 1, max_items - 1].min].max
|
vcursor = @vcursor = [0, [@vcursor, mcount - 1, max_items - 1].min].max
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Output
|
# Output
|
||||||
@smtx.synchronize do
|
@smtx.synchronize do
|
||||||
item_length = [new_length, max_items].min
|
item_length = [mcount, max_items].min
|
||||||
if item_length < plength
|
if item_length < plcount
|
||||||
plength.downto(item_length) do |idx|
|
plcount.downto(item_length) do |idx|
|
||||||
C.setpos cursor_y - idx - 2, 0
|
C.setpos cursor_y - idx - 2, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
end
|
end
|
||||||
@ -294,11 +288,11 @@ searcher = Thread.new {
|
|||||||
|
|
||||||
maxc = C.cols - 3
|
maxc = C.cols - 3
|
||||||
matches[0, max_items].each_with_index do |item, idx|
|
matches[0, max_items].each_with_index do |item, idx|
|
||||||
next if !new_search && !((pvcursor-1)..(pvcursor+1)).include?(idx)
|
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
|
||||||
|
|
||||||
line, offset = item
|
line, offset = item
|
||||||
row = cursor_y - idx - 2
|
row = cursor_y - idx - 2
|
||||||
chosen = idx == pvcursor
|
chosen = idx == vcursor
|
||||||
|
|
||||||
if line.length > maxc
|
if line.length > maxc
|
||||||
line = line[0, maxc] + '..'
|
line = line[0, maxc] + '..'
|
||||||
@ -321,13 +315,13 @@ searcher = Thread.new {
|
|||||||
C.attroff C.color_pair(3) | C::A_BOLD if chosen
|
C.attroff C.color_pair(3) | C::A_BOLD if chosen
|
||||||
end
|
end
|
||||||
|
|
||||||
print_info if !@lists.empty? || ploaded
|
print_info if !@lists.empty? || events[:loaded]
|
||||||
print_input
|
print_input
|
||||||
refresh
|
refresh
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@main.raise e
|
main.raise e
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -349,18 +343,8 @@ begin
|
|||||||
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
||||||
ctrl(:a) => proc { cursor = 0 },
|
ctrl(:a) => proc { cursor = 0 },
|
||||||
ctrl(:e) => proc { cursor = input.length },
|
ctrl(:e) => proc { cursor = input.length },
|
||||||
ctrl(:j) => proc {
|
ctrl(:j) => proc { emit(:vcursor) { @vcursor -= 1 } },
|
||||||
@mtx.synchronize do
|
ctrl(:k) => proc { emit(:vcursor) { @vcursor += 1 } },
|
||||||
@vcursor -= 1
|
|
||||||
@cv.broadcast
|
|
||||||
end
|
|
||||||
},
|
|
||||||
ctrl(:k) => proc {
|
|
||||||
@mtx.synchronize do
|
|
||||||
@vcursor += 1
|
|
||||||
@cv.broadcast
|
|
||||||
end
|
|
||||||
},
|
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
||||||
input = input[0...ridx] + input[cursor..-1]
|
input = input[0...ridx] + input[cursor..-1]
|
||||||
@ -400,10 +384,7 @@ begin
|
|||||||
}).call(ord)
|
}).call(ord)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
@mtx.synchronize do
|
emit(:key) { @query = input.dup }
|
||||||
@query = input.dup
|
|
||||||
@cv.broadcast
|
|
||||||
end
|
|
||||||
|
|
||||||
# Update user input
|
# Update user input
|
||||||
@smtx.synchronize do
|
@smtx.synchronize do
|
||||||
|
Loading…
Reference in New Issue
Block a user