mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-15 06:43:47 -05:00
Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80819f3c44 | ||
|
|
7571baadb4 | ||
|
|
da03a66e69 | ||
|
|
3c47b7fa5f | ||
|
|
ba9365c438 | ||
|
|
db37e67575 | ||
|
|
76a3ef8c37 | ||
|
|
6fd6fff3a6 | ||
|
|
d1387bf512 | ||
|
|
4c923a2d19 | ||
|
|
4ee85f11e8 | ||
|
|
829c7f909c | ||
|
|
990fa00660 | ||
|
|
77592825f0 | ||
|
|
ce53b9b2a5 | ||
|
|
175fe158ed | ||
|
|
80efafcceb | ||
|
|
b241409e4b | ||
|
|
11967be017 | ||
|
|
6ee811ea03 | ||
|
|
d5e7303a25 | ||
|
|
2924fd3e23 | ||
|
|
75b44aac13 | ||
|
|
86c73105ee | ||
|
|
2d00abc7cb | ||
|
|
1e07b3b1c2 | ||
|
|
4313c1c25c | ||
|
|
cc9938d4c9 | ||
|
|
a54784cd53 | ||
|
|
22989b0488 | ||
|
|
892aa1e78b | ||
|
|
b9ab7d2413 | ||
|
|
69b2a0a733 | ||
|
|
13cd4ed546 | ||
|
|
7261d3afcd | ||
|
|
84fc73ad9c | ||
|
|
4103f5c3cc | ||
|
|
5390616694 | ||
|
|
daf08f801f | ||
|
|
4e2a1fe5c8 | ||
|
|
03f155484c | ||
|
|
89298a8d23 | ||
|
|
3b14c5230c | ||
|
|
91401514ab | ||
|
|
91d986b6c0 | ||
|
|
4d72bd098a | ||
|
|
502973ff75 | ||
|
|
3e91c189ae | ||
|
|
b0f80b686c | ||
|
|
b824928b0b | ||
|
|
ccca34f9f7 | ||
|
|
b5350b24ff | ||
|
|
56ace10a37 | ||
|
|
72ec0a3408 | ||
|
|
05118cc440 | ||
|
|
e392da20e8 | ||
|
|
6e69339f6b | ||
|
|
30cdc06bcd | ||
|
|
9ce43d46f6 | ||
|
|
de09656197 | ||
|
|
3827a1b09e |
9
.travis.yml
Normal file
9
.travis.yml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
language: ruby
|
||||||
|
rvm:
|
||||||
|
- "1.8.7"
|
||||||
|
- "1.9.3"
|
||||||
|
- "2.0.0"
|
||||||
|
- "2.1.1"
|
||||||
|
|
||||||
|
install: gem install curses minitest
|
||||||
|
|
||||||
70
README.md
70
README.md
@@ -51,8 +51,12 @@ Once you have cloned the repository, add the following line to your .vimrc.
|
|||||||
set rtp+=~/.fzf
|
set rtp+=~/.fzf
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you may use any Vim plugin manager, such as
|
Or you may use [vim-plug](https://github.com/junegunn/vim-plug) to manage fzf
|
||||||
[vim-plug](https://github.com/junegunn/vim-plug).
|
inside Vim:
|
||||||
|
|
||||||
|
```vim
|
||||||
|
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' }
|
||||||
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
@@ -61,36 +65,39 @@ Usage
|
|||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
Search
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END])
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
Search result
|
Search result
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
+c, --no-color Disable colors
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--reverse Reverse orientation
|
--reverse Reverse orientation
|
||||||
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
-q, --query=STR Start the finder with the given query
|
-q, --query=STR Start the finder with the given query
|
||||||
-1, --select-1 Automatically select the only match
|
-1, --select-1 Automatically select the only match
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
--print-query Print query as the first line
|
||||||
|
|
||||||
Environment variables
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
||||||
```
|
```
|
||||||
|
|
||||||
fzf will launch curses-based finder, read the list from STDIN, and write the
|
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||||
@@ -268,6 +275,14 @@ ssh **<TAB>
|
|||||||
telnet **<TAB>
|
telnet **<TAB>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Environment variables / Aliases
|
||||||
|
|
||||||
|
```sh
|
||||||
|
unset **<TAB>
|
||||||
|
export **<TAB>
|
||||||
|
unalias **<TAB>
|
||||||
|
```
|
||||||
|
|
||||||
#### Settings
|
#### Settings
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -322,6 +337,11 @@ let g:fzf_launcher = 'xterm -e bash -ic %s'
|
|||||||
let g:fzf_launcher = 'urxvt -geometry 120x30 -e sh -c %s'
|
let g:fzf_launcher = 'urxvt -geometry 120x30 -e sh -c %s'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you're running MacVim on OSX, I recommend you to use iTerm2 as the launcher.
|
||||||
|
Refer to the [this wiki
|
||||||
|
page](https://github.com/junegunn/fzf/wiki/fzf-with-MacVim-and-iTerm2) to see
|
||||||
|
how to set up.
|
||||||
|
|
||||||
### `fzf#run([options])`
|
### `fzf#run([options])`
|
||||||
|
|
||||||
For more advanced uses, you can call `fzf#run()` function which returns the list
|
For more advanced uses, you can call `fzf#run()` function which returns the list
|
||||||
@@ -457,7 +477,7 @@ speed of the traversal.
|
|||||||
```sh
|
```sh
|
||||||
# Copy the original fzf function to __fzf
|
# Copy the original fzf function to __fzf
|
||||||
declare -f __fzf > /dev/null ||
|
declare -f __fzf > /dev/null ||
|
||||||
eval "$(echo "__fzf() {"; declare -f fzf | grep -v '^{' | tail -n +2)"
|
eval "$(echo "__fzf() {"; declare -f fzf | \grep -v '^{' | tail -n +2)"
|
||||||
|
|
||||||
# Use git ls-tree when possible
|
# Use git ls-tree when possible
|
||||||
fzf() {
|
fzf() {
|
||||||
|
|||||||
1
Rakefile
1
Rakefile
@@ -6,3 +6,4 @@ Rake::TestTask.new(:test) do |test|
|
|||||||
test.verbose = true
|
test.verbose = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
task :default => :test
|
||||||
|
|||||||
393
fzf
393
fzf
@@ -7,7 +7,7 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# Version: 0.8.5 (Jun 15, 2014)
|
# Version: 0.8.8 (Nov 4, 2014)
|
||||||
#
|
#
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
@@ -36,8 +36,13 @@
|
|||||||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'curses'
|
||||||
|
rescue LoadError
|
||||||
|
$stderr.puts 'curses gem is not installed. Try `gem install curses`.'
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'curses'
|
|
||||||
require 'set'
|
require 'set'
|
||||||
|
|
||||||
unless String.method_defined? :force_encoding
|
unless String.method_defined? :force_encoding
|
||||||
@@ -48,29 +53,55 @@ unless String.method_defined? :force_encoding
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class String
|
||||||
|
attr_accessor :orig
|
||||||
|
|
||||||
|
def tokenize delim, nth
|
||||||
|
unless delim
|
||||||
|
# AWK default
|
||||||
|
prefix_length = (index(/\S/) || 0) rescue 0
|
||||||
|
tokens = scan(/\S+\s*/) rescue []
|
||||||
|
else
|
||||||
|
prefix_length = 0
|
||||||
|
tokens = scan(delim) rescue []
|
||||||
|
end
|
||||||
|
nth.map { |n|
|
||||||
|
if n.begin == 0 && n.end == -1
|
||||||
|
[prefix_length, tokens.join]
|
||||||
|
elsif part = tokens[n]
|
||||||
|
[prefix_length + (tokens[0...(n.begin)] || []).join.length,
|
||||||
|
part.join]
|
||||||
|
end
|
||||||
|
}.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse,
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
||||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
||||||
|
:print_query, :with_nth
|
||||||
|
|
||||||
class AtomicVar
|
def sync
|
||||||
def initialize value
|
@shr_mtx.synchronize { yield }
|
||||||
@value = value
|
end
|
||||||
@mutex = Mutex.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def get
|
def get name
|
||||||
@mutex.synchronize { @value }
|
sync { instance_variable_get name }
|
||||||
end
|
end
|
||||||
|
|
||||||
def set value = nil
|
def geta(*names)
|
||||||
@mutex.synchronize do
|
sync { names.map { |name| instance_variable_get name } }
|
||||||
@value = block_given? ? yield(@value) : value
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_missing sym, *args, &blk
|
def call(name, method, *args)
|
||||||
@mutex.synchronize { @value.send(sym, *args, &blk) }
|
sync { instance_variable_get(name).send(method, *args) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def set name, value = nil
|
||||||
|
sync do
|
||||||
|
instance_variable_set name,
|
||||||
|
(block_given? ? yield(instance_variable_get(name)) : value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -87,8 +118,12 @@ class FZF
|
|||||||
@exit0 = false
|
@exit0 = false
|
||||||
@filter = nil
|
@filter = nil
|
||||||
@nth = nil
|
@nth = nil
|
||||||
|
@with_nth = nil
|
||||||
@delim = nil
|
@delim = nil
|
||||||
@reverse = false
|
@reverse = false
|
||||||
|
@prompt = '> '
|
||||||
|
@shr_mtx = Mutex.new
|
||||||
|
@print_query = false
|
||||||
|
|
||||||
argv =
|
argv =
|
||||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||||
@@ -124,9 +159,9 @@ class FZF
|
|||||||
when '+0', '--no-exit-0' then @exit0 = false
|
when '+0', '--no-exit-0' then @exit0 = false
|
||||||
when '-q', '--query'
|
when '-q', '--query'
|
||||||
usage 1, 'query string required' unless query = argv.shift
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
@query = AtomicVar.new query.dup
|
@query = query
|
||||||
when /^-q(.*)$/, /^--query=(.*)$/
|
when /^-q(.*)$/, /^--query=(.*)$/
|
||||||
@query = AtomicVar.new($1)
|
@query = $1
|
||||||
when '-f', '--filter'
|
when '-f', '--filter'
|
||||||
usage 1, 'query string required' unless query = argv.shift
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
@filter = query
|
@filter = query
|
||||||
@@ -137,6 +172,11 @@ class FZF
|
|||||||
@nth = parse_nth nth
|
@nth = parse_nth nth
|
||||||
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
||||||
@nth = parse_nth $1
|
@nth = parse_nth $1
|
||||||
|
when '--with-nth'
|
||||||
|
usage 1, 'field expression required' unless nth = argv.shift
|
||||||
|
@with_nth = parse_nth nth
|
||||||
|
when /^--with-nth=([0-9,-\.]+)$/
|
||||||
|
@with_nth = parse_nth $1
|
||||||
when '-d', '--delimiter'
|
when '-d', '--delimiter'
|
||||||
usage 1, 'delimiter required' unless delim = argv.shift
|
usage 1, 'delimiter required' unless delim = argv.shift
|
||||||
@delim = FZF.build_delim_regex delim
|
@delim = FZF.build_delim_regex delim
|
||||||
@@ -148,6 +188,13 @@ class FZF
|
|||||||
@sort = sort.to_i
|
@sort = sort.to_i
|
||||||
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||||
@sort = $1.to_i
|
@sort = $1.to_i
|
||||||
|
when '--prompt'
|
||||||
|
usage 1, 'prompt string required' unless prompt = argv.shift
|
||||||
|
@prompt = prompt
|
||||||
|
when /^--prompt=(.*)$/
|
||||||
|
@prompt = $1
|
||||||
|
when '--print-query' then @print_query = true
|
||||||
|
when '--no-print-query' then @print_query = false
|
||||||
when '-e', '--extended-exact' then @extended = :exact
|
when '-e', '--extended-exact' then @extended = :exact
|
||||||
when '+e', '--no-extended-exact' then @extended = nil
|
when '+e', '--no-extended-exact' then @extended = nil
|
||||||
else
|
else
|
||||||
@@ -155,30 +202,35 @@ class FZF
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@source = source.clone
|
@source = source.clone
|
||||||
@mtx = Mutex.new
|
@evt_mtx = Mutex.new
|
||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@pending = nil
|
@pending = nil
|
||||||
|
@rev_dir = @reverse ? -1 : 1
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
|
||||||
unless @filter
|
unless @filter
|
||||||
@query ||= AtomicVar.new('')
|
# Shared variables: needs protection
|
||||||
@cursor_x = AtomicVar.new(@query.length)
|
@query ||= ''
|
||||||
@matches = AtomicVar.new([])
|
@matches = []
|
||||||
@count = AtomicVar.new(0)
|
@count = 0
|
||||||
@vcursor = AtomicVar.new(0)
|
@xcur = @query.length
|
||||||
@vcursors = AtomicVar.new(Set.new)
|
@ycur = 0
|
||||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
@yoff = 0
|
||||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
@dirty = Set.new
|
||||||
@main = Thread.current
|
@spinner = '-\|/-\|/'.split(//)
|
||||||
@plcount = 0
|
@selects = {} # ordered >= 1.9
|
||||||
|
|
||||||
|
@main = Thread.current
|
||||||
|
@plcount = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_nth nth
|
def parse_nth nth
|
||||||
nth.split(',').map { |expr|
|
ranges = nth.split(',').map { |expr|
|
||||||
x = proc { usage 1, "invalid field expression: #{expr}" }
|
x = proc { usage 1, "invalid field expression: #{expr}" }
|
||||||
first, second = expr.split('..', 2)
|
first, second = expr.split('..', 2)
|
||||||
x.call if !first.empty? && first.to_i == 0 ||
|
x.call if !first.empty? && first.to_i == 0 ||
|
||||||
@@ -193,6 +245,7 @@ class FZF
|
|||||||
|
|
||||||
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
||||||
}
|
}
|
||||||
|
ranges == [0..-1] ? nil : ranges
|
||||||
end
|
end
|
||||||
|
|
||||||
def FZF.build_delim_regex delim
|
def FZF.build_delim_regex delim
|
||||||
@@ -200,21 +253,28 @@ class FZF
|
|||||||
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def burp string, orig = nil
|
||||||
|
@stdout.puts(orig || string.orig || string)
|
||||||
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
if @filter
|
if @filter
|
||||||
start_reader.join
|
start_reader.join
|
||||||
filter_list @new
|
filter_list @new
|
||||||
else
|
else
|
||||||
start_reader
|
start_reader
|
||||||
emit(:key) { q = @query.get; [q, q.length] } unless empty = @query.empty?
|
query = get(:@query)
|
||||||
|
emit(:key) { [query, query.length] } unless empty = query.empty?
|
||||||
if @select1 || @exit0
|
if @select1 || @exit0
|
||||||
start_search do |loaded, matches|
|
start_search do |loaded, matches|
|
||||||
len = empty ? @count.get : matches.length
|
len = empty ? get(:@count) : matches.length
|
||||||
if loaded
|
if loaded
|
||||||
if @select1 && len == 1
|
if @select1 && len == 1
|
||||||
puts empty ? matches.first : matches.first.first
|
puts @query if @print_query
|
||||||
|
burp(empty ? matches.first : matches.first.first)
|
||||||
exit 0
|
exit 0
|
||||||
elsif @exit0 && len == 0
|
elsif @exit0 && len == 0
|
||||||
|
puts @query if @print_query
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -235,6 +295,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_list list
|
def filter_list list
|
||||||
|
puts @filter if @print_query
|
||||||
matches = matcher.match(list, @filter, '', '')
|
matches = matcher.match(list, @filter, '', '')
|
||||||
if @sort && matches.length <= @sort
|
if @sort && matches.length <= @sort
|
||||||
matches = FZF.sort(matches)
|
matches = FZF.sort(matches)
|
||||||
@@ -286,41 +347,44 @@ class FZF
|
|||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
Search
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END])
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
Search result
|
Search result
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
+c, --no-color Disable colors
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--reverse Reverse orientation
|
--reverse Reverse orientation
|
||||||
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
-q, --query=STR Start the finder with the given query
|
-q, --query=STR Start the finder with the given query
|
||||||
-1, --select-1 Automatically select the only match
|
-1, --select-1 Automatically select the only match
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
--print-query Print query as the first line
|
||||||
|
|
||||||
Environment variables
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||||
exit x
|
exit x
|
||||||
end
|
end
|
||||||
|
|
||||||
def emit event
|
def emit event
|
||||||
@mtx.synchronize do
|
@evt_mtx.synchronize do
|
||||||
@events[event] = yield
|
@events[event] = yield
|
||||||
@cv.broadcast
|
@cv.broadcast
|
||||||
end
|
end
|
||||||
@@ -338,39 +402,44 @@ class FZF
|
|||||||
end if str
|
end if str
|
||||||
end
|
end
|
||||||
def addstr_safe str
|
def addstr_safe str
|
||||||
C.addstr str.gsub("\0", '')
|
str = str.gsub("\0", '') rescue str
|
||||||
|
C.addstr str
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_input
|
def print_input
|
||||||
C.setpos cursor_y, 0
|
C.setpos cursor_y, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
cprint '> ', color(:prompt, true)
|
cprint @prompt, color(:prompt, true)
|
||||||
C.attron(C::A_BOLD) do
|
C.attron(C::A_BOLD) do
|
||||||
C.addstr @query.get
|
C.addstr get(:@query)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_info msg = nil
|
def print_info msg = nil
|
||||||
C.setpos cursor_y(1), 0
|
C.setpos cursor_y(1), 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
|
|
||||||
prefix =
|
prefix =
|
||||||
if spinner = @spinner.first
|
if spin_char = call(:@spinner, :first)
|
||||||
cprint spinner, color(:spinner, true)
|
cprint spin_char, color(:spinner, true)
|
||||||
' '
|
' '
|
||||||
else
|
else
|
||||||
' '
|
' '
|
||||||
end
|
end
|
||||||
C.attron color(:info, false) do
|
C.attron color(:info, false) do
|
||||||
C.addstr "#{prefix}#{@matches.length}/#{@count.get}"
|
sync do
|
||||||
if (selected = @selects.length) > 0
|
C.addstr "#{prefix}#{@matches.length}/#{@count}"
|
||||||
C.addstr " (#{selected})"
|
if (selected = @selects.length) > 0
|
||||||
|
C.addstr " (#{selected})"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
C.addstr msg if msg
|
C.addstr msg if msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get])
|
query, xcur = geta(:@query, :@xcur)
|
||||||
|
C.setpos cursor_y, @prompt.length + width(query[0, xcur])
|
||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -487,7 +556,6 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def init_screen
|
def init_screen
|
||||||
@stdout = $stdout.clone
|
|
||||||
$stdout.reopen($stderr)
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
C.init_screen
|
C.init_screen
|
||||||
@@ -562,14 +630,28 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
while line = stream.gets
|
if @with_nth
|
||||||
emit(:new) { @new << line.chomp }
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << transform(line) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << line.chomp }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@spinner.clear if @spinner
|
@spinner.clear if @spinner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform line
|
||||||
|
line = line.chomp
|
||||||
|
mut = (line =~ / $/ ? line : line + ' ').
|
||||||
|
tokenize(@delim, @with_nth).map { |e| e.last }.join('').sub(/ *$/, '')
|
||||||
|
mut.orig = line
|
||||||
|
mut
|
||||||
|
end
|
||||||
|
|
||||||
def start_search &callback
|
def start_search &callback
|
||||||
Thread.new do
|
Thread.new do
|
||||||
lists = []
|
lists = []
|
||||||
@@ -580,12 +662,12 @@ class FZF
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
while true
|
while true
|
||||||
@mtx.synchronize do
|
@evt_mtx.synchronize do
|
||||||
while true
|
while true
|
||||||
events.merge! @events
|
events.merge! @events
|
||||||
|
|
||||||
if @events.empty? # No new events
|
if @events.empty? # No new events
|
||||||
@cv.wait @mtx
|
@cv.wait @evt_mtx
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
@events.clear
|
@events.clear
|
||||||
@@ -594,8 +676,8 @@ class FZF
|
|||||||
|
|
||||||
if events[:new]
|
if events[:new]
|
||||||
lists << @new
|
lists << @new
|
||||||
@count.set { |c| c + @new.length }
|
set(:@count) { |c| c + @new.length }
|
||||||
@spinner.set { |spinner|
|
set(:@spinner) { |spinner|
|
||||||
if e = spinner.shift
|
if e = spinner.shift
|
||||||
spinner.push e
|
spinner.push e
|
||||||
end; spinner
|
end; spinner
|
||||||
@@ -619,10 +701,10 @@ class FZF
|
|||||||
cnt = 0
|
cnt = 0
|
||||||
lists.each do |list|
|
lists.each do |list|
|
||||||
cnt += list.length
|
cnt += list.length
|
||||||
skip = @mtx.synchronize { @events[:key] }
|
skip = @evt_mtx.synchronize { @events[:key] }
|
||||||
break if skip
|
break if skip
|
||||||
|
|
||||||
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
if !empty && (progress = 100 * cnt / get(:@count)) < 100 && Time.now - started_at > 0.5
|
||||||
render { print_info " (#{progress}%)" }
|
render { print_info " (#{progress}%)" }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -641,7 +723,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Atomic update
|
# Atomic update
|
||||||
@matches.set matches
|
set(:@matches, matches)
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
callback = nil if callback &&
|
callback = nil if callback &&
|
||||||
@@ -660,14 +742,45 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pick
|
def pick
|
||||||
items = @matches[0, max_items]
|
sync do
|
||||||
curr = [0, [@vcursor.get, items.length - 1].min].max
|
item = @matches[@ycur]
|
||||||
[*items.fetch(curr, [])][0]
|
item.is_a?(Array) ? item[0] : item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def constrain offset, cursor, count, height
|
||||||
|
original = [offset, cursor]
|
||||||
|
diffpos = cursor - offset
|
||||||
|
|
||||||
|
# Constrain cursor
|
||||||
|
cursor = [0, [cursor, count - 1].min].max
|
||||||
|
|
||||||
|
# Ceil
|
||||||
|
if cursor > offset + (height - 1)
|
||||||
|
offset = cursor - (height - 1)
|
||||||
|
# Floor
|
||||||
|
elsif offset > cursor
|
||||||
|
offset = cursor
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adjustment
|
||||||
|
if count - offset < height
|
||||||
|
offset = [0, count - height].max
|
||||||
|
cursor = [0, [offset + diffpos, count - 1].min].max
|
||||||
|
end
|
||||||
|
|
||||||
|
[[offset, cursor] != original, offset, cursor]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_list wipe
|
def update_list wipe
|
||||||
render do
|
render do
|
||||||
items = @matches[0, max_items]
|
pos, items = sync {
|
||||||
|
changed, @yoff, @ycur =
|
||||||
|
constrain(@yoff, @ycur, @matches.length, max_items)
|
||||||
|
wipe ||= changed
|
||||||
|
|
||||||
|
[@ycur - @yoff, @matches[@yoff, max_items]]
|
||||||
|
}
|
||||||
|
|
||||||
# Wipe
|
# Wipe
|
||||||
if items.length < @plcount
|
if items.length < @plcount
|
||||||
@@ -678,20 +791,18 @@ class FZF
|
|||||||
end
|
end
|
||||||
@plcount = items.length
|
@plcount = items.length
|
||||||
|
|
||||||
maxc = C.cols - 3
|
dirty = Set[pos]
|
||||||
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }
|
set(:@dirty) do |vs|
|
||||||
cleanse = Set[vcursor]
|
dirty.merge vs
|
||||||
@vcursors.set { |vs|
|
|
||||||
cleanse.merge vs
|
|
||||||
Set.new
|
Set.new
|
||||||
}
|
end
|
||||||
items.each_with_index do |item, idx|
|
items.each_with_index do |item, idx|
|
||||||
next unless wipe || cleanse.include?(idx)
|
next unless wipe || dirty.include?(idx)
|
||||||
row = cursor_y(idx + 2)
|
row = cursor_y(idx + 2)
|
||||||
chosen = idx == vcursor
|
chosen = idx == pos
|
||||||
selected = @selects.include?([*item][0])
|
selected = @selects.include?([*item][0])
|
||||||
line, offsets = item
|
line, offsets = item
|
||||||
tokens = format line, maxc, offsets
|
tokens = format line, C.cols - 3, offsets
|
||||||
print_item row, tokens, chosen, selected
|
print_item row, tokens, chosen, selected
|
||||||
end
|
end
|
||||||
print_info
|
print_info
|
||||||
@@ -720,7 +831,10 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def vselect &prc
|
def vselect &prc
|
||||||
@vcursor.set { |v| @vcursors << v; prc.call v }
|
sync do
|
||||||
|
@dirty << @ycur - @yoff
|
||||||
|
@ycur = prc.call @ycur
|
||||||
|
end
|
||||||
update_list false
|
update_list false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -777,7 +891,13 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_input actions
|
def get_input actions
|
||||||
@tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')
|
@tty ||=
|
||||||
|
begin
|
||||||
|
require 'io/console'
|
||||||
|
IO.console
|
||||||
|
rescue LoadError
|
||||||
|
IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
|
end
|
||||||
|
|
||||||
if pending = @pending
|
if pending = @pending
|
||||||
@pending = nil
|
@pending = nil
|
||||||
@@ -824,6 +944,8 @@ class FZF
|
|||||||
case read_nbs
|
case read_nbs
|
||||||
when [59, 50, 68] then ctrl(:a)
|
when [59, 50, 68] then ctrl(:a)
|
||||||
when [59, 50, 67] then ctrl(:e)
|
when [59, 50, 67] then ctrl(:e)
|
||||||
|
when [59, 53, 68] then :alt_b
|
||||||
|
when [59, 53, 67] then :alt_f
|
||||||
when [126] then ctrl(:a)
|
when [126] then ctrl(:a)
|
||||||
end
|
end
|
||||||
when 52 then read_nb; ctrl(:e)
|
when 52 then read_nb; ctrl(:e)
|
||||||
@@ -885,7 +1007,7 @@ class FZF
|
|||||||
def start_loop
|
def start_loop
|
||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
input = @query.get.dup
|
input = call(:@query, :dup)
|
||||||
cursor = input.length
|
cursor = input.length
|
||||||
yanked = ''
|
yanked = ''
|
||||||
mouse_event = MouseEvent.new
|
mouse_event = MouseEvent.new
|
||||||
@@ -912,8 +1034,8 @@ class FZF
|
|||||||
},
|
},
|
||||||
ctrl(:a) => proc { cursor = 0; nil },
|
ctrl(:a) => proc { cursor = 0; nil },
|
||||||
ctrl(:e) => proc { cursor = input.length; nil },
|
ctrl(:e) => proc { cursor = input.length; nil },
|
||||||
ctrl(:j) => proc { vselect { |v| v - (@reverse ? -1 : 1) } },
|
ctrl(:j) => proc { vselect { |v| v - @rev_dir } },
|
||||||
ctrl(:k) => proc { vselect { |v| v + (@reverse ? -1 : 1) } },
|
ctrl(:k) => proc { vselect { |v| v + @rev_dir } },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
pcursor = cursor
|
pcursor = cursor
|
||||||
backword.call
|
backword.call
|
||||||
@@ -924,26 +1046,28 @@ class FZF
|
|||||||
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
ctrl(:i) => proc { |o|
|
ctrl(:i) => proc { |o|
|
||||||
if @multi && sel = pick
|
if @multi && sel = pick
|
||||||
if @selects.has_key? sel
|
sync do
|
||||||
@selects.delete sel
|
if @selects.has_key? sel
|
||||||
else
|
@selects.delete sel
|
||||||
@selects[sel] = 1
|
else
|
||||||
|
@selects[sel] = sel.orig
|
||||||
|
end
|
||||||
end
|
end
|
||||||
vselect { |v| v + case o
|
vselect { |v| v + case o
|
||||||
when :stab then 1
|
when :stab then 1
|
||||||
when :sclick then 0
|
when :sclick then 0
|
||||||
else -1
|
else -1
|
||||||
end * (@reverse ? -1 : 1) }
|
end * @rev_dir }
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||||
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
||||||
:del => proc { input[cursor] = '' if input.length > cursor },
|
:del => proc { input[cursor] = '' if input.length > cursor },
|
||||||
:pgup => proc { vselect { |_| max_items } },
|
:pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } },
|
||||||
:pgdn => proc { vselect { |_| 0 } },
|
:pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } },
|
||||||
:alt_b => proc { backword.call; nil },
|
:alt_b => proc { backword.call; nil },
|
||||||
:alt_f => proc {
|
:alt_f => proc {
|
||||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||||
nil
|
nil
|
||||||
},
|
},
|
||||||
@@ -958,10 +1082,10 @@ class FZF
|
|||||||
when :click, :release
|
when :click, :release
|
||||||
x, y, shift = val.values_at :x, :y, :shift
|
x, y, shift = val.values_at :x, :y, :shift
|
||||||
y = @reverse ? (C.lines - 1 - y) : y
|
y = @reverse ? (C.lines - 1 - y) : y
|
||||||
if y == cursor_y
|
if y == C.lines - 1
|
||||||
cursor = [0, [input.length, x - 2].min].max
|
cursor = [0, [input.length, x - @prompt.length].min].max
|
||||||
elsif x > 1 && y <= max_items
|
elsif x > 1 && y <= max_items
|
||||||
tv = max_items - y - 1
|
tv = get(:@yoff) + max_items - y - 1
|
||||||
|
|
||||||
case event
|
case event
|
||||||
when :click
|
when :click
|
||||||
@@ -979,6 +1103,7 @@ class FZF
|
|||||||
actions[ctrl(:i)].call(:sclick) if shift
|
actions[ctrl(:i)].call(:sclick) if shift
|
||||||
actions[ctrl(diff > 0 ? :j : :k)].call
|
actions[ctrl(diff > 0 ? :j : :k)].call
|
||||||
end
|
end
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -989,24 +1114,26 @@ class FZF
|
|||||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||||
|
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
set(:@xcur, cursor)
|
||||||
render { print_input }
|
render { print_input }
|
||||||
|
|
||||||
if key = get_input(actions)
|
if key = get_input(actions)
|
||||||
upd = actions.fetch(key, actions[:default]).call(key)
|
upd = actions.fetch(key, actions[:default]).call(key)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
emit(:key) { [set(:@query, input.dup), cursor] } if upd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
|
q, selects = geta(:@query, :@selects)
|
||||||
|
@stdout.puts q if @print_query
|
||||||
if got
|
if got
|
||||||
if @selects.empty?
|
if selects.empty?
|
||||||
@stdout.puts got
|
burp got
|
||||||
else
|
else
|
||||||
@selects.each do |sel, _|
|
selects.each do |sel, orig|
|
||||||
@stdout.puts sel
|
burp sel, orig
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1031,27 +1158,15 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tokenize str
|
def tokenize str
|
||||||
@tokens_cache[str] ||=
|
@tokens_cache[str] ||= str.tokenize(@delim, @nth)
|
||||||
unless @delim
|
|
||||||
# AWK default
|
|
||||||
prefix_length = str[/^\s+/].length rescue 0
|
|
||||||
[prefix_length, (str.strip.scan(/\S+\s*/) rescue [])]
|
|
||||||
else
|
|
||||||
prefix_length = 0
|
|
||||||
[prefix_length, (str.scan(@delim) rescue [])]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_match str, pat
|
def do_match str, pat
|
||||||
if @nth
|
if @nth
|
||||||
prefix_length, tokens = tokenize str
|
tokenize(str).each do |pair|
|
||||||
|
prefix_length, token = pair
|
||||||
@nth.each do |n|
|
if md = token.match(pat) rescue nil
|
||||||
if (range = tokens[n]) && (token = range.join) &&
|
return MatchData.new(md.offset(0).map { |o| o + prefix_length })
|
||||||
(md = token.sub(/\s+$/, '').match(pat) rescue nil)
|
|
||||||
prefix_length += (tokens[0...(n.begin)] || []).join.length
|
|
||||||
offset = md.offset(0).map { |o| o + prefix_length }
|
|
||||||
return MatchData.new(offset)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
|
|||||||
@@ -8,12 +8,35 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
|
_fzf_orig_completion_filter() {
|
||||||
|
sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |
|
||||||
|
sed 's/[^a-z0-9_= ;]/_/g'
|
||||||
|
}
|
||||||
|
|
||||||
_fzf_opts_completion() {
|
_fzf_opts_completion() {
|
||||||
local cur prev opts
|
local cur prev opts
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
opts="-m --multi -x --extended -s --sort +s +i +c --no-color"
|
opts="
|
||||||
|
-x --extended
|
||||||
|
-e --extended-exact
|
||||||
|
-i +i
|
||||||
|
-n --nth
|
||||||
|
-d --delimiter
|
||||||
|
-s --sort +s
|
||||||
|
-m --multi
|
||||||
|
--no-mouse
|
||||||
|
+c --no-color
|
||||||
|
+2 --no-256
|
||||||
|
--black
|
||||||
|
--reverse
|
||||||
|
--prompt
|
||||||
|
-q --query
|
||||||
|
-1 --select-1
|
||||||
|
-0 --exit-0
|
||||||
|
-f --filter
|
||||||
|
--print-query"
|
||||||
|
|
||||||
case "${prev}" in
|
case "${prev}" in
|
||||||
--sort|-s)
|
--sort|-s)
|
||||||
@@ -30,8 +53,9 @@ _fzf_opts_completion() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_generic_completion() {
|
_fzf_path_completion() {
|
||||||
local cur base dir leftover matches trigger
|
local cur base dir leftover matches trigger cmd orig
|
||||||
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
@@ -46,7 +70,7 @@ _fzf_generic_completion() {
|
|||||||
leftover=${leftover/#\/}
|
leftover=${leftover/#\/}
|
||||||
[ "$dir" = './' ] && dir=''
|
[ "$dir" = './' ] && dir=''
|
||||||
tput sc
|
tput sc
|
||||||
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done)
|
done)
|
||||||
matches=${matches% }
|
matches=${matches% }
|
||||||
@@ -61,25 +85,63 @@ _fzf_generic_completion() {
|
|||||||
dir=$(dirname "$dir")
|
dir=$(dirname "$dir")
|
||||||
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
||||||
done
|
done
|
||||||
|
else
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
orig=$(eval "echo \$_fzf_orig_completion_$cmd")
|
||||||
|
[ -n "$orig" ] && type "$orig" > /dev/null 2>&1 && $orig "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_list_completion() {
|
||||||
|
local cur selected trigger cmd src ret
|
||||||
|
read -r src
|
||||||
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
if [[ ${cur} == *"$trigger" ]]; then
|
||||||
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
tput sc
|
||||||
|
selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
||||||
|
selected=${selected% }
|
||||||
|
tput rc
|
||||||
|
|
||||||
|
if [ -n "$selected" ]; then
|
||||||
|
COMPREPLY=("$selected")
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
shift
|
||||||
|
orig=$(eval "echo \$_fzf_orig_completion_$cmd")
|
||||||
|
if [ -n "$orig" ] && type "$orig" > /dev/null; then
|
||||||
|
$orig "$@"
|
||||||
|
elif [ -n "$_fzf_completion_loader" ]; then
|
||||||
|
_completion_loader "$@"
|
||||||
|
ret=$?
|
||||||
|
eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)
|
||||||
|
source $BASH_SOURCE
|
||||||
|
return $ret
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_all_completion() {
|
||||||
_fzf_generic_completion \
|
_fzf_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||||
"-m"
|
"-m" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_file_completion() {
|
_fzf_file_completion() {
|
||||||
_fzf_generic_completion \
|
_fzf_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
||||||
"-m"
|
"-m" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
_fzf_generic_completion \
|
_fzf_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||||
""
|
"" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_kill_completion() {
|
_fzf_kill_completion() {
|
||||||
@@ -97,64 +159,69 @@ _fzf_kill_completion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_telnet_completion() {
|
||||||
local cur selected trigger
|
_fzf_list_completion '+m' "$@" << "EOF"
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
EOF
|
||||||
[[ ${cur} == *"$trigger" ]] || return 1
|
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
|
||||||
|
|
||||||
tput sc
|
|
||||||
selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
|
||||||
tput rc
|
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
|
||||||
COMPREPLY=("$selected")
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_ssh_completion() {
|
||||||
local cur selected trigger
|
_fzf_list_completion '+m' "$@" << "EOF"
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts) | awk '{print $2}' | sort -u
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
EOF
|
||||||
[[ ${cur} == *"$trigger" ]] || return 1
|
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
|
||||||
|
|
||||||
tput sc
|
|
||||||
selected=$(cat \
|
|
||||||
<(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | grep -i ^host) \
|
|
||||||
<(grep -v '^\s*\(#\|$\)' /etc/hosts) | \
|
|
||||||
awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
|
||||||
tput rc
|
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
|
||||||
COMPREPLY=("$selected")
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_fzf_env_var_completion() {
|
||||||
|
_fzf_list_completion '-m' "$@" << "EOF"
|
||||||
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_alias_completion() {
|
||||||
|
_fzf_list_completion '-m' "$@" << "EOF"
|
||||||
|
alias | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# fzf options
|
||||||
complete -F _fzf_opts_completion fzf
|
complete -F _fzf_opts_completion fzf
|
||||||
|
|
||||||
|
d_cmds="cd pushd rmdir"
|
||||||
|
f_cmds="
|
||||||
|
awk cat diff diff3
|
||||||
|
emacs ex file ftp g++ gcc gvim head hg java
|
||||||
|
javac ld less more mvim patch perl python ruby
|
||||||
|
sed sftp sort source tail tee uniq vi view vim wc"
|
||||||
|
a_cmds="
|
||||||
|
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||||
|
find git grep gunzip gzip hg jar
|
||||||
|
ln ls mv open rm rsync scp
|
||||||
|
svn tar unzip zip"
|
||||||
|
x_cmds="kill ssh telnet unset unalias export"
|
||||||
|
|
||||||
|
# Preserve existing completion
|
||||||
|
if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then
|
||||||
|
# Really wish I could use associative array but OSX comes with bash 3.2 :(
|
||||||
|
eval $(complete | \grep '\-F' | \grep -v _fzf_ |
|
||||||
|
\grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
|
||||||
|
export _fzf_completion_loaded=0.8.6-1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type _completion_loader > /dev/null 2>&1; then
|
||||||
|
_fzf_completion_loader=1
|
||||||
|
fi
|
||||||
|
|
||||||
# Directory
|
# Directory
|
||||||
for cmd in "cd pushd rmdir"; do
|
for cmd in $d_cmds; do
|
||||||
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
# File
|
# File
|
||||||
for cmd in "
|
for cmd in $f_cmds; do
|
||||||
awk cat diff diff3
|
|
||||||
emacs ex file ftp g++ gcc gvim head hg java
|
|
||||||
javac ld less more mvim patch perl python ruby
|
|
||||||
sed sftp sort source tail tee uniq vi view vim wc"; do
|
|
||||||
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
# Anything
|
# Anything
|
||||||
for cmd in "
|
for cmd in $a_cmds; do
|
||||||
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
|
||||||
find git grep gunzip gzip hg jar
|
|
||||||
ln ls mv open rm rsync scp
|
|
||||||
svn tar unzip zip"; do
|
|
||||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -165,3 +232,9 @@ complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
|||||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
||||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
||||||
|
|
||||||
|
# Environment variables / Aliases
|
||||||
|
complete -F _fzf_env_var_completion -o default -o bashdefault unset
|
||||||
|
complete -F _fzf_env_var_completion -o default -o bashdefault export
|
||||||
|
complete -F _fzf_alias_completion -o default -o bashdefault unalias
|
||||||
|
|
||||||
|
unset cmd d_cmds f_cmds a_cmds x_cmds
|
||||||
|
|||||||
93
install
93
install
@@ -28,7 +28,11 @@ if [ $? -eq 0 ]; then
|
|||||||
else
|
else
|
||||||
echo "Not found"
|
echo "Not found"
|
||||||
echo "Installing 'curses' gem ... "
|
echo "Installing 'curses' gem ... "
|
||||||
/usr/bin/env gem install curses -v 1.0.0 --user-install
|
if (( EUID )); then
|
||||||
|
/usr/bin/env gem install curses --user-install
|
||||||
|
else
|
||||||
|
/usr/bin/env gem install curses
|
||||||
|
fi
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo
|
echo
|
||||||
echo "Failed to install 'curses' gem."
|
echo "Failed to install 'curses' gem."
|
||||||
@@ -98,7 +102,7 @@ EOF
|
|||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
__fsel() {
|
__fsel() {
|
||||||
find * -path '*/\.*' -prune \
|
command find * -path '*/\.*' -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||||
@@ -122,13 +126,13 @@ __fsel_tmux() {
|
|||||||
|
|
||||||
__fcd() {
|
__fcd() {
|
||||||
local dir
|
local dir
|
||||||
dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir"
|
dir=$(command find -L ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
__use_tmux=0
|
__use_tmux=0
|
||||||
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
||||||
|
|
||||||
if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
if [ -z "$(set -o | \grep '^vi.*on')" ]; then
|
||||||
# Required to refresh the prompt after fzf
|
# Required to refresh the prompt after fzf
|
||||||
bind '"\er": redraw-current-line'
|
bind '"\er": redraw-current-line'
|
||||||
|
|
||||||
@@ -140,7 +144,7 @@ if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s +m -n..,1,2.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
||||||
@@ -155,12 +159,15 @@ else
|
|||||||
else
|
else
|
||||||
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
||||||
fi
|
fi
|
||||||
|
bind -m vi-command '"\C-t": "i\C-t"'
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s +m -n..,1,2.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s +m -n2..,.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
||||||
|
bind -m vi-command '"\C-r": "i\C-r"'
|
||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
||||||
|
bind -m vi-command '"\ec": "i\ec"'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset __use_tmux
|
unset __use_tmux
|
||||||
@@ -174,7 +181,7 @@ EOFZF
|
|||||||
# CTRL-T - Paste the selected file path(s) into the command line
|
# CTRL-T - Paste the selected file path(s) into the command line
|
||||||
__fsel() {
|
__fsel() {
|
||||||
set -o nonomatch
|
set -o nonomatch
|
||||||
find * -path '*/\.*' -prune \
|
command find * -path '*/\.*' -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||||
@@ -207,7 +214,7 @@ bindkey '^T' fzf-file-widget
|
|||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
fzf-cd-widget() {
|
fzf-cd-widget() {
|
||||||
cd "${$(set -o nonomatch; find * -path '*/\.*' -prune \
|
cd "${$(set -o nonomatch; command find -L * -path '*/\.*' -prune \
|
||||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||||
zle reset-prompt
|
zle reset-prompt
|
||||||
}
|
}
|
||||||
@@ -216,7 +223,7 @@ bindkey '\ec' fzf-cd-widget
|
|||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
fzf-history-widget() {
|
fzf-history-widget() {
|
||||||
LBUFFER=$(fc -l 1 | fzf +s +m -n..,1,2.. | sed "s/ *[0-9*]* *//")
|
LBUFFER=$(fc -l 1 | fzf +s +m -n2..,.. | sed "s/ *[0-9*]* *//")
|
||||||
zle redisplay
|
zle redisplay
|
||||||
}
|
}
|
||||||
zle -N fzf-history-widget
|
zle -N fzf-history-widget
|
||||||
@@ -247,46 +254,58 @@ EOFZF
|
|||||||
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
||||||
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
||||||
function fzf_key_bindings
|
function fzf_key_bindings
|
||||||
function __fzf_select
|
# Due to a bug of fish, we cannot use command substitution,
|
||||||
find * -path '*/\.*' -prune \
|
# so we use temporary file instead
|
||||||
|
if [ -z "$TMPDIR" ]
|
||||||
|
set -g TMPDIR /tmp
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_list
|
||||||
|
command find * -path '*/\.*' -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | fzf -m | while read item
|
-o -type l -print 2> /dev/null
|
||||||
echo -n (echo -n "$item" | sed 's/ /\\\\ /g')' '
|
end
|
||||||
|
|
||||||
|
function __fzf_list_dir
|
||||||
|
command find -L * -path '*/\.*' -prune -o -type d -print 2> /dev/null
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_escape
|
||||||
|
while read item
|
||||||
|
echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '
|
||||||
end
|
end
|
||||||
echo
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_ctrl_t
|
function __fzf_ctrl_t
|
||||||
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
||||||
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
||||||
else
|
else
|
||||||
__fzf_select > $TMPDIR/fzf.result
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||||
and commandline -i (cat $TMPDIR/fzf.result)
|
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||||
|
commandline -f repaint
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_ctrl_t_tmux
|
function __fzf_ctrl_t_tmux
|
||||||
__fzf_select > $TMPDIR/fzf.result
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||||
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result)
|
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_ctrl_r
|
function __fzf_ctrl_r
|
||||||
if history | fzf +s +m > $TMPDIR/fzf.result
|
history | fzf +s +m > $TMPDIR/fzf.result
|
||||||
commandline (cat $TMPDIR/fzf.result)
|
and commandline (cat $TMPDIR/fzf.result)
|
||||||
else
|
commandline -f repaint
|
||||||
commandline -f repaint
|
|
||||||
end
|
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_alt_c
|
function __fzf_alt_c
|
||||||
find * -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m > $TMPDIR/fzf.result
|
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
||||||
if [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
__fzf_list_dir | fzf +m > $TMPDIR/fzf.result
|
||||||
cd (cat $TMPDIR/fzf.result)
|
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
||||||
end
|
and cd (cat $TMPDIR/fzf.result)
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
@@ -297,7 +316,7 @@ function fzf_key_bindings
|
|||||||
else
|
else
|
||||||
set height 40%
|
set height 40%
|
||||||
end
|
end
|
||||||
if echo $height | grep -q -E '%$'
|
if echo $height | \grep -q -E '%$'
|
||||||
echo "-p "(echo $height | sed 's/%$//')
|
echo "-p "(echo $height | sed 's/%$//')
|
||||||
else
|
else
|
||||||
echo "-l $height"
|
echo "-l $height"
|
||||||
@@ -318,24 +337,34 @@ append_line() {
|
|||||||
echo "Update $2:"
|
echo "Update $2:"
|
||||||
echo " - $1"
|
echo " - $1"
|
||||||
[ -f "$2" ] || touch "$2"
|
[ -f "$2" ] || touch "$2"
|
||||||
line=$(grep -nF "$1" "$2" | sed 's/:.*//')
|
if [ $# -lt 3 ]; then
|
||||||
|
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||||
|
else
|
||||||
|
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||||
|
fi
|
||||||
if [ -n "$line" ]; then
|
if [ -n "$line" ]; then
|
||||||
echo " - Already exists (line #$line)"
|
echo " - Already exists: line #$line"
|
||||||
else
|
else
|
||||||
echo "$1" >> "$2"
|
echo "$1" >> "$2"
|
||||||
echo " - Added"
|
echo " + Added"
|
||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
echo
|
echo
|
||||||
for shell in bash zsh; do
|
for shell in bash zsh; do
|
||||||
append_line "source ~/.fzf.${shell}" ~/.${shell}rc
|
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
||||||
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
append_line "fzf_key_bindings" "$bind_file"
|
append_line "fzf_key_bindings" "$bind_file"
|
||||||
|
|
||||||
|
echo ' * Due to a known bug of fish, you may have issues running fzf on fish.'
|
||||||
|
echo ' * If that happens, try the following:'
|
||||||
|
echo ' - Remove ~/.config/fish/functions/fzf.fish'
|
||||||
|
echo ' - Place fzf executable in a directory included in $PATH'
|
||||||
|
echo
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
|||||||
@@ -25,22 +25,27 @@ let s:min_tmux_width = 10
|
|||||||
let s:min_tmux_height = 3
|
let s:min_tmux_height = 3
|
||||||
let s:default_tmux_height = '40%'
|
let s:default_tmux_height = '40%'
|
||||||
let s:launcher = 'xterm -e bash -ic %s'
|
let s:launcher = 'xterm -e bash -ic %s'
|
||||||
|
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
||||||
|
|
||||||
let s:cpo_save = &cpo
|
let s:cpo_save = &cpo
|
||||||
set cpo&vim
|
set cpo&vim
|
||||||
|
|
||||||
call system('type fzf')
|
function! s:fzf_exec()
|
||||||
if v:shell_error
|
if !exists('s:exec')
|
||||||
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
call system('type fzf')
|
||||||
if executable(s:fzf_rb)
|
if v:shell_error
|
||||||
let s:exec = s:fzf_rb
|
let s:exec = executable(s:fzf_rb) ? s:fzf_rb : ''
|
||||||
|
else
|
||||||
|
let s:exec = 'fzf'
|
||||||
|
endif
|
||||||
|
return s:fzf_exec()
|
||||||
|
elseif empty(s:exec)
|
||||||
|
unlet s:exec
|
||||||
|
throw 'fzf executable not found'
|
||||||
else
|
else
|
||||||
echoerr 'fzf executable not found'
|
return s:exec
|
||||||
finish
|
|
||||||
endif
|
endif
|
||||||
else
|
endfunction
|
||||||
let s:exec = 'fzf'
|
|
||||||
endif
|
|
||||||
|
|
||||||
function! s:tmux_enabled()
|
function! s:tmux_enabled()
|
||||||
if has('gui_running')
|
if has('gui_running')
|
||||||
@@ -71,6 +76,11 @@ function! fzf#run(...) abort
|
|||||||
let dict = exists('a:1') ? a:1 : {}
|
let dict = exists('a:1') ? a:1 : {}
|
||||||
let temps = { 'result': tempname() }
|
let temps = { 'result': tempname() }
|
||||||
let optstr = get(dict, 'options', '')
|
let optstr = get(dict, 'options', '')
|
||||||
|
try
|
||||||
|
let fzf_exec = s:fzf_exec()
|
||||||
|
catch
|
||||||
|
throw v:exception
|
||||||
|
endtry
|
||||||
|
|
||||||
if has_key(dict, 'source')
|
if has_key(dict, 'source')
|
||||||
let source = dict.source
|
let source = dict.source
|
||||||
@@ -87,7 +97,7 @@ function! fzf#run(...) abort
|
|||||||
else
|
else
|
||||||
let prefix = ''
|
let prefix = ''
|
||||||
endif
|
endif
|
||||||
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
let command = prefix.fzf_exec.' '.optstr.' > '.temps.result
|
||||||
|
|
||||||
if s:tmux_enabled() && s:tmux_splittable(dict)
|
if s:tmux_enabled() && s:tmux_splittable(dict)
|
||||||
return s:execute_tmux(dict, command, temps)
|
return s:execute_tmux(dict, command, temps)
|
||||||
@@ -103,7 +113,7 @@ function! s:tmux_splittable(dict)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:pushd(dict)
|
function! s:pushd(dict)
|
||||||
if has_key(a:dict, 'dir')
|
if !empty(get(a:dict, 'dir', ''))
|
||||||
let a:dict.prev_dir = getcwd()
|
let a:dict.prev_dir = getcwd()
|
||||||
execute 'chdir '.s:escape(a:dict.dir)
|
execute 'chdir '.s:escape(a:dict.dir)
|
||||||
endif
|
endif
|
||||||
@@ -127,19 +137,29 @@ function! s:execute(dict, command, temps)
|
|||||||
execute 'silent !'.command
|
execute 'silent !'.command
|
||||||
redraw!
|
redraw!
|
||||||
if v:shell_error
|
if v:shell_error
|
||||||
echohl Error
|
" Do not print error message on exit status 1
|
||||||
echo 'Error running ' . command
|
if v:shell_error > 1
|
||||||
|
echohl ErrorMsg
|
||||||
|
echo 'Error running ' . command
|
||||||
|
endif
|
||||||
return []
|
return []
|
||||||
else
|
else
|
||||||
return s:callback(a:dict, a:temps, 0)
|
return s:callback(a:dict, a:temps, 0)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute_tmux(dict, command, temps)
|
function! s:env_var(name)
|
||||||
if has_key(a:dict, 'dir')
|
if exists('$'.a:name)
|
||||||
let command = 'cd '.s:escape(a:dict.dir).' && '.a:command
|
return a:name . "='". substitute(expand('$'.a:name), "'", "'\\\\''", 'g') . "' "
|
||||||
else
|
else
|
||||||
let command = a:command
|
return ''
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:execute_tmux(dict, command, temps)
|
||||||
|
let command = s:env_var('FZF_DEFAULT_OPTS').s:env_var('FZF_DEFAULT_COMMAND').a:command
|
||||||
|
if !empty(get(a:dict, 'dir', ''))
|
||||||
|
let command = 'cd '.s:escape(a:dict.dir).' && '.command
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let splitopt = '-v'
|
let splitopt = '-v'
|
||||||
|
|||||||
290
test/test_fzf.rb
290
test/test_fzf.rb
@@ -1,14 +1,58 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'rubygems'
|
||||||
require 'curses'
|
require 'curses'
|
||||||
require 'timeout'
|
require 'timeout'
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
|
require 'tempfile'
|
||||||
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||||
ENV['FZF_EXECUTABLE'] = '0'
|
ENV['FZF_EXECUTABLE'] = '0'
|
||||||
load 'fzf'
|
load 'fzf'
|
||||||
|
|
||||||
|
class MockTTY
|
||||||
|
def initialize
|
||||||
|
@buffer = ''
|
||||||
|
@mutex = Mutex.new
|
||||||
|
@condv = ConditionVariable.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_nonblock sz
|
||||||
|
@mutex.synchronize do
|
||||||
|
take sz
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def take sz
|
||||||
|
if @buffer.length >= sz
|
||||||
|
ret = @buffer[0, sz]
|
||||||
|
@buffer = @buffer[sz..-1]
|
||||||
|
ret
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def getc
|
||||||
|
sleep 0.1
|
||||||
|
while true
|
||||||
|
@mutex.synchronize do
|
||||||
|
if char = take(1)
|
||||||
|
return char
|
||||||
|
else
|
||||||
|
@condv.wait(@mutex)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def << str
|
||||||
|
@mutex.synchronize do
|
||||||
|
@buffer << str
|
||||||
|
@condv.broadcast
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class TestFZF < MiniTest::Unit::TestCase
|
class TestFZF < MiniTest::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
ENV.delete 'FZF_DEFAULT_SORT'
|
ENV.delete 'FZF_DEFAULT_SORT'
|
||||||
@@ -24,15 +68,18 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.rxflag
|
assert_equal nil, fzf.rxflag
|
||||||
assert_equal true, fzf.mouse
|
assert_equal true, fzf.mouse
|
||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal true, fzf.ansi256
|
assert_equal true, fzf.ansi256
|
||||||
assert_equal '', fzf.query.get
|
assert_equal '', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal nil, fzf.filter
|
assert_equal nil, fzf.filter
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal false, fzf.reverse
|
assert_equal false, fzf.reverse
|
||||||
|
assert_equal '> ', fzf.prompt
|
||||||
|
assert_equal false, fzf.print_query
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_environment_variables
|
def test_environment_variables
|
||||||
@@ -43,12 +90,12 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
|
||||||
ENV['FZF_DEFAULT_OPTS'] =
|
ENV['FZF_DEFAULT_OPTS'] =
|
||||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +
|
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
||||||
'--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse'
|
'--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 10000, fzf.sort
|
assert_equal 10000, fzf.sort
|
||||||
assert_equal ' hello world ',
|
assert_equal ' hello world ',
|
||||||
fzf.query.get
|
fzf.query
|
||||||
assert_equal 'goodbye world',
|
assert_equal 'goodbye world',
|
||||||
fzf.filter
|
fzf.filter
|
||||||
assert_equal :fuzzy, fzf.extended
|
assert_equal :fuzzy, fzf.extended
|
||||||
@@ -60,14 +107,17 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.select1
|
assert_equal true, fzf.select1
|
||||||
assert_equal true, fzf.exit0
|
assert_equal true, fzf.exit0
|
||||||
assert_equal true, fzf.reverse
|
assert_equal true, fzf.reverse
|
||||||
|
assert_equal true, fzf.print_query
|
||||||
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
||||||
|
assert_equal [2..2, -3..-1, 1..1], fzf.with_nth
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_option_parser
|
def test_option_parser
|
||||||
# Long opts
|
# Long opts
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||||
--exit-0 --filter=howdy --extended-exact
|
--exit-0 --filter=howdy --extended-exact
|
||||||
--no-mouse --no-256 --nth=1 --reverse]
|
--no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)
|
||||||
|
--print-query]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
@@ -75,20 +125,24 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal false, fzf.mouse
|
assert_equal false, fzf.mouse
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal true, fzf.select1
|
assert_equal true, fzf.select1
|
||||||
assert_equal true, fzf.exit0
|
assert_equal true, fzf.exit0
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :exact, fzf.extended
|
assert_equal :exact, fzf.extended
|
||||||
assert_equal [0..0], fzf.nth
|
assert_equal [0..0], fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
assert_equal true, fzf.reverse
|
assert_equal true, fzf.reverse
|
||||||
|
assert_equal '(hi)', fzf.prompt
|
||||||
|
assert_equal true, fzf.print_query
|
||||||
|
|
||||||
# Long opts (left-to-right)
|
# Long opts (left-to-right)
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||||
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
||||||
--select-1 --exit-0 --no-select-1 --no-exit-0
|
--select-1 --exit-0 --no-select-1 --no-exit-0
|
||||||
--no-sort -i --color --no-multi --256
|
--no-sort -i --color --no-multi --256
|
||||||
--reverse --no-reverse]
|
--reverse --no-reverse --prompt (hi) --prompt=(HI)
|
||||||
|
--print-query --no-print-query]
|
||||||
assert_equal nil, fzf.sort
|
assert_equal nil, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
@@ -97,12 +151,14 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.mouse
|
assert_equal true, fzf.mouse
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'b', fzf.filter
|
assert_equal 'b', fzf.filter
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal [-2..-2], fzf.nth
|
assert_equal [-2..-2], fzf.nth
|
||||||
assert_equal false, fzf.reverse
|
assert_equal false, fzf.reverse
|
||||||
|
assert_equal '(HI)', fzf.prompt
|
||||||
|
assert_equal false, fzf.print_query
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||||
@@ -111,7 +167,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
assert_equal false, fzf.ansi256
|
assert_equal false, fzf.ansi256
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :fuzzy, fzf.extended
|
assert_equal :fuzzy, fzf.extended
|
||||||
assert_equal [2..2], fzf.nth
|
assert_equal [2..2], fzf.nth
|
||||||
@@ -129,7 +185,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.ansi256
|
assert_equal true, fzf.ansi256
|
||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'world', fzf.query.get
|
assert_equal 'world', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal 'world', fzf.filter
|
assert_equal 'world', fzf.filter
|
||||||
@@ -158,13 +214,12 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME Only on 1.9 or above
|
|
||||||
def test_width
|
def test_width
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 5, fzf.width('abcde')
|
assert_equal 5, fzf.width('abcde')
|
||||||
assert_equal 4, fzf.width('한글')
|
assert_equal 4, fzf.width('한글')
|
||||||
assert_equal 5, fzf.width('한글.')
|
assert_equal 5, fzf.width('한글.')
|
||||||
end
|
end if RUBY_VERSION >= '1.9'
|
||||||
|
|
||||||
def test_trim
|
def test_trim
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
@@ -177,7 +232,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
||||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
||||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
||||||
end
|
end if RUBY_VERSION >= '1.9'
|
||||||
|
|
||||||
def test_format
|
def test_format
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
@@ -553,89 +608,115 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')
|
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')
|
||||||
end
|
end
|
||||||
|
|
||||||
def stream_for str
|
def stream_for str, delay = 0
|
||||||
StringIO.new(str).tap do |sio|
|
StringIO.new(str).tap do |sio|
|
||||||
sio.instance_eval do
|
sio.instance_eval do
|
||||||
alias org_gets gets
|
alias org_gets gets
|
||||||
|
|
||||||
def gets
|
def gets
|
||||||
org_gets.tap { |e| sleep 0.5 unless e.nil? }
|
org_gets.tap { |e| sleep(@delay) unless e.nil? }
|
||||||
|
end
|
||||||
|
|
||||||
|
def reopen _
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
sio.instance_variable_set :@delay, delay
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_fzf_output opts, given, expected
|
||||||
|
stream = stream_for given
|
||||||
|
output = stream_for ''
|
||||||
|
|
||||||
|
def sorted_lines line
|
||||||
|
line.split($/).sort
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
tty = MockTTY.new
|
||||||
|
$stdout = output
|
||||||
|
fzf = FZF.new(opts, stream)
|
||||||
|
fzf.instance_variable_set :@tty, tty
|
||||||
|
thr = block_given? && Thread.new { yield tty }
|
||||||
|
fzf.start
|
||||||
|
thr && thr.join
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal sorted_lines(expected), sorted_lines(output.string)
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filter
|
||||||
|
{
|
||||||
|
%w[--filter=ol] => 'World',
|
||||||
|
%w[--filter=ol --print-query] => "ol\nWorld",
|
||||||
|
}.each do |opts, expected|
|
||||||
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1
|
def test_select_1
|
||||||
stream = stream_for "Hello\nWorld"
|
{
|
||||||
output = StringIO.new
|
%w[--query=ol --select-1] => 'World',
|
||||||
|
%w[--query=ol --select-1 --print-query] => "ol\nWorld",
|
||||||
begin
|
}.each do |opts, expected|
|
||||||
$stdout = output
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
FZF.new(%w[--query=ol --select-1], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal 'World', output.string.chomp
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1_without_query
|
def test_select_1_without_query
|
||||||
stream = stream_for "Hello World"
|
assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'
|
||||||
output = StringIO.new
|
|
||||||
|
|
||||||
begin
|
|
||||||
$stdout = output
|
|
||||||
FZF.new(%w[--select-1], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal 'Hello World', output.string.chomp
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1_ambiguity
|
def test_select_1_ambiguity
|
||||||
stream = stream_for "Hello\nWorld"
|
|
||||||
begin
|
begin
|
||||||
Timeout::timeout(3) do
|
Timeout::timeout(0.5) do
|
||||||
FZF.new(%w[--query=o --select-1], stream).start
|
assert_fzf_output %w[--query=o --select-1], "hello\nworld", "should not match"
|
||||||
end
|
end
|
||||||
flunk 'Should not reach here'
|
rescue Timeout::Error
|
||||||
rescue Exception => e
|
|
||||||
Curses.close_screen
|
Curses.close_screen
|
||||||
assert_instance_of Timeout::Error, e
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0
|
def test_exit_0
|
||||||
stream = stream_for "Hello\nWorld"
|
{
|
||||||
output = StringIO.new
|
%w[--query=zz --exit-0] => '',
|
||||||
|
%w[--query=zz --exit-0 --print-query] => 'zz',
|
||||||
begin
|
}.each do |opts, expected|
|
||||||
$stdout = output
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
FZF.new(%w[--query=zz --exit-0], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal '', output.string
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0_without_query
|
def test_exit_0_without_query
|
||||||
stream = stream_for ""
|
assert_fzf_output %w[--exit-0], '', ''
|
||||||
output = StringIO.new
|
end
|
||||||
|
|
||||||
begin
|
def test_with_nth
|
||||||
$stdout = output
|
source = "hello world\nbatman"
|
||||||
FZF.new(%w[--exit-0], stream).start
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],
|
||||||
rescue SystemExit => e
|
source, 'hello world'
|
||||||
assert_equal 0, e.status
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],
|
||||||
assert_equal '', output.string
|
source, 'hello world'
|
||||||
ensure
|
assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],
|
||||||
$stdout = STDOUT
|
source, ''
|
||||||
end
|
assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],
|
||||||
|
source, 'hello world'
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],
|
||||||
|
source, 'batman'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_transform
|
||||||
|
fzf = FZF.new %w[--with-nth 2..,1]
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world ')
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--with-nth 2,-1,2]
|
||||||
|
assert_equal 'my world my', fzf.transform('hello my world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ranking_overlap_match_regions
|
def test_ranking_overlap_match_regions
|
||||||
@@ -648,5 +729,82 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
['1 3 4 2', [[0, 24], [12, 17]]],
|
['1 3 4 2', [[0, 24], [12, 17]]],
|
||||||
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_constrain
|
||||||
|
fzf = FZF.new []
|
||||||
|
|
||||||
|
# [#**** ]
|
||||||
|
assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)
|
||||||
|
|
||||||
|
# *****[**#** ... ] => [**#******* ... ]
|
||||||
|
assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)
|
||||||
|
|
||||||
|
# [**********]**#** => ***[*********#]**
|
||||||
|
assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)
|
||||||
|
|
||||||
|
# *****[**#** ] => ***[**#****]
|
||||||
|
assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)
|
||||||
|
|
||||||
|
# *****[**#** ] => ****[**#***]
|
||||||
|
assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)
|
||||||
|
|
||||||
|
# ***** [#] => ****[#]
|
||||||
|
assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)
|
||||||
|
|
||||||
|
# [ ] #**** => [#]****
|
||||||
|
assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)
|
||||||
|
|
||||||
|
# [ ] **#** => **[#]**
|
||||||
|
assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)
|
||||||
|
|
||||||
|
# [***** #] => [****# ]
|
||||||
|
assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)
|
||||||
|
|
||||||
|
# **[***** #] => [******# ]
|
||||||
|
assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_utf8
|
||||||
|
tmp = Tempfile.new('fzf')
|
||||||
|
tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*')
|
||||||
|
tmp.close
|
||||||
|
begin
|
||||||
|
Timeout::timeout(0.5) do
|
||||||
|
FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start
|
||||||
|
end
|
||||||
|
rescue Timeout::Error
|
||||||
|
Curses.close_screen
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
tmp.unlink
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_mock_tty
|
||||||
|
# Manual selection with input
|
||||||
|
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||||
|
tty << "world"
|
||||||
|
tty << "hell"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection without input
|
||||||
|
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection with input and --multi
|
||||||
|
lines = "hello world\ngoodbye world"
|
||||||
|
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||||
|
tty << "o"
|
||||||
|
tty << "\e[Z\e[Z"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection without input and --multi
|
||||||
|
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||||
|
tty << "\e[Z\e[Z"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
24
uninstall
24
uninstall
@@ -18,18 +18,24 @@ remove() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
remove_line() {
|
remove_line() {
|
||||||
src=$(readlink "$2")
|
src=$(readlink "$1")
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "Remove from $2 ($src):"
|
echo "Remove from $1 ($src):"
|
||||||
else
|
else
|
||||||
src=$2
|
src=$1
|
||||||
echo "Remove from $2:"
|
echo "Remove from $1:"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
shift
|
||||||
line_no=1
|
line_no=1
|
||||||
match=0
|
match=0
|
||||||
while [ 1 ]; do
|
while [ -n "$1" ]; do
|
||||||
line=$(sed -n "$line_no,\$p" "$src" | grep -m1 -nF "$1") || break
|
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
shift
|
||||||
|
line_no=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
|
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
|
||||||
content=$(sed 's/^[0-9]*://' <<< "$line")
|
content=$(sed 's/^[0-9]*://' <<< "$line")
|
||||||
match=1
|
match=1
|
||||||
@@ -50,12 +56,14 @@ remove_line() {
|
|||||||
|
|
||||||
for shell in bash zsh; do
|
for shell in bash zsh; do
|
||||||
remove ~/.fzf.${shell}
|
remove ~/.fzf.${shell}
|
||||||
remove_line "source ~/.fzf.${shell}" ~/.${shell}rc
|
remove_line ~/.${shell}rc \
|
||||||
|
"[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" \
|
||||||
|
"source ~/.fzf.${shell}"
|
||||||
done
|
done
|
||||||
|
|
||||||
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
if [ -f "$bind_file" ]; then
|
if [ -f "$bind_file" ]; then
|
||||||
remove_line "fzf_key_bindings" "$bind_file"
|
remove_line "$bind_file" "fzf_key_bindings"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -d ~/.config/fish/functions ]; then
|
if [ -d ~/.config/fish/functions ]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user