summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2013-10-25 18:31:16 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2013-10-25 18:31:16 +0900
commit5b3af8ec1ece55ba3f888e4c4cc7b441c4e8643a (patch)
treee356d72df3b8e97b82db5158efb2b9899b9eafa2
parent7f2ffb97468daa67b5cd3a84d57e2dd288454020 (diff)
Implement -s, +s, and -i options
-rw-r--r--README.md15
-rwxr-xr-xfzf29
2 files changed, 35 insertions, 9 deletions
diff --git a/README.md b/README.md
index 654a4084..45c01b9d 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,14 @@ You can use any plugin manager. If you don't use one, I recommend you try
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
selected item to STDOUT.
@@ -69,10 +77,11 @@ files (excluding hidden ones).
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
-history | fzf --no-sort
+history | fzf +s
```
### Key binding
@@ -123,7 +132,7 @@ fda() {
# fh - repeat history
fh() {
- eval $(history | fzf --no-sort | sed 's/ *[0-9]* *//')
+ eval $(history | fzf +s | sed 's/ *[0-9]* *//')
}
# fkill - kill process
diff --git a/fzf b/fzf
index 60bd6a01..ba7da71a 100755
--- a/fzf
+++ b/fzf
@@ -39,12 +39,29 @@ exec /usr/bin/env ruby -x "$0" $* 3>&1 1>&2 2>&3
#!ruby
# 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 'curses'
-MAX_SORT_LEN = 500
-C = Curses
-
@mtx = Mutex.new
@smtx = Mutex.new
@cv = ConditionVariable.new
@@ -56,7 +73,6 @@ C = Curses
@cursor_x = 0
@vcursor = 0
@events = {}
-@sort = ARGV.delete('--no-sort').nil?
@stat = { :hit => 0, :partial_hit => 0, :prefix_hit => 0, :search => 0 }
def emit event
@@ -66,6 +82,7 @@ def emit event
end
end
+C = Curses
def max_items; C.lines - 2; end
def cursor_y; C.lines - 1; end
def cprint str, col, flag = C::A_BOLD
@@ -213,7 +230,7 @@ searcher = Thread.new {
Regexp.new(q.split(//).inject('') { |sum, e|
e = Regexp.escape e
sum << "#{e}[^#{e}]*?"
- }, Regexp::IGNORECASE)
+ }, @rxflag)
matches =
if fcache.has_key?(q)
@@ -257,7 +274,7 @@ searcher = Thread.new {
@stat[:search] += 1
mcount = matches.length
- if @sort && mcount <= MAX_SORT_LEN
+ if @sort && mcount <= @sort
matches.replace matches.sort_by { |pair|
line, offset = pair
[offset.last - offset.first, line.length, line]