From 5b3af8ec1ece55ba3f888e4c4cc7b441c4e8643a Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 25 Oct 2013 18:31:16 +0900 Subject: [PATCH] Implement -s, +s, and -i options --- README.md | 15 ++++++++++++--- fzf | 29 +++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 654a408..45c01b9 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 60bd6a0..ba7da71 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]