mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-02 05:02:04 -07:00
Compare commits
79 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1da065e50e | ||
|
86bc9d506f | ||
|
eee45a9578 | ||
|
659f49a09a | ||
|
8fa9e85980 | ||
|
92a75c9563 | ||
|
7c7a30c472 | ||
|
ea271cd4e2 | ||
|
6a38d07a4c | ||
|
c4e5ee63bb | ||
|
862da2c0b1 | ||
|
545370d2b3 | ||
|
59220c63a6 | ||
|
86306dd45a | ||
|
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 | ||
|
9a2d9ad947 | ||
|
90b0cd44ac | ||
|
698e8008df | ||
|
1de4cc3ba8 | ||
|
0d66ad23c6 | ||
|
7f7741099b | ||
|
5a72dc6922 | ||
|
80ed02e72e | ||
|
8fb31e1b4d | ||
|
148f21415a | ||
|
1c31e07d34 | ||
|
55d566b72f | ||
|
60336c7423 | ||
|
7ae877bd3a | ||
|
c601fc6437 | ||
|
e5fec408c4 | ||
|
8156e9894e | ||
|
cacc212f12 | ||
|
d0f2c00f9f | ||
|
766427de0c | ||
|
a7b75c99a5 | ||
|
bae10a6582 | ||
|
c4cf90a3d2 | ||
|
15c49a3e08 | ||
|
ae87f6548a | ||
|
7833fa7396 | ||
|
9278f3acd2 | ||
|
e83ae34a3b | ||
|
e13bafc1ab | ||
|
0ea66329b8 | ||
|
634670e3ea | ||
|
dea60b11bc | ||
|
5e90f0a57b | ||
|
0b4542fcdf | ||
|
02bd2d2adf | ||
|
dce6fe6f2d | ||
|
fcae99f09b | ||
|
fb1b026d3d | ||
|
9f953fc944 |
@@ -4,7 +4,7 @@ rvm:
|
|||||||
|
|
||||||
install:
|
install:
|
||||||
- sudo apt-get update
|
- 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 add-apt-repository -y ppa:pi-rho/dev
|
||||||
- sudo apt-add-repository -y ppa:fish-shell/release-2
|
- sudo apt-add-repository -y ppa:fish-shell/release-2
|
||||||
- sudo apt-get update
|
- sudo apt-get update
|
||||||
|
54
CHANGELOG.md
54
CHANGELOG.md
@@ -1,6 +1,60 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.10.7
|
||||||
|
------
|
||||||
|
|
||||||
|
- Fixed unserialized interrupt handling during execute action which often
|
||||||
|
caused invalid memory access and crash
|
||||||
|
- Changed `--tiebreak=length` (default) to use trimmed length when `--nth` is
|
||||||
|
used
|
||||||
|
|
||||||
|
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
|
||||||
|
------
|
||||||
|
|
||||||
|
- Fixed to remove ANSI code from output when `--with-nth` is set
|
||||||
|
|
||||||
|
0.10.3
|
||||||
|
------
|
||||||
|
|
||||||
|
- Fixed slow performance of `--with-nth` when used with `--delimiter`
|
||||||
|
- Regular expression engine of Golang as of now is very slow, so the fixed
|
||||||
|
version will treat the given delimiter pattern as a plain string instead
|
||||||
|
of a regular expression unless it contains special characters and is
|
||||||
|
a valid regular expression.
|
||||||
|
- Simpler regular expression for delimiter for better performance
|
||||||
|
|
||||||
|
0.10.2
|
||||||
|
------
|
||||||
|
|
||||||
|
### Fixes and improvements
|
||||||
|
|
||||||
|
- Improvement in perceived response time of queries
|
||||||
|
- Eager, efficient rune array conversion
|
||||||
|
- Graceful exit when failed to initialize ncurses (invalid $TERM)
|
||||||
|
- Improved ranking algorithm when `--nth` option is set
|
||||||
|
- Changed the default command not to fail when there are files whose names
|
||||||
|
start with dash
|
||||||
|
|
||||||
0.10.1
|
0.10.1
|
||||||
------
|
------
|
||||||
|
|
||||||
|
84
README.md
84
README.md
@@ -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.
|
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) |
|
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
| `!'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
|
If you don't prefer fuzzy matching and do not wish to "quote" every word,
|
||||||
fzf with `-e` or `--extended-exact` option.
|
start fzf with `-e` or `--extended-exact` option. Note that in
|
||||||
|
`--extended-exact` mode, `'`-prefix "unquotes" the term.
|
||||||
|
|
||||||
#### Environment variables
|
#### Environment variables
|
||||||
|
|
||||||
@@ -254,6 +255,9 @@ export FZF_COMPLETION_OPTS='+c -x'
|
|||||||
Usage as Vim plugin
|
Usage as Vim plugin
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
This repository only enables basic integration with Vim. If you're looking for
|
||||||
|
more, check out [fzf.vim](https://github.com/junegunn/fzf.vim) project.
|
||||||
|
|
||||||
(Note: To use fzf in GVim, an external terminal emulator is required.)
|
(Note: To use fzf in GVim, an external terminal emulator is required.)
|
||||||
|
|
||||||
#### `:FZF[!]`
|
#### `:FZF[!]`
|
||||||
@@ -286,10 +290,8 @@ customization.
|
|||||||
|
|
||||||
#### `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 use `fzf#run()` function with the following
|
||||||
of the selected items.
|
options.
|
||||||
|
|
||||||
`fzf#run()` may take an options-dictionary:
|
|
||||||
|
|
||||||
| Option name | Type | Description |
|
| Option name | Type | Description |
|
||||||
| -------------------------- | ------------- | ---------------------------------------------------------------- |
|
| -------------------------- | ------------- | ---------------------------------------------------------------- |
|
||||||
@@ -305,65 +307,7 @@ of the selected items.
|
|||||||
| `launcher` | string | External terminal emulator to start fzf with (GVim only) |
|
| `launcher` | string | External terminal emulator to start fzf with (GVim only) |
|
||||||
| `launcher` | funcref | Function for generating `launcher` string (GVim only) |
|
| `launcher` | funcref | Function for generating `launcher` string (GVim only) |
|
||||||
|
|
||||||
_However on Neovim `fzf#run` is asynchronous and does not return values so you
|
Examples can be found on [the wiki
|
||||||
should use `sink` or `sink*` to process the output from fzf._
|
|
||||||
|
|
||||||
##### Examples
|
|
||||||
|
|
||||||
If `sink` option is not given, `fzf#run` will simply return the list.
|
|
||||||
|
|
||||||
```vim
|
|
||||||
let items = fzf#run({ 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
|
||||||
```
|
|
||||||
|
|
||||||
But if `sink` is given as a string, the command will be executed for each
|
|
||||||
selected item.
|
|
||||||
|
|
||||||
```vim
|
|
||||||
" Each selected item will be opened in a new tab
|
|
||||||
let items = fzf#run({ 'sink': 'tabe', 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
|
||||||
```
|
|
||||||
|
|
||||||
We can also use a Vim list as the source as follows:
|
|
||||||
|
|
||||||
```vim
|
|
||||||
" Choose a color scheme with fzf
|
|
||||||
nnoremap <silent> <Leader>C :call fzf#run({
|
|
||||||
\ 'source':
|
|
||||||
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
|
||||||
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
|
||||||
\ 'sink': 'colo',
|
|
||||||
\ 'options': '+m',
|
|
||||||
\ 'left': 20,
|
|
||||||
\ 'launcher': 'xterm -geometry 20x30 -e bash -ic %s'
|
|
||||||
\ })<CR>
|
|
||||||
```
|
|
||||||
|
|
||||||
`sink` option can be a function reference. The following example creates a
|
|
||||||
handy mapping that selects an open buffer.
|
|
||||||
|
|
||||||
```vim
|
|
||||||
" List of buffers
|
|
||||||
function! s:buflist()
|
|
||||||
redir => ls
|
|
||||||
silent ls
|
|
||||||
redir END
|
|
||||||
return split(ls, '\n')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:bufopen(e)
|
|
||||||
execute 'buffer' matchstr(a:e, '^[ 0-9]*')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
|
||||||
\ 'source': reverse(<sid>buflist()),
|
|
||||||
\ 'sink': function('<sid>bufopen'),
|
|
||||||
\ 'options': '+m',
|
|
||||||
\ 'down': len(<sid>buflist()) + 2
|
|
||||||
\ })<CR>
|
|
||||||
```
|
|
||||||
|
|
||||||
More examples can be found on [the wiki
|
|
||||||
page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).
|
page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
@@ -425,14 +369,6 @@ of fzf to a temporary file.
|
|||||||
fzf > $TMPDIR/fzf.result; and vim (cat $TMPDIR/fzf.result)
|
fzf > $TMPDIR/fzf.result; and vim (cat $TMPDIR/fzf.result)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Handling UTF-8 NFD paths on OSX
|
|
||||||
|
|
||||||
Use iconv to convert NFD paths to NFC:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
find . | iconv -f utf-8-mac -t utf8//ignore | fzf
|
|
||||||
```
|
|
||||||
|
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
20
bin/fzf-tmux
20
bin/fzf-tmux
@@ -82,7 +82,7 @@ while [ $# -gt 0 ]; do
|
|||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z "$TMUX_PANE" ]; then
|
if [ -z "$TMUX_PANE" ] || tmux list-panes -F '#F' | grep -q Z; then
|
||||||
fzf "${args[@]}"
|
fzf "${args[@]}"
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
@@ -91,10 +91,10 @@ set -e
|
|||||||
|
|
||||||
# Clean up named pipes on exit
|
# Clean up named pipes on exit
|
||||||
id=$RANDOM
|
id=$RANDOM
|
||||||
argsf=/tmp/fzf-args-$id
|
argsf="${TMPDIR:-/tmp}/fzf-args-$id"
|
||||||
fifo1=/tmp/fzf-fifo1-$id
|
fifo1="${TMPDIR:-/tmp}/fzf-fifo1-$id"
|
||||||
fifo2=/tmp/fzf-fifo2-$id
|
fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id"
|
||||||
fifo3=/tmp/fzf-fifo3-$id
|
fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"
|
||||||
cleanup() {
|
cleanup() {
|
||||||
rm -f $argsf $fifo1 $fifo2 $fifo3
|
rm -f $argsf $fifo1 $fifo2 $fifo3
|
||||||
}
|
}
|
||||||
@@ -102,12 +102,12 @@ trap cleanup EXIT SIGINT SIGTERM
|
|||||||
|
|
||||||
fail() {
|
fail() {
|
||||||
>&2 echo "$1"
|
>&2 echo "$1"
|
||||||
exit 1
|
exit 2
|
||||||
}
|
}
|
||||||
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
||||||
[ -x "$fzf" ] || fail "fzf executable not found"
|
[ -x "$fzf" ] || fail "fzf executable not found"
|
||||||
|
|
||||||
envs="env "
|
envs="env TERM=$TERM "
|
||||||
[ -n "$FZF_DEFAULT_OPTS" ] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
|
[ -n "$FZF_DEFAULT_OPTS" ] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
|
||||||
[ -n "$FZF_DEFAULT_COMMAND" ] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
|
[ -n "$FZF_DEFAULT_COMMAND" ] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
|
||||||
|
|
||||||
@@ -117,7 +117,9 @@ mkfifo $fifo3
|
|||||||
# Build arguments to fzf
|
# Build arguments to fzf
|
||||||
opts=""
|
opts=""
|
||||||
for arg in "${args[@]}"; do
|
for arg in "${args[@]}"; do
|
||||||
opts="$opts \"${arg//\"/\\\"}\""
|
arg="${arg//\"/\\\"}"
|
||||||
|
arg="${arg//\`/\\\`}"
|
||||||
|
opts="$opts \"$arg\""
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -n "$term" -o -t 0 ]; then
|
if [ -n "$term" -o -t 0 ]; then
|
||||||
@@ -132,5 +134,5 @@ else
|
|||||||
cat <&0 > $fifo1 &
|
cat <&0 > $fifo1 &
|
||||||
fi
|
fi
|
||||||
cat $fifo2
|
cat $fifo2
|
||||||
[ "$(cat $fifo3)" = '0' ]
|
exit "$(cat $fifo3)"
|
||||||
|
|
||||||
|
44
install
44
install
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
version=0.10.1
|
[[ "$@" =~ --pre ]] && version=0.10.7 pre=1 ||
|
||||||
|
version=0.10.7 pre=0
|
||||||
|
|
||||||
cd $(dirname $BASH_SOURCE)
|
cd $(dirname $BASH_SOURCE)
|
||||||
fzf_base=$(pwd)
|
fzf_base=$(pwd)
|
||||||
@@ -20,16 +21,21 @@ ask() {
|
|||||||
|
|
||||||
check_binary() {
|
check_binary() {
|
||||||
echo -n " - Checking fzf executable ... "
|
echo -n " - Checking fzf executable ... "
|
||||||
local output=$("$fzf_base"/bin/fzf --version 2>&1)
|
local output
|
||||||
if [ "$version" = "$output" ]; then
|
output=$("$fzf_base"/bin/fzf --version 2>&1)
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Error: $output"
|
||||||
|
binary_error="Invalid binary"
|
||||||
|
elif [ "$version" != "$output" ]; then
|
||||||
|
echo "$output != $version"
|
||||||
|
binary_error="Invalid version"
|
||||||
|
else
|
||||||
echo "$output"
|
echo "$output"
|
||||||
binary_error=""
|
binary_error=""
|
||||||
else
|
return 0
|
||||||
echo "$output != $version"
|
|
||||||
rm -f "$fzf_base"/bin/fzf
|
|
||||||
binary_error="Invalid binary"
|
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
|
rm -f "$fzf_base"/bin/fzf
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
symlink() {
|
symlink() {
|
||||||
@@ -45,11 +51,13 @@ symlink() {
|
|||||||
|
|
||||||
download() {
|
download() {
|
||||||
echo "Downloading bin/fzf ..."
|
echo "Downloading bin/fzf ..."
|
||||||
if [[ ! $1 =~ dev && -x "$fzf_base"/bin/fzf ]]; then
|
if [ $pre = 0 ]; then
|
||||||
echo " - Already exists"
|
if [ -x "$fzf_base"/bin/fzf ]; then
|
||||||
check_binary && return
|
echo " - Already exists"
|
||||||
elif [ -x "$fzf_base"/bin/$1 ]; then
|
check_binary && return
|
||||||
symlink $1 && check_binary && return
|
elif [ -x "$fzf_base"/bin/$1 ]; then
|
||||||
|
symlink $1 && check_binary && return
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
@@ -93,6 +101,7 @@ if [ -n "$binary_error" ]; then
|
|||||||
echo "No prebuilt binary for $archi ... "
|
echo "No prebuilt binary for $archi ... "
|
||||||
else
|
else
|
||||||
echo " - $binary_error !!!"
|
echo " - $binary_error !!!"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Installing legacy Ruby version ..."
|
echo "Installing legacy Ruby version ..."
|
||||||
|
|
||||||
@@ -176,7 +185,7 @@ for shell in bash zsh; do
|
|||||||
echo -n "Generate ~/.fzf.$shell ... "
|
echo -n "Generate ~/.fzf.$shell ... "
|
||||||
src=~/.fzf.${shell}
|
src=~/.fzf.${shell}
|
||||||
|
|
||||||
fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
|
fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
|
||||||
if [ $auto_completion -ne 0 ]; then
|
if [ $auto_completion -ne 0 ]; then
|
||||||
fzf_completion="# $fzf_completion"
|
fzf_completion="# $fzf_completion"
|
||||||
fi
|
fi
|
||||||
@@ -189,13 +198,13 @@ for shell in bash zsh; do
|
|||||||
cat > $src << EOF
|
cat > $src << EOF
|
||||||
# Setup fzf
|
# Setup fzf
|
||||||
# ---------
|
# ---------
|
||||||
if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then
|
if [[ ! "\$PATH" == *$fzf_base/bin* ]]; then
|
||||||
export PATH="\$PATH:$fzf_base/bin"
|
export PATH="\$PATH:$fzf_base/bin"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Man path
|
# Man path
|
||||||
# --------
|
# --------
|
||||||
if [[ ! "\$MANPATH" =~ "$fzf_base/man" && -d "$fzf_base/man" ]]; then
|
if [[ ! "\$MANPATH" == *$fzf_base/man* && -d "$fzf_base/man" ]]; then
|
||||||
export MANPATH="\$MANPATH:$fzf_base/man"
|
export MANPATH="\$MANPATH:$fzf_base/man"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -236,6 +245,7 @@ EOF
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
append_line() {
|
append_line() {
|
||||||
|
set -e
|
||||||
echo "Update $2:"
|
echo "Update $2:"
|
||||||
echo " - $1"
|
echo " - $1"
|
||||||
[ -f "$2" ] || touch "$2"
|
[ -f "$2" ] || touch "$2"
|
||||||
@@ -247,10 +257,12 @@ append_line() {
|
|||||||
if [ -n "$line" ]; then
|
if [ -n "$line" ]; then
|
||||||
echo " - Already exists: line #$line"
|
echo " - Already exists: line #$line"
|
||||||
else
|
else
|
||||||
|
echo >> "$2"
|
||||||
echo "$1" >> "$2"
|
echo "$1" >> "$2"
|
||||||
echo " + Added"
|
echo " + Added"
|
||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
|
set +e
|
||||||
}
|
}
|
||||||
|
|
||||||
echo
|
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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
..
|
..
|
||||||
.TH fzf 1 "July 2015" "fzf 0.10.1" "fzf - a command-line fuzzy finder"
|
.TH fzf 1 "Oct 2015" "fzf 0.10.7" "fzf - a command-line fuzzy finder"
|
||||||
|
|
||||||
.SH NAME
|
.SH NAME
|
||||||
fzf - a command-line fuzzy finder
|
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
|
Maximum number of entries in the history file (default: 1000). The file is
|
||||||
automatically truncated when the number of the lines exceeds the value.
|
automatically truncated when the number of the lines exceeds the value.
|
||||||
.TP
|
.TP
|
||||||
.BI "--header-file=" "FILE"
|
.BI "--header=" "STR"
|
||||||
The content of the file will be printed as the sticky header. The lines in the
|
The given string will be printed as the sticky header. The lines are displayed
|
||||||
file are displayed in order from top to bottom regardless of \fB--reverse\fR,
|
in the given order from top to bottom regardless of \fB--reverse\fR option, and
|
||||||
and are not affected by \fB--with-nth\fR. ANSI color codes are processed even
|
are not affected by \fB--with-nth\fR. ANSI color codes are processed even when
|
||||||
when \fB--ansi\fR is not set.
|
\fB--ansi\fR is not set.
|
||||||
.TP
|
.TP
|
||||||
.BI "--header-lines=" "N"
|
.BI "--header-lines=" "N"
|
||||||
The first N lines of the input are treated as the sticky header. When
|
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
|
such as: \fB'wild ^music .mp3$ sbtrkt !rmx\fR
|
||||||
|
|
||||||
.SS Exact-match (quoted)
|
.SS Exact-match (quoted)
|
||||||
A term that is prefixed by a single-quote character (') is interpreted as an
|
A term that is prefixed by a single-quote character (\fB'\fR) is interpreted as
|
||||||
"exact-match" (or "non-fuzzy") term. fzf will search for the exact occurrences
|
an "exact-match" (or "non-fuzzy") term. fzf will search for the exact
|
||||||
of the string.
|
occurrences of the string.
|
||||||
|
|
||||||
.SS Anchored-match
|
.SS Anchored-match
|
||||||
A term can be prefixed by ^, or suffixed by $ to become an anchored-match term.
|
A term can be prefixed by \fB^\fR, or suffixed by \fB$\fR to become an
|
||||||
Then fzf will search for the items that start with or end with the given
|
anchored-match term. Then fzf will search for the items that start with or end
|
||||||
string. An anchored-match term is also an exact-match term.
|
with the given string. An anchored-match term is also an exact-match term.
|
||||||
|
|
||||||
.SS Negation
|
.SS Negation
|
||||||
If a term is prefixed by !, fzf will exclude the items that satisfy the term
|
If a term is prefixed by \fB!\fR, fzf will exclude the items that satisfy the
|
||||||
from the result.
|
term from the result.
|
||||||
|
|
||||||
.SS Extended-exact mode
|
.SS Extended-exact mode
|
||||||
If you don't need fuzzy matching at all and do not wish to "quote" (prefixing
|
If you don't prefer fuzzy matching and do not wish to "quote" (prefixing with
|
||||||
with ') every word, start fzf with \fB-e\fR or \fB--extended-exact\fR option
|
\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).
|
(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
|
.SH AUTHOR
|
||||||
Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
||||||
|
|
||||||
.SH SEE ALSO
|
.SH SEE ALSO
|
||||||
|
.B Project homepage:
|
||||||
|
.RS
|
||||||
.I https://github.com/junegunn/fzf
|
.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
|
.SH LICENSE
|
||||||
MIT
|
MIT
|
||||||
|
122
plugin/fzf.vim
122
plugin/fzf.vim
@@ -40,9 +40,7 @@ function! s:fzf_exec()
|
|||||||
\ input('fzf executable not found. Download binary? (y/n) ') =~? '^y'
|
\ input('fzf executable not found. Download binary? (y/n) ') =~? '^y'
|
||||||
redraw
|
redraw
|
||||||
echo
|
echo
|
||||||
echohl WarningMsg
|
call s:warn('Downloading fzf binary. Please wait ...')
|
||||||
echo 'Downloading fzf binary. Please wait ...'
|
|
||||||
echohl None
|
|
||||||
let s:installed = 1
|
let s:installed = 1
|
||||||
call system(s:install.' --bin')
|
call system(s:install.' --bin')
|
||||||
return s:fzf_exec()
|
return s:fzf_exec()
|
||||||
@@ -54,13 +52,17 @@ function! s:fzf_exec()
|
|||||||
return s:exec
|
return s:exec
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:tmux_not_zoomed()
|
||||||
|
return system('tmux list-panes -F "#F"') !~# 'Z'
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:tmux_enabled()
|
function! s:tmux_enabled()
|
||||||
if has('gui_running')
|
if has('gui_running')
|
||||||
return 0
|
return 0
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if exists('s:tmux')
|
if exists('s:tmux')
|
||||||
return s:tmux
|
return s:tmux && s:tmux_not_zoomed()
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let s:tmux = 0
|
let s:tmux = 0
|
||||||
@@ -68,7 +70,7 @@ function! s:tmux_enabled()
|
|||||||
let output = system('tmux -V')
|
let output = system('tmux -V')
|
||||||
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
||||||
endif
|
endif
|
||||||
return s:tmux
|
return s:tmux && s:tmux_not_zoomed()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:shellesc(arg)
|
function! s:shellesc(arg)
|
||||||
@@ -76,7 +78,7 @@ function! s:shellesc(arg)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:escape(path)
|
function! s:escape(path)
|
||||||
return escape(a:path, ' %#\')
|
return escape(a:path, ' %#''"\')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Upgrade legacy options
|
" Upgrade legacy options
|
||||||
@@ -94,14 +96,24 @@ function! s:upgrade(dict)
|
|||||||
return copy
|
return copy
|
||||||
endfunction
|
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
|
function! fzf#run(...) abort
|
||||||
try
|
try
|
||||||
let oshell = &shell
|
let oshell = &shell
|
||||||
set shell=sh
|
set shell=sh
|
||||||
if has('nvim') && bufexists('term://*:FZF')
|
if has('nvim') && bufexists('term://*:FZF')
|
||||||
echohl WarningMsg
|
call s:warn('FZF is already running!')
|
||||||
echomsg 'FZF is already running!'
|
|
||||||
echohl None
|
|
||||||
return []
|
return []
|
||||||
endif
|
endif
|
||||||
let dict = exists('a:1') ? s:upgrade(a:1) : {}
|
let dict = exists('a:1') ? s:upgrade(a:1) : {}
|
||||||
@@ -160,7 +172,13 @@ function! s:fzf_tmux(dict)
|
|||||||
let size = ''
|
let size = ''
|
||||||
for o in ['up', 'down', 'left', 'right']
|
for o in ['up', 'down', 'left', 'right']
|
||||||
if s:present(a:dict, o)
|
if s:present(a:dict, o)
|
||||||
let size = '-'.o[0].(a:dict[o] == 1 ? '' : a:dict[o])
|
let spec = a:dict[o]
|
||||||
|
if (o == 'up' || o == 'down') && spec[0] == '~'
|
||||||
|
let size = '-'.o[0].s:calc_size(&lines, spec[1:], a:dict)
|
||||||
|
else
|
||||||
|
" Legacy boolean option
|
||||||
|
let size = '-'.o[0].(spec == 1 ? '' : spec)
|
||||||
|
endif
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
@@ -195,7 +213,7 @@ endfunction
|
|||||||
function! s:xterm_launcher()
|
function! s:xterm_launcher()
|
||||||
let fmt = 'xterm -T "[fzf]" -bg "\%s" -fg "\%s" -geometry %dx%d+%d+%d -e bash -ic %%s'
|
let fmt = 'xterm -T "[fzf]" -bg "\%s" -fg "\%s" -geometry %dx%d+%d+%d -e bash -ic %%s'
|
||||||
if has('gui_macvim')
|
if has('gui_macvim')
|
||||||
let fmt .= '; osascript -e "tell application \"MacVim\" to activate"'
|
let fmt .= '&& osascript -e "tell application \"MacVim\" to activate"'
|
||||||
endif
|
endif
|
||||||
return printf(fmt,
|
return printf(fmt,
|
||||||
\ synIDattr(hlID("Normal"), "bg"), synIDattr(hlID("Normal"), "fg"),
|
\ synIDattr(hlID("Normal"), "bg"), synIDattr(hlID("Normal"), "fg"),
|
||||||
@@ -204,28 +222,33 @@ endfunction
|
|||||||
unlet! s:launcher
|
unlet! s:launcher
|
||||||
let s:launcher = function('s:xterm_launcher')
|
let s:launcher = function('s:xterm_launcher')
|
||||||
|
|
||||||
|
function! s:exit_handler(code, command, ...)
|
||||||
|
if a:code == 130
|
||||||
|
return 0
|
||||||
|
elseif a:code > 1
|
||||||
|
call s:error('Error running ' . a:command)
|
||||||
|
if !empty(a:000)
|
||||||
|
sleep
|
||||||
|
endif
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
return 1
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:execute(dict, command, temps)
|
function! s:execute(dict, command, temps)
|
||||||
call s:pushd(a:dict)
|
call s:pushd(a:dict)
|
||||||
silent! !clear 2> /dev/null
|
silent! !clear 2> /dev/null
|
||||||
|
let escaped = escape(substitute(a:command, '\n', '\\n', 'g'), '%#')
|
||||||
if has('gui_running')
|
if has('gui_running')
|
||||||
let Launcher = get(a:dict, 'launcher', get(g:, 'Fzf_launcher', get(g:, 'fzf_launcher', s:launcher)))
|
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 fmt = type(Launcher) == 2 ? call(Launcher, []) : Launcher
|
||||||
let command = printf(fmt, "'".substitute(a:command, "'", "'\"'\"'", 'g')."'")
|
let command = printf(fmt, "'".substitute(escaped, "'", "'\"'\"'", 'g')."'")
|
||||||
else
|
else
|
||||||
let command = a:command
|
let command = escaped
|
||||||
endif
|
endif
|
||||||
execute 'silent !'.command
|
execute 'silent !'.command
|
||||||
redraw!
|
redraw!
|
||||||
if v:shell_error
|
return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []
|
||||||
" Do not print error message on exit status 1
|
|
||||||
if v:shell_error > 1
|
|
||||||
echohl ErrorMsg
|
|
||||||
echo 'Error running ' . command
|
|
||||||
endif
|
|
||||||
return []
|
|
||||||
else
|
|
||||||
return s:callback(a:dict, a:temps)
|
|
||||||
endif
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute_tmux(dict, command, temps)
|
function! s:execute_tmux(dict, command, temps)
|
||||||
@@ -237,15 +260,24 @@ function! s:execute_tmux(dict, command, temps)
|
|||||||
|
|
||||||
call system(command)
|
call system(command)
|
||||||
redraw!
|
redraw!
|
||||||
return s:callback(a:dict, a:temps)
|
return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:calc_size(max, val)
|
function! s:calc_size(max, val, dict)
|
||||||
if a:val =~ '%$'
|
if a:val =~ '%$'
|
||||||
return a:max * str2nr(a:val[:-2]) / 100
|
let size = a:max * str2nr(a:val[:-2]) / 100
|
||||||
else
|
else
|
||||||
return min([a:max, a:val])
|
let size = min([a:max, str2nr(a:val)])
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
let srcsz = -1
|
||||||
|
if type(get(a:dict, 'source', 0)) == type([])
|
||||||
|
let srcsz = len(a:dict.source)
|
||||||
|
endif
|
||||||
|
|
||||||
|
let opts = get(a:dict, 'options', '').$FZF_DEFAULT_OPTS
|
||||||
|
let margin = stridx(opts, '--inline-info') > stridx(opts, '--no-inline-info') ? 1 : 2
|
||||||
|
return srcsz >= 0 ? min([srcsz + margin, size]) : size
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:getpos()
|
function! s:getpos()
|
||||||
@@ -264,7 +296,11 @@ function! s:split(dict)
|
|||||||
let val = get(a:dict, dir, '')
|
let val = get(a:dict, dir, '')
|
||||||
if !empty(val)
|
if !empty(val)
|
||||||
let [cmd, resz, max] = triple
|
let [cmd, resz, max] = triple
|
||||||
let sz = s:calc_size(max, val)
|
if (dir == 'up' || dir == 'down') && val[0] == '~'
|
||||||
|
let sz = s:calc_size(max, val[1:], a:dict)
|
||||||
|
else
|
||||||
|
let sz = s:calc_size(max, val, {})
|
||||||
|
endif
|
||||||
execute cmd sz.'new'
|
execute cmd sz.'new'
|
||||||
execute resz sz
|
execute resz sz
|
||||||
return
|
return
|
||||||
@@ -285,6 +321,7 @@ function! s:execute_term(dict, command, temps)
|
|||||||
call s:pushd(a:dict)
|
call s:pushd(a:dict)
|
||||||
|
|
||||||
let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps, 'name': 'FZF' }
|
let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps, 'name': 'FZF' }
|
||||||
|
let s:command = a:command
|
||||||
function! fzf.on_exit(id, code)
|
function! fzf.on_exit(id, code)
|
||||||
let pos = s:getpos()
|
let pos = s:getpos()
|
||||||
let inplace = pos == s:ppos " {'window': 'enew'}
|
let inplace = pos == s:ppos " {'window': 'enew'}
|
||||||
@@ -298,9 +335,13 @@ function! s:execute_term(dict, command, temps)
|
|||||||
wincmd p
|
wincmd p
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if !s:exit_handler(a:code, s:command, 1)
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
call s:pushd(self.dict)
|
call s:pushd(self.dict)
|
||||||
try
|
try
|
||||||
redraw!
|
|
||||||
call s:callback(self.dict, self.temps)
|
call s:callback(self.dict, self.temps)
|
||||||
|
|
||||||
if inplace && bufnr('') == self.buf
|
if inplace && bufnr('') == self.buf
|
||||||
@@ -354,7 +395,7 @@ endfunction
|
|||||||
|
|
||||||
let s:default_action = {
|
let s:default_action = {
|
||||||
\ 'ctrl-m': 'e',
|
\ 'ctrl-m': 'e',
|
||||||
\ 'ctrl-t': 'tabedit',
|
\ 'ctrl-t': 'tab split',
|
||||||
\ 'ctrl-x': 'split',
|
\ 'ctrl-x': 'split',
|
||||||
\ 'ctrl-v': 'vsplit' }
|
\ 'ctrl-v': 'vsplit' }
|
||||||
|
|
||||||
@@ -364,9 +405,22 @@ function! s:cmd_callback(lines) abort
|
|||||||
endif
|
endif
|
||||||
let key = remove(a:lines, 0)
|
let key = remove(a:lines, 0)
|
||||||
let cmd = get(s:action, key, 'e')
|
let cmd = get(s:action, key, 'e')
|
||||||
for item in a:lines
|
if len(a:lines) > 1
|
||||||
execute cmd s:escape(item)
|
augroup fzf_swap
|
||||||
endfor
|
autocmd SwapExists * let v:swapchoice='o'
|
||||||
|
\| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))
|
||||||
|
augroup END
|
||||||
|
endif
|
||||||
|
try
|
||||||
|
let autochdir = &autochdir
|
||||||
|
set noautochdir
|
||||||
|
for item in a:lines
|
||||||
|
execute cmd s:escape(item)
|
||||||
|
endfor
|
||||||
|
finally
|
||||||
|
let &autochdir = autochdir
|
||||||
|
silent! autocmd! fzf_swap
|
||||||
|
endtry
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:cmd(bang, ...) abort
|
function! s:cmd(bang, ...) abort
|
||||||
@@ -374,7 +428,7 @@ function! s:cmd(bang, ...) abort
|
|||||||
let args = extend(['--expect='.join(keys(s:action), ',')], a:000)
|
let args = extend(['--expect='.join(keys(s:action), ',')], a:000)
|
||||||
let opts = {}
|
let opts = {}
|
||||||
if len(args) > 0 && isdirectory(expand(args[-1]))
|
if len(args) > 0 && isdirectory(expand(args[-1]))
|
||||||
let opts.dir = remove(args, -1)
|
let opts.dir = substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g')
|
||||||
endif
|
endif
|
||||||
if !a:bang
|
if !a:bang
|
||||||
let opts.down = get(g:, 'fzf_height', get(g:, 'fzf_tmux_height', s:default_height))
|
let opts.down = get(g:, 'fzf_height', get(g:, 'fzf_tmux_height', s:default_height))
|
||||||
|
@@ -49,7 +49,7 @@ _fzf_opts_completion() {
|
|||||||
--cycle
|
--cycle
|
||||||
--history
|
--history
|
||||||
--history-size
|
--history-size
|
||||||
--header-file
|
--header
|
||||||
--header-lines
|
--header-lines
|
||||||
--margin"
|
--margin"
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ _fzf_opts_completion() {
|
|||||||
COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--history|--header-file)
|
--history)
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
@@ -94,7 +94,7 @@ _fzf_handle_dynamic_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local cur base dir leftover matches trigger cmd fzf
|
local cur base dir leftover matches trigger cmd fzf
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
@@ -135,20 +135,29 @@ _fzf_path_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_list_completion() {
|
_fzf_feed_fifo() (
|
||||||
local cur selected trigger cmd src fzf
|
rm -f "$fifo"
|
||||||
|
mkfifo "$fifo"
|
||||||
|
cat <&0 > "$fifo" &
|
||||||
|
)
|
||||||
|
|
||||||
|
_fzf_complete() {
|
||||||
|
local fifo cur selected trigger cmd fzf
|
||||||
|
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
read -r src
|
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
if [[ ${cur} == *"$trigger" ]]; then
|
if [[ ${cur} == *"$trigger" ]]; then
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
_fzf_feed_fifo "$fifo"
|
||||||
tput sc
|
tput sc
|
||||||
selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
selected=$(eval "cat '$fifo' | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
||||||
selected=${selected% }
|
selected=${selected% } # Strip trailing space not to repeat "-o nospace"
|
||||||
tput rc
|
tput rc
|
||||||
|
rm -f "$fifo"
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
if [ -n "$selected" ]; then
|
||||||
COMPREPLY=("$selected")
|
COMPREPLY=("$selected")
|
||||||
@@ -160,25 +169,25 @@ _fzf_list_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_path_completion() {
|
||||||
_fzf_path_completion \
|
__fzf_generic_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_path_completion \
|
__fzf_generic_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_path_completion \
|
__fzf_generic_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_complete_kill() {
|
||||||
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||||
|
|
||||||
local selected fzf
|
local selected fzf
|
||||||
@@ -193,28 +202,37 @@ _fzf_kill_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
||||||
EOF
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
||||||
EOF
|
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
||||||
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_env_var_completion() {
|
_fzf_complete_unset() {
|
||||||
_fzf_list_completion '-m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_alias_completion() {
|
_fzf_complete_export() {
|
||||||
_fzf_list_completion '-m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
alias | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_complete_unalias() {
|
||||||
|
_fzf_complete '-m' "$@" < <(
|
||||||
|
alias | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
# fzf options
|
# fzf options
|
||||||
@@ -257,19 +275,19 @@ done
|
|||||||
|
|
||||||
# Anything
|
# Anything
|
||||||
for cmd in $a_cmds; do
|
for cmd in $a_cmds; do
|
||||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
complete -F _fzf_path_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
# Kill completion
|
# Kill completion
|
||||||
complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill
|
||||||
|
|
||||||
# Host completion
|
# Host completion
|
||||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
complete -F _fzf_complete_ssh -o default -o bashdefault ssh
|
||||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
complete -F _fzf_complete_telnet -o default -o bashdefault telnet
|
||||||
|
|
||||||
# Environment variables / Aliases
|
# Environment variables / Aliases
|
||||||
complete -F _fzf_env_var_completion -o default -o bashdefault unset
|
complete -F _fzf_complete_unset -o default -o bashdefault unset
|
||||||
complete -F _fzf_env_var_completion -o default -o bashdefault export
|
complete -F _fzf_complete_export -o default -o bashdefault export
|
||||||
complete -F _fzf_alias_completion -o default -o bashdefault unalias
|
complete -F _fzf_complete_unalias -o default -o bashdefault unalias
|
||||||
|
|
||||||
unset cmd d_cmds f_cmds a_cmds x_cmds
|
unset cmd d_cmds f_cmds a_cmds x_cmds
|
||||||
|
@@ -10,8 +10,9 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
_fzf_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm
|
local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm
|
||||||
|
# (Q) flag removes a quoting level: "foo\ bar" => "foo bar"
|
||||||
base=${(Q)1}
|
base=${(Q)1}
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
find_opts=$3
|
find_opts=$3
|
||||||
@@ -47,59 +48,79 @@ _fzf_path_completion() {
|
|||||||
[ -n "$nnm" ] && unsetopt nonomatch
|
[ -n "$nnm" ] && unsetopt nonomatch
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_path_completion() {
|
||||||
_fzf_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" \
|
||||||
"-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_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
_fzf_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||||
"" "/" ""
|
"" "/" ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_list_completion() {
|
_fzf_feed_fifo() (
|
||||||
local prefix lbuf fzf_opts src fzf matches
|
rm -f "$fifo"
|
||||||
prefix=$1
|
mkfifo "$fifo"
|
||||||
|
cat <&0 > "$fifo" &
|
||||||
|
)
|
||||||
|
|
||||||
|
_fzf_complete() {
|
||||||
|
local fifo fzf_opts lbuf fzf matches
|
||||||
|
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
||||||
|
fzf_opts=$1
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
fzf_opts=$3
|
|
||||||
read -r src
|
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
|
|
||||||
matches=$(eval "$src" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$prefix")
|
_fzf_feed_fifo "$fifo"
|
||||||
|
matches=$(cat "$fifo" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "${(Q)prefix}" | tr '\n' ' ')
|
||||||
if [ -n "$matches" ]; then
|
if [ -n "$matches" ]; then
|
||||||
LBUFFER="$lbuf$matches "
|
LBUFFER="$lbuf$matches"
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
rm -f "$fifo"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_list_completion "$1" "$2" '+m' << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
||||||
EOF
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_list_completion "$1" "$2" '+m' << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
||||||
EOF
|
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
||||||
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_env_var_completion() {
|
_fzf_complete_export() {
|
||||||
_fzf_list_completion "$1" "$2" '+m' << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_alias_completion() {
|
_fzf_complete_unset() {
|
||||||
_fzf_list_completion "$1" "$2" '+m' << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
alias | sed 's/=.*//'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_complete_unalias() {
|
||||||
|
_fzf_complete '+m' "$@" < <(
|
||||||
|
alias | sed 's/=.*//'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fzf-completion() {
|
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/FAQ/zshfaq03.html
|
||||||
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
||||||
@@ -131,23 +152,18 @@ fzf-completion() {
|
|||||||
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
||||||
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
||||||
|
|
||||||
if [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
|
if eval "type _fzf_complete_${cmd} > /dev/null"; then
|
||||||
_fzf_dir_completion "$prefix" $lbuf
|
eval "prefix=\"$prefix\" _fzf_complete_${cmd} \"$lbuf\""
|
||||||
elif [ $cmd = telnet ]; then
|
elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
|
||||||
_fzf_telnet_completion "$prefix" $lbuf
|
_fzf_dir_completion "$prefix" "$lbuf"
|
||||||
elif [ $cmd = ssh ]; then
|
|
||||||
_fzf_ssh_completion "$prefix" $lbuf
|
|
||||||
elif [ $cmd = unset -o $cmd = export ]; then
|
|
||||||
_fzf_env_var_completion "$prefix" $lbuf
|
|
||||||
elif [ $cmd = unalias ]; then
|
|
||||||
_fzf_alias_completion "$prefix" $lbuf
|
|
||||||
else
|
else
|
||||||
_fzf_all_completion "$prefix" $lbuf
|
_fzf_path_completion "$prefix" "$lbuf"
|
||||||
fi
|
fi
|
||||||
# Fall back to default completion
|
# Fall back to default completion
|
||||||
else
|
else
|
||||||
eval "zle ${fzf_default_completion:-expand-or-complete}"
|
eval "zle ${fzf_default_completion:-expand-or-complete}"
|
||||||
fi
|
fi
|
||||||
|
[ -n "$sws" ] && setopt shwordsplit
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -z "$fzf_default_completion" ] &&
|
[ -z "$fzf_default_completion" ] &&
|
||||||
|
@@ -19,7 +19,7 @@ function fzf_key_bindings
|
|||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | sed 1d | cut -b3-"
|
-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)
|
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
if [[ $- =~ i ]]; then
|
if [[ $- == *i* ]]; then
|
||||||
|
|
||||||
# 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() {
|
||||||
|
@@ -2,6 +2,7 @@ FROM base/archlinux:2014.07.03
|
|||||||
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||||
|
|
||||||
# apt-get
|
# apt-get
|
||||||
|
RUN pacman-key --populate archlinux && pacman-key --refresh-keys
|
||||||
RUN pacman-db-upgrade && pacman -Syu --noconfirm base-devel git
|
RUN pacman-db-upgrade && pacman -Syu --noconfirm base-devel git
|
||||||
|
|
||||||
# Install Go 1.4
|
# Install Go 1.4
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
FROM centos:centos7
|
FROM centos:centos6
|
||||||
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||||
|
|
||||||
# yum
|
# 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
|
# Install Go 1.4
|
||||||
RUN cd / && curl \
|
RUN cd / && curl \
|
||||||
@@ -13,6 +15,9 @@ ENV GOPATH /go
|
|||||||
ENV GOROOT /go1.4
|
ENV GOROOT /go1.4
|
||||||
ENV PATH /go1.4/bin:$PATH
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
|
# For i386 build
|
||||||
|
RUN cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
# Volume
|
# Volume
|
||||||
VOLUME /go
|
VOLUME /go
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@ MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
|||||||
|
|
||||||
# apt-get
|
# apt-get
|
||||||
RUN apt-get update && apt-get -y upgrade && \
|
RUN apt-get update && apt-get -y upgrade && \
|
||||||
apt-get install -y --force-yes git curl build-essential libncurses-dev
|
apt-get install -y --force-yes git curl build-essential libncurses-dev libgpm-dev
|
||||||
|
|
||||||
# Install Go 1.4
|
# Install Go 1.4
|
||||||
RUN cd / && curl \
|
RUN cd / && curl \
|
||||||
|
45
src/Makefile
45
src/Makefile
@@ -25,10 +25,9 @@ RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd64
|
|||||||
all: release
|
all: release
|
||||||
|
|
||||||
release: build
|
release: build
|
||||||
cd fzf && \
|
-cd fzf && cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32)
|
||||||
cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32) && \
|
cd fzf && cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
||||||
cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
rm -f $(RELEASE32) $(RELEASE64)
|
||||||
rm $(RELEASE32) $(RELEASE64)
|
|
||||||
|
|
||||||
build: test fzf/$(BINARY32) fzf/$(BINARY64)
|
build: test fzf/$(BINARY32) fzf/$(BINARY64)
|
||||||
|
|
||||||
@@ -42,13 +41,13 @@ uninstall:
|
|||||||
rm -f $(BINDIR)/fzf $(BINDIR)/$(BINARY64)
|
rm -f $(BINDIR)/fzf $(BINDIR)/$(BINARY64)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
cd fzf && rm -f $(BINARY32) $(BINARY64) $(RELEASE32).tgz $(RELEASE64).tgz
|
cd fzf && rm -f fzf-*
|
||||||
|
|
||||||
fzf/$(BINARY32): $(SOURCES)
|
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)
|
fzf/$(BINARY64): $(SOURCES)
|
||||||
cd fzf && go build -o $(BINARY64)
|
cd fzf && go build -a -tags "$(TAGS)" -o $(BINARY64)
|
||||||
|
|
||||||
$(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
$(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
||||||
cp -f fzf/$(BINARY64) $(BINDIR)
|
cp -f fzf/$(BINARY64) $(BINDIR)
|
||||||
@@ -57,18 +56,30 @@ $(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
|||||||
$(BINDIR):
|
$(BINDIR):
|
||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
|
|
||||||
# Linux distribution to build fzf on
|
docker-arch:
|
||||||
DISTRO := arch
|
docker build -t junegunn/arch-sandbox - < Dockerfile.arch
|
||||||
|
|
||||||
docker:
|
docker-ubuntu:
|
||||||
docker build -t junegunn/$(DISTRO)-sandbox - < Dockerfile.$(DISTRO)
|
docker build -t junegunn/ubuntu-sandbox - < Dockerfile.ubuntu
|
||||||
|
|
||||||
linux: docker
|
docker-centos:
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/$(DISTRO)-sandbox \
|
docker build -t junegunn/centos-sandbox - < Dockerfile.centos
|
||||||
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make'
|
|
||||||
|
|
||||||
$(DISTRO): docker
|
arch: docker-arch
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/$(DISTRO)-sandbox \
|
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
||||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||||
|
|
||||||
.PHONY: all build release test install uninstall clean docker linux $(DISTRO)
|
ubuntu: docker-ubuntu
|
||||||
|
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
||||||
|
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||||
|
|
||||||
|
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: 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 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.
|
* 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
|
// 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 {
|
if len(pattern) == 0 {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
@@ -34,7 +41,11 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
|||||||
sidx := -1
|
sidx := -1
|
||||||
eidx := -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
|
// This is considerably faster than blindly applying strings.ToLower to the
|
||||||
// whole string
|
// whole string
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
@@ -47,11 +58,12 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
|||||||
char = unicode.To(unicode.LowerCase, char)
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if char == pattern[pidx] {
|
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||||
|
if char == pchar {
|
||||||
if sidx < 0 {
|
if sidx < 0 {
|
||||||
sidx = index
|
sidx = index
|
||||||
}
|
}
|
||||||
if pidx++; pidx == len(pattern) {
|
if pidx++; pidx == lenPattern {
|
||||||
eidx = index + 1
|
eidx = index + 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -61,7 +73,7 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
|||||||
if sidx >= 0 && eidx >= 0 {
|
if sidx >= 0 && eidx >= 0 {
|
||||||
pidx--
|
pidx--
|
||||||
for index := eidx - 1; index >= sidx; index-- {
|
for index := eidx - 1; index >= sidx; index-- {
|
||||||
char := (*runes)[index]
|
char := runeAt(runes, index, lenRunes, forward)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
if char >= 'A' && char <= 'Z' {
|
if char >= 'A' && char <= 'Z' {
|
||||||
char += 32
|
char += 32
|
||||||
@@ -69,14 +81,19 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
|||||||
char = unicode.To(unicode.LowerCase, char)
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if char == pattern[pidx] {
|
|
||||||
|
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||||
|
if char == pchar {
|
||||||
if pidx--; pidx < 0 {
|
if pidx--; pidx < 0 {
|
||||||
sidx = index
|
sidx = index
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sidx, eidx
|
if forward {
|
||||||
|
return sidx, eidx
|
||||||
|
}
|
||||||
|
return lenRunes - eidx, lenRunes - sidx
|
||||||
}
|
}
|
||||||
return -1, -1
|
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:
|
// We might try to implement better algorithms in the future:
|
||||||
// http://en.wikipedia.org/wiki/String_searching_algorithm
|
// 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 {
|
if len(pattern) == 0 {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
numRunes := len(*runes)
|
lenRunes := len(runes)
|
||||||
plen := len(pattern)
|
lenPattern := len(pattern)
|
||||||
if numRunes < plen {
|
|
||||||
|
if lenRunes < lenPattern {
|
||||||
return -1, -1
|
return -1, -1
|
||||||
}
|
}
|
||||||
|
|
||||||
pidx := 0
|
pidx := 0
|
||||||
for index := 0; index < numRunes; index++ {
|
for index := 0; index < lenRunes; index++ {
|
||||||
char := (*runes)[index]
|
char := runeAt(runes, index, lenRunes, forward)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
if char >= 'A' && char <= 'Z' {
|
if char >= 'A' && char <= 'Z' {
|
||||||
char += 32
|
char += 32
|
||||||
@@ -109,10 +127,14 @@ func ExactMatchNaive(caseSensitive bool, runes *[]rune, pattern []rune) (int, in
|
|||||||
char = unicode.To(unicode.LowerCase, char)
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if pattern[pidx] == char {
|
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
||||||
|
if pchar == char {
|
||||||
pidx++
|
pidx++
|
||||||
if pidx == plen {
|
if pidx == lenPattern {
|
||||||
return index - plen + 1, index + 1
|
if forward {
|
||||||
|
return index - lenPattern + 1, index + 1
|
||||||
|
}
|
||||||
|
return lenRunes - (index + 1), lenRunes - (index - lenPattern + 1)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
index -= pidx
|
index -= pidx
|
||||||
@@ -123,13 +145,13 @@ func ExactMatchNaive(caseSensitive bool, runes *[]rune, pattern []rune) (int, in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PrefixMatch performs prefix-match
|
// 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) {
|
if len(runes) < len(pattern) {
|
||||||
return -1, -1
|
return -1, -1
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, r := range pattern {
|
for index, r := range pattern {
|
||||||
char := (*runes)[index]
|
char := runes[index]
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
char = unicode.ToLower(char)
|
char = unicode.ToLower(char)
|
||||||
}
|
}
|
||||||
@@ -141,7 +163,7 @@ func PrefixMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SuffixMatch performs suffix-match
|
// 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)
|
runes := util.TrimRight(input)
|
||||||
trimmedLen := len(runes)
|
trimmedLen := len(runes)
|
||||||
diff := trimmedLen - len(pattern)
|
diff := trimmedLen - len(pattern)
|
||||||
@@ -161,11 +183,12 @@ func SuffixMatch(caseSensitive bool, input *[]rune, pattern []rune) (int, int) {
|
|||||||
return trimmedLen - len(pattern), trimmedLen
|
return trimmedLen - len(pattern), trimmedLen
|
||||||
}
|
}
|
||||||
|
|
||||||
func EqualMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
|
// EqualMatch performs equal-match
|
||||||
if len(*runes) != len(pattern) {
|
func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
||||||
|
if len(runes) != len(pattern) {
|
||||||
return -1, -1
|
return -1, -1
|
||||||
}
|
}
|
||||||
runesStr := string(*runes)
|
runesStr := string(runes)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
runesStr = strings.ToLower(runesStr)
|
runesStr = strings.ToLower(runesStr)
|
||||||
}
|
}
|
||||||
|
@@ -5,12 +5,11 @@ import (
|
|||||||
"testing"
|
"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 {
|
if !caseSensitive {
|
||||||
pattern = strings.ToLower(pattern)
|
pattern = strings.ToLower(pattern)
|
||||||
}
|
}
|
||||||
runes := []rune(input)
|
s, e := fun(caseSensitive, forward, []rune(input), []rune(pattern))
|
||||||
s, e := fun(caseSensitive, &runes, []rune(pattern))
|
|
||||||
if s != sidx {
|
if s != sidx {
|
||||||
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
|
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
|
||||||
}
|
}
|
||||||
@@ -20,33 +19,51 @@ func assertMatch(t *testing.T, fun func(bool, *[]rune, []rune) (int, int), caseS
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFuzzyMatch(t *testing.T) {
|
func TestFuzzyMatch(t *testing.T) {
|
||||||
assertMatch(t, FuzzyMatch, false, "fooBarbaz", "oBZ", 2, 9)
|
assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9)
|
||||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBZ", -1, -1)
|
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1)
|
||||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBz", 2, 9)
|
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9)
|
||||||
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
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) {
|
func TestExactMatchNaive(t *testing.T) {
|
||||||
assertMatch(t, ExactMatchNaive, false, "fooBarbaz", "oBA", 2, 5)
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "oBA", -1, -1)
|
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5)
|
||||||
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
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) {
|
func TestPrefixMatch(t *testing.T) {
|
||||||
assertMatch(t, PrefixMatch, false, "fooBarbaz", "Foo", 0, 3)
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, PrefixMatch, true, "fooBarbaz", "Foo", -1, -1)
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3)
|
||||||
assertMatch(t, PrefixMatch, false, "fooBarbaz", "baz", -1, -1)
|
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1)
|
||||||
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSuffixMatch(t *testing.T) {
|
func TestSuffixMatch(t *testing.T) {
|
||||||
assertMatch(t, SuffixMatch, false, "fooBarbaz", "Foo", -1, -1)
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9)
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1)
|
||||||
assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -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) {
|
func TestEmptyPattern(t *testing.T) {
|
||||||
assertMatch(t, FuzzyMatch, true, "foobar", "", 0, 0)
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, ExactMatchNaive, true, "foobar", "", 0, 0)
|
assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0)
|
||||||
assertMatch(t, PrefixMatch, true, "foobar", "", 0, 0)
|
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0)
|
||||||
assertMatch(t, SuffixMatch, true, "foobar", "", 6, 6)
|
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0)
|
||||||
|
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
13
src/ansi.go
13
src/ansi.go
@@ -36,7 +36,7 @@ func init() {
|
|||||||
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")
|
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractColor(str *string, state *ansiState) (*string, []ansiOffset, *ansiState) {
|
func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiState) {
|
||||||
var offsets []ansiOffset
|
var offsets []ansiOffset
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
|
|
||||||
@@ -45,9 +45,9 @@ func extractColor(str *string, state *ansiState) (*string, []ansiOffset, *ansiSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
for _, offset := range ansiRegex.FindAllStringIndex(*str, -1) {
|
for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {
|
||||||
output.WriteString((*str)[idx:offset[0]])
|
output.WriteString(str[idx:offset[0]])
|
||||||
newState := interpretCode((*str)[offset[0]:offset[1]], state)
|
newState := interpretCode(str[offset[0]:offset[1]], state)
|
||||||
|
|
||||||
if !newState.equals(state) {
|
if !newState.equals(state) {
|
||||||
if state != nil {
|
if state != nil {
|
||||||
@@ -69,7 +69,7 @@ func extractColor(str *string, state *ansiState) (*string, []ansiOffset, *ansiSt
|
|||||||
idx = offset[1]
|
idx = offset[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
rest := (*str)[idx:]
|
rest := str[idx:]
|
||||||
if len(rest) > 0 {
|
if len(rest) > 0 {
|
||||||
output.WriteString(rest)
|
output.WriteString(rest)
|
||||||
if state != nil {
|
if state != nil {
|
||||||
@@ -77,8 +77,7 @@ func extractColor(str *string, state *ansiState) (*string, []ansiOffset, *ansiSt
|
|||||||
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputStr := output.String()
|
return output.String(), offsets, state
|
||||||
return &outputStr, offsets, state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
||||||
|
@@ -17,9 +17,9 @@ func TestExtractColor(t *testing.T) {
|
|||||||
var state *ansiState
|
var state *ansiState
|
||||||
clean := "\x1b[0m"
|
clean := "\x1b[0m"
|
||||||
check := func(assertion func(ansiOffsets []ansiOffset, state *ansiState)) {
|
check := func(assertion func(ansiOffsets []ansiOffset, state *ansiState)) {
|
||||||
output, ansiOffsets, newState := extractColor(&src, state)
|
output, ansiOffsets, newState := extractColor(src, state)
|
||||||
state = newState
|
state = newState
|
||||||
if *output != "hello world" {
|
if output != "hello world" {
|
||||||
t.Errorf("Invalid output: {}", output)
|
t.Errorf("Invalid output: {}", output)
|
||||||
}
|
}
|
||||||
fmt.Println(src, ansiOffsets, clean)
|
fmt.Println(src, ansiOffsets, clean)
|
||||||
|
@@ -7,7 +7,7 @@ type Chunk []*Item // >>> []Item
|
|||||||
|
|
||||||
// ItemBuilder is a closure type that builds Item object from a pointer to a
|
// ItemBuilder is a closure type that builds Item object from a pointer to a
|
||||||
// string and an integer
|
// string and an integer
|
||||||
type ItemBuilder func(*string, int) *Item
|
type ItemBuilder func([]byte, int) *Item
|
||||||
|
|
||||||
// ChunkList is a list of Chunks
|
// ChunkList is a list of Chunks
|
||||||
type ChunkList struct {
|
type ChunkList struct {
|
||||||
@@ -26,7 +26,7 @@ func NewChunkList(trans ItemBuilder) *ChunkList {
|
|||||||
trans: trans}
|
trans: trans}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Chunk) push(trans ItemBuilder, data *string, index int) bool {
|
func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {
|
||||||
item := trans(data, index)
|
item := trans(data, index)
|
||||||
if item != nil {
|
if item != nil {
|
||||||
*c = append(*c, item)
|
*c = append(*c, item)
|
||||||
@@ -53,7 +53,7 @@ func CountItems(cs []*Chunk) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Push adds the item to the list
|
// Push adds the item to the list
|
||||||
func (cl *ChunkList) Push(data string) bool {
|
func (cl *ChunkList) Push(data []byte) bool {
|
||||||
cl.mutex.Lock()
|
cl.mutex.Lock()
|
||||||
defer cl.mutex.Unlock()
|
defer cl.mutex.Unlock()
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ func (cl *ChunkList) Push(data string) bool {
|
|||||||
cl.chunks = append(cl.chunks, &newChunk)
|
cl.chunks = append(cl.chunks, &newChunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cl.lastChunk().push(cl.trans, &data, cl.count) {
|
if cl.lastChunk().push(cl.trans, data, cl.count) {
|
||||||
cl.count++
|
cl.count++
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@@ -6,8 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestChunkList(t *testing.T) {
|
func TestChunkList(t *testing.T) {
|
||||||
cl := NewChunkList(func(s *string, i int) *Item {
|
cl := NewChunkList(func(s []byte, i int) *Item {
|
||||||
return &Item{text: s, rank: Rank{0, 0, uint32(i * 2)}}
|
return &Item{text: []rune(string(s)), rank: Rank{0, 0, uint32(i * 2)}}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Snapshot
|
// Snapshot
|
||||||
@@ -17,8 +17,8 @@ func TestChunkList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add some data
|
// Add some data
|
||||||
cl.Push("hello")
|
cl.Push([]byte("hello"))
|
||||||
cl.Push("world")
|
cl.Push([]byte("world"))
|
||||||
|
|
||||||
// Previously created snapshot should remain the same
|
// Previously created snapshot should remain the same
|
||||||
if len(snapshot) > 0 {
|
if len(snapshot) > 0 {
|
||||||
@@ -36,8 +36,8 @@ func TestChunkList(t *testing.T) {
|
|||||||
if len(*chunk1) != 2 {
|
if len(*chunk1) != 2 {
|
||||||
t.Error("Snapshot should contain only two items")
|
t.Error("Snapshot should contain only two items")
|
||||||
}
|
}
|
||||||
if *(*chunk1)[0].text != "hello" || (*chunk1)[0].rank.index != 0 ||
|
if string((*chunk1)[0].text) != "hello" || (*chunk1)[0].rank.index != 0 ||
|
||||||
*(*chunk1)[1].text != "world" || (*chunk1)[1].rank.index != 2 {
|
string((*chunk1)[1].text) != "world" || (*chunk1)[1].rank.index != 2 {
|
||||||
t.Error("Invalid data")
|
t.Error("Invalid data")
|
||||||
}
|
}
|
||||||
if chunk1.IsFull() {
|
if chunk1.IsFull() {
|
||||||
@@ -46,7 +46,7 @@ func TestChunkList(t *testing.T) {
|
|||||||
|
|
||||||
// Add more data
|
// Add more data
|
||||||
for i := 0; i < chunkSize*2; i++ {
|
for i := 0; i < chunkSize*2; i++ {
|
||||||
cl.Push(fmt.Sprintf("item %d", i))
|
cl.Push([]byte(fmt.Sprintf("item %d", i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Previous snapshot should remain the same
|
// Previous snapshot should remain the same
|
||||||
@@ -64,8 +64,8 @@ func TestChunkList(t *testing.T) {
|
|||||||
t.Error("Unexpected number of items")
|
t.Error("Unexpected number of items")
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.Push("hello")
|
cl.Push([]byte("hello"))
|
||||||
cl.Push("world")
|
cl.Push([]byte("world"))
|
||||||
|
|
||||||
lastChunkCount := len(*snapshot[len(snapshot)-1])
|
lastChunkCount := len(*snapshot[len(snapshot)-1])
|
||||||
if lastChunkCount != 2 {
|
if lastChunkCount != 2 {
|
||||||
|
@@ -8,14 +8,14 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// Current version
|
// Current version
|
||||||
Version = "0.10.1"
|
version = "0.10.7"
|
||||||
|
|
||||||
// Core
|
// Core
|
||||||
coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
||||||
coordinatorDelayStep time.Duration = 10 * time.Millisecond
|
coordinatorDelayStep time.Duration = 10 * time.Millisecond
|
||||||
|
|
||||||
// Reader
|
// Reader
|
||||||
defaultCommand = `find * -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null`
|
defaultCommand = `find . -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | sed s/^..//`
|
||||||
|
|
||||||
// Terminal
|
// Terminal
|
||||||
initialDelay = 100 * time.Millisecond
|
initialDelay = 100 * time.Millisecond
|
||||||
@@ -47,3 +47,10 @@ const (
|
|||||||
EvtHeader
|
EvtHeader
|
||||||
EvtClose
|
EvtClose
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
exitOk = 0
|
||||||
|
exitNoMatch = 1
|
||||||
|
exitError = 2
|
||||||
|
exitInterrupt = 130
|
||||||
|
)
|
||||||
|
76
src/core.go
76
src/core.go
@@ -55,70 +55,76 @@ func Run(opts *Options) {
|
|||||||
rankTiebreak = opts.Tiebreak
|
rankTiebreak = opts.Tiebreak
|
||||||
|
|
||||||
if opts.Version {
|
if opts.Version {
|
||||||
fmt.Println(Version)
|
fmt.Println(version)
|
||||||
os.Exit(0)
|
os.Exit(exitOk)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event channel
|
// Event channel
|
||||||
eventBox := util.NewEventBox()
|
eventBox := util.NewEventBox()
|
||||||
|
|
||||||
// ANSI code processor
|
// ANSI code processor
|
||||||
ansiProcessor := func(data *string) (*string, []ansiOffset) {
|
ansiProcessor := func(data []byte) ([]rune, []ansiOffset) {
|
||||||
// By default, we do nothing
|
return util.BytesToRunes(data), nil
|
||||||
|
}
|
||||||
|
ansiProcessorRunes := func(data []rune) ([]rune, []ansiOffset) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
if opts.Ansi {
|
if opts.Ansi {
|
||||||
if opts.Theme != nil {
|
if opts.Theme != nil {
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
ansiProcessor = func(data *string) (*string, []ansiOffset) {
|
ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {
|
||||||
trimmed, offsets, newState := extractColor(data, state)
|
trimmed, offsets, newState := extractColor(string(data), state)
|
||||||
state = newState
|
state = newState
|
||||||
return trimmed, offsets
|
return []rune(trimmed), offsets
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// When color is disabled but ansi option is given,
|
// When color is disabled but ansi option is given,
|
||||||
// we simply strip out ANSI codes from the input
|
// we simply strip out ANSI codes from the input
|
||||||
ansiProcessor = func(data *string) (*string, []ansiOffset) {
|
ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {
|
||||||
trimmed, _, _ := extractColor(data, nil)
|
trimmed, _, _ := extractColor(string(data), nil)
|
||||||
return trimmed, nil
|
return []rune(trimmed), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ansiProcessorRunes = func(data []rune) ([]rune, []ansiOffset) {
|
||||||
|
return ansiProcessor([]byte(string(data)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chunk list
|
// Chunk list
|
||||||
var chunkList *ChunkList
|
var chunkList *ChunkList
|
||||||
header := make([]string, 0, opts.HeaderLines)
|
header := make([]string, 0, opts.HeaderLines)
|
||||||
if len(opts.WithNth) == 0 {
|
if len(opts.WithNth) == 0 {
|
||||||
chunkList = NewChunkList(func(data *string, index int) *Item {
|
chunkList = NewChunkList(func(data []byte, index int) *Item {
|
||||||
if len(header) < opts.HeaderLines {
|
if len(header) < opts.HeaderLines {
|
||||||
header = append(header, *data)
|
header = append(header, string(data))
|
||||||
eventBox.Set(EvtHeader, header)
|
eventBox.Set(EvtHeader, header)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
data, colors := ansiProcessor(data)
|
runes, colors := ansiProcessor(data)
|
||||||
return &Item{
|
return &Item{
|
||||||
text: data,
|
text: runes,
|
||||||
index: uint32(index),
|
index: uint32(index),
|
||||||
colors: colors,
|
colors: colors,
|
||||||
rank: Rank{0, 0, uint32(index)}}
|
rank: Rank{0, 0, uint32(index)}}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
chunkList = NewChunkList(func(data *string, index int) *Item {
|
chunkList = NewChunkList(func(data []byte, index int) *Item {
|
||||||
tokens := Tokenize(data, opts.Delimiter)
|
runes := util.BytesToRunes(data)
|
||||||
|
tokens := Tokenize(runes, opts.Delimiter)
|
||||||
trans := Transform(tokens, opts.WithNth)
|
trans := Transform(tokens, opts.WithNth)
|
||||||
if len(header) < opts.HeaderLines {
|
if len(header) < opts.HeaderLines {
|
||||||
header = append(header, *joinTokens(trans))
|
header = append(header, string(joinTokens(trans)))
|
||||||
eventBox.Set(EvtHeader, header)
|
eventBox.Set(EvtHeader, header)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
item := Item{
|
item := Item{
|
||||||
text: joinTokens(trans),
|
text: joinTokens(trans),
|
||||||
origText: data,
|
origText: &runes,
|
||||||
index: uint32(index),
|
index: uint32(index),
|
||||||
colors: nil,
|
colors: nil,
|
||||||
rank: Rank{0, 0, uint32(index)}}
|
rank: Rank{0, 0, uint32(index)}}
|
||||||
|
|
||||||
trimmed, colors := ansiProcessor(item.text)
|
trimmed, colors := ansiProcessorRunes(item.text)
|
||||||
item.text = trimmed
|
item.text = trimmed
|
||||||
item.colors = colors
|
item.colors = colors
|
||||||
return &item
|
return &item
|
||||||
@@ -128,8 +134,8 @@ func Run(opts *Options) {
|
|||||||
// Reader
|
// Reader
|
||||||
streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync
|
streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync
|
||||||
if !streamingFilter {
|
if !streamingFilter {
|
||||||
reader := Reader{func(str string) bool {
|
reader := Reader{func(data []byte) bool {
|
||||||
return chunkList.Push(str)
|
return chunkList.Push(data)
|
||||||
}, eventBox, opts.ReadZero}
|
}, eventBox, opts.ReadZero}
|
||||||
go reader.ReadSource()
|
go reader.ReadSource()
|
||||||
}
|
}
|
||||||
@@ -137,7 +143,8 @@ func Run(opts *Options) {
|
|||||||
// Matcher
|
// Matcher
|
||||||
patternBuilder := func(runes []rune) *Pattern {
|
patternBuilder := func(runes []rune) *Pattern {
|
||||||
return BuildPattern(
|
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)
|
matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)
|
||||||
|
|
||||||
@@ -149,12 +156,14 @@ func Run(opts *Options) {
|
|||||||
|
|
||||||
pattern := patternBuilder([]rune(*opts.Filter))
|
pattern := patternBuilder([]rune(*opts.Filter))
|
||||||
|
|
||||||
|
found := false
|
||||||
if streamingFilter {
|
if streamingFilter {
|
||||||
reader := Reader{
|
reader := Reader{
|
||||||
func(str string) bool {
|
func(runes []byte) bool {
|
||||||
item := chunkList.trans(&str, 0)
|
item := chunkList.trans(runes, 0)
|
||||||
if item != nil && pattern.MatchItem(item) {
|
if item != nil && pattern.MatchItem(item) {
|
||||||
fmt.Println(*item.text)
|
fmt.Println(string(item.text))
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}, eventBox, opts.ReadZero}
|
}, eventBox, opts.ReadZero}
|
||||||
@@ -168,10 +177,14 @@ func Run(opts *Options) {
|
|||||||
chunks: snapshot,
|
chunks: snapshot,
|
||||||
pattern: pattern})
|
pattern: pattern})
|
||||||
for i := 0; i < merger.Length(); i++ {
|
for i := 0; i < merger.Length(); i++ {
|
||||||
fmt.Println(merger.Get(i).AsString())
|
fmt.Println(merger.Get(i).AsString(opts.Ansi))
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
if found {
|
||||||
|
os.Exit(exitOk)
|
||||||
|
}
|
||||||
|
os.Exit(exitNoMatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Synchronous search
|
// Synchronous search
|
||||||
@@ -225,7 +238,7 @@ func Run(opts *Options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case EvtHeader:
|
case EvtHeader:
|
||||||
terminal.UpdateHeader(value.([]string), opts.HeaderLines)
|
terminal.UpdateHeader(value.([]string))
|
||||||
|
|
||||||
case EvtSearchFin:
|
case EvtSearchFin:
|
||||||
switch val := value.(type) {
|
switch val := value.(type) {
|
||||||
@@ -244,9 +257,12 @@ func Run(opts *Options) {
|
|||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
fmt.Println(val.Get(i).AsString())
|
fmt.Println(val.Get(i).AsString(opts.Ansi))
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
if count > 0 {
|
||||||
|
os.Exit(exitOk)
|
||||||
|
}
|
||||||
|
os.Exit(exitNoMatch)
|
||||||
}
|
}
|
||||||
deferred = false
|
deferred = false
|
||||||
terminal.startChan <- true
|
terminal.startChan <- true
|
||||||
|
@@ -3,13 +3,14 @@ package curses
|
|||||||
/*
|
/*
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <locale.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"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -258,6 +259,10 @@ func Init(theme *ColorTheme, black bool, mouse bool) {
|
|||||||
|
|
||||||
C.setlocale(C.LC_ALL, C.CString(""))
|
C.setlocale(C.LC_ALL, C.CString(""))
|
||||||
_screen = C.newterm(nil, C.stderr, C.stdin)
|
_screen = C.newterm(nil, C.stderr, C.stdin)
|
||||||
|
if _screen == nil {
|
||||||
|
fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
C.set_term(_screen)
|
C.set_term(_screen)
|
||||||
if mouse {
|
if mouse {
|
||||||
C.mousemask(C.ALL_MOUSE_EVENTS, nil)
|
C.mousemask(C.ALL_MOUSE_EVENTS, nil)
|
||||||
@@ -265,14 +270,6 @@ func Init(theme *ColorTheme, black bool, mouse bool) {
|
|||||||
C.noecho()
|
C.noecho()
|
||||||
C.raw() // stty dsusp undef
|
C.raw() // stty dsusp undef
|
||||||
|
|
||||||
intChan := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(intChan, os.Interrupt, os.Kill)
|
|
||||||
go func() {
|
|
||||||
<-intChan
|
|
||||||
Close()
|
|
||||||
os.Exit(1)
|
|
||||||
}()
|
|
||||||
|
|
||||||
if theme != nil {
|
if theme != nil {
|
||||||
C.start_color()
|
C.start_color()
|
||||||
initPairs(theme, black)
|
initPairs(theme, black)
|
||||||
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// History struct represents input history
|
||||||
type History struct {
|
type History struct {
|
||||||
path string
|
path string
|
||||||
lines []string
|
lines []string
|
||||||
@@ -15,6 +16,7 @@ type History struct {
|
|||||||
cursor int
|
cursor int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHistory returns the pointer to a new History struct
|
||||||
func NewHistory(path string, maxSize int) (*History, error) {
|
func NewHistory(path string, maxSize int) (*History, error) {
|
||||||
fmtError := func(e error) error {
|
fmtError := func(e error) error {
|
||||||
if os.IsPermission(e) {
|
if os.IsPermission(e) {
|
||||||
|
62
src/item.go
62
src/item.go
@@ -6,8 +6,8 @@ import (
|
|||||||
"github.com/junegunn/fzf/src/curses"
|
"github.com/junegunn/fzf/src/curses"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
// Offset holds three 32-bit integers denoting the offsets of a matched substring
|
||||||
type Offset [2]int32
|
type Offset [3]int32
|
||||||
|
|
||||||
type colorOffset struct {
|
type colorOffset struct {
|
||||||
offset [2]int32
|
offset [2]int32
|
||||||
@@ -17,9 +17,9 @@ type colorOffset struct {
|
|||||||
|
|
||||||
// Item represents each input line
|
// Item represents each input line
|
||||||
type Item struct {
|
type Item struct {
|
||||||
text *string
|
text []rune
|
||||||
origText *string
|
origText *[]rune
|
||||||
transformed *[]Token
|
transformed []Token
|
||||||
index uint32
|
index uint32
|
||||||
offsets []Offset
|
offsets []Offset
|
||||||
colors []ansiOffset
|
colors []ansiOffset
|
||||||
@@ -37,16 +37,19 @@ type Rank struct {
|
|||||||
var rankTiebreak tiebreak
|
var rankTiebreak tiebreak
|
||||||
|
|
||||||
// Rank calculates rank of the Item
|
// Rank calculates rank of the Item
|
||||||
func (i *Item) Rank(cache bool) Rank {
|
func (item *Item) Rank(cache bool) Rank {
|
||||||
if cache && (i.rank.matchlen > 0 || i.rank.tiebreak > 0) {
|
if cache && (item.rank.matchlen > 0 || item.rank.tiebreak > 0) {
|
||||||
return i.rank
|
return item.rank
|
||||||
}
|
}
|
||||||
matchlen := 0
|
matchlen := 0
|
||||||
prevEnd := 0
|
prevEnd := 0
|
||||||
|
lenSum := 0
|
||||||
minBegin := math.MaxUint16
|
minBegin := math.MaxUint16
|
||||||
for _, offset := range i.offsets {
|
for _, offset := range item.offsets {
|
||||||
begin := int(offset[0])
|
begin := int(offset[0])
|
||||||
end := int(offset[1])
|
end := int(offset[1])
|
||||||
|
trimLen := int(offset[2])
|
||||||
|
lenSum += trimLen
|
||||||
if prevEnd > begin {
|
if prevEnd > begin {
|
||||||
begin = prevEnd
|
begin = prevEnd
|
||||||
}
|
}
|
||||||
@@ -63,13 +66,19 @@ func (i *Item) Rank(cache bool) Rank {
|
|||||||
var tiebreak uint16
|
var tiebreak uint16
|
||||||
switch rankTiebreak {
|
switch rankTiebreak {
|
||||||
case byLength:
|
case byLength:
|
||||||
tiebreak = uint16(len(*i.text))
|
// It is guaranteed that .transformed in not null in normal execution
|
||||||
|
if item.transformed != nil {
|
||||||
|
// If offsets is empty, lenSum will be 0, but we don't care
|
||||||
|
tiebreak = uint16(lenSum)
|
||||||
|
} else {
|
||||||
|
tiebreak = uint16(len(item.text))
|
||||||
|
}
|
||||||
case byBegin:
|
case byBegin:
|
||||||
// We can't just look at i.offsets[0][0] because it can be an inverse term
|
// We can't just look at item.offsets[0][0] because it can be an inverse term
|
||||||
tiebreak = uint16(minBegin)
|
tiebreak = uint16(minBegin)
|
||||||
case byEnd:
|
case byEnd:
|
||||||
if prevEnd > 0 {
|
if prevEnd > 0 {
|
||||||
tiebreak = uint16(1 + len(*i.text) - prevEnd)
|
tiebreak = uint16(1 + len(item.text) - prevEnd)
|
||||||
} else {
|
} else {
|
||||||
// Empty offsets due to inverse terms.
|
// Empty offsets due to inverse terms.
|
||||||
tiebreak = 1
|
tiebreak = 1
|
||||||
@@ -77,31 +86,38 @@ func (i *Item) Rank(cache bool) Rank {
|
|||||||
case byIndex:
|
case byIndex:
|
||||||
tiebreak = 1
|
tiebreak = 1
|
||||||
}
|
}
|
||||||
rank := Rank{uint16(matchlen), tiebreak, i.index}
|
rank := Rank{uint16(matchlen), tiebreak, item.index}
|
||||||
if cache {
|
if cache {
|
||||||
i.rank = rank
|
item.rank = rank
|
||||||
}
|
}
|
||||||
return rank
|
return rank
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsString returns the original string
|
// AsString returns the original string
|
||||||
func (i *Item) AsString() string {
|
func (item *Item) AsString(stripAnsi bool) string {
|
||||||
return *i.StringPtr()
|
return *item.StringPtr(stripAnsi)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringPtr returns the pointer to the original string
|
// StringPtr returns the pointer to the original string
|
||||||
func (i *Item) StringPtr() *string {
|
func (item *Item) StringPtr(stripAnsi bool) *string {
|
||||||
if i.origText != nil {
|
if item.origText != nil {
|
||||||
return i.origText
|
if stripAnsi {
|
||||||
|
trimmed, _, _ := extractColor(string(*item.origText), nil)
|
||||||
|
return &trimmed
|
||||||
|
}
|
||||||
|
orig := string(*item.origText)
|
||||||
|
return &orig
|
||||||
}
|
}
|
||||||
return i.text
|
str := string(item.text)
|
||||||
|
return &str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {
|
func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {
|
||||||
if len(item.colors) == 0 {
|
if len(item.colors) == 0 {
|
||||||
var offsets []colorOffset
|
var offsets []colorOffset
|
||||||
for _, off := range item.offsets {
|
for _, off := range item.offsets {
|
||||||
offsets = append(offsets, colorOffset{offset: off, color: color, bold: bold})
|
|
||||||
|
offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, bold: bold})
|
||||||
}
|
}
|
||||||
return offsets
|
return offsets
|
||||||
}
|
}
|
||||||
@@ -145,7 +161,7 @@ func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset
|
|||||||
if curr != 0 && idx > start {
|
if curr != 0 && idx > start {
|
||||||
if curr == -1 {
|
if curr == -1 {
|
||||||
offsets = append(offsets, colorOffset{
|
offsets = append(offsets, colorOffset{
|
||||||
offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})
|
offset: [2]int32{int32(start), int32(idx)}, color: color, bold: bold})
|
||||||
} else {
|
} else {
|
||||||
ansi := item.colors[curr-1]
|
ansi := item.colors[curr-1]
|
||||||
fg := ansi.color.fg
|
fg := ansi.color.fg
|
||||||
@@ -165,7 +181,7 @@ func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
offsets = append(offsets, colorOffset{
|
offsets = append(offsets, colorOffset{
|
||||||
offset: Offset{int32(start), int32(idx)},
|
offset: [2]int32{int32(start), int32(idx)},
|
||||||
color: curses.PairFor(fg, bg),
|
color: curses.PairFor(fg, bg),
|
||||||
bold: ansi.color.bold || bold})
|
bold: ansi.color.bold || bold})
|
||||||
}
|
}
|
||||||
|
@@ -39,14 +39,14 @@ func TestRankComparison(t *testing.T) {
|
|||||||
|
|
||||||
// Match length, string length, index
|
// Match length, string length, index
|
||||||
func TestItemRank(t *testing.T) {
|
func TestItemRank(t *testing.T) {
|
||||||
strs := []string{"foo", "foobar", "bar", "baz"}
|
strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
|
||||||
item1 := Item{text: &strs[0], index: 1, offsets: []Offset{}}
|
item1 := Item{text: strs[0], index: 1, offsets: []Offset{}}
|
||||||
rank1 := item1.Rank(true)
|
rank1 := item1.Rank(true)
|
||||||
if rank1.matchlen != 0 || rank1.tiebreak != 3 || rank1.index != 1 {
|
if rank1.matchlen != 0 || rank1.tiebreak != 3 || rank1.index != 1 {
|
||||||
t.Error(item1.Rank(true))
|
t.Error(item1.Rank(true))
|
||||||
}
|
}
|
||||||
// Only differ in index
|
// Only differ in index
|
||||||
item2 := Item{text: &strs[0], index: 0, offsets: []Offset{}}
|
item2 := Item{text: strs[0], index: 0, offsets: []Offset{}}
|
||||||
|
|
||||||
items := []*Item{&item1, &item2}
|
items := []*Item{&item1, &item2}
|
||||||
sort.Sort(ByRelevance(items))
|
sort.Sort(ByRelevance(items))
|
||||||
@@ -62,10 +62,10 @@ func TestItemRank(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort by relevance
|
// Sort by relevance
|
||||||
item3 := Item{text: &strs[1], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}
|
item3 := Item{text: strs[1], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}
|
||||||
item4 := Item{text: &strs[1], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
item4 := Item{text: strs[1], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
||||||
item5 := Item{text: &strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}
|
item5 := Item{text: strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}
|
||||||
item6 := Item{text: &strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
item6 := Item{text: strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
||||||
items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}
|
items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}
|
||||||
sort.Sort(ByRelevance(items))
|
sort.Sort(ByRelevance(items))
|
||||||
if items[0] != &item2 || items[1] != &item1 ||
|
if items[0] != &item2 || items[1] != &item1 ||
|
||||||
|
@@ -96,7 +96,7 @@ func (m *Matcher) Loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !cancelled {
|
if !cancelled {
|
||||||
if merger.Cacheable() {
|
if merger.cacheable() {
|
||||||
m.mergerCache[patternString] = merger
|
m.mergerCache[patternString] = merger
|
||||||
}
|
}
|
||||||
merger.final = request.final
|
merger.final = request.final
|
||||||
|
@@ -82,7 +82,7 @@ func (mg *Merger) Get(idx int) *Item {
|
|||||||
panic(fmt.Sprintf("Index out of bounds (unsorted, %d/%d)", idx, mg.count))
|
panic(fmt.Sprintf("Index out of bounds (unsorted, %d/%d)", idx, mg.count))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Merger) Cacheable() bool {
|
func (mg *Merger) cacheable() bool {
|
||||||
return mg.count < mergerCacheMax
|
return mg.count < mergerCacheMax
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -22,7 +22,7 @@ func randItem() *Item {
|
|||||||
offsets[idx] = Offset{sidx, eidx}
|
offsets[idx] = Offset{sidx, eidx}
|
||||||
}
|
}
|
||||||
return &Item{
|
return &Item{
|
||||||
text: &str,
|
text: []rune(str),
|
||||||
index: rand.Uint32(),
|
index: rand.Uint32(),
|
||||||
offsets: offsets}
|
offsets: offsets}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -46,7 +44,7 @@ const usage = `usage: fzf [options]
|
|||||||
--bind=KEYBINDS Custom key bindings. Refer to the man page.
|
--bind=KEYBINDS Custom key bindings. Refer to the man page.
|
||||||
--history=FILE History file
|
--history=FILE History file
|
||||||
--history-size=N Maximum number of history entries (default: 1000)
|
--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
|
--header-lines=N The first N lines of the input are treated as header
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
@@ -104,7 +102,7 @@ type Options struct {
|
|||||||
Case Case
|
Case Case
|
||||||
Nth []Range
|
Nth []Range
|
||||||
WithNth []Range
|
WithNth []Range
|
||||||
Delimiter *regexp.Regexp
|
Delimiter Delimiter
|
||||||
Sort int
|
Sort int
|
||||||
Tac bool
|
Tac bool
|
||||||
Tiebreak tiebreak
|
Tiebreak tiebreak
|
||||||
@@ -149,7 +147,7 @@ func defaultOptions() *Options {
|
|||||||
Case: CaseSmart,
|
Case: CaseSmart,
|
||||||
Nth: make([]Range, 0),
|
Nth: make([]Range, 0),
|
||||||
WithNth: make([]Range, 0),
|
WithNth: make([]Range, 0),
|
||||||
Delimiter: nil,
|
Delimiter: Delimiter{},
|
||||||
Sort: 1000,
|
Sort: 1000,
|
||||||
Tac: false,
|
Tac: false,
|
||||||
Tiebreak: byLength,
|
Tiebreak: byLength,
|
||||||
@@ -181,14 +179,14 @@ func defaultOptions() *Options {
|
|||||||
Version: false}
|
Version: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func help(ok int) {
|
func help(code int) {
|
||||||
os.Stderr.WriteString(usage)
|
os.Stderr.WriteString(usage)
|
||||||
os.Exit(ok)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorExit(msg string) {
|
func errorExit(msg string) {
|
||||||
os.Stderr.WriteString(msg + "\n")
|
os.Stderr.WriteString(msg + "\n")
|
||||||
os.Exit(1)
|
os.Exit(exitError)
|
||||||
}
|
}
|
||||||
|
|
||||||
func optString(arg string, prefixes ...string) (bool, string) {
|
func optString(arg string, prefixes ...string) (bool, string) {
|
||||||
@@ -268,17 +266,23 @@ func splitNth(str string) []Range {
|
|||||||
return ranges
|
return ranges
|
||||||
}
|
}
|
||||||
|
|
||||||
func delimiterRegexp(str string) *regexp.Regexp {
|
func delimiterRegexp(str string) Delimiter {
|
||||||
rx, e := regexp.Compile(str)
|
// Special handling of \t
|
||||||
if e != nil {
|
str = strings.Replace(str, "\\t", "\t", -1)
|
||||||
str = regexp.QuoteMeta(str)
|
|
||||||
|
// 1. Pattern does not contain any special character
|
||||||
|
if regexp.QuoteMeta(str) == str {
|
||||||
|
return Delimiter{str: &str}
|
||||||
}
|
}
|
||||||
|
|
||||||
rx, e = regexp.Compile(fmt.Sprintf("(?:.*?%s)|(?:.+?$)", str))
|
rx, e := regexp.Compile(str)
|
||||||
|
// 2. Pattern is not a valid regular expression
|
||||||
if e != nil {
|
if e != nil {
|
||||||
errorExit("invalid regular expression: " + e.Error())
|
return Delimiter{str: &str}
|
||||||
}
|
}
|
||||||
return rx
|
|
||||||
|
// 3. Pattern as regular expression. Slow.
|
||||||
|
return Delimiter{regex: rx}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAlphabet(char uint8) bool {
|
func isAlphabet(char uint8) bool {
|
||||||
@@ -513,7 +517,7 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
case "delete-char":
|
case "delete-char":
|
||||||
keymap[key] = actDeleteChar
|
keymap[key] = actDeleteChar
|
||||||
case "delete-char/eof":
|
case "delete-char/eof":
|
||||||
keymap[key] = actDeleteCharEof
|
keymap[key] = actDeleteCharEOF
|
||||||
case "end-of-line":
|
case "end-of-line":
|
||||||
keymap[key] = actEndOfLine
|
keymap[key] = actEndOfLine
|
||||||
case "cancel":
|
case "cancel":
|
||||||
@@ -599,12 +603,8 @@ func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {
|
|||||||
return keymap
|
return keymap
|
||||||
}
|
}
|
||||||
|
|
||||||
func readHeaderFile(filename string) []string {
|
func strLines(str string) []string {
|
||||||
content, err := ioutil.ReadFile(filename)
|
return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
|
||||||
if err != nil {
|
|
||||||
errorExit("failed to read header file: " + filename)
|
|
||||||
}
|
|
||||||
return strings.Split(strings.TrimSuffix(string(content), "\n"), "\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMargin(margin string) [4]string {
|
func parseMargin(margin string) [4]string {
|
||||||
@@ -677,7 +677,7 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
arg := allArgs[i]
|
arg := allArgs[i]
|
||||||
switch arg {
|
switch arg {
|
||||||
case "-h", "--help":
|
case "-h", "--help":
|
||||||
help(0)
|
help(exitOk)
|
||||||
case "-x", "--extended":
|
case "-x", "--extended":
|
||||||
opts.Mode = ModeExtended
|
opts.Mode = ModeExtended
|
||||||
case "-e", "--extended-exact":
|
case "-e", "--extended-exact":
|
||||||
@@ -788,16 +788,13 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
setHistory(nextString(allArgs, &i, "history file path required"))
|
setHistory(nextString(allArgs, &i, "history file path required"))
|
||||||
case "--history-size":
|
case "--history-size":
|
||||||
setHistoryMax(nextInt(allArgs, &i, "history max size required"))
|
setHistoryMax(nextInt(allArgs, &i, "history max size required"))
|
||||||
case "--no-header-file":
|
case "--no-header":
|
||||||
opts.Header = []string{}
|
opts.Header = []string{}
|
||||||
case "--no-header-lines":
|
case "--no-header-lines":
|
||||||
opts.HeaderLines = 0
|
opts.HeaderLines = 0
|
||||||
case "--header-file":
|
case "--header":
|
||||||
opts.Header = readHeaderFile(
|
opts.Header = strLines(nextString(allArgs, &i, "header string required"))
|
||||||
nextString(allArgs, &i, "header file name required"))
|
|
||||||
opts.HeaderLines = 0
|
|
||||||
case "--header-lines":
|
case "--header-lines":
|
||||||
opts.Header = []string{}
|
|
||||||
opts.HeaderLines = atoi(
|
opts.HeaderLines = atoi(
|
||||||
nextString(allArgs, &i, "number of header lines required"))
|
nextString(allArgs, &i, "number of header lines required"))
|
||||||
case "--no-margin":
|
case "--no-margin":
|
||||||
@@ -838,11 +835,9 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
setHistory(value)
|
setHistory(value)
|
||||||
} else if match, value := optString(arg, "--history-size="); match {
|
} else if match, value := optString(arg, "--history-size="); match {
|
||||||
setHistoryMax(atoi(value))
|
setHistoryMax(atoi(value))
|
||||||
} else if match, value := optString(arg, "--header-file="); match {
|
} else if match, value := optString(arg, "--header="); match {
|
||||||
opts.Header = readHeaderFile(value)
|
opts.Header = strLines(value)
|
||||||
opts.HeaderLines = 0
|
|
||||||
} else if match, value := optString(arg, "--header-lines="); match {
|
} else if match, value := optString(arg, "--header-lines="); match {
|
||||||
opts.Header = []string{}
|
|
||||||
opts.HeaderLines = atoi(value)
|
opts.HeaderLines = atoi(value)
|
||||||
} else if match, value := optString(arg, "--margin="); match {
|
} else if match, value := optString(arg, "--margin="); match {
|
||||||
opts.Margin = parseMargin(value)
|
opts.Margin = parseMargin(value)
|
||||||
|
@@ -8,11 +8,59 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDelimiterRegex(t *testing.T) {
|
func TestDelimiterRegex(t *testing.T) {
|
||||||
rx := delimiterRegexp("*")
|
// Valid regex
|
||||||
tokens := rx.FindAllString("-*--*---**---", -1)
|
delim := delimiterRegexp(".")
|
||||||
if tokens[0] != "-*" || tokens[1] != "--*" || tokens[2] != "---*" ||
|
if delim.regex == nil || delim.str != nil {
|
||||||
tokens[3] != "*" || tokens[4] != "---" {
|
t.Error(delim)
|
||||||
t.Errorf("%s %s %d", rx, tokens, len(tokens))
|
}
|
||||||
|
// Broken regex -> string
|
||||||
|
delim = delimiterRegexp("[0-9")
|
||||||
|
if delim.regex != nil || *delim.str != "[0-9" {
|
||||||
|
t.Error(delim)
|
||||||
|
}
|
||||||
|
// Valid regex
|
||||||
|
delim = delimiterRegexp("[0-9]")
|
||||||
|
if delim.regex.String() != "[0-9]" || delim.str != nil {
|
||||||
|
t.Error(delim)
|
||||||
|
}
|
||||||
|
// Tab character
|
||||||
|
delim = delimiterRegexp("\t")
|
||||||
|
if delim.regex != nil || *delim.str != "\t" {
|
||||||
|
t.Error(delim)
|
||||||
|
}
|
||||||
|
// Tab expression
|
||||||
|
delim = delimiterRegexp("\\t")
|
||||||
|
if delim.regex != nil || *delim.str != "\t" {
|
||||||
|
t.Error(delim)
|
||||||
|
}
|
||||||
|
// Tabs -> regex
|
||||||
|
delim = delimiterRegexp("\t+")
|
||||||
|
if delim.regex == nil || delim.str != nil {
|
||||||
|
t.Error(delim)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelimiterRegexString(t *testing.T) {
|
||||||
|
delim := delimiterRegexp("*")
|
||||||
|
tokens := Tokenize([]rune("-*--*---**---"), delim)
|
||||||
|
if delim.regex != nil ||
|
||||||
|
string(tokens[0].text) != "-*" ||
|
||||||
|
string(tokens[1].text) != "--*" ||
|
||||||
|
string(tokens[2].text) != "---*" ||
|
||||||
|
string(tokens[3].text) != "*" ||
|
||||||
|
string(tokens[4].text) != "---" {
|
||||||
|
t.Errorf("%s %s %d", delim, tokens, len(tokens))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelimiterRegexRegex(t *testing.T) {
|
||||||
|
delim := delimiterRegexp("--\\*")
|
||||||
|
tokens := Tokenize([]rune("-*--*---**---"), delim)
|
||||||
|
if delim.str != nil ||
|
||||||
|
string(tokens[0].text) != "-*--*" ||
|
||||||
|
string(tokens[1].text) != "---*" ||
|
||||||
|
string(tokens[2].text) != "*---" {
|
||||||
|
t.Errorf("%s %d", tokens, len(tokens))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/algo"
|
"github.com/junegunn/fzf/src/algo"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fuzzy
|
// fuzzy
|
||||||
@@ -39,12 +40,13 @@ type term struct {
|
|||||||
type Pattern struct {
|
type Pattern struct {
|
||||||
mode Mode
|
mode Mode
|
||||||
caseSensitive bool
|
caseSensitive bool
|
||||||
|
forward bool
|
||||||
text []rune
|
text []rune
|
||||||
terms []term
|
terms []term
|
||||||
hasInvTerm bool
|
hasInvTerm bool
|
||||||
delimiter *regexp.Regexp
|
delimiter Delimiter
|
||||||
nth []Range
|
nth []Range
|
||||||
procFun map[termType]func(bool, *[]rune, []rune) (int, int)
|
procFun map[termType]func(bool, bool, []rune, []rune) (int, int)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -70,8 +72,8 @@ func clearChunkCache() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildPattern builds Pattern object from the given arguments
|
// 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 *regexp.Regexp, runes []rune) *Pattern {
|
nth []Range, delimiter Delimiter, runes []rune) *Pattern {
|
||||||
|
|
||||||
var asString string
|
var asString string
|
||||||
switch mode {
|
switch mode {
|
||||||
@@ -109,12 +111,13 @@ func BuildPattern(mode Mode, caseMode Case,
|
|||||||
ptr := &Pattern{
|
ptr := &Pattern{
|
||||||
mode: mode,
|
mode: mode,
|
||||||
caseSensitive: caseSensitive,
|
caseSensitive: caseSensitive,
|
||||||
|
forward: forward,
|
||||||
text: []rune(asString),
|
text: []rune(asString),
|
||||||
terms: terms,
|
terms: terms,
|
||||||
hasInvTerm: hasInvTerm,
|
hasInvTerm: hasInvTerm,
|
||||||
nth: nth,
|
nth: nth,
|
||||||
delimiter: delimiter,
|
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[termFuzzy] = algo.FuzzyMatch
|
||||||
ptr.procFun[termEqual] = algo.EqualMatch
|
ptr.procFun[termEqual] = algo.EqualMatch
|
||||||
@@ -151,6 +154,9 @@ func parseTerms(mode Mode, caseMode Case, str string) []term {
|
|||||||
if mode == ModeExtended {
|
if mode == ModeExtended {
|
||||||
typ = termExact
|
typ = termExact
|
||||||
text = text[1:]
|
text = text[1:]
|
||||||
|
} else if mode == ModeExtendedExact {
|
||||||
|
typ = termFuzzy
|
||||||
|
text = text[1:]
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(text, "^") {
|
} else if strings.HasPrefix(text, "^") {
|
||||||
if strings.HasSuffix(text, "$") {
|
if strings.HasSuffix(text, "$") {
|
||||||
@@ -246,9 +252,9 @@ func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
|||||||
matches := []*Item{}
|
matches := []*Item{}
|
||||||
if p.mode == ModeFuzzy {
|
if p.mode == ModeFuzzy {
|
||||||
for _, item := range *chunk {
|
for _, item := range *chunk {
|
||||||
if sidx, eidx := p.fuzzyMatch(item); sidx >= 0 {
|
if sidx, eidx, tlen := p.fuzzyMatch(item); sidx >= 0 {
|
||||||
matches = append(matches,
|
matches = append(matches,
|
||||||
dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))
|
dupItem(item, []Offset{Offset{int32(sidx), int32(eidx), int32(tlen)}}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -264,7 +270,7 @@ func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
|||||||
// MatchItem returns true if the Item is a match
|
// MatchItem returns true if the Item is a match
|
||||||
func (p *Pattern) MatchItem(item *Item) bool {
|
func (p *Pattern) MatchItem(item *Item) bool {
|
||||||
if p.mode == ModeFuzzy {
|
if p.mode == ModeFuzzy {
|
||||||
sidx, _ := p.fuzzyMatch(item)
|
sidx, _, _ := p.fuzzyMatch(item)
|
||||||
return sidx >= 0
|
return sidx >= 0
|
||||||
}
|
}
|
||||||
offsets := p.extendedMatch(item)
|
offsets := p.extendedMatch(item)
|
||||||
@@ -283,9 +289,9 @@ func dupItem(item *Item, offsets []Offset) *Item {
|
|||||||
rank: Rank{0, 0, item.index}}
|
rank: Rank{0, 0, item.index}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) fuzzyMatch(item *Item) (int, int) {
|
func (p *Pattern) fuzzyMatch(item *Item) (int, int, int) {
|
||||||
input := p.prepareInput(item)
|
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 {
|
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
||||||
@@ -293,43 +299,41 @@ func (p *Pattern) extendedMatch(item *Item) []Offset {
|
|||||||
offsets := []Offset{}
|
offsets := []Offset{}
|
||||||
for _, term := range p.terms {
|
for _, term := range p.terms {
|
||||||
pfun := p.procFun[term.typ]
|
pfun := p.procFun[term.typ]
|
||||||
if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {
|
if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {
|
||||||
if term.inv {
|
if term.inv {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
offsets = append(offsets, Offset{int32(sidx), int32(eidx)})
|
offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})
|
||||||
} else if term.inv {
|
} else if term.inv {
|
||||||
offsets = append(offsets, Offset{0, 0})
|
offsets = append(offsets, Offset{0, 0, 0})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return offsets
|
return offsets
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) prepareInput(item *Item) *[]Token {
|
func (p *Pattern) prepareInput(item *Item) []Token {
|
||||||
if item.transformed != nil {
|
if item.transformed != nil {
|
||||||
return item.transformed
|
return item.transformed
|
||||||
}
|
}
|
||||||
|
|
||||||
var ret *[]Token
|
var ret []Token
|
||||||
if len(p.nth) > 0 {
|
if len(p.nth) > 0 {
|
||||||
tokens := Tokenize(item.text, p.delimiter)
|
tokens := Tokenize(item.text, p.delimiter)
|
||||||
ret = Transform(tokens, p.nth)
|
ret = Transform(tokens, p.nth)
|
||||||
} else {
|
} else {
|
||||||
runes := []rune(*item.text)
|
ret = []Token{Token{text: item.text, prefixLength: 0, trimLength: util.TrimLen(item.text)}}
|
||||||
trans := []Token{Token{text: &runes, prefixLength: 0}}
|
|
||||||
ret = &trans
|
|
||||||
}
|
}
|
||||||
item.transformed = ret
|
item.transformed = ret
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) iter(pfun func(bool, *[]rune, []rune) (int, int),
|
func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),
|
||||||
tokens *[]Token, caseSensitive bool, pattern []rune) (int, int) {
|
tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int, int) {
|
||||||
for _, part := range *tokens {
|
for _, part := range tokens {
|
||||||
prefixLength := part.prefixLength
|
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
|
return sidx + prefixLength, eidx + prefixLength, part.trimLength
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1, -1
|
return -1, -1, -1 // math.MaxUint16
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/algo"
|
"github.com/junegunn/fzf/src/algo"
|
||||||
@@ -36,11 +37,11 @@ func TestParseTermsExtendedExact(t *testing.T) {
|
|||||||
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
||||||
if len(terms) != 8 ||
|
if len(terms) != 8 ||
|
||||||
terms[0].typ != termExact || terms[0].inv || len(terms[0].text) != 3 ||
|
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[2].typ != termPrefix || terms[2].inv || len(terms[2].text) != 3 ||
|
||||||
terms[3].typ != termSuffix || terms[3].inv || len(terms[3].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[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[6].typ != termPrefix || !terms[6].inv || len(terms[6].text) != 3 ||
|
||||||
terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {
|
terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {
|
||||||
t.Errorf("%s", terms)
|
t.Errorf("%s", terms)
|
||||||
@@ -57,10 +58,10 @@ func TestParseTermsEmpty(t *testing.T) {
|
|||||||
func TestExact(t *testing.T) {
|
func TestExact(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pattern := BuildPattern(ModeExtended, CaseSmart,
|
pattern := BuildPattern(ModeExtended, CaseSmart, true,
|
||||||
[]Range{}, nil, []rune("'abc"))
|
[]Range{}, Delimiter{}, []rune("'abc"))
|
||||||
runes := []rune("aabbcc abc")
|
sidx, eidx := algo.ExactMatchNaive(
|
||||||
sidx, eidx := algo.ExactMatchNaive(pattern.caseSensitive, &runes, pattern.terms[0].text)
|
pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)
|
||||||
if sidx != 7 || eidx != 10 {
|
if sidx != 7 || eidx != 10 {
|
||||||
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
||||||
}
|
}
|
||||||
@@ -69,11 +70,11 @@ func TestExact(t *testing.T) {
|
|||||||
func TestEqual(t *testing.T) {
|
func TestEqual(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, nil, []rune("^AbC$"))
|
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
||||||
|
|
||||||
match := func(str string, sidxExpected int, eidxExpected int) {
|
match := func(str string, sidxExpected int, eidxExpected int) {
|
||||||
runes := []rune(str)
|
sidx, eidx := algo.EqualMatch(
|
||||||
sidx, eidx := algo.EqualMatch(pattern.caseSensitive, &runes, pattern.terms[0].text)
|
pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)
|
||||||
if sidx != sidxExpected || eidx != eidxExpected {
|
if sidx != sidxExpected || eidx != eidxExpected {
|
||||||
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
||||||
}
|
}
|
||||||
@@ -85,17 +86,17 @@ func TestEqual(t *testing.T) {
|
|||||||
func TestCaseSensitivity(t *testing.T) {
|
func TestCaseSensitivity(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat1 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, nil, []rune("abc"))
|
pat1 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat2 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, nil, []rune("Abc"))
|
pat2 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, nil, []rune("abc"))
|
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, nil, []rune("Abc"))
|
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat5 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, nil, []rune("abc"))
|
pat5 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat6 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, nil, []rune("Abc"))
|
pat6 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
|
|
||||||
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
||||||
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
||||||
@@ -108,25 +109,23 @@ func TestCaseSensitivity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOrigTextAndTransformed(t *testing.T) {
|
func TestOrigTextAndTransformed(t *testing.T) {
|
||||||
strptr := func(str string) *string {
|
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))
|
||||||
return &str
|
tokens := Tokenize([]rune("junegunn"), Delimiter{})
|
||||||
}
|
|
||||||
pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, nil, []rune("jg"))
|
|
||||||
tokens := Tokenize(strptr("junegunn"), nil)
|
|
||||||
trans := Transform(tokens, []Range{Range{1, 1}})
|
trans := Transform(tokens, []Range{Range{1, 1}})
|
||||||
|
|
||||||
|
origRunes := []rune("junegunn.choi")
|
||||||
for _, mode := range []Mode{ModeFuzzy, ModeExtended} {
|
for _, mode := range []Mode{ModeFuzzy, ModeExtended} {
|
||||||
chunk := Chunk{
|
chunk := Chunk{
|
||||||
&Item{
|
&Item{
|
||||||
text: strptr("junegunn"),
|
text: []rune("junegunn"),
|
||||||
origText: strptr("junegunn.choi"),
|
origText: &origRunes,
|
||||||
transformed: trans},
|
transformed: trans},
|
||||||
}
|
}
|
||||||
pattern.mode = mode
|
pattern.mode = mode
|
||||||
matches := pattern.matchChunk(&chunk)
|
matches := pattern.matchChunk(&chunk)
|
||||||
if *matches[0].text != "junegunn" || *matches[0].origText != "junegunn.choi" ||
|
if string(matches[0].text) != "junegunn" || string(*matches[0].origText) != "junegunn.choi" ||
|
||||||
matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||
|
matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||
|
||||||
matches[0].transformed != trans {
|
!reflect.DeepEqual(matches[0].transformed, trans) {
|
||||||
t.Error("Invalid match result", matches)
|
t.Error("Invalid match result", matches)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
// Reader reads from command or standard input
|
// Reader reads from command or standard input
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
pusher func(string) bool
|
pusher func([]byte) bool
|
||||||
eventBox *util.EventBox
|
eventBox *util.EventBox
|
||||||
delimNil bool
|
delimNil bool
|
||||||
}
|
}
|
||||||
@@ -37,13 +37,14 @@ func (r *Reader) feed(src io.Reader) {
|
|||||||
}
|
}
|
||||||
reader := bufio.NewReader(src)
|
reader := bufio.NewReader(src)
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString(delim)
|
// ReadBytes returns err != nil if and only if the returned data does not
|
||||||
if line != "" {
|
// end in delim.
|
||||||
// "ReadString returns err != nil if and only if the returned data does not end in delim."
|
bytea, err := reader.ReadBytes(delim)
|
||||||
|
if len(bytea) > 0 {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
line = line[:len(line)-1]
|
bytea = bytea[:len(bytea)-1]
|
||||||
}
|
}
|
||||||
if r.pusher(line) {
|
if r.pusher(bytea) {
|
||||||
r.eventBox.Set(EvtReadNew, nil)
|
r.eventBox.Set(EvtReadNew, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,7 @@ func TestReadFromCommand(t *testing.T) {
|
|||||||
strs := []string{}
|
strs := []string{}
|
||||||
eb := util.NewEventBox()
|
eb := util.NewEventBox()
|
||||||
reader := Reader{
|
reader := Reader{
|
||||||
pusher: func(s string) bool { strs = append(strs, s); return true },
|
pusher: func(s []byte) bool { strs = append(strs, string(s)); return true },
|
||||||
eventBox: eb}
|
eventBox: eb}
|
||||||
|
|
||||||
// Check EventBox
|
// Check EventBox
|
||||||
|
@@ -42,6 +42,8 @@ type Terminal struct {
|
|||||||
history *History
|
history *History
|
||||||
cycle bool
|
cycle bool
|
||||||
header []string
|
header []string
|
||||||
|
header0 []string
|
||||||
|
ansi bool
|
||||||
margin [4]string
|
margin [4]string
|
||||||
marginInt [4]int
|
marginInt [4]int
|
||||||
count int
|
count int
|
||||||
@@ -106,7 +108,7 @@ const (
|
|||||||
actCancel
|
actCancel
|
||||||
actClearScreen
|
actClearScreen
|
||||||
actDeleteChar
|
actDeleteChar
|
||||||
actDeleteCharEof
|
actDeleteCharEOF
|
||||||
actEndOfLine
|
actEndOfLine
|
||||||
actForwardChar
|
actForwardChar
|
||||||
actForwardWord
|
actForwardWord
|
||||||
@@ -141,7 +143,7 @@ func defaultKeymap() map[int]actionType {
|
|||||||
keymap[C.CtrlG] = actAbort
|
keymap[C.CtrlG] = actAbort
|
||||||
keymap[C.CtrlQ] = actAbort
|
keymap[C.CtrlQ] = actAbort
|
||||||
keymap[C.ESC] = actAbort
|
keymap[C.ESC] = actAbort
|
||||||
keymap[C.CtrlD] = actDeleteCharEof
|
keymap[C.CtrlD] = actDeleteCharEOF
|
||||||
keymap[C.CtrlE] = actEndOfLine
|
keymap[C.CtrlE] = actEndOfLine
|
||||||
keymap[C.CtrlF] = actForwardChar
|
keymap[C.CtrlF] = actForwardChar
|
||||||
keymap[C.CtrlH] = actBackwardDeleteChar
|
keymap[C.CtrlH] = actBackwardDeleteChar
|
||||||
@@ -184,6 +186,12 @@ func defaultKeymap() map[int]actionType {
|
|||||||
// NewTerminal returns new Terminal object
|
// NewTerminal returns new Terminal object
|
||||||
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
||||||
input := []rune(opts.Query)
|
input := []rune(opts.Query)
|
||||||
|
var header []string
|
||||||
|
if opts.Reverse {
|
||||||
|
header = opts.Header
|
||||||
|
} else {
|
||||||
|
header = reverseStringArray(opts.Header)
|
||||||
|
}
|
||||||
return &Terminal{
|
return &Terminal{
|
||||||
inlineInfo: opts.InlineInfo,
|
inlineInfo: opts.InlineInfo,
|
||||||
prompt: opts.Prompt,
|
prompt: opts.Prompt,
|
||||||
@@ -206,7 +214,9 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
|||||||
margin: opts.Margin,
|
margin: opts.Margin,
|
||||||
marginInt: [4]int{0, 0, 0, 0},
|
marginInt: [4]int{0, 0, 0, 0},
|
||||||
cycle: opts.Cycle,
|
cycle: opts.Cycle,
|
||||||
header: opts.Header,
|
header: header,
|
||||||
|
header0: header,
|
||||||
|
ansi: opts.Ansi,
|
||||||
reading: true,
|
reading: true,
|
||||||
merger: EmptyMerger,
|
merger: EmptyMerger,
|
||||||
selected: make(map[uint32]selectedItem),
|
selected: make(map[uint32]selectedItem),
|
||||||
@@ -239,18 +249,19 @@ func (t *Terminal) UpdateCount(cnt int, final bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateHeader updates the header
|
func reverseStringArray(input []string) []string {
|
||||||
func (t *Terminal) UpdateHeader(header []string, lines int) {
|
size := len(input)
|
||||||
t.mutex.Lock()
|
reversed := make([]string, size)
|
||||||
t.header = make([]string, lines)
|
for idx, str := range input {
|
||||||
copy(t.header, header)
|
reversed[size-idx-1] = str
|
||||||
if !t.reverse {
|
|
||||||
reversed := make([]string, lines)
|
|
||||||
for idx, str := range t.header {
|
|
||||||
reversed[lines-idx-1] = str
|
|
||||||
}
|
|
||||||
t.header = reversed
|
|
||||||
}
|
}
|
||||||
|
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.mutex.Unlock()
|
||||||
t.reqBox.Set(reqHeader, nil)
|
t.reqBox.Set(reqHeader, nil)
|
||||||
}
|
}
|
||||||
@@ -278,17 +289,19 @@ func (t *Terminal) UpdateList(merger *Merger) {
|
|||||||
t.reqBox.Set(reqList, nil)
|
t.reqBox.Set(reqList, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Terminal) output() {
|
func (t *Terminal) output() bool {
|
||||||
if t.printQuery {
|
if t.printQuery {
|
||||||
fmt.Println(string(t.input))
|
fmt.Println(string(t.input))
|
||||||
}
|
}
|
||||||
if len(t.expect) > 0 {
|
if len(t.expect) > 0 {
|
||||||
fmt.Println(t.pressed)
|
fmt.Println(t.pressed)
|
||||||
}
|
}
|
||||||
if len(t.selected) == 0 {
|
found := len(t.selected) > 0
|
||||||
|
if !found {
|
||||||
cnt := t.merger.Length()
|
cnt := t.merger.Length()
|
||||||
if cnt > 0 && cnt > t.cy {
|
if cnt > 0 && cnt > t.cy {
|
||||||
fmt.Println(t.merger.Get(t.cy).AsString())
|
fmt.Println(t.merger.Get(t.cy).AsString(t.ansi))
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sels := make([]selectedItem, 0, len(t.selected))
|
sels := make([]selectedItem, 0, len(t.selected))
|
||||||
@@ -300,6 +313,7 @@ func (t *Terminal) output() {
|
|||||||
fmt.Println(*sel.text)
|
fmt.Println(*sel.text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
func runeWidth(r rune, prefixWidth int) int {
|
func runeWidth(r rune, prefixWidth int) int {
|
||||||
@@ -431,20 +445,17 @@ func (t *Terminal) printHeader() {
|
|||||||
max := t.maxHeight()
|
max := t.maxHeight()
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
for idx, lineStr := range t.header {
|
for idx, lineStr := range t.header {
|
||||||
if !t.reverse {
|
|
||||||
idx = len(t.header) - idx - 1
|
|
||||||
}
|
|
||||||
line := idx + 2
|
line := idx + 2
|
||||||
if t.inlineInfo {
|
if t.inlineInfo {
|
||||||
line -= 1
|
line--
|
||||||
}
|
}
|
||||||
if line >= max {
|
if line >= max {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
trimmed, colors, newState := extractColor(&lineStr, state)
|
trimmed, colors, newState := extractColor(lineStr, state)
|
||||||
state = newState
|
state = newState
|
||||||
item := &Item{
|
item := &Item{
|
||||||
text: trimmed,
|
text: []rune(trimmed),
|
||||||
index: 0,
|
index: 0,
|
||||||
colors: colors,
|
colors: colors,
|
||||||
rank: Rank{0, 0, 0}}
|
rank: Rank{0, 0, 0}}
|
||||||
@@ -462,7 +473,7 @@ func (t *Terminal) printList() {
|
|||||||
for i := 0; i < maxy; i++ {
|
for i := 0; i < maxy; i++ {
|
||||||
line := i + 2 + len(t.header)
|
line := i + 2 + len(t.header)
|
||||||
if t.inlineInfo {
|
if t.inlineInfo {
|
||||||
line -= 1
|
line--
|
||||||
}
|
}
|
||||||
t.move(line, 0, true)
|
t.move(line, 0, true)
|
||||||
if i < count {
|
if i < count {
|
||||||
@@ -537,7 +548,8 @@ func (t *Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Overflow
|
// Overflow
|
||||||
text := []rune(*item.text)
|
text := make([]rune, len(item.text))
|
||||||
|
copy(text, item.text)
|
||||||
offsets := item.colorOffsets(col2, bold, current)
|
offsets := item.colorOffsets(col2, bold, current)
|
||||||
maxWidth := C.MaxX() - 3 - t.marginInt[1] - t.marginInt[3]
|
maxWidth := C.MaxX() - 3 - t.marginInt[1] - t.marginInt[3]
|
||||||
fullWidth := displayWidth(text)
|
fullWidth := displayWidth(text)
|
||||||
@@ -715,6 +727,13 @@ func (t *Terminal) Loop() {
|
|||||||
t.reqBox.Set(reqRefresh, nil)
|
t.reqBox.Set(reqRefresh, nil)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
intChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(intChan, os.Interrupt, os.Kill)
|
||||||
|
go func() {
|
||||||
|
<-intChan
|
||||||
|
t.reqBox.Set(reqQuit, nil)
|
||||||
|
}()
|
||||||
|
|
||||||
resizeChan := make(chan os.Signal, 1)
|
resizeChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(resizeChan, syscall.SIGWINCH)
|
signal.Notify(resizeChan, syscall.SIGWINCH)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -740,7 +759,7 @@ func (t *Terminal) Loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exit := func(code int) {
|
exit := func(code int) {
|
||||||
if code == 0 && t.history != nil {
|
if code <= exitNoMatch && t.history != nil {
|
||||||
t.history.append(string(t.input))
|
t.history.append(string(t.input))
|
||||||
}
|
}
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
@@ -773,11 +792,13 @@ func (t *Terminal) Loop() {
|
|||||||
t.printAll()
|
t.printAll()
|
||||||
case reqClose:
|
case reqClose:
|
||||||
C.Close()
|
C.Close()
|
||||||
t.output()
|
if t.output() {
|
||||||
exit(0)
|
exit(exitOk)
|
||||||
|
}
|
||||||
|
exit(exitNoMatch)
|
||||||
case reqQuit:
|
case reqQuit:
|
||||||
C.Close()
|
C.Close()
|
||||||
exit(1)
|
exit(exitInterrupt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.placeCursor()
|
t.placeCursor()
|
||||||
@@ -804,7 +825,7 @@ func (t *Terminal) Loop() {
|
|||||||
}
|
}
|
||||||
selectItem := func(item *Item) bool {
|
selectItem := func(item *Item) bool {
|
||||||
if _, found := t.selected[item.index]; !found {
|
if _, found := t.selected[item.index]; !found {
|
||||||
t.selected[item.index] = selectedItem{time.Now(), item.StringPtr()}
|
t.selected[item.index] = selectedItem{time.Now(), item.StringPtr(t.ansi)}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -842,7 +863,7 @@ func (t *Terminal) Loop() {
|
|||||||
case actExecute:
|
case actExecute:
|
||||||
if t.cy >= 0 && t.cy < t.merger.Length() {
|
if t.cy >= 0 && t.cy < t.merger.Length() {
|
||||||
item := t.merger.Get(t.cy)
|
item := t.merger.Get(t.cy)
|
||||||
executeCommand(t.execmap[mapkey], item.AsString())
|
executeCommand(t.execmap[mapkey], item.AsString(t.ansi))
|
||||||
}
|
}
|
||||||
case actInvalid:
|
case actInvalid:
|
||||||
t.mutex.Unlock()
|
t.mutex.Unlock()
|
||||||
@@ -862,7 +883,7 @@ func (t *Terminal) Loop() {
|
|||||||
req(reqQuit)
|
req(reqQuit)
|
||||||
case actDeleteChar:
|
case actDeleteChar:
|
||||||
t.delChar()
|
t.delChar()
|
||||||
case actDeleteCharEof:
|
case actDeleteCharEOF:
|
||||||
if !t.delChar() && t.cx == 0 {
|
if !t.delChar() && t.cx == 0 {
|
||||||
req(reqQuit)
|
req(reqQuit)
|
||||||
}
|
}
|
||||||
@@ -1013,7 +1034,7 @@ func (t *Terminal) Loop() {
|
|||||||
}
|
}
|
||||||
min := 2 + len(t.header)
|
min := 2 + len(t.header)
|
||||||
if t.inlineInfo {
|
if t.inlineInfo {
|
||||||
min -= 1
|
min--
|
||||||
}
|
}
|
||||||
if me.Double {
|
if me.Double {
|
||||||
// Double-click
|
// Double-click
|
||||||
@@ -1099,7 +1120,7 @@ func (t *Terminal) vset(o int) bool {
|
|||||||
func (t *Terminal) maxItems() int {
|
func (t *Terminal) maxItems() int {
|
||||||
max := t.maxHeight() - 2 - len(t.header)
|
max := t.maxHeight() - 2 - len(t.header)
|
||||||
if t.inlineInfo {
|
if t.inlineInfo {
|
||||||
max += 1
|
max++
|
||||||
}
|
}
|
||||||
return util.Max(max, 0)
|
return util.Max(max, 0)
|
||||||
}
|
}
|
||||||
|
@@ -18,8 +18,15 @@ type Range struct {
|
|||||||
|
|
||||||
// Token contains the tokenized part of the strings and its prefix length
|
// Token contains the tokenized part of the strings and its prefix length
|
||||||
type Token struct {
|
type Token struct {
|
||||||
text *[]rune
|
text []rune
|
||||||
prefixLength int
|
prefixLength int
|
||||||
|
trimLength int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delimiter for tokenizing the input
|
||||||
|
type Delimiter struct {
|
||||||
|
regex *regexp.Regexp
|
||||||
|
str *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRange(begin int, end int) Range {
|
func newRange(begin int, end int) Range {
|
||||||
@@ -68,16 +75,15 @@ func ParseRange(str *string) (Range, bool) {
|
|||||||
return newRange(n, n), true
|
return newRange(n, n), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func withPrefixLengths(tokens []string, begin int) []Token {
|
func withPrefixLengths(tokens [][]rune, begin int) []Token {
|
||||||
ret := make([]Token, len(tokens))
|
ret := make([]Token, len(tokens))
|
||||||
|
|
||||||
prefixLength := begin
|
prefixLength := begin
|
||||||
for idx, token := range tokens {
|
for idx, token := range tokens {
|
||||||
// Need to define a new local variable instead of the reused token to take
|
// Need to define a new local variable instead of the reused token to take
|
||||||
// the pointer to it
|
// the pointer to it
|
||||||
runes := []rune(token)
|
ret[idx] = Token{token, prefixLength, util.TrimLen(token)}
|
||||||
ret[idx] = Token{text: &runes, prefixLength: prefixLength}
|
prefixLength += len(token)
|
||||||
prefixLength += len([]rune(token))
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@@ -88,13 +94,13 @@ const (
|
|||||||
awkWhite
|
awkWhite
|
||||||
)
|
)
|
||||||
|
|
||||||
func awkTokenizer(input *string) ([]string, int) {
|
func awkTokenizer(input []rune) ([][]rune, int) {
|
||||||
// 9, 32
|
// 9, 32
|
||||||
ret := []string{}
|
ret := [][]rune{}
|
||||||
str := []rune{}
|
str := []rune{}
|
||||||
prefixLength := 0
|
prefixLength := 0
|
||||||
state := awkNil
|
state := awkNil
|
||||||
for _, r := range []rune(*input) {
|
for _, r := range input {
|
||||||
white := r == 9 || r == 32
|
white := r == 9 || r == 32
|
||||||
switch state {
|
switch state {
|
||||||
case awkNil:
|
case awkNil:
|
||||||
@@ -113,47 +119,69 @@ func awkTokenizer(input *string) ([]string, int) {
|
|||||||
if white {
|
if white {
|
||||||
str = append(str, r)
|
str = append(str, r)
|
||||||
} else {
|
} else {
|
||||||
ret = append(ret, string(str))
|
ret = append(ret, str)
|
||||||
state = awkBlack
|
state = awkBlack
|
||||||
str = []rune{r}
|
str = []rune{r}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(str) > 0 {
|
if len(str) > 0 {
|
||||||
ret = append(ret, string(str))
|
ret = append(ret, str)
|
||||||
}
|
}
|
||||||
return ret, prefixLength
|
return ret, prefixLength
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tokenize tokenizes the given string with the delimiter
|
// Tokenize tokenizes the given string with the delimiter
|
||||||
func Tokenize(str *string, delimiter *regexp.Regexp) []Token {
|
func Tokenize(runes []rune, delimiter Delimiter) []Token {
|
||||||
if delimiter == nil {
|
if delimiter.str == nil && delimiter.regex == nil {
|
||||||
// AWK-style (\S+\s*)
|
// AWK-style (\S+\s*)
|
||||||
tokens, prefixLength := awkTokenizer(str)
|
tokens, prefixLength := awkTokenizer(runes)
|
||||||
return withPrefixLengths(tokens, prefixLength)
|
return withPrefixLengths(tokens, prefixLength)
|
||||||
}
|
}
|
||||||
tokens := delimiter.FindAllString(*str, -1)
|
|
||||||
return withPrefixLengths(tokens, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func joinTokens(tokens *[]Token) *string {
|
var tokens []string
|
||||||
ret := ""
|
if delimiter.str != nil {
|
||||||
for _, token := range *tokens {
|
tokens = strings.Split(string(runes), *delimiter.str)
|
||||||
ret += string(*token.text)
|
for i := 0; i < len(tokens)-1; i++ {
|
||||||
|
tokens[i] = tokens[i] + *delimiter.str
|
||||||
|
}
|
||||||
|
} else if delimiter.regex != nil {
|
||||||
|
str := string(runes)
|
||||||
|
for len(str) > 0 {
|
||||||
|
loc := delimiter.regex.FindStringIndex(str)
|
||||||
|
if loc == nil {
|
||||||
|
loc = []int{0, len(str)}
|
||||||
|
}
|
||||||
|
last := util.Max(loc[1], 1)
|
||||||
|
tokens = append(tokens, str[:last])
|
||||||
|
str = str[last:]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &ret
|
asRunes := make([][]rune, len(tokens))
|
||||||
|
for i, token := range tokens {
|
||||||
|
asRunes[i] = []rune(token)
|
||||||
|
}
|
||||||
|
return withPrefixLengths(asRunes, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func joinTokensAsRunes(tokens *[]Token) *[]rune {
|
func joinTokens(tokens []Token) []rune {
|
||||||
ret := []rune{}
|
ret := []rune{}
|
||||||
for _, token := range *tokens {
|
for _, token := range tokens {
|
||||||
ret = append(ret, *token.text...)
|
ret = append(ret, token.text...)
|
||||||
}
|
}
|
||||||
return &ret
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinTokensAsRunes(tokens []Token) []rune {
|
||||||
|
ret := []rune{}
|
||||||
|
for _, token := range tokens {
|
||||||
|
ret = append(ret, token.text...)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform is used to transform the input when --with-nth option is given
|
// Transform is used to transform the input when --with-nth option is given
|
||||||
func Transform(tokens []Token, withNth []Range) *[]Token {
|
func Transform(tokens []Token, withNth []Range) []Token {
|
||||||
transTokens := make([]Token, len(withNth))
|
transTokens := make([]Token, len(withNth))
|
||||||
numTokens := len(tokens)
|
numTokens := len(tokens)
|
||||||
for idx, r := range withNth {
|
for idx, r := range withNth {
|
||||||
@@ -162,14 +190,14 @@ func Transform(tokens []Token, withNth []Range) *[]Token {
|
|||||||
if r.begin == r.end {
|
if r.begin == r.end {
|
||||||
idx := r.begin
|
idx := r.begin
|
||||||
if idx == rangeEllipsis {
|
if idx == rangeEllipsis {
|
||||||
part = append(part, *joinTokensAsRunes(&tokens)...)
|
part = append(part, joinTokensAsRunes(tokens)...)
|
||||||
} else {
|
} else {
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
idx += numTokens + 1
|
idx += numTokens + 1
|
||||||
}
|
}
|
||||||
if idx >= 1 && idx <= numTokens {
|
if idx >= 1 && idx <= numTokens {
|
||||||
minIdx = idx - 1
|
minIdx = idx - 1
|
||||||
part = append(part, *tokens[idx-1].text...)
|
part = append(part, tokens[idx-1].text...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -196,7 +224,7 @@ func Transform(tokens []Token, withNth []Range) *[]Token {
|
|||||||
minIdx = util.Max(0, begin-1)
|
minIdx = util.Max(0, begin-1)
|
||||||
for idx := begin; idx <= end; idx++ {
|
for idx := begin; idx <= end; idx++ {
|
||||||
if idx >= 1 && idx <= numTokens {
|
if idx >= 1 && idx <= numTokens {
|
||||||
part = append(part, *tokens[idx-1].text...)
|
part = append(part, tokens[idx-1].text...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,7 +234,7 @@ func Transform(tokens []Token, withNth []Range) *[]Token {
|
|||||||
} else {
|
} else {
|
||||||
prefixLength = 0
|
prefixLength = 0
|
||||||
}
|
}
|
||||||
transTokens[idx] = Token{&part, prefixLength}
|
transTokens[idx] = Token{part, prefixLength, util.TrimLen(part)}
|
||||||
}
|
}
|
||||||
return &transTokens
|
return transTokens
|
||||||
}
|
}
|
||||||
|
@@ -43,14 +43,23 @@ func TestParseRange(t *testing.T) {
|
|||||||
func TestTokenize(t *testing.T) {
|
func TestTokenize(t *testing.T) {
|
||||||
// AWK-style
|
// AWK-style
|
||||||
input := " abc: def: ghi "
|
input := " abc: def: ghi "
|
||||||
tokens := Tokenize(&input, nil)
|
tokens := Tokenize([]rune(input), Delimiter{})
|
||||||
if string(*tokens[0].text) != "abc: " || tokens[0].prefixLength != 2 {
|
if string(tokens[0].text) != "abc: " || tokens[0].prefixLength != 2 || tokens[0].trimLength != 4 {
|
||||||
t.Errorf("%s", tokens)
|
t.Errorf("%s", tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With delimiter
|
// With delimiter
|
||||||
tokens = Tokenize(&input, delimiterRegexp(":"))
|
tokens = Tokenize([]rune(input), delimiterRegexp(":"))
|
||||||
if string(*tokens[0].text) != " abc:" || tokens[0].prefixLength != 0 {
|
if string(tokens[0].text) != " abc:" || tokens[0].prefixLength != 0 || tokens[0].trimLength != 4 {
|
||||||
|
t.Errorf("%s", tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
// With delimiter regex
|
||||||
|
tokens = Tokenize([]rune(input), delimiterRegexp("\\s+"))
|
||||||
|
if string(tokens[0].text) != " " || tokens[0].prefixLength != 0 || tokens[0].trimLength != 0 ||
|
||||||
|
string(tokens[1].text) != "abc: " || tokens[1].prefixLength != 2 || tokens[1].trimLength != 4 ||
|
||||||
|
string(tokens[2].text) != "def: " || tokens[2].prefixLength != 8 || tokens[2].trimLength != 4 ||
|
||||||
|
string(tokens[3].text) != "ghi " || tokens[3].prefixLength != 14 || tokens[3].trimLength != 3 {
|
||||||
t.Errorf("%s", tokens)
|
t.Errorf("%s", tokens)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,39 +67,39 @@ func TestTokenize(t *testing.T) {
|
|||||||
func TestTransform(t *testing.T) {
|
func TestTransform(t *testing.T) {
|
||||||
input := " abc: def: ghi: jkl"
|
input := " abc: def: ghi: jkl"
|
||||||
{
|
{
|
||||||
tokens := Tokenize(&input, nil)
|
tokens := Tokenize([]rune(input), Delimiter{})
|
||||||
{
|
{
|
||||||
ranges := splitNth("1,2,3")
|
ranges := splitNth("1,2,3")
|
||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
if *joinTokens(tx) != "abc: def: ghi: " {
|
if string(joinTokens(tx)) != "abc: def: ghi: " {
|
||||||
t.Errorf("%s", *tx)
|
t.Errorf("%s", tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
ranges := splitNth("1..2,3,2..,1")
|
ranges := splitNth("1..2,3,2..,1")
|
||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
if *joinTokens(tx) != "abc: def: ghi: def: ghi: jklabc: " ||
|
if string(joinTokens(tx)) != "abc: def: ghi: def: ghi: jklabc: " ||
|
||||||
len(*tx) != 4 ||
|
len(tx) != 4 ||
|
||||||
string(*(*tx)[0].text) != "abc: def: " || (*tx)[0].prefixLength != 2 ||
|
string(tx[0].text) != "abc: def: " || tx[0].prefixLength != 2 ||
|
||||||
string(*(*tx)[1].text) != "ghi: " || (*tx)[1].prefixLength != 14 ||
|
string(tx[1].text) != "ghi: " || tx[1].prefixLength != 14 ||
|
||||||
string(*(*tx)[2].text) != "def: ghi: jkl" || (*tx)[2].prefixLength != 8 ||
|
string(tx[2].text) != "def: ghi: jkl" || tx[2].prefixLength != 8 ||
|
||||||
string(*(*tx)[3].text) != "abc: " || (*tx)[3].prefixLength != 2 {
|
string(tx[3].text) != "abc: " || tx[3].prefixLength != 2 {
|
||||||
t.Errorf("%s", *tx)
|
t.Errorf("%s", tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
tokens := Tokenize(&input, delimiterRegexp(":"))
|
tokens := Tokenize([]rune(input), delimiterRegexp(":"))
|
||||||
{
|
{
|
||||||
ranges := splitNth("1..2,3,2..,1")
|
ranges := splitNth("1..2,3,2..,1")
|
||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
if *joinTokens(tx) != " abc: def: ghi: def: ghi: jkl abc:" ||
|
if string(joinTokens(tx)) != " abc: def: ghi: def: ghi: jkl abc:" ||
|
||||||
len(*tx) != 4 ||
|
len(tx) != 4 ||
|
||||||
string(*(*tx)[0].text) != " abc: def:" || (*tx)[0].prefixLength != 0 ||
|
string(tx[0].text) != " abc: def:" || tx[0].prefixLength != 0 ||
|
||||||
string(*(*tx)[1].text) != " ghi:" || (*tx)[1].prefixLength != 12 ||
|
string(tx[1].text) != " ghi:" || tx[1].prefixLength != 12 ||
|
||||||
string(*(*tx)[2].text) != " def: ghi: jkl" || (*tx)[2].prefixLength != 6 ||
|
string(tx[2].text) != " def: ghi: jkl" || tx[2].prefixLength != 6 ||
|
||||||
string(*(*tx)[3].text) != " abc:" || (*tx)[3].prefixLength != 0 {
|
string(tx[3].text) != " abc:" || tx[3].prefixLength != 0 {
|
||||||
t.Errorf("%s", *tx)
|
t.Errorf("%s", tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Max returns the largest integer
|
// Max returns the largest integer
|
||||||
@@ -19,7 +20,7 @@ func Max(first int, items ...int) int {
|
|||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
|
|
||||||
// Max32 returns the smallest 32-bit integer
|
// Min32 returns the smallest 32-bit integer
|
||||||
func Min32(first int32, second int32) int32 {
|
func Min32(first int32, second int32) int32 {
|
||||||
if first <= second {
|
if first <= second {
|
||||||
return first
|
return first
|
||||||
@@ -69,22 +70,59 @@ func DurWithin(
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func Between(val int, min int, max int) bool {
|
|
||||||
return val >= min && val <= max
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsTty returns true is stdin is a terminal
|
// IsTty returns true is stdin is a terminal
|
||||||
func IsTty() bool {
|
func IsTty() bool {
|
||||||
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func TrimRight(runes *[]rune) []rune {
|
// TrimRight returns rune array with trailing white spaces cut off
|
||||||
|
func TrimRight(runes []rune) []rune {
|
||||||
var i int
|
var i int
|
||||||
for i = len(*runes) - 1; i >= 0; i-- {
|
for i = len(runes) - 1; i >= 0; i-- {
|
||||||
char := (*runes)[i]
|
char := runes[i]
|
||||||
if char != ' ' && char != '\t' {
|
if char != ' ' && char != '\t' {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (*runes)[0 : i+1]
|
return runes[0 : i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// BytesToRunes converts byte array into rune array
|
||||||
|
func BytesToRunes(bytea []byte) []rune {
|
||||||
|
runes := make([]rune, 0, len(bytea))
|
||||||
|
for i := 0; i < len(bytea); {
|
||||||
|
if bytea[i] < utf8.RuneSelf {
|
||||||
|
runes = append(runes, rune(bytea[i]))
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
r, sz := utf8.DecodeRune(bytea[i:])
|
||||||
|
i += sz
|
||||||
|
runes = append(runes, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return runes
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrimLen returns the length of trimmed rune array
|
||||||
|
func TrimLen(runes []rune) int {
|
||||||
|
var i int
|
||||||
|
for i = len(runes) - 1; i >= 0; i-- {
|
||||||
|
char := runes[i]
|
||||||
|
if char != ' ' && char != '\t' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Completely empty
|
||||||
|
if i < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var j int
|
||||||
|
for j = 0; j < len(runes); j++ {
|
||||||
|
char := runes[j]
|
||||||
|
if char != ' ' && char != '\t' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i - j + 1
|
||||||
}
|
}
|
||||||
|
@@ -20,3 +20,23 @@ func TestContrain(t *testing.T) {
|
|||||||
t.Error("Expected", 3)
|
t.Error("Expected", 3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrimLen(t *testing.T) {
|
||||||
|
check := func(str string, exp int) {
|
||||||
|
trimmed := TrimLen([]rune(str))
|
||||||
|
if trimmed != exp {
|
||||||
|
t.Errorf("Invalid TrimLen result for '%s': %d (expected %d)",
|
||||||
|
str, trimmed, exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check("hello", 5)
|
||||||
|
check("hello ", 5)
|
||||||
|
check("hello ", 5)
|
||||||
|
check(" hello", 5)
|
||||||
|
check(" hello", 5)
|
||||||
|
check(" hello ", 5)
|
||||||
|
check(" hello ", 5)
|
||||||
|
check("h o", 5)
|
||||||
|
check(" h o ", 5)
|
||||||
|
check(" ", 0)
|
||||||
|
}
|
||||||
|
169
test/test_go.rb
169
test/test_go.rb
@@ -501,6 +501,90 @@ class TestGoFZF < TestBase
|
|||||||
assert_equal input, `cat #{tempname} | #{FZF} -f"!z" -x --tiebreak end`.split($/)
|
assert_equal input, `cat #{tempname} | #{FZF} -f"!z" -x --tiebreak end`.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_tiebreak_length_with_nth
|
||||||
|
input = %w[
|
||||||
|
1:hell
|
||||||
|
123:hello
|
||||||
|
12345:he
|
||||||
|
1234567:h
|
||||||
|
]
|
||||||
|
writelines tempname, input
|
||||||
|
|
||||||
|
output = %w[
|
||||||
|
1:hell
|
||||||
|
12345:he
|
||||||
|
123:hello
|
||||||
|
1234567:h
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fh`.split($/)
|
||||||
|
|
||||||
|
output = %w[
|
||||||
|
1234567:h
|
||||||
|
12345:he
|
||||||
|
1:hell
|
||||||
|
123:hello
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tiebreak_length_with_nth_trim_length
|
||||||
|
input = [
|
||||||
|
"apple juice bottle 1",
|
||||||
|
"apple ui bottle 2",
|
||||||
|
"app ice bottle 3",
|
||||||
|
"app ic bottle 4",
|
||||||
|
]
|
||||||
|
writelines tempname, input
|
||||||
|
|
||||||
|
# len(1)
|
||||||
|
output = [
|
||||||
|
"app ice bottle 3",
|
||||||
|
"app ic bottle 4",
|
||||||
|
"apple juice bottle 1",
|
||||||
|
"apple ui bottle 2",
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fa -n1`.split($/)
|
||||||
|
|
||||||
|
# len(1 ~ 2)
|
||||||
|
output = [
|
||||||
|
"apple ui bottle 2",
|
||||||
|
"app ic bottle 4",
|
||||||
|
"apple juice bottle 1",
|
||||||
|
"app ice bottle 3",
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fai -n1..2`.split($/)
|
||||||
|
|
||||||
|
# len(1) + len(2)
|
||||||
|
output = [
|
||||||
|
"app ic bottle 4",
|
||||||
|
"app ice bottle 3",
|
||||||
|
"apple ui bottle 2",
|
||||||
|
"apple juice bottle 1",
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -x -f"a i" -n1,2`.split($/)
|
||||||
|
|
||||||
|
# len(2)
|
||||||
|
output = [
|
||||||
|
"apple ui bottle 2",
|
||||||
|
"app ic bottle 4",
|
||||||
|
"app ice bottle 3",
|
||||||
|
"apple juice bottle 1",
|
||||||
|
]
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fi -n2`.split($/)
|
||||||
|
assert_equal output, `cat #{tempname} | #{FZF} -fi -n2,1..2`.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
|
def test_invalid_cache
|
||||||
tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter
|
tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include? '2/3' }
|
tmux.until { |lines| lines[-2].include? '2/3' }
|
||||||
@@ -700,8 +784,8 @@ class TestGoFZF < TestBase
|
|||||||
assert_equal '6', readonce.chomp
|
assert_equal '6', readonce.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_header_file
|
def test_header
|
||||||
tmux.send_keys "seq 100 | #{fzf "--header-file <(head -5 #{__FILE__})"}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[-2].include?('100/100') &&
|
lines[-2].include?('100/100') &&
|
||||||
@@ -709,8 +793,8 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_header_file_reverse
|
def test_header_reverse
|
||||||
tmux.send_keys "seq 100 | #{fzf "--header-file=<(head -5 #{__FILE__}) --reverse"}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--header=\\\"\\$(head -5 #{__FILE__})\\\" --reverse"}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(__FILE__).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[1].include?('100/100') &&
|
lines[1].include?('100/100') &&
|
||||||
@@ -718,6 +802,26 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
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
|
def test_canel
|
||||||
tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter
|
tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include?('10/10') }
|
tmux.until { |lines| lines[-2].include?('10/10') }
|
||||||
@@ -743,6 +847,63 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
end
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_ansi
|
||||||
|
writelines tempname, ["\x1b[33mhello \x1b[34;1mworld\x1b[m ", 'byebye']
|
||||||
|
assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi`.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_no_ansi
|
||||||
|
src = "\x1b[33mhello \x1b[34;1mworld\x1b[m "
|
||||||
|
writelines tempname, [src, 'byebye']
|
||||||
|
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
|
private
|
||||||
def writelines path, lines
|
def writelines path, lines
|
||||||
File.unlink path while File.exists? path
|
File.unlink path while File.exists? path
|
||||||
|
Reference in New Issue
Block a user