Implement -s, +s, and -i options

This commit is contained in:
Junegunn Choi 2013-10-25 18:31:16 +09:00
parent 7f2ffb9746
commit 5b3af8ec1e
2 changed files with 35 additions and 9 deletions

View File

@ -55,6 +55,14 @@ You can use any plugin manager. If you don't use one, I recommend you try
Usage Usage
----- -----
```
usage: fzf [options]
-s, --sort=MAX Maximum number of matched items to sort. Default: 500
+s, --no-sort Keep the sequence unchanged.
+i Case-sensitive match
```
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
selected item to STDOUT. selected item to STDOUT.
@ -69,10 +77,11 @@ files (excluding hidden ones).
vim `fzf` vim `fzf`
``` ```
If you do not want the matched items to be sorted, provide `--no-sort` option. If you want to preserve the exact sequence of the input, provide `--no-sort` (or
`+s`) option.
```sh ```sh
history | fzf --no-sort history | fzf +s
``` ```
### Key binding ### Key binding
@ -123,7 +132,7 @@ fda() {
# fh - repeat history # fh - repeat history
fh() { fh() {
eval $(history | fzf --no-sort | sed 's/ *[0-9]* *//') eval $(history | fzf +s | sed 's/ *[0-9]* *//')
} }
# fkill - kill process # fkill - kill process

29
fzf
View File

@ -39,12 +39,29 @@ exec /usr/bin/env ruby -x "$0" $* 3>&1 1>&2 2>&3
#!ruby #!ruby
# encoding: utf-8 # encoding: utf-8
def usage x
puts %[usage: fzf [options]
-s, --sort=MAX Maximum number of matched items to sort. Default: 500.
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
+i Case-sensitive match]
exit x
end
usage 0 unless (%w[--help -h] & ARGV).empty?
@rxflag = ARGV.delete('+i') ? 0 : Regexp::IGNORECASE
@sort = (ARGV.delete('+s') || ARGV.delete('--no-sort')) ? nil : 500
rest = ARGV.join ' '
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
usage 1 unless @sort
@sort = sort[2].to_i
rest = rest.delete sort[0]
end
usage 1 unless rest.empty?
require 'thread' require 'thread'
require 'curses' require 'curses'
MAX_SORT_LEN = 500
C = Curses
@mtx = Mutex.new @mtx = Mutex.new
@smtx = Mutex.new @smtx = Mutex.new
@cv = ConditionVariable.new @cv = ConditionVariable.new
@ -56,7 +73,6 @@ C = Curses
@cursor_x = 0 @cursor_x = 0
@vcursor = 0 @vcursor = 0
@events = {} @events = {}
@sort = ARGV.delete('--no-sort').nil?
@stat = { :hit => 0, :partial_hit => 0, :prefix_hit => 0, :search => 0 } @stat = { :hit => 0, :partial_hit => 0, :prefix_hit => 0, :search => 0 }
def emit event def emit event
@ -66,6 +82,7 @@ def emit event
end end
end end
C = Curses
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
def cprint str, col, flag = C::A_BOLD def cprint str, col, flag = C::A_BOLD
@ -213,7 +230,7 @@ searcher = Thread.new {
Regexp.new(q.split(//).inject('') { |sum, e| Regexp.new(q.split(//).inject('') { |sum, e|
e = Regexp.escape e e = Regexp.escape e
sum << "#{e}[^#{e}]*?" sum << "#{e}[^#{e}]*?"
}, Regexp::IGNORECASE) }, @rxflag)
matches = matches =
if fcache.has_key?(q) if fcache.has_key?(q)
@ -257,7 +274,7 @@ searcher = Thread.new {
@stat[:search] += 1 @stat[:search] += 1
mcount = matches.length mcount = matches.length
if @sort && mcount <= MAX_SORT_LEN if @sort && mcount <= @sort
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]