mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-01 04:32:05 -07:00
Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
98d2bfa0db | ||
|
aec48f159b | ||
|
ad7e433a7d | ||
|
5a60aa5050 | ||
|
ebea470875 | ||
|
d980e00961 | ||
|
987799f8fb | ||
|
d2f3604c1d | ||
|
72cc558fdc | ||
|
6bc3fe6e67 | ||
|
9398878048 | ||
|
ca19762e58 | ||
|
8764be07e2 | ||
|
2022a3ad96 | ||
|
65d9d416b4 | ||
|
fa2f9f1f21 | ||
|
c656cfbdce | ||
|
de829c0938 | ||
|
64443221aa | ||
|
9017e29741 | ||
|
0a22142d88 | ||
|
ac160f98a8 | ||
|
62e01a2a62 | ||
|
5660cebaf6 | ||
|
a7e588ceac | ||
|
5baf1c5536 |
@@ -4,7 +4,7 @@ rvm:
|
||||
|
||||
install:
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -y libncurses-dev lib32ncurses5-dev
|
||||
- sudo apt-get install -y libncurses-dev lib32ncurses5-dev libgpm-dev
|
||||
- sudo add-apt-repository -y ppa:pi-rho/dev
|
||||
- sudo apt-add-repository -y ppa:fish-shell/release-2
|
||||
- sudo apt-get update
|
||||
|
19
CHANGELOG.md
19
CHANGELOG.md
@@ -1,6 +1,25 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.10.6
|
||||
------
|
||||
|
||||
- Replaced `--header-file` with `--header` option
|
||||
- `--header` and `--header-lines` can be used together
|
||||
- Changed exit status
|
||||
- 0: Okay
|
||||
- 1: No match
|
||||
- 2: Error
|
||||
- 130: Interrupted
|
||||
- 64-bit linux binary is statically-linked with ncurses to avoid
|
||||
compatibility issues.
|
||||
|
||||
0.10.5
|
||||
------
|
||||
|
||||
- `'`-prefix to unquote the term in `--extended-exact` mode
|
||||
- Backward scan when `--tiebreak=end` is set
|
||||
|
||||
0.10.4
|
||||
------
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<img src="https://raw.githubusercontent.com/junegunn/i/master/fzf.png" height="170" alt="fzf - a command-line fuzzy finder"> [](https://travis-ci.org/junegunn/fzf) <a href="http://flattr.com/thing/3115381/junegunnfzf-on-GitHub" target="_blank"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a>
|
||||
<img src="https://raw.githubusercontent.com/junegunn/i/master/fzf.png" height="170" alt="fzf - a command-line fuzzy finder"> [](https://travis-ci.org/junegunn/fzf)
|
||||
===
|
||||
|
||||
fzf is a general-purpose command-line fuzzy finder.
|
||||
@@ -124,8 +124,9 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
||||
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||
|
||||
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
||||
fzf with `-e` or `--extended-exact` option.
|
||||
If you don't prefer fuzzy matching and do not wish to "quote" every word,
|
||||
start fzf with `-e` or `--extended-exact` option. Note that in
|
||||
`--extended-exact` mode, `'`-prefix "unquotes" the term.
|
||||
|
||||
#### Environment variables
|
||||
|
||||
|
@@ -102,7 +102,7 @@ trap cleanup EXIT SIGINT SIGTERM
|
||||
|
||||
fail() {
|
||||
>&2 echo "$1"
|
||||
exit 1
|
||||
exit 2
|
||||
}
|
||||
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
||||
[ -x "$fzf" ] || fail "fzf executable not found"
|
||||
@@ -117,7 +117,9 @@ mkfifo $fifo3
|
||||
# Build arguments to fzf
|
||||
opts=""
|
||||
for arg in "${args[@]}"; do
|
||||
opts="$opts \"${arg//\"/\\\"}\""
|
||||
arg="${arg//\"/\\\"}"
|
||||
arg="${arg//\`/\\\`}"
|
||||
opts="$opts \"$arg\""
|
||||
done
|
||||
|
||||
if [ -n "$term" -o -t 0 ]; then
|
||||
@@ -132,5 +134,5 @@ else
|
||||
cat <&0 > $fifo1 &
|
||||
fi
|
||||
cat $fifo2
|
||||
[ "$(cat $fifo3)" = '0' ]
|
||||
exit "$(cat $fifo3)"
|
||||
|
||||
|
13
install
13
install
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
[[ "$@" =~ --pre ]] && version=0.10.4 pre=1 ||
|
||||
version=0.10.4 pre=0
|
||||
[[ "$@" =~ --pre ]] && version=0.10.6 pre=1 ||
|
||||
version=0.10.6 pre=0
|
||||
|
||||
cd $(dirname $BASH_SOURCE)
|
||||
fzf_base=$(pwd)
|
||||
@@ -80,12 +80,7 @@ download() {
|
||||
return
|
||||
fi
|
||||
|
||||
chmod +x $1 && symlink $1 || return 1
|
||||
if [[ $1 =~ linux_amd64$ ]]; then
|
||||
check_binary || download $1-static
|
||||
else
|
||||
check_binary
|
||||
fi
|
||||
chmod +x $1 && symlink $1 && check_binary
|
||||
}
|
||||
|
||||
# Try to download binary executable
|
||||
@@ -250,6 +245,7 @@ EOF
|
||||
fi
|
||||
|
||||
append_line() {
|
||||
set -e
|
||||
echo "Update $2:"
|
||||
echo " - $1"
|
||||
[ -f "$2" ] || touch "$2"
|
||||
@@ -266,6 +262,7 @@ append_line() {
|
||||
echo " + Added"
|
||||
fi
|
||||
echo
|
||||
set +e
|
||||
}
|
||||
|
||||
echo
|
||||
|
@@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
..
|
||||
.TH fzf 1 "Aug 2015" "fzf 0.10.4" "fzf - a command-line fuzzy finder"
|
||||
.TH fzf 1 "Sep 2015" "fzf 0.10.6" "fzf - a command-line fuzzy finder"
|
||||
|
||||
.SH NAME
|
||||
fzf - a command-line fuzzy finder
|
||||
@@ -285,11 +285,11 @@ When enabled, \fBCTRL-N\fR and \fBCTRL-P\fR are automatically remapped to
|
||||
Maximum number of entries in the history file (default: 1000). The file is
|
||||
automatically truncated when the number of the lines exceeds the value.
|
||||
.TP
|
||||
.BI "--header-file=" "FILE"
|
||||
The content of the file will be printed as the sticky header. The lines in the
|
||||
file are displayed in order from top to bottom regardless of \fB--reverse\fR,
|
||||
and are not affected by \fB--with-nth\fR. ANSI color codes are processed even
|
||||
when \fB--ansi\fR is not set.
|
||||
.BI "--header=" "STR"
|
||||
The given string will be printed as the sticky header. The lines are displayed
|
||||
in the given order from top to bottom regardless of \fB--reverse\fR option, and
|
||||
are not affected by \fB--with-nth\fR. ANSI color codes are processed even when
|
||||
\fB--ansi\fR is not set.
|
||||
.TP
|
||||
.BI "--header-lines=" "N"
|
||||
The first N lines of the input are treated as the sticky header. When
|
||||
@@ -374,29 +374,40 @@ mode". In this mode, you can specify multiple patterns delimited by spaces,
|
||||
such as: \fB'wild ^music .mp3$ sbtrkt !rmx\fR
|
||||
|
||||
.SS Exact-match (quoted)
|
||||
A term that is prefixed by a single-quote character (') is interpreted as an
|
||||
"exact-match" (or "non-fuzzy") term. fzf will search for the exact occurrences
|
||||
of the string.
|
||||
A term that is prefixed by a single-quote character (\fB'\fR) is interpreted as
|
||||
an "exact-match" (or "non-fuzzy") term. fzf will search for the exact
|
||||
occurrences of the string.
|
||||
|
||||
.SS Anchored-match
|
||||
A term can be prefixed by ^, or suffixed by $ to become an anchored-match term.
|
||||
Then fzf will search for the items that start with or end with the given
|
||||
string. An anchored-match term is also an exact-match term.
|
||||
A term can be prefixed by \fB^\fR, or suffixed by \fB$\fR to become an
|
||||
anchored-match term. Then fzf will search for the items that start with or end
|
||||
with the given string. An anchored-match term is also an exact-match term.
|
||||
|
||||
.SS Negation
|
||||
If a term is prefixed by !, fzf will exclude the items that satisfy the term
|
||||
from the result.
|
||||
If a term is prefixed by \fB!\fR, fzf will exclude the items that satisfy the
|
||||
term from the result.
|
||||
|
||||
.SS Extended-exact mode
|
||||
If you don't need fuzzy matching at all and do not wish to "quote" (prefixing
|
||||
with ') every word, start fzf with \fB-e\fR or \fB--extended-exact\fR option
|
||||
(instead of \fB-x\fR or \fB--extended\fR).
|
||||
If you don't prefer fuzzy matching and do not wish to "quote" (prefixing with
|
||||
\fB'\fR) every word, start fzf with \fB-e\fR or \fB--extended-exact\fR option
|
||||
(instead of \fB-x\fR or \fB--extended\fR). Note that in \fB--extended-exact\fR
|
||||
mode, \fB'\fR-prefix "unquotes" the term.
|
||||
|
||||
.SH AUTHOR
|
||||
Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
||||
|
||||
.SH SEE ALSO
|
||||
.B Project homepage:
|
||||
.RS
|
||||
.I https://github.com/junegunn/fzf
|
||||
.RE
|
||||
.br
|
||||
.R ""
|
||||
.br
|
||||
.B Extra Vim plugin:
|
||||
.RS
|
||||
.I https://github.com/junegunn/fzf.vim
|
||||
.RE
|
||||
|
||||
.SH LICENSE
|
||||
MIT
|
||||
|
@@ -40,9 +40,7 @@ function! s:fzf_exec()
|
||||
\ input('fzf executable not found. Download binary? (y/n) ') =~? '^y'
|
||||
redraw
|
||||
echo
|
||||
echohl WarningMsg
|
||||
echo 'Downloading fzf binary. Please wait ...'
|
||||
echohl None
|
||||
call s:warn('Downloading fzf binary. Please wait ...')
|
||||
let s:installed = 1
|
||||
call system(s:install.' --bin')
|
||||
return s:fzf_exec()
|
||||
@@ -98,14 +96,24 @@ function! s:upgrade(dict)
|
||||
return copy
|
||||
endfunction
|
||||
|
||||
function! s:error(msg)
|
||||
echohl ErrorMsg
|
||||
echom a:msg
|
||||
echohl None
|
||||
endfunction
|
||||
|
||||
function! s:warn(msg)
|
||||
echohl WarningMsg
|
||||
echom a:msg
|
||||
echohl None
|
||||
endfunction
|
||||
|
||||
function! fzf#run(...) abort
|
||||
try
|
||||
let oshell = &shell
|
||||
set shell=sh
|
||||
if has('nvim') && bufexists('term://*:FZF')
|
||||
echohl WarningMsg
|
||||
echomsg 'FZF is already running!'
|
||||
echohl None
|
||||
call s:warn('FZF is already running!')
|
||||
return []
|
||||
endif
|
||||
let dict = exists('a:1') ? s:upgrade(a:1) : {}
|
||||
@@ -217,20 +225,20 @@ let s:launcher = function('s:xterm_launcher')
|
||||
function! s:execute(dict, command, temps)
|
||||
call s:pushd(a:dict)
|
||||
silent! !clear 2> /dev/null
|
||||
let escaped = escape(substitute(a:command, '\n', '\\n', 'g'), '%#')
|
||||
if has('gui_running')
|
||||
let Launcher = get(a:dict, 'launcher', get(g:, 'Fzf_launcher', get(g:, 'fzf_launcher', s:launcher)))
|
||||
let fmt = type(Launcher) == 2 ? call(Launcher, []) : Launcher
|
||||
let command = printf(fmt, "'".substitute(a:command, "'", "'\"'\"'", 'g')."'")
|
||||
let command = printf(fmt, "'".substitute(escaped, "'", "'\"'\"'", 'g')."'")
|
||||
else
|
||||
let command = a:command
|
||||
let command = escaped
|
||||
endif
|
||||
execute 'silent !'.escape(command, '%#')
|
||||
execute 'silent !'.command
|
||||
redraw!
|
||||
if v:shell_error
|
||||
" Do not print error message on exit status 1
|
||||
if v:shell_error > 1
|
||||
echohl ErrorMsg
|
||||
echo 'Error running ' . command
|
||||
" Do not print error message on exit status 1 (no match) or 130 (interrupt)
|
||||
if v:shell_error == 2
|
||||
call s:error('Error running ' . command)
|
||||
endif
|
||||
return []
|
||||
else
|
||||
@@ -247,6 +255,10 @@ function! s:execute_tmux(dict, command, temps)
|
||||
|
||||
call system(command)
|
||||
redraw!
|
||||
if v:shell_error == 2
|
||||
call s:error('Error running ' . command)
|
||||
return []
|
||||
endif
|
||||
return s:callback(a:dict, a:temps)
|
||||
endfunction
|
||||
|
||||
@@ -308,6 +320,7 @@ function! s:execute_term(dict, command, temps)
|
||||
call s:pushd(a:dict)
|
||||
|
||||
let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps, 'name': 'FZF' }
|
||||
let s:command = a:command
|
||||
function! fzf.on_exit(id, code)
|
||||
let pos = s:getpos()
|
||||
let inplace = pos == s:ppos " {'window': 'enew'}
|
||||
@@ -321,9 +334,15 @@ function! s:execute_term(dict, command, temps)
|
||||
wincmd p
|
||||
endif
|
||||
endif
|
||||
|
||||
if a:code == 2
|
||||
call s:error('Error running ' . s:command)
|
||||
sleep
|
||||
return
|
||||
endif
|
||||
|
||||
call s:pushd(self.dict)
|
||||
try
|
||||
redraw!
|
||||
call s:callback(self.dict, self.temps)
|
||||
|
||||
if inplace && bufnr('') == self.buf
|
||||
|
@@ -49,7 +49,7 @@ _fzf_opts_completion() {
|
||||
--cycle
|
||||
--history
|
||||
--history-size
|
||||
--header-file
|
||||
--header
|
||||
--header-lines
|
||||
--margin"
|
||||
|
||||
@@ -62,7 +62,7 @@ _fzf_opts_completion() {
|
||||
COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
--history|--header-file)
|
||||
--history)
|
||||
COMPREPLY=()
|
||||
return 0
|
||||
;;
|
||||
|
@@ -99,7 +99,11 @@ EOF
|
||||
}
|
||||
|
||||
fzf-completion() {
|
||||
local tokens cmd prefix trigger tail fzf matches lbuf d_cmds
|
||||
local tokens cmd prefix trigger tail fzf matches lbuf d_cmds sws
|
||||
if setopt | grep shwordsplit > /dev/null; then
|
||||
sws=1
|
||||
unsetopt shwordsplit
|
||||
fi
|
||||
|
||||
# http://zsh.sourceforge.net/FAQ/zshfaq03.html
|
||||
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
||||
@@ -148,6 +152,7 @@ fzf-completion() {
|
||||
else
|
||||
eval "zle ${fzf_default_completion:-expand-or-complete}"
|
||||
fi
|
||||
[ -n "$sws" ] && setopt shwordsplit
|
||||
}
|
||||
|
||||
[ -z "$fzf_default_completion" ] &&
|
||||
|
@@ -19,7 +19,7 @@ function fzf_key_bindings
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null | sed 1d | cut -b3-"
|
||||
eval $FZF_CTRL_T_COMMAND | eval (__fzfcmd) -m > $TMPDIR/fzf.result
|
||||
eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m > $TMPDIR/fzf.result"
|
||||
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||
commandline -f repaint
|
||||
rm -f $TMPDIR/fzf.result
|
||||
|
@@ -1,8 +1,10 @@
|
||||
FROM centos:centos7
|
||||
FROM centos:centos6
|
||||
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||
|
||||
# yum
|
||||
RUN yum install -y git gcc make tar ncurses-devel
|
||||
RUN yum install -y git gcc make tar glibc-devel glibc-devel.i686 \
|
||||
ncurses-devel ncurses-static ncurses-devel.i686 \
|
||||
gpm-devel gpm-static libgcc.i686
|
||||
|
||||
# Install Go 1.4
|
||||
RUN cd / && curl \
|
||||
@@ -13,6 +15,9 @@ ENV GOPATH /go
|
||||
ENV GOROOT /go1.4
|
||||
ENV PATH /go1.4/bin:$PATH
|
||||
|
||||
# For i386 build
|
||||
RUN cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||
|
||||
# Volume
|
||||
VOLUME /go
|
||||
|
||||
|
44
src/Makefile
44
src/Makefile
@@ -5,19 +5,8 @@ endif
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
GOOS := darwin
|
||||
LDFLAGS :=
|
||||
ifdef STATIC
|
||||
$(error Static linking not possible on OS X)
|
||||
endif
|
||||
else ifeq ($(UNAME_S),Linux)
|
||||
GOOS := linux
|
||||
ifdef STATIC
|
||||
SUFFIX := -static
|
||||
LDFLAGS := --ldflags '-extldflags "-static -ltinfo -lgpm"'
|
||||
else
|
||||
SUFFIX :=
|
||||
LDFLAGS :=
|
||||
endif
|
||||
endif
|
||||
|
||||
ifneq ($(shell uname -m),x86_64)
|
||||
@@ -27,11 +16,11 @@ endif
|
||||
SOURCES := $(wildcard *.go */*.go)
|
||||
BINDIR := ../bin
|
||||
|
||||
BINARY32 := fzf-$(GOOS)_386$(SUFFIX)
|
||||
BINARY64 := fzf-$(GOOS)_amd64$(SUFFIX)
|
||||
BINARY32 := fzf-$(GOOS)_386
|
||||
BINARY64 := fzf-$(GOOS)_amd64
|
||||
VERSION = $(shell fzf/$(BINARY64) --version)
|
||||
RELEASE32 = fzf-$(VERSION)-$(GOOS)_386$(SUFFIX)
|
||||
RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd64$(SUFFIX)
|
||||
RELEASE32 = fzf-$(VERSION)-$(GOOS)_386
|
||||
RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd64
|
||||
|
||||
all: release
|
||||
|
||||
@@ -40,11 +29,7 @@ release: build
|
||||
cd fzf && cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
||||
rm -f $(RELEASE32) $(RELEASE64)
|
||||
|
||||
ifndef STATIC
|
||||
build: test fzf/$(BINARY32) fzf/$(BINARY64)
|
||||
else
|
||||
build: test fzf/$(BINARY64)
|
||||
endif
|
||||
|
||||
test:
|
||||
go get
|
||||
@@ -59,10 +44,10 @@ clean:
|
||||
cd fzf && rm -f fzf-*
|
||||
|
||||
fzf/$(BINARY32): $(SOURCES)
|
||||
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -o $(BINARY32)
|
||||
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -a -o $(BINARY32)
|
||||
|
||||
fzf/$(BINARY64): $(SOURCES)
|
||||
cd fzf && go build $(LDFLAGS) -o $(BINARY64)
|
||||
cd fzf && go build -a -tags "$(TAGS)" -o $(BINARY64)
|
||||
|
||||
$(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
||||
cp -f fzf/$(BINARY64) $(BINDIR)
|
||||
@@ -77,6 +62,9 @@ docker-arch:
|
||||
docker-ubuntu:
|
||||
docker build -t junegunn/ubuntu-sandbox - < Dockerfile.ubuntu
|
||||
|
||||
docker-centos:
|
||||
docker build -t junegunn/centos-sandbox - < Dockerfile.centos
|
||||
|
||||
arch: docker-arch
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||
@@ -85,13 +73,13 @@ ubuntu: docker-ubuntu
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||
|
||||
linux: docker-arch
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/arch-sandbox \
|
||||
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make'
|
||||
centos: docker-centos
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||
|
||||
linux-static: docker-ubuntu
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/ubuntu-sandbox \
|
||||
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make STATIC=1'
|
||||
linux: docker-centos
|
||||
docker run -i -t -v $(GOPATH):/go junegunn/centos-sandbox \
|
||||
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make TAGS=static'
|
||||
|
||||
.PHONY: all build release test install uninstall clean docker \
|
||||
linux linux-static arch ubuntu docker-arch docker-ubuntu
|
||||
linux arch ubuntu centos docker-arch docker-ubuntu docker-centos
|
||||
|
@@ -15,8 +15,15 @@ import (
|
||||
* In short: They try to do as little work as possible.
|
||||
*/
|
||||
|
||||
func runeAt(runes []rune, index int, max int, forward bool) rune {
|
||||
if forward {
|
||||
return runes[index]
|
||||
}
|
||||
return runes[max-index-1]
|
||||
}
|
||||
|
||||
// FuzzyMatch performs fuzzy-match
|
||||
func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
||||
if len(pattern) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
@@ -34,7 +41,11 @@ func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
sidx := -1
|
||||
eidx := -1
|
||||
|
||||
for index, char := range runes {
|
||||
lenRunes := len(runes)
|
||||
lenPattern := len(pattern)
|
||||
|
||||
for index := range runes {
|
||||
char := runeAt(runes, index, lenRunes, forward)
|
||||
// This is considerably faster than blindly applying strings.ToLower to the
|
||||
// whole string
|
||||
if !caseSensitive {
|
||||
@@ -47,11 +58,12 @@ func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
char = unicode.To(unicode.LowerCase, char)
|
||||
}
|
||||
}
|
||||
if char == pattern[pidx] {
|
||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||
if char == pchar {
|
||||
if sidx < 0 {
|
||||
sidx = index
|
||||
}
|
||||
if pidx++; pidx == len(pattern) {
|
||||
if pidx++; pidx == lenPattern {
|
||||
eidx = index + 1
|
||||
break
|
||||
}
|
||||
@@ -61,7 +73,7 @@ func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
if sidx >= 0 && eidx >= 0 {
|
||||
pidx--
|
||||
for index := eidx - 1; index >= sidx; index-- {
|
||||
char := runes[index]
|
||||
char := runeAt(runes, index, lenRunes, forward)
|
||||
if !caseSensitive {
|
||||
if char >= 'A' && char <= 'Z' {
|
||||
char += 32
|
||||
@@ -69,14 +81,19 @@ func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
char = unicode.To(unicode.LowerCase, char)
|
||||
}
|
||||
}
|
||||
if char == pattern[pidx] {
|
||||
|
||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||
if char == pchar {
|
||||
if pidx--; pidx < 0 {
|
||||
sidx = index
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return sidx, eidx
|
||||
if forward {
|
||||
return sidx, eidx
|
||||
}
|
||||
return lenRunes - eidx, lenRunes - sidx
|
||||
}
|
||||
return -1, -1
|
||||
}
|
||||
@@ -88,20 +105,21 @@ func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
//
|
||||
// We might try to implement better algorithms in the future:
|
||||
// http://en.wikipedia.org/wiki/String_searching_algorithm
|
||||
func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
||||
if len(pattern) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
numRunes := len(runes)
|
||||
plen := len(pattern)
|
||||
if numRunes < plen {
|
||||
lenRunes := len(runes)
|
||||
lenPattern := len(pattern)
|
||||
|
||||
if lenRunes < lenPattern {
|
||||
return -1, -1
|
||||
}
|
||||
|
||||
pidx := 0
|
||||
for index := 0; index < numRunes; index++ {
|
||||
char := runes[index]
|
||||
for index := 0; index < lenRunes; index++ {
|
||||
char := runeAt(runes, index, lenRunes, forward)
|
||||
if !caseSensitive {
|
||||
if char >= 'A' && char <= 'Z' {
|
||||
char += 32
|
||||
@@ -109,10 +127,14 @@ func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int
|
||||
char = unicode.To(unicode.LowerCase, char)
|
||||
}
|
||||
}
|
||||
if pattern[pidx] == char {
|
||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||
if pchar == char {
|
||||
pidx++
|
||||
if pidx == plen {
|
||||
return index - plen + 1, index + 1
|
||||
if pidx == lenPattern {
|
||||
if forward {
|
||||
return index - lenPattern + 1, index + 1
|
||||
}
|
||||
return lenRunes - (index + 1), lenRunes - (index - lenPattern + 1)
|
||||
}
|
||||
} else {
|
||||
index -= pidx
|
||||
@@ -123,7 +145,7 @@ func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int
|
||||
}
|
||||
|
||||
// PrefixMatch performs prefix-match
|
||||
func PrefixMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
func PrefixMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
||||
if len(runes) < len(pattern) {
|
||||
return -1, -1
|
||||
}
|
||||
@@ -141,7 +163,7 @@ func PrefixMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
}
|
||||
|
||||
// SuffixMatch performs suffix-match
|
||||
func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {
|
||||
func SuffixMatch(caseSensitive bool, forward bool, input []rune, pattern []rune) (int, int) {
|
||||
runes := util.TrimRight(input)
|
||||
trimmedLen := len(runes)
|
||||
diff := trimmedLen - len(pattern)
|
||||
@@ -162,7 +184,7 @@ func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {
|
||||
}
|
||||
|
||||
// EqualMatch performs equal-match
|
||||
func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
|
||||
func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
||||
if len(runes) != len(pattern) {
|
||||
return -1, -1
|
||||
}
|
||||
|
@@ -5,11 +5,11 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func assertMatch(t *testing.T, fun func(bool, []rune, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {
|
||||
func assertMatch(t *testing.T, fun func(bool, bool, []rune, []rune) (int, int), caseSensitive bool, forward bool, input string, pattern string, sidx int, eidx int) {
|
||||
if !caseSensitive {
|
||||
pattern = strings.ToLower(pattern)
|
||||
}
|
||||
s, e := fun(caseSensitive, []rune(input), []rune(pattern))
|
||||
s, e := fun(caseSensitive, forward, []rune(input), []rune(pattern))
|
||||
if s != sidx {
|
||||
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
|
||||
}
|
||||
@@ -19,33 +19,51 @@ func assertMatch(t *testing.T, fun func(bool, []rune, []rune) (int, int), caseSe
|
||||
}
|
||||
|
||||
func TestFuzzyMatch(t *testing.T) {
|
||||
assertMatch(t, FuzzyMatch, false, "fooBarbaz", "oBZ", 2, 9)
|
||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBZ", -1, -1)
|
||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBz", 2, 9)
|
||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||
assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9)
|
||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1)
|
||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9)
|
||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||
}
|
||||
|
||||
func TestFuzzyMatchBackward(t *testing.T) {
|
||||
assertMatch(t, FuzzyMatch, false, true, "foobar fb", "fb", 0, 4)
|
||||
assertMatch(t, FuzzyMatch, false, false, "foobar fb", "fb", 7, 9)
|
||||
}
|
||||
|
||||
func TestExactMatchNaive(t *testing.T) {
|
||||
assertMatch(t, ExactMatchNaive, false, "fooBarbaz", "oBA", 2, 5)
|
||||
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "oBA", -1, -1)
|
||||
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||
for _, dir := range []bool{true, false} {
|
||||
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5)
|
||||
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1)
|
||||
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExactMatchNaiveBackward(t *testing.T) {
|
||||
assertMatch(t, FuzzyMatch, false, true, "foobar foob", "oo", 1, 3)
|
||||
assertMatch(t, FuzzyMatch, false, false, "foobar foob", "oo", 8, 10)
|
||||
}
|
||||
|
||||
func TestPrefixMatch(t *testing.T) {
|
||||
assertMatch(t, PrefixMatch, false, "fooBarbaz", "Foo", 0, 3)
|
||||
assertMatch(t, PrefixMatch, true, "fooBarbaz", "Foo", -1, -1)
|
||||
assertMatch(t, PrefixMatch, false, "fooBarbaz", "baz", -1, -1)
|
||||
for _, dir := range []bool{true, false} {
|
||||
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3)
|
||||
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1)
|
||||
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuffixMatch(t *testing.T) {
|
||||
assertMatch(t, SuffixMatch, false, "fooBarbaz", "Foo", -1, -1)
|
||||
assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9)
|
||||
assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1)
|
||||
for _, dir := range []bool{true, false} {
|
||||
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1)
|
||||
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9)
|
||||
assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyPattern(t *testing.T) {
|
||||
assertMatch(t, FuzzyMatch, true, "foobar", "", 0, 0)
|
||||
assertMatch(t, ExactMatchNaive, true, "foobar", "", 0, 0)
|
||||
assertMatch(t, PrefixMatch, true, "foobar", "", 0, 0)
|
||||
assertMatch(t, SuffixMatch, true, "foobar", "", 6, 6)
|
||||
for _, dir := range []bool{true, false} {
|
||||
assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0)
|
||||
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0)
|
||||
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0)
|
||||
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6)
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
const (
|
||||
// Current version
|
||||
version = "0.10.4"
|
||||
version = "0.10.6"
|
||||
|
||||
// Core
|
||||
coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
||||
@@ -47,3 +47,10 @@ const (
|
||||
EvtHeader
|
||||
EvtClose
|
||||
)
|
||||
|
||||
const (
|
||||
exitOk = 0
|
||||
exitNoMatch = 1
|
||||
exitError = 2
|
||||
exitInterrupt = 130
|
||||
)
|
||||
|
20
src/core.go
20
src/core.go
@@ -56,7 +56,7 @@ func Run(opts *Options) {
|
||||
|
||||
if opts.Version {
|
||||
fmt.Println(version)
|
||||
os.Exit(0)
|
||||
os.Exit(exitOk)
|
||||
}
|
||||
|
||||
// Event channel
|
||||
@@ -143,7 +143,8 @@ func Run(opts *Options) {
|
||||
// Matcher
|
||||
patternBuilder := func(runes []rune) *Pattern {
|
||||
return BuildPattern(
|
||||
opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)
|
||||
opts.Mode, opts.Case, opts.Tiebreak != byEnd,
|
||||
opts.Nth, opts.Delimiter, runes)
|
||||
}
|
||||
matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)
|
||||
|
||||
@@ -155,12 +156,14 @@ func Run(opts *Options) {
|
||||
|
||||
pattern := patternBuilder([]rune(*opts.Filter))
|
||||
|
||||
found := false
|
||||
if streamingFilter {
|
||||
reader := Reader{
|
||||
func(runes []byte) bool {
|
||||
item := chunkList.trans(runes, 0)
|
||||
if item != nil && pattern.MatchItem(item) {
|
||||
fmt.Println(string(item.text))
|
||||
found = true
|
||||
}
|
||||
return false
|
||||
}, eventBox, opts.ReadZero}
|
||||
@@ -175,9 +178,13 @@ func Run(opts *Options) {
|
||||
pattern: pattern})
|
||||
for i := 0; i < merger.Length(); i++ {
|
||||
fmt.Println(merger.Get(i).AsString(opts.Ansi))
|
||||
found = true
|
||||
}
|
||||
}
|
||||
os.Exit(0)
|
||||
if found {
|
||||
os.Exit(exitOk)
|
||||
}
|
||||
os.Exit(exitNoMatch)
|
||||
}
|
||||
|
||||
// Synchronous search
|
||||
@@ -231,7 +238,7 @@ func Run(opts *Options) {
|
||||
}
|
||||
|
||||
case EvtHeader:
|
||||
terminal.UpdateHeader(value.([]string), opts.HeaderLines)
|
||||
terminal.UpdateHeader(value.([]string))
|
||||
|
||||
case EvtSearchFin:
|
||||
switch val := value.(type) {
|
||||
@@ -252,7 +259,10 @@ func Run(opts *Options) {
|
||||
for i := 0; i < count; i++ {
|
||||
fmt.Println(val.Get(i).AsString(opts.Ansi))
|
||||
}
|
||||
os.Exit(0)
|
||||
if count > 0 {
|
||||
os.Exit(exitOk)
|
||||
}
|
||||
os.Exit(exitNoMatch)
|
||||
}
|
||||
deferred = false
|
||||
terminal.startChan <- true
|
||||
|
@@ -3,7 +3,8 @@ package curses
|
||||
/*
|
||||
#include <ncurses.h>
|
||||
#include <locale.h>
|
||||
#cgo LDFLAGS: -lncurses
|
||||
#cgo !static LDFLAGS: -lncurses
|
||||
#cgo static LDFLAGS: -l:libncurses.a -l:libtinfo.a -l:libgpm.a -ldl
|
||||
*/
|
||||
import "C"
|
||||
|
||||
@@ -261,7 +262,7 @@ func Init(theme *ColorTheme, black bool, mouse bool) {
|
||||
_screen = C.newterm(nil, C.stderr, C.stdin)
|
||||
if _screen == nil {
|
||||
fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))
|
||||
os.Exit(1)
|
||||
os.Exit(2)
|
||||
}
|
||||
C.set_term(_screen)
|
||||
if mouse {
|
||||
@@ -275,7 +276,7 @@ func Init(theme *ColorTheme, black bool, mouse bool) {
|
||||
go func() {
|
||||
<-intChan
|
||||
Close()
|
||||
os.Exit(1)
|
||||
os.Exit(2)
|
||||
}()
|
||||
|
||||
if theme != nil {
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package fzf
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -45,7 +44,7 @@ const usage = `usage: fzf [options]
|
||||
--bind=KEYBINDS Custom key bindings. Refer to the man page.
|
||||
--history=FILE History file
|
||||
--history-size=N Maximum number of history entries (default: 1000)
|
||||
--header-file=FILE The file whose content to be printed as header
|
||||
--header=STR String to print as header
|
||||
--header-lines=N The first N lines of the input are treated as header
|
||||
|
||||
Scripting
|
||||
@@ -180,14 +179,14 @@ func defaultOptions() *Options {
|
||||
Version: false}
|
||||
}
|
||||
|
||||
func help(ok int) {
|
||||
func help(code int) {
|
||||
os.Stderr.WriteString(usage)
|
||||
os.Exit(ok)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func errorExit(msg string) {
|
||||
os.Stderr.WriteString(msg + "\n")
|
||||
os.Exit(1)
|
||||
os.Exit(exitError)
|
||||
}
|
||||
|
||||
func optString(arg string, prefixes ...string) (bool, string) {
|
||||
@@ -604,12 +603,8 @@ func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {
|
||||
return keymap
|
||||
}
|
||||
|
||||
func readHeaderFile(filename string) []string {
|
||||
content, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
errorExit("failed to read header file: " + filename)
|
||||
}
|
||||
return strings.Split(strings.TrimSuffix(string(content), "\n"), "\n")
|
||||
func strLines(str string) []string {
|
||||
return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
|
||||
}
|
||||
|
||||
func parseMargin(margin string) [4]string {
|
||||
@@ -682,7 +677,7 @@ func parseOptions(opts *Options, allArgs []string) {
|
||||
arg := allArgs[i]
|
||||
switch arg {
|
||||
case "-h", "--help":
|
||||
help(0)
|
||||
help(exitOk)
|
||||
case "-x", "--extended":
|
||||
opts.Mode = ModeExtended
|
||||
case "-e", "--extended-exact":
|
||||
@@ -793,16 +788,13 @@ func parseOptions(opts *Options, allArgs []string) {
|
||||
setHistory(nextString(allArgs, &i, "history file path required"))
|
||||
case "--history-size":
|
||||
setHistoryMax(nextInt(allArgs, &i, "history max size required"))
|
||||
case "--no-header-file":
|
||||
case "--no-header":
|
||||
opts.Header = []string{}
|
||||
case "--no-header-lines":
|
||||
opts.HeaderLines = 0
|
||||
case "--header-file":
|
||||
opts.Header = readHeaderFile(
|
||||
nextString(allArgs, &i, "header file name required"))
|
||||
opts.HeaderLines = 0
|
||||
case "--header":
|
||||
opts.Header = strLines(nextString(allArgs, &i, "header string required"))
|
||||
case "--header-lines":
|
||||
opts.Header = []string{}
|
||||
opts.HeaderLines = atoi(
|
||||
nextString(allArgs, &i, "number of header lines required"))
|
||||
case "--no-margin":
|
||||
@@ -843,11 +835,9 @@ func parseOptions(opts *Options, allArgs []string) {
|
||||
setHistory(value)
|
||||
} else if match, value := optString(arg, "--history-size="); match {
|
||||
setHistoryMax(atoi(value))
|
||||
} else if match, value := optString(arg, "--header-file="); match {
|
||||
opts.Header = readHeaderFile(value)
|
||||
opts.HeaderLines = 0
|
||||
} else if match, value := optString(arg, "--header="); match {
|
||||
opts.Header = strLines(value)
|
||||
} else if match, value := optString(arg, "--header-lines="); match {
|
||||
opts.Header = []string{}
|
||||
opts.HeaderLines = atoi(value)
|
||||
} else if match, value := optString(arg, "--margin="); match {
|
||||
opts.Margin = parseMargin(value)
|
||||
|
@@ -39,12 +39,13 @@ type term struct {
|
||||
type Pattern struct {
|
||||
mode Mode
|
||||
caseSensitive bool
|
||||
forward bool
|
||||
text []rune
|
||||
terms []term
|
||||
hasInvTerm bool
|
||||
delimiter Delimiter
|
||||
nth []Range
|
||||
procFun map[termType]func(bool, []rune, []rune) (int, int)
|
||||
procFun map[termType]func(bool, bool, []rune, []rune) (int, int)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -70,7 +71,7 @@ func clearChunkCache() {
|
||||
}
|
||||
|
||||
// BuildPattern builds Pattern object from the given arguments
|
||||
func BuildPattern(mode Mode, caseMode Case,
|
||||
func BuildPattern(mode Mode, caseMode Case, forward bool,
|
||||
nth []Range, delimiter Delimiter, runes []rune) *Pattern {
|
||||
|
||||
var asString string
|
||||
@@ -109,12 +110,13 @@ func BuildPattern(mode Mode, caseMode Case,
|
||||
ptr := &Pattern{
|
||||
mode: mode,
|
||||
caseSensitive: caseSensitive,
|
||||
forward: forward,
|
||||
text: []rune(asString),
|
||||
terms: terms,
|
||||
hasInvTerm: hasInvTerm,
|
||||
nth: nth,
|
||||
delimiter: delimiter,
|
||||
procFun: make(map[termType]func(bool, []rune, []rune) (int, int))}
|
||||
procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}
|
||||
|
||||
ptr.procFun[termFuzzy] = algo.FuzzyMatch
|
||||
ptr.procFun[termEqual] = algo.EqualMatch
|
||||
@@ -151,6 +153,9 @@ func parseTerms(mode Mode, caseMode Case, str string) []term {
|
||||
if mode == ModeExtended {
|
||||
typ = termExact
|
||||
text = text[1:]
|
||||
} else if mode == ModeExtendedExact {
|
||||
typ = termFuzzy
|
||||
text = text[1:]
|
||||
}
|
||||
} else if strings.HasPrefix(text, "^") {
|
||||
if strings.HasSuffix(text, "$") {
|
||||
@@ -285,7 +290,7 @@ func dupItem(item *Item, offsets []Offset) *Item {
|
||||
|
||||
func (p *Pattern) fuzzyMatch(item *Item) (int, int) {
|
||||
input := p.prepareInput(item)
|
||||
return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.text)
|
||||
return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.forward, p.text)
|
||||
}
|
||||
|
||||
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
||||
@@ -293,7 +298,7 @@ func (p *Pattern) extendedMatch(item *Item) []Offset {
|
||||
offsets := []Offset{}
|
||||
for _, term := range p.terms {
|
||||
pfun := p.procFun[term.typ]
|
||||
if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {
|
||||
if sidx, eidx := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {
|
||||
if term.inv {
|
||||
break
|
||||
}
|
||||
@@ -321,11 +326,11 @@ func (p *Pattern) prepareInput(item *Item) []Token {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (p *Pattern) iter(pfun func(bool, []rune, []rune) (int, int),
|
||||
tokens []Token, caseSensitive bool, pattern []rune) (int, int) {
|
||||
func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),
|
||||
tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int) {
|
||||
for _, part := range tokens {
|
||||
prefixLength := part.prefixLength
|
||||
if sidx, eidx := pfun(caseSensitive, part.text, pattern); sidx >= 0 {
|
||||
if sidx, eidx := pfun(caseSensitive, forward, part.text, pattern); sidx >= 0 {
|
||||
return sidx + prefixLength, eidx + prefixLength
|
||||
}
|
||||
}
|
||||
|
@@ -37,11 +37,11 @@ func TestParseTermsExtendedExact(t *testing.T) {
|
||||
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
||||
if len(terms) != 8 ||
|
||||
terms[0].typ != termExact || terms[0].inv || len(terms[0].text) != 3 ||
|
||||
terms[1].typ != termExact || terms[1].inv || len(terms[1].text) != 4 ||
|
||||
terms[1].typ != termFuzzy || terms[1].inv || len(terms[1].text) != 3 ||
|
||||
terms[2].typ != termPrefix || terms[2].inv || len(terms[2].text) != 3 ||
|
||||
terms[3].typ != termSuffix || terms[3].inv || len(terms[3].text) != 3 ||
|
||||
terms[4].typ != termExact || !terms[4].inv || len(terms[4].text) != 3 ||
|
||||
terms[5].typ != termExact || !terms[5].inv || len(terms[5].text) != 4 ||
|
||||
terms[5].typ != termFuzzy || !terms[5].inv || len(terms[5].text) != 3 ||
|
||||
terms[6].typ != termPrefix || !terms[6].inv || len(terms[6].text) != 3 ||
|
||||
terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {
|
||||
t.Errorf("%s", terms)
|
||||
@@ -58,10 +58,10 @@ func TestParseTermsEmpty(t *testing.T) {
|
||||
func TestExact(t *testing.T) {
|
||||
defer clearPatternCache()
|
||||
clearPatternCache()
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart,
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart, true,
|
||||
[]Range{}, Delimiter{}, []rune("'abc"))
|
||||
sidx, eidx := algo.ExactMatchNaive(
|
||||
pattern.caseSensitive, []rune("aabbcc abc"), pattern.terms[0].text)
|
||||
pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)
|
||||
if sidx != 7 || eidx != 10 {
|
||||
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
||||
}
|
||||
@@ -70,11 +70,11 @@ func TestExact(t *testing.T) {
|
||||
func TestEqual(t *testing.T) {
|
||||
defer clearPatternCache()
|
||||
clearPatternCache()
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("^AbC$"))
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
||||
|
||||
match := func(str string, sidxExpected int, eidxExpected int) {
|
||||
sidx, eidx := algo.EqualMatch(
|
||||
pattern.caseSensitive, []rune(str), pattern.terms[0].text)
|
||||
pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)
|
||||
if sidx != sidxExpected || eidx != eidxExpected {
|
||||
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
||||
}
|
||||
@@ -86,17 +86,17 @@ func TestEqual(t *testing.T) {
|
||||
func TestCaseSensitivity(t *testing.T) {
|
||||
defer clearPatternCache()
|
||||
clearPatternCache()
|
||||
pat1 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("abc"))
|
||||
pat1 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||
clearPatternCache()
|
||||
pat2 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
pat2 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
clearPatternCache()
|
||||
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("abc"))
|
||||
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||
clearPatternCache()
|
||||
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
clearPatternCache()
|
||||
pat5 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("abc"))
|
||||
pat5 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||
clearPatternCache()
|
||||
pat6 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
pat6 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||
|
||||
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
||||
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
||||
@@ -109,7 +109,7 @@ func TestCaseSensitivity(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestOrigTextAndTransformed(t *testing.T) {
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("jg"))
|
||||
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))
|
||||
tokens := Tokenize([]rune("junegunn"), Delimiter{})
|
||||
trans := Transform(tokens, []Range{Range{1, 1}})
|
||||
|
||||
|
@@ -42,6 +42,7 @@ type Terminal struct {
|
||||
history *History
|
||||
cycle bool
|
||||
header []string
|
||||
header0 []string
|
||||
ansi bool
|
||||
margin [4]string
|
||||
marginInt [4]int
|
||||
@@ -185,6 +186,12 @@ func defaultKeymap() map[int]actionType {
|
||||
// NewTerminal returns new Terminal object
|
||||
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
||||
input := []rune(opts.Query)
|
||||
var header []string
|
||||
if opts.Reverse {
|
||||
header = opts.Header
|
||||
} else {
|
||||
header = reverseStringArray(opts.Header)
|
||||
}
|
||||
return &Terminal{
|
||||
inlineInfo: opts.InlineInfo,
|
||||
prompt: opts.Prompt,
|
||||
@@ -207,7 +214,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
||||
margin: opts.Margin,
|
||||
marginInt: [4]int{0, 0, 0, 0},
|
||||
cycle: opts.Cycle,
|
||||
header: opts.Header,
|
||||
header: header,
|
||||
header0: header,
|
||||
ansi: opts.Ansi,
|
||||
reading: true,
|
||||
merger: EmptyMerger,
|
||||
@@ -241,18 +249,19 @@ func (t *Terminal) UpdateCount(cnt int, final bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateHeader updates the header
|
||||
func (t *Terminal) UpdateHeader(header []string, lines int) {
|
||||
t.mutex.Lock()
|
||||
t.header = make([]string, lines)
|
||||
copy(t.header, header)
|
||||
if !t.reverse {
|
||||
reversed := make([]string, lines)
|
||||
for idx, str := range t.header {
|
||||
reversed[lines-idx-1] = str
|
||||
}
|
||||
t.header = reversed
|
||||
func reverseStringArray(input []string) []string {
|
||||
size := len(input)
|
||||
reversed := make([]string, size)
|
||||
for idx, str := range input {
|
||||
reversed[size-idx-1] = str
|
||||
}
|
||||
return reversed
|
||||
}
|
||||
|
||||
// UpdateHeader updates the header
|
||||
func (t *Terminal) UpdateHeader(header []string) {
|
||||
t.mutex.Lock()
|
||||
t.header = append(append([]string{}, t.header0...), header...)
|
||||
t.mutex.Unlock()
|
||||
t.reqBox.Set(reqHeader, nil)
|
||||
}
|
||||
@@ -280,17 +289,19 @@ func (t *Terminal) UpdateList(merger *Merger) {
|
||||
t.reqBox.Set(reqList, nil)
|
||||
}
|
||||
|
||||
func (t *Terminal) output() {
|
||||
func (t *Terminal) output() bool {
|
||||
if t.printQuery {
|
||||
fmt.Println(string(t.input))
|
||||
}
|
||||
if len(t.expect) > 0 {
|
||||
fmt.Println(t.pressed)
|
||||
}
|
||||
if len(t.selected) == 0 {
|
||||
found := len(t.selected) > 0
|
||||
if !found {
|
||||
cnt := t.merger.Length()
|
||||
if cnt > 0 && cnt > t.cy {
|
||||
fmt.Println(t.merger.Get(t.cy).AsString(t.ansi))
|
||||
found = true
|
||||
}
|
||||
} else {
|
||||
sels := make([]selectedItem, 0, len(t.selected))
|
||||
@@ -302,6 +313,7 @@ func (t *Terminal) output() {
|
||||
fmt.Println(*sel.text)
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
func runeWidth(r rune, prefixWidth int) int {
|
||||
@@ -433,9 +445,6 @@ func (t *Terminal) printHeader() {
|
||||
max := t.maxHeight()
|
||||
var state *ansiState
|
||||
for idx, lineStr := range t.header {
|
||||
if !t.reverse {
|
||||
idx = len(t.header) - idx - 1
|
||||
}
|
||||
line := idx + 2
|
||||
if t.inlineInfo {
|
||||
line--
|
||||
@@ -743,7 +752,7 @@ func (t *Terminal) Loop() {
|
||||
}
|
||||
|
||||
exit := func(code int) {
|
||||
if code == 0 && t.history != nil {
|
||||
if code <= exitNoMatch && t.history != nil {
|
||||
t.history.append(string(t.input))
|
||||
}
|
||||
os.Exit(code)
|
||||
@@ -776,11 +785,13 @@ func (t *Terminal) Loop() {
|
||||
t.printAll()
|
||||
case reqClose:
|
||||
C.Close()
|
||||
t.output()
|
||||
exit(0)
|
||||
if t.output() {
|
||||
exit(exitOk)
|
||||
}
|
||||
exit(exitNoMatch)
|
||||
case reqQuit:
|
||||
C.Close()
|
||||
exit(1)
|
||||
exit(exitInterrupt)
|
||||
}
|
||||
}
|
||||
t.placeCursor()
|
||||
|
@@ -527,6 +527,17 @@ class TestGoFZF < TestBase
|
||||
assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/)
|
||||
end
|
||||
|
||||
def test_tiebreak_end_backward_scan
|
||||
input = %w[
|
||||
foobar-fb
|
||||
fubar
|
||||
]
|
||||
writelines tempname, input
|
||||
|
||||
assert_equal input.reverse, `cat #{tempname} | #{FZF} -f fb`.split($/)
|
||||
assert_equal input, `cat #{tempname} | #{FZF} -f fb --tiebreak=end`.split($/)
|
||||
end
|
||||
|
||||
def test_invalid_cache
|
||||
tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter
|
||||
tmux.until { |lines| lines[-2].include? '2/3' }
|
||||
@@ -726,8 +737,8 @@ class TestGoFZF < TestBase
|
||||
assert_equal '6', readonce.chomp
|
||||
end
|
||||
|
||||
def test_header_file
|
||||
tmux.send_keys "seq 100 | #{fzf "--header-file <(head -5 #{__FILE__})"}", :Enter
|
||||
def test_header
|
||||
tmux.send_keys "seq 100 | #{fzf "--header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||
tmux.until do |lines|
|
||||
lines[-2].include?('100/100') &&
|
||||
@@ -735,8 +746,8 @@ class TestGoFZF < TestBase
|
||||
end
|
||||
end
|
||||
|
||||
def test_header_file_reverse
|
||||
tmux.send_keys "seq 100 | #{fzf "--header-file=<(head -5 #{__FILE__}) --reverse"}", :Enter
|
||||
def test_header_reverse
|
||||
tmux.send_keys "seq 100 | #{fzf "--header=\\\"\\$(head -5 #{__FILE__})\\\" --reverse"}", :Enter
|
||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||
tmux.until do |lines|
|
||||
lines[1].include?('100/100') &&
|
||||
@@ -744,6 +755,26 @@ class TestGoFZF < TestBase
|
||||
end
|
||||
end
|
||||
|
||||
def test_header_and_header_lines
|
||||
tmux.send_keys "seq 100 | #{fzf "--header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||
tmux.until do |lines|
|
||||
lines[-2].include?('90/90') &&
|
||||
lines[-7...-2].map(&:strip) == header &&
|
||||
lines[-17...-7].map(&:strip) == (1..10).map(&:to_s).reverse
|
||||
end
|
||||
end
|
||||
|
||||
def test_header_and_header_lines_reverse
|
||||
tmux.send_keys "seq 100 | #{fzf "--reverse --header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||
tmux.until do |lines|
|
||||
lines[1].include?('90/90') &&
|
||||
lines[2...7].map(&:strip) == header &&
|
||||
lines[7...17].map(&:strip) == (1..10).map(&:to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def test_canel
|
||||
tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter
|
||||
tmux.until { |lines| lines[-2].include?('10/10') }
|
||||
@@ -769,11 +800,6 @@ class TestGoFZF < TestBase
|
||||
tmux.send_keys :Enter
|
||||
end
|
||||
|
||||
def test_invalid_term
|
||||
tmux.send_keys "TERM=xxx fzf", :Enter
|
||||
tmux.until { |lines| lines.any? { |l| l.include? 'Invalid $TERM: xxx' } }
|
||||
end
|
||||
|
||||
def test_with_nth
|
||||
writelines tempname, ['hello world ', 'byebye']
|
||||
assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1`.chomp
|
||||
@@ -790,6 +816,47 @@ class TestGoFZF < TestBase
|
||||
assert_equal src, `cat #{tempname} | #{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi`.chomp
|
||||
end
|
||||
|
||||
def test_exit_0_exit_code
|
||||
`echo foo | #{FZF} -q bar -0`
|
||||
assert_equal 1, $?.exitstatus
|
||||
end
|
||||
|
||||
def test_invalid_term
|
||||
lines = `TERM=xxx #{FZF}`
|
||||
assert_equal 2, $?.exitstatus
|
||||
assert lines.include?('Invalid $TERM: xxx')
|
||||
end
|
||||
|
||||
def test_invalid_option
|
||||
lines = `#{FZF} --foobar 2>&1`
|
||||
assert_equal 2, $?.exitstatus
|
||||
assert lines.include?('unknown option: --foobar'), lines
|
||||
end
|
||||
|
||||
def test_filter_exitstatus
|
||||
# filter / streaming filter
|
||||
["", "--no-sort"].each do |opts|
|
||||
assert `echo foo | #{FZF} -f foo #{opts}`.include?('foo')
|
||||
assert_equal 0, $?.exitstatus
|
||||
|
||||
assert `echo foo | #{FZF} -f bar #{opts}`.empty?
|
||||
assert_equal 1, $?.exitstatus
|
||||
end
|
||||
end
|
||||
|
||||
def test_exitstatus_empty
|
||||
{ '99' => '0', '999' => '1' }.each do |query, status|
|
||||
tmux.send_keys "seq 100 | #{FZF} -q #{query}", :Enter
|
||||
tmux.until { |lines| lines[-2] =~ %r{ [10]/100} }
|
||||
tmux.send_keys :Enter
|
||||
|
||||
tmux.send_keys 'echo --\$?--'
|
||||
tmux.until { |lines| lines.last.include? "echo --$?--" }
|
||||
tmux.send_keys :Enter
|
||||
tmux.until { |lines| lines.last.include? "--#{status}--" }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def writelines path, lines
|
||||
File.unlink path while File.exists? path
|
||||
|
Reference in New Issue
Block a user