mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-01 20:52:06 -07:00
Compare commits
243 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
49c752b1f7 | ||
|
daa79a6df2 | ||
|
48e0c1e721 | ||
|
12d81e212f | ||
|
c22e729d9c | ||
|
2b8a1c0d70 | ||
|
e4b56b9702 | ||
|
789a474b28 | ||
|
fb2959c514 | ||
|
62a28468a7 | ||
|
23dba99eda | ||
|
5f62d224b0 | ||
|
6728870071 | ||
|
87c71a3ea6 | ||
|
06ab399497 | ||
|
f7b52d2541 | ||
|
c111af0ed2 | ||
|
07e2bd673e | ||
|
e4ce64d10b | ||
|
5f3326a888 | ||
|
1304428003 | ||
|
55828f389a | ||
|
7e2c18a1f6 | ||
|
79c147ed78 | ||
|
d4b41c5e03 | ||
|
b15a0e9650 | ||
|
fe09559ee9 | ||
|
94e8e6419f | ||
|
4d2d18649c | ||
|
c1aa5c5f33 | ||
|
4a1752d3fc | ||
|
b9b1eeffce | ||
|
5667667d1f | ||
|
f5b034095a | ||
|
95e5beb34e | ||
|
e808151c28 | ||
|
d760b790b3 | ||
|
1b5599972a | ||
|
6c2ce28d0d | ||
|
ff09c275d4 | ||
|
93dcd932e8 | ||
|
e6a0de4094 | ||
|
9f39671e65 | ||
|
423317b82a | ||
|
47201c2c4d | ||
|
53d5d9d162 | ||
|
9cb0cdb4ac | ||
|
448132c46c | ||
|
1476fc7f3b | ||
|
71a7b3a26f | ||
|
a47c06cb61 | ||
|
48e16edb47 | ||
|
c35d98dc42 | ||
|
8bead4ae34 | ||
|
1b6cb3532d | ||
|
0a0955755a | ||
|
a3101120fd | ||
|
30f9651f99 | ||
|
4dcc0f10b8 | ||
|
3d39ab5ded | ||
|
c3a198d0c7 | ||
|
be5c17612a | ||
|
fe89ac8a89 | ||
|
4c3ae847b6 | ||
|
5c0dc79ffa | ||
|
0a83705d21 | ||
|
ea22292d2c | ||
|
1990f3c992 | ||
|
c0b432f7b4 | ||
|
ae3180f919 | ||
|
62acb9adc4 | ||
|
0b5fa56444 | ||
|
789f26b1a5 | ||
|
a3068a33d5 | ||
|
b8c4b35415 | ||
|
209a6d36ad | ||
|
5c491d573a | ||
|
2c86e728b5 | ||
|
cd847affb7 | ||
|
7a2bc2cada | ||
|
9dbf6b02d2 | ||
|
1db68a3976 | ||
|
1c31352675 | ||
|
6c3489087c | ||
|
313578a1a0 | ||
|
bd7331ecf5 | ||
|
e293cd4d08 | ||
|
ca4bdfb4bd | ||
|
4f40314433 | ||
|
f670f4f076 | ||
|
6e86fee588 | ||
|
2d9b38b93e | ||
|
b8a9861f95 | ||
|
188c90bf25 | ||
|
8b02ae650c | ||
|
b7bb100810 | ||
|
aa05bf5206 | ||
|
d303c5b3eb | ||
|
f401c42f9c | ||
|
efec9acd6f | ||
|
3ed86445e1 | ||
|
23f27f3ce5 | ||
|
f99f66570b | ||
|
3e129ac68c | ||
|
8a0ab20a70 | ||
|
b277f5ae6f | ||
|
6109a0fe44 | ||
|
383f908cf7 | ||
|
3e6c950e12 | ||
|
ee2ee02599 | ||
|
b42dcdb7a7 | ||
|
82156d34cc | ||
|
4a5142c60b | ||
|
ea25e9674f | ||
|
dee0909d2b | ||
|
8e5ecf6b38 | ||
|
7557737569 | ||
|
53bce0581e | ||
|
f9f9b671c5 | ||
|
606d33e77e | ||
|
d2f7acbc69 | ||
|
0dd024a09f | ||
|
0a6cb62169 | ||
|
9930a1d4d9 | ||
|
40d0a6347c | ||
|
baad26a0fd | ||
|
f3177305d5 | ||
|
4ceb520c1d | ||
|
d761ea5158 | ||
|
7ba93d9f83 | ||
|
b34f93f307 | ||
|
ec040d82dd | ||
|
00190677d4 | ||
|
d38f7a5eb5 | ||
|
ee433ef6e9 | ||
|
d89c9e94ba | ||
|
7e2dfef930 | ||
|
0296fcb5cd | ||
|
80819f3c44 | ||
|
7571baadb4 | ||
|
da03a66e69 | ||
|
3c47b7fa5f | ||
|
ba9365c438 | ||
|
db37e67575 | ||
|
76a3ef8c37 | ||
|
6fd6fff3a6 | ||
|
d1387bf512 | ||
|
4c923a2d19 | ||
|
4ee85f11e8 | ||
|
829c7f909c | ||
|
990fa00660 | ||
|
77592825f0 | ||
|
ce53b9b2a5 | ||
|
175fe158ed | ||
|
80efafcceb | ||
|
b241409e4b | ||
|
11967be017 | ||
|
6ee811ea03 | ||
|
d5e7303a25 | ||
|
2924fd3e23 | ||
|
75b44aac13 | ||
|
86c73105ee | ||
|
2d00abc7cb | ||
|
1e07b3b1c2 | ||
|
4313c1c25c | ||
|
cc9938d4c9 | ||
|
a54784cd53 | ||
|
22989b0488 | ||
|
892aa1e78b | ||
|
b9ab7d2413 | ||
|
69b2a0a733 | ||
|
13cd4ed546 | ||
|
7261d3afcd | ||
|
84fc73ad9c | ||
|
4103f5c3cc | ||
|
5390616694 | ||
|
daf08f801f | ||
|
4e2a1fe5c8 | ||
|
03f155484c | ||
|
89298a8d23 | ||
|
3b14c5230c | ||
|
91401514ab | ||
|
91d986b6c0 | ||
|
4d72bd098a | ||
|
502973ff75 | ||
|
3e91c189ae | ||
|
b0f80b686c | ||
|
b824928b0b | ||
|
ccca34f9f7 | ||
|
b5350b24ff | ||
|
56ace10a37 | ||
|
72ec0a3408 | ||
|
05118cc440 | ||
|
e392da20e8 | ||
|
6e69339f6b | ||
|
30cdc06bcd | ||
|
9ce43d46f6 | ||
|
de09656197 | ||
|
3827a1b09e | ||
|
61ba8d5a11 | ||
|
4a3a5ee70d | ||
|
f58a53a001 | ||
|
65c1b53275 | ||
|
0b43f988c7 | ||
|
f8e357fa19 | ||
|
c3a4e4cd23 | ||
|
9dac12cb32 | ||
|
d76a3646b7 | ||
|
d7c734acd6 | ||
|
ed13fc8618 | ||
|
edcd7c6aa6 | ||
|
b0fdd6db99 | ||
|
edf27f47f2 | ||
|
3b218b77eb | ||
|
1e02471940 | ||
|
1b9dadb3d3 | ||
|
c3827dea10 | ||
|
6a1b916598 | ||
|
a2c7b001d5 | ||
|
3c6e938bb1 | ||
|
5a0afc5fea | ||
|
f37be006c3 | ||
|
459c332351 | ||
|
153a87d84a | ||
|
05da892cd2 | ||
|
f6b1a6278f | ||
|
db58182483 | ||
|
6e9f0882da | ||
|
7ed18579dc | ||
|
f250fc8f86 | ||
|
6eea9603c2 | ||
|
20915529b7 | ||
|
b3efccca81 | ||
|
809d465de5 | ||
|
7d15071c63 | ||
|
89eb1575e7 | ||
|
5d6ed935a4 | ||
|
0528435386 | ||
|
fe22213b51 | ||
|
aab42eaaba | ||
|
16031b0d54 | ||
|
ded184daaf | ||
|
ecf90bd25b |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
bin
|
||||||
|
src/fzf/fzf_*
|
||||||
pkg
|
pkg
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
28
.travis.yml
Normal file
28
.travis.yml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
language: ruby
|
||||||
|
rvm:
|
||||||
|
- 2.2.0
|
||||||
|
|
||||||
|
install:
|
||||||
|
- sudo apt-get update
|
||||||
|
- sudo apt-get install -y libncurses-dev lib32ncurses5-dev
|
||||||
|
- sudo add-apt-repository -y ppa:pi-rho/dev
|
||||||
|
- sudo apt-add-repository -y ppa:fish-shell/release-2
|
||||||
|
- sudo apt-get update
|
||||||
|
- sudo apt-get install -y tmux=1.9a-1~ppa1~p
|
||||||
|
- sudo apt-get install -y zsh fish
|
||||||
|
|
||||||
|
script: |
|
||||||
|
export GOROOT=~/go1.4
|
||||||
|
export GOPATH=~/go
|
||||||
|
export FZF_BASE=~/go/src/github.com/junegunn/fzf
|
||||||
|
|
||||||
|
mkdir -p ~/go/src/github.com/junegunn
|
||||||
|
ln -s $(pwd) $FZF_BASE
|
||||||
|
|
||||||
|
curl https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz | tar -xz
|
||||||
|
mv go $GOROOT
|
||||||
|
|
||||||
|
cd $FZF_BASE/src && make test fzf/fzf-linux_amd64 install &&
|
||||||
|
cd $FZF_BASE/bin && ln -sf fzf-linux_amd64 fzf-$(./fzf --version)-linux_amd64 &&
|
||||||
|
cd $FZF_BASE && yes | ./install &&
|
||||||
|
tmux new "rake test > out && touch ok" && cat out && [ -e ok ]
|
60
CHANGELOG.md
Normal file
60
CHANGELOG.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
0.9.4
|
||||||
|
-----
|
||||||
|
|
||||||
|
### New features
|
||||||
|
|
||||||
|
#### Added `--tac` option to reverse the order of the input.
|
||||||
|
|
||||||
|
One might argue that this option is unnecessary since we can already put `tac`
|
||||||
|
or `tail -r` in the command pipeline to achieve the same result. However, the
|
||||||
|
advantage of `--tac` is that it does not block until the input is complete.
|
||||||
|
|
||||||
|
### *Backward incompatible changes*
|
||||||
|
|
||||||
|
#### Changed behavior on `--no-sort`
|
||||||
|
|
||||||
|
`--no-sort` option will no longer reverse the display order within finder. You
|
||||||
|
may want to use the new `--tac` option with `--no-sort`.
|
||||||
|
|
||||||
|
```
|
||||||
|
history | fzf +s --tac
|
||||||
|
```
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
|
||||||
|
#### `--filter` will not block when sort is disabled
|
||||||
|
|
||||||
|
When fzf works in filtering mode (`--filter`) and sort is disabled
|
||||||
|
(`--no-sort`), there's no need to block until input is complete. The new
|
||||||
|
version of fzf will print the matches on-the-fly when the following condition
|
||||||
|
is met:
|
||||||
|
|
||||||
|
--filter TERM --no-sort [--no-tac --no-sync]
|
||||||
|
|
||||||
|
or simply:
|
||||||
|
|
||||||
|
-f TERM +s
|
||||||
|
|
||||||
|
This change removes unnecessary delay in the use cases like the following:
|
||||||
|
|
||||||
|
fzf -f xxx +s | head -5
|
||||||
|
|
||||||
|
However, in this case, fzf processes the lines sequentially, so it cannot
|
||||||
|
utilize multiple cores, and fzf will run slightly slower than the previous
|
||||||
|
mode of execution where filtering is done in parallel after the entire input
|
||||||
|
is loaded. If the user is concerned about this performance problem, one can
|
||||||
|
add `--sync` option to re-enable buffering.
|
||||||
|
|
||||||
|
0.9.3
|
||||||
|
-----
|
||||||
|
|
||||||
|
### New features
|
||||||
|
- Added `--sync` option for multi-staged filtering
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
- `--select-1` and `--exit-0` will start finder immediately when the condition
|
||||||
|
cannot be met
|
||||||
|
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Junegunn Choi
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
425
README.md
425
README.md
@@ -5,19 +5,27 @@ fzf is a general-purpose fuzzy finder for your shell.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
([tmux integration!](https://cloud.githubusercontent.com/assets/700826/2593609/3ec13962-ba83-11e3-88d3-f9f95bd8a64b.gif))
|
|
||||||
|
|
||||||
It was heavily inspired by [ctrlp.vim](https://github.com/kien/ctrlp.vim) and
|
It was heavily inspired by [ctrlp.vim](https://github.com/kien/ctrlp.vim) and
|
||||||
the likes.
|
the likes.
|
||||||
|
|
||||||
Requirements
|
|
||||||
------------
|
|
||||||
|
|
||||||
fzf requires Ruby (>= 1.8.5).
|
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
fzf project consists of the followings:
|
||||||
|
|
||||||
|
- `fzf` executable
|
||||||
|
- `fzf-tmux` script for launching fzf in a tmux pane
|
||||||
|
- Shell extensions
|
||||||
|
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)
|
||||||
|
- Fuzzy auto-completion (bash only)
|
||||||
|
|
||||||
|
You can [download fzf executable][bin] alone, but it's recommended that you
|
||||||
|
install the extra stuff using the attached install script.
|
||||||
|
|
||||||
|
[bin]: https://github.com/junegunn/fzf-bin/releases
|
||||||
|
|
||||||
|
#### Using git (recommended)
|
||||||
|
|
||||||
Clone this repository and run
|
Clone this repository and run
|
||||||
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||||
|
|
||||||
@@ -26,16 +34,29 @@ git clone https://github.com/junegunn/fzf.git ~/.fzf
|
|||||||
~/.fzf/install
|
~/.fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
The script will setup:
|
#### Using curl
|
||||||
|
|
||||||
- `fzf` executable
|
In case you don't have git installed:
|
||||||
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) for bash and zsh
|
|
||||||
- Fuzzy auto-completion for bash
|
|
||||||
|
|
||||||
If you don't use bash or zsh, you have to manually place fzf executable in a
|
```sh
|
||||||
directory included in `$PATH`. Key bindings are not yet supported.
|
mkdir -p ~/.fzf
|
||||||
|
curl -L https://github.com/junegunn/fzf/archive/master.tar.gz |
|
||||||
|
tar xz --strip-components 1 -C ~/.fzf
|
||||||
|
~/.fzf/install
|
||||||
|
```
|
||||||
|
|
||||||
### Install as Vim plugin
|
#### Using Homebrew
|
||||||
|
|
||||||
|
On OS X, you can use [Homebrew](http://brew.sh/) to install fzf.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
brew install fzf
|
||||||
|
|
||||||
|
# Install shell extensions - this should be done whenever fzf is updated
|
||||||
|
$(brew info fzf | grep /install)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Install as Vim plugin
|
||||||
|
|
||||||
Once you have cloned the repository, add the following line to your .vimrc.
|
Once you have cloned the repository, add the following line to your .vimrc.
|
||||||
|
|
||||||
@@ -43,46 +64,16 @@ Once you have cloned the repository, add the following line to your .vimrc.
|
|||||||
set rtp+=~/.fzf
|
set rtp+=~/.fzf
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you may use any Vim plugin manager, such as
|
Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf
|
||||||
[vim-plug](https://github.com/junegunn/vim-plug).
|
(recommended):
|
||||||
|
|
||||||
|
```vim
|
||||||
|
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' }
|
||||||
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
```
|
|
||||||
usage: fzf [options]
|
|
||||||
|
|
||||||
Search
|
|
||||||
-x, --extended Extended-search mode
|
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
|
||||||
-i Case-insensitive match (default: smart-case match)
|
|
||||||
+i Case-sensitive match
|
|
||||||
-n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting
|
|
||||||
search scope (positive or negative integers)
|
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
|
||||||
|
|
||||||
Search result
|
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
|
||||||
|
|
||||||
Interface
|
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
|
||||||
--no-mouse Disable mouse
|
|
||||||
+c, --no-color Disable colors
|
|
||||||
+2, --no-256 Disable 256-color
|
|
||||||
--black Use black background
|
|
||||||
|
|
||||||
Scripting
|
|
||||||
-q, --query=STR Start the finder with the given query
|
|
||||||
-1, --select-1 Automatically select the only match
|
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
|
||||||
|
|
||||||
Environment variables
|
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
|
||||||
```
|
|
||||||
|
|
||||||
fzf will launch curses-based finder, read the list from STDIN, and write the
|
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||||
selected item to STDOUT.
|
selected item to STDOUT.
|
||||||
|
|
||||||
@@ -98,33 +89,16 @@ files excluding hidden ones. (You can override the default command with
|
|||||||
vim $(fzf)
|
vim $(fzf)
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
#### Using the finder
|
||||||
`+s`) option.
|
|
||||||
|
|
||||||
```sh
|
- `CTRL-J` / `CTRL-K` (or `CTRL-N` / `CTRL-P)` to move cursor up and down
|
||||||
history | fzf +s
|
- `Enter` key to select the item, `CTRL-C` / `CTRL-G` / `ESC` to exit
|
||||||
```
|
- On multi-select mode (`-m`), `TAB` and `Shift-TAB` to mark multiple items
|
||||||
|
- Emacs style key bindings
|
||||||
|
- Mouse: scroll, click, double-click; shift-click and shift-scroll on
|
||||||
|
multi-select mode
|
||||||
|
|
||||||
### Keys
|
#### Extended-search mode
|
||||||
|
|
||||||
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press
|
|
||||||
enter key to select the item. CTRL-C, CTRL-G, or ESC will terminate the finder.
|
|
||||||
|
|
||||||
The following readline key bindings should also work as expected.
|
|
||||||
|
|
||||||
- CTRL-A / CTRL-E
|
|
||||||
- CTRL-B / CTRL-F
|
|
||||||
- CTRL-W / CTRL-U / CTRL-Y
|
|
||||||
- ALT-B / ALT-F
|
|
||||||
|
|
||||||
If you enable multi-select mode with `-m` option, you can select multiple items
|
|
||||||
with TAB or Shift-TAB key.
|
|
||||||
|
|
||||||
You can also use mouse. Double-click on an item to select it or shift-click (or
|
|
||||||
ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and
|
|
||||||
down.
|
|
||||||
|
|
||||||
### Extended-search mode
|
|
||||||
|
|
||||||
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||||
|
|
||||||
@@ -143,77 +117,18 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
|||||||
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
||||||
fzf with `-e` or `--extended-exact` option.
|
fzf with `-e` or `--extended-exact` option.
|
||||||
|
|
||||||
Useful examples
|
Examples
|
||||||
---------------
|
--------
|
||||||
|
|
||||||
```sh
|
Many useful examples can be found on [the wiki
|
||||||
# fe [FUZZY PATTERN] - Open the selected file with the default editor
|
page](https://github.com/junegunn/fzf/wiki/examples). Feel free to add your
|
||||||
# - Bypass fuzzy finder if there's only one match (--select-1)
|
own as well.
|
||||||
# - Exit if there's no match (--exit-0)
|
|
||||||
fe() {
|
|
||||||
local file
|
|
||||||
file=$(fzf --query="$1" --select-1 --exit-0)
|
|
||||||
[ -n "$file" ] && ${EDITOR:-vim} "$file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# fd - cd to selected directory
|
|
||||||
fd() {
|
|
||||||
local dir
|
|
||||||
dir=$(find ${1:-*} -path '*/\.*' -prune \
|
|
||||||
-o -type d -print 2> /dev/null | fzf +m) &&
|
|
||||||
cd "$dir"
|
|
||||||
}
|
|
||||||
|
|
||||||
# fda - including hidden directories
|
|
||||||
fda() {
|
|
||||||
local dir
|
|
||||||
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
|
|
||||||
}
|
|
||||||
|
|
||||||
# fh - repeat history
|
|
||||||
fh() {
|
|
||||||
eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s | sed 's/ *[0-9]* *//')
|
|
||||||
}
|
|
||||||
|
|
||||||
# fkill - kill process
|
|
||||||
fkill() {
|
|
||||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
|
||||||
}
|
|
||||||
|
|
||||||
# fbr - checkout git branch
|
|
||||||
fbr() {
|
|
||||||
local branches branch
|
|
||||||
branches=$(git branch) &&
|
|
||||||
branch=$(echo "$branches" | fzf +s +m) &&
|
|
||||||
git checkout $(echo "$branch" | sed "s/.* //")
|
|
||||||
}
|
|
||||||
|
|
||||||
# fco - checkout git commit
|
|
||||||
fco() {
|
|
||||||
local commits commit
|
|
||||||
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
|
|
||||||
commit=$(echo "$commits" | fzf +s +m -e) &&
|
|
||||||
git checkout $(echo "$commit" | sed "s/ .*//")
|
|
||||||
}
|
|
||||||
|
|
||||||
# ftags - search ctags
|
|
||||||
ftags() {
|
|
||||||
local line
|
|
||||||
[ -e tags ] &&
|
|
||||||
line=$(
|
|
||||||
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
|
|
||||||
cut -c1-80 | fzf --nth=1,2
|
|
||||||
) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \
|
|
||||||
-c "silent tag $(cut -f2 <<< "$line")"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Key bindings for command line
|
Key bindings for command line
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
The install script will setup the following key bindings.
|
The install script will setup the following key bindings for bash, zsh, and
|
||||||
|
fish.
|
||||||
### bash/zsh
|
|
||||||
|
|
||||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||||
- `CTRL-R` - Paste the selected command from history into the command line
|
- `CTRL-R` - Paste the selected command from history into the command line
|
||||||
@@ -223,15 +138,35 @@ If you're on a tmux session, `CTRL-T` will launch fzf in a new split-window. You
|
|||||||
may disable this tmux integration by setting `FZF_TMUX` to 0, or change the
|
may disable this tmux integration by setting `FZF_TMUX` to 0, or change the
|
||||||
height of the window with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
height of the window with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
||||||
|
|
||||||
The source code can be found in `~/.fzf.bash` and in `~/.fzf.zsh`.
|
If you use vi mode on bash, you need to add `set -o vi` *before* `source
|
||||||
|
~/.fzf.bash` in your .bashrc, so that it correctly sets up key bindings for vi
|
||||||
|
mode.
|
||||||
|
|
||||||
Auto-completion
|
If you want to customize the key bindings, consider editing the
|
||||||
---------------
|
installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and
|
||||||
|
`~/.config/fish/functions/fzf_key_bindings.fish`.
|
||||||
|
|
||||||
Disclaimer: *Auto-completion feature is currently experimental, it can change
|
`fzf-tmux` script
|
||||||
over time*
|
-----------------
|
||||||
|
|
||||||
### bash
|
[fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
||||||
|
# (-[udlr]: up/down/left/right)
|
||||||
|
|
||||||
|
# select git branches in horizontal split below (15 lines)
|
||||||
|
git branch | fzf-tmux -d 15
|
||||||
|
|
||||||
|
# select multiple words in vertical split on the left (20% of screen width)
|
||||||
|
cat /usr/share/dict/words | fzf-tmux -l 20% --multi --reverse
|
||||||
|
```
|
||||||
|
|
||||||
|
It will still work even when you're not on tmux, silently ignoring `-[udlr]`
|
||||||
|
options, so you can invariably use `fzf-tmux` in your scripts.
|
||||||
|
|
||||||
|
Fuzzy completion for bash
|
||||||
|
-------------------------
|
||||||
|
|
||||||
#### Files and directories
|
#### Files and directories
|
||||||
|
|
||||||
@@ -282,6 +217,14 @@ ssh **<TAB>
|
|||||||
telnet **<TAB>
|
telnet **<TAB>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Environment variables / Aliases
|
||||||
|
|
||||||
|
```sh
|
||||||
|
unset **<TAB>
|
||||||
|
export **<TAB>
|
||||||
|
unalias **<TAB>
|
||||||
|
```
|
||||||
|
|
||||||
#### Settings
|
#### Settings
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -292,18 +235,12 @@ export FZF_COMPLETION_TRIGGER='~~'
|
|||||||
export FZF_COMPLETION_OPTS='+c -x'
|
export FZF_COMPLETION_OPTS='+c -x'
|
||||||
```
|
```
|
||||||
|
|
||||||
### zsh
|
|
||||||
|
|
||||||
TODO :smiley:
|
|
||||||
|
|
||||||
(Pull requests are appreciated.)
|
|
||||||
|
|
||||||
Usage as Vim plugin
|
Usage as Vim plugin
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
(fzf is a command-line utility, naturally it is only accessible in terminal Vim)
|
(Note: To use fzf in GVim, an external terminal emulator is required.)
|
||||||
|
|
||||||
### `:FZF[!]`
|
#### `:FZF[!]`
|
||||||
|
|
||||||
If you have set up fzf for Vim, `:FZF` command will be added.
|
If you have set up fzf for Vim, `:FZF` command will be added.
|
||||||
|
|
||||||
@@ -325,24 +262,41 @@ If you're on a tmux session, `:FZF` will launch fzf in a new split-window whose
|
|||||||
height can be adjusted with `g:fzf_tmux_height` (default: '40%'). However, the
|
height can be adjusted with `g:fzf_tmux_height` (default: '40%'). However, the
|
||||||
bang version (`:FZF!`) will always start in fullscreen.
|
bang version (`:FZF!`) will always start in fullscreen.
|
||||||
|
|
||||||
### `fzf#run([options])`
|
In GVim, you need an external terminal emulator to start fzf with. `xterm`
|
||||||
|
command is used by default, but you can customize it with `g:fzf_launcher`.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
" This is the default. %s is replaced with fzf command
|
||||||
|
let g:fzf_launcher = 'xterm -e bash -ic %s'
|
||||||
|
|
||||||
|
" Use urxvt instead
|
||||||
|
let g:fzf_launcher = 'urxvt -geometry 120x30 -e sh -c %s'
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're running MacVim on OSX, I recommend you to use iTerm2 as the launcher.
|
||||||
|
Refer to the [this wiki
|
||||||
|
page](https://github.com/junegunn/fzf/wiki/fzf-with-MacVim-and-iTerm2) to see
|
||||||
|
how to set up.
|
||||||
|
|
||||||
|
#### `fzf#run([options])`
|
||||||
|
|
||||||
For more advanced uses, you can call `fzf#run()` function which returns the list
|
For more advanced uses, you can call `fzf#run()` function which returns the list
|
||||||
of the selected items.
|
of the selected items.
|
||||||
|
|
||||||
`fzf#run()` may take an options-dictionary:
|
`fzf#run()` may take an options-dictionary:
|
||||||
|
|
||||||
| Option name | Type | Description |
|
| Option name | Type | Description |
|
||||||
| ----------- | ------------- | ------------------------------------------------------------------- |
|
| -------------------------- | ------------- | ---------------------------------------------------------------- |
|
||||||
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
||||||
| `source` | list | Vim list as input to fzf |
|
| `source` | list | Vim list as input to fzf |
|
||||||
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
||||||
| `sink` | funcref | Reference to function to process each selected item |
|
| `sink` | funcref | Reference to function to process each selected item |
|
||||||
| `options` | string | Options to fzf |
|
| `options` | string | Options to fzf |
|
||||||
| `dir` | string | Working directory |
|
| `dir` | string | Working directory |
|
||||||
| `tmux` | number/string | Use tmux split if possible with the given height (e.g. `20`, `50%`) |
|
| `up`/`down`/`left`/`right` | number/string | Use tmux pane with the given size (e.g. `20`, `50%`) |
|
||||||
|
| `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |
|
||||||
|
|
||||||
#### Examples
|
##### Examples
|
||||||
|
|
||||||
If `sink` option is not given, `fzf#run` will simply return the list.
|
If `sink` option is not given, `fzf#run` will simply return the list.
|
||||||
|
|
||||||
@@ -366,9 +320,10 @@ nnoremap <silent> <Leader>C :call fzf#run({
|
|||||||
\ 'source':
|
\ 'source':
|
||||||
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
||||||
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
||||||
\ 'sink': 'colo',
|
\ 'sink': 'colo',
|
||||||
\ 'options': '+m',
|
\ 'options': '+m',
|
||||||
\ 'tmux': 15
|
\ 'left': 20,
|
||||||
|
\ 'launcher': 'xterm -geometry 20x30 -e bash -ic %s'
|
||||||
\ })<CR>
|
\ })<CR>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -377,29 +332,36 @@ handy mapping that selects an open buffer.
|
|||||||
|
|
||||||
```vim
|
```vim
|
||||||
" List of buffers
|
" List of buffers
|
||||||
function! g:buflist()
|
function! BufList()
|
||||||
redir => ls
|
redir => ls
|
||||||
silent ls
|
silent ls
|
||||||
redir END
|
redir END
|
||||||
return split(ls, '\n')
|
return split(ls, '\n')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! g:bufopen(e)
|
function! BufOpen(e)
|
||||||
execute 'buffer '. matchstr(a:e, '^[ 0-9]*')
|
execute 'buffer '. matchstr(a:e, '^[ 0-9]*')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
||||||
\ 'source': g:buflist(),
|
\ 'source': reverse(BufList()),
|
||||||
\ 'sink': function('g:bufopen'),
|
\ 'sink': function('BufOpen'),
|
||||||
\ 'options': '+m +s',
|
\ 'options': '+m',
|
||||||
\ 'tmux': 15
|
\ 'down': '40%'
|
||||||
\ })<CR>
|
\ })<CR>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
More examples can be found on [the wiki
|
||||||
|
page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).
|
||||||
|
|
||||||
|
#### Articles
|
||||||
|
|
||||||
|
- [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
----
|
----
|
||||||
|
|
||||||
### Rendering issues
|
#### Rendering issues
|
||||||
|
|
||||||
If you have any rendering issues, check the followings:
|
If you have any rendering issues, check the followings:
|
||||||
|
|
||||||
@@ -412,29 +374,116 @@ If you have any rendering issues, check the followings:
|
|||||||
option. And if it solves your problem, I recommend including it in
|
option. And if it solves your problem, I recommend including it in
|
||||||
`FZF_DEFAULT_OPTS` for further convenience.
|
`FZF_DEFAULT_OPTS` for further convenience.
|
||||||
4. If you still have problem, try `--no-256` option or even `--no-color`.
|
4. If you still have problem, try `--no-256` option or even `--no-color`.
|
||||||
5. Ruby 1.9 or above is required for correctly displaying unicode characters.
|
|
||||||
|
|
||||||
### Ranking algorithm
|
#### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`
|
||||||
|
|
||||||
fzf sorts the result first by the length of the matched substring, then by the
|
[ag](https://github.com/ggreer/the_silver_searcher) or
|
||||||
length of the whole string. However it only does so when the number of matches
|
[pt](https://github.com/monochromegane/the_platinum_searcher) will do the
|
||||||
is less than the limit which is by default 1000, in order to avoid the cost of
|
filtering:
|
||||||
sorting a large list and limit the response time of the query.
|
|
||||||
|
|
||||||
This limit can be adjusted with `-s` option, or with the environment variable
|
|
||||||
`FZF_DEFAULT_OPTS`.
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
export FZF_DEFAULT_OPTS="--sort 20000"
|
# Feed the output of ag into fzf
|
||||||
|
ag -l -g "" | fzf
|
||||||
|
|
||||||
|
# Setting ag as the default source for fzf
|
||||||
|
export FZF_DEFAULT_COMMAND='ag -l -g ""'
|
||||||
|
|
||||||
|
# Now fzf (w/o pipe) will use ag instead of find
|
||||||
|
fzf
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `git ls-tree` for fast traversal
|
||||||
|
|
||||||
|
If you're running fzf in a large git repository, `git ls-tree` can boost up the
|
||||||
|
speed of the traversal.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Copy the original fzf function to __fzf
|
||||||
|
declare -f __fzf > /dev/null ||
|
||||||
|
eval "$(echo "__fzf() {"; declare -f fzf | \grep -v '^{' | tail -n +2)"
|
||||||
|
|
||||||
|
# Use git ls-tree when possible
|
||||||
|
fzf() {
|
||||||
|
if [ -n "$(git rev-parse HEAD 2> /dev/null)" ]; then
|
||||||
|
FZF_DEFAULT_COMMAND="git ls-tree -r --name-only HEAD" __fzf "$@"
|
||||||
|
else
|
||||||
|
__fzf "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Using fzf with tmux panes
|
||||||
|
|
||||||
|
The supplied [fzf-tmux](bin/fzf-tmux) script should suffice in most of the
|
||||||
|
cases, but if you want to be able to update command line like the default
|
||||||
|
`CTRL-T` key binding, you'll have to use `send-keys` command of tmux. The
|
||||||
|
following example will show you how it can be done.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# This is a helper function that splits the current pane to start the given
|
||||||
|
# command ($1) and sends its output back to the original pane with any number of
|
||||||
|
# optional keys (shift; $*).
|
||||||
|
fzf_tmux_helper() {
|
||||||
|
[ -n "$TMUX_PANE" ] || return
|
||||||
|
local cmd=$1
|
||||||
|
shift
|
||||||
|
tmux split-window -p 40 \
|
||||||
|
"bash -c \"\$(tmux send-keys -t $TMUX_PANE \"\$(source ~/.fzf.bash; $cmd)\" $*)\""
|
||||||
|
}
|
||||||
|
|
||||||
|
# This is the function we are going to run in the split pane.
|
||||||
|
# - "find" to list the directories
|
||||||
|
# - "sed" will escape spaces in the paths.
|
||||||
|
# - "paste" will join the selected paths into a single line
|
||||||
|
fzf_tmux_dir() {
|
||||||
|
fzf_tmux_helper \
|
||||||
|
'find * -path "*/\.*" -prune -o -type d -print 2> /dev/null |
|
||||||
|
fzf --multi |
|
||||||
|
sed "s/ /\\\\ /g" |
|
||||||
|
paste -sd" " -' Space
|
||||||
|
}
|
||||||
|
|
||||||
|
# Bind CTRL-X-CTRL-D to fzf_tmux_dir
|
||||||
|
bind '"\C-x\C-d": "$(fzf_tmux_dir)\e\C-e"'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Fish shell
|
||||||
|
|
||||||
|
It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)
|
||||||
|
that it doesn't allow reading from STDIN in command substitution, which means
|
||||||
|
simple `vim (fzf)` won't work as expected. The workaround is to store the result
|
||||||
|
of fzf to a temporary file.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
function vimf
|
||||||
|
if fzf > $TMPDIR/fzf.result
|
||||||
|
vim (cat $TMPDIR/fzf.result)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function fe
|
||||||
|
set tmp $TMPDIR/fzf.result
|
||||||
|
fzf --query="$argv[1]" --select-1 --exit-0 > $tmp
|
||||||
|
if [ (cat $tmp | wc -l) -gt 0 ]
|
||||||
|
vim (cat $tmp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 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
|
||||||
-------
|
-------
|
||||||
|
|
||||||
MIT
|
[MIT](LICENSE)
|
||||||
|
|
||||||
Author
|
Author
|
||||||
------
|
------
|
||||||
|
|
||||||
Junegunn Choi
|
Junegunn Choi
|
||||||
|
|
||||||
|
8
Rakefile
8
Rakefile
@@ -2,7 +2,11 @@ require "bundler/gem_tasks"
|
|||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
|
|
||||||
Rake::TestTask.new(:test) do |test|
|
Rake::TestTask.new(:test) do |test|
|
||||||
test.pattern = 'test/**/test_*.rb'
|
test.pattern = 'test/test_go.rb'
|
||||||
test.verbose = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Rake::TestTask.new(:testall) do |test|
|
||||||
|
test.pattern = 'test/test_*.rb'
|
||||||
|
end
|
||||||
|
|
||||||
|
task :default => :test
|
||||||
|
125
bin/fzf-tmux
Executable file
125
bin/fzf-tmux
Executable file
@@ -0,0 +1,125 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# fzf-tmux: starts fzf in a tmux pane
|
||||||
|
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
||||||
|
|
||||||
|
args=()
|
||||||
|
opt=""
|
||||||
|
skip=""
|
||||||
|
swap=""
|
||||||
|
close=""
|
||||||
|
term=""
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
arg="$1"
|
||||||
|
case "$arg" in
|
||||||
|
-)
|
||||||
|
term=1
|
||||||
|
;;
|
||||||
|
-w*|-h*|-d*|-u*|-r*|-l*)
|
||||||
|
if [ -n "$skip" ]; then
|
||||||
|
args+=("$1")
|
||||||
|
shift
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [[ "$arg" =~ ^.[lrw] ]]; then
|
||||||
|
opt="-h"
|
||||||
|
if [[ "$arg" =~ ^.l ]]; then
|
||||||
|
opt="$opt -d"
|
||||||
|
swap="; swap-pane -D ; select-pane -L"
|
||||||
|
close="; tmux swap-pane -D"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
opt=""
|
||||||
|
if [[ "$arg" =~ ^.u ]]; then
|
||||||
|
opt="$opt -d"
|
||||||
|
swap="; swap-pane -D ; select-pane -U"
|
||||||
|
close="; tmux swap-pane -D"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ ${#arg} -gt 2 ]; then
|
||||||
|
size="${arg:2}"
|
||||||
|
else
|
||||||
|
shift
|
||||||
|
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
|
||||||
|
size="$1"
|
||||||
|
else
|
||||||
|
[ -n "$1" -a "$1" != "--" ] && args+=("$1")
|
||||||
|
shift
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$size" =~ %$ ]]; then
|
||||||
|
size=${size:0:((${#size}-1))}
|
||||||
|
if [ -n "$swap" ]; then
|
||||||
|
opt="$opt -p $(( 100 - size ))"
|
||||||
|
else
|
||||||
|
opt="$opt -p $size"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ -n "$swap" ]; then
|
||||||
|
if [[ "$arg" =~ ^.l ]]; then
|
||||||
|
[ -n "$COLUMNS" ] && max=$COLUMNS || max=$(tput cols)
|
||||||
|
else
|
||||||
|
[ -n "$LINES" ] && max=$LINES || max=$(tput lines)
|
||||||
|
fi
|
||||||
|
size=$(( max - size ))
|
||||||
|
[ $size -lt 0 ] && size=0
|
||||||
|
opt="$opt -l $size"
|
||||||
|
else
|
||||||
|
opt="$opt -l $size"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
# "--" can be used to separate fzf-tmux options from fzf options to
|
||||||
|
# avoid conflicts
|
||||||
|
skip=1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
args+=("$1")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z "$TMUX_PANE" ]; then
|
||||||
|
fzf "${args[@]}"
|
||||||
|
exit $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Build arguments to fzf
|
||||||
|
[ ${#args[@]} -gt 0 ] && fzf_args=$(printf '\\"%s\\" ' "${args[@]}"; echo '')
|
||||||
|
|
||||||
|
# Clean up named pipes on exit
|
||||||
|
id=$RANDOM
|
||||||
|
fifo1=/tmp/fzf-fifo1-$id
|
||||||
|
fifo2=/tmp/fzf-fifo2-$id
|
||||||
|
fifo3=/tmp/fzf-fifo3-$id
|
||||||
|
cleanup() {
|
||||||
|
rm -f $fifo1 $fifo2 $fifo3
|
||||||
|
}
|
||||||
|
trap cleanup EXIT SIGINT SIGTERM
|
||||||
|
|
||||||
|
fail() {
|
||||||
|
>&2 echo "$1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
fzf=$(which fzf 2> /dev/null) || fail "fzf executable not found"
|
||||||
|
envs="FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
|
||||||
|
|
||||||
|
mkfifo $fifo2
|
||||||
|
mkfifo $fifo3
|
||||||
|
if [ -n "$term" -o -t 0 ]; then
|
||||||
|
tmux set-window-option -q synchronize-panes off \;\
|
||||||
|
split-window $opt "$envs"' sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
|
||||||
|
else
|
||||||
|
mkfifo $fifo1
|
||||||
|
tmux set-window-option -q synchronize-panes off \;\
|
||||||
|
split-window $opt "$envs"' sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
|
||||||
|
cat <&0 > $fifo1 &
|
||||||
|
fi
|
||||||
|
cat $fifo2
|
||||||
|
[ "$(cat $fifo3)" = '0' ]
|
||||||
|
|
615
fzf
615
fzf
@@ -7,7 +7,9 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# Version: 0.8.3 (April 3, 2014)
|
# Version: 0.8.9 (Dec 24, 2014)
|
||||||
|
# Deprecation alert:
|
||||||
|
# This script is no longer maintained. Use the new Go version.
|
||||||
#
|
#
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
@@ -36,8 +38,14 @@
|
|||||||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'curses'
|
||||||
|
rescue LoadError
|
||||||
|
$stderr.puts 'curses gem is not installed. Try `gem install curses`.'
|
||||||
|
sleep 1
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'curses'
|
|
||||||
require 'set'
|
require 'set'
|
||||||
|
|
||||||
unless String.method_defined? :force_encoding
|
unless String.method_defined? :force_encoding
|
||||||
@@ -48,29 +56,55 @@ unless String.method_defined? :force_encoding
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class String
|
||||||
|
attr_accessor :orig
|
||||||
|
|
||||||
|
def tokenize delim, nth
|
||||||
|
unless delim
|
||||||
|
# AWK default
|
||||||
|
prefix_length = (index(/\S/) || 0) rescue 0
|
||||||
|
tokens = scan(/\S+\s*/) rescue []
|
||||||
|
else
|
||||||
|
prefix_length = 0
|
||||||
|
tokens = scan(delim) rescue []
|
||||||
|
end
|
||||||
|
nth.map { |n|
|
||||||
|
if n.begin == 0 && n.end == -1
|
||||||
|
[prefix_length, tokens.join]
|
||||||
|
elsif part = tokens[n]
|
||||||
|
[prefix_length + (tokens[0...(n.begin)] || []).join.length,
|
||||||
|
part.join]
|
||||||
|
end
|
||||||
|
}.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
||||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
||||||
|
:print_query, :with_nth
|
||||||
|
|
||||||
class AtomicVar
|
def sync
|
||||||
def initialize value
|
@shr_mtx.synchronize { yield }
|
||||||
@value = value
|
end
|
||||||
@mutex = Mutex.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def get
|
def get name
|
||||||
@mutex.synchronize { @value }
|
sync { instance_variable_get name }
|
||||||
end
|
end
|
||||||
|
|
||||||
def set value = nil
|
def geta(*names)
|
||||||
@mutex.synchronize do
|
sync { names.map { |name| instance_variable_get name } }
|
||||||
@value = block_given? ? yield(@value) : value
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_missing sym, *args, &blk
|
def call(name, method, *args)
|
||||||
@mutex.synchronize { @value.send(sym, *args, &blk) }
|
sync { instance_variable_get(name).send(method, *args) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def set name, value = nil
|
||||||
|
sync do
|
||||||
|
instance_variable_set name,
|
||||||
|
(block_given? ? yield(instance_variable_get(name)) : value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -87,7 +121,12 @@ class FZF
|
|||||||
@exit0 = false
|
@exit0 = false
|
||||||
@filter = nil
|
@filter = nil
|
||||||
@nth = nil
|
@nth = nil
|
||||||
|
@with_nth = nil
|
||||||
@delim = nil
|
@delim = nil
|
||||||
|
@reverse = false
|
||||||
|
@prompt = '> '
|
||||||
|
@shr_mtx = Mutex.new
|
||||||
|
@print_query = false
|
||||||
|
|
||||||
argv =
|
argv =
|
||||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||||
@@ -114,6 +153,8 @@ class FZF
|
|||||||
when '--no-black' then @black = false
|
when '--no-black' then @black = false
|
||||||
when '--mouse' then @mouse = true
|
when '--mouse' then @mouse = true
|
||||||
when '--no-mouse' then @mouse = false
|
when '--no-mouse' then @mouse = false
|
||||||
|
when '--reverse' then @reverse = true
|
||||||
|
when '--no-reverse' then @reverse = false
|
||||||
when '+s', '--no-sort' then @sort = nil
|
when '+s', '--no-sort' then @sort = nil
|
||||||
when '-1', '--select-1' then @select1 = true
|
when '-1', '--select-1' then @select1 = true
|
||||||
when '+1', '--no-select-1' then @select1 = false
|
when '+1', '--no-select-1' then @select1 = false
|
||||||
@@ -121,20 +162,24 @@ class FZF
|
|||||||
when '+0', '--no-exit-0' then @exit0 = false
|
when '+0', '--no-exit-0' then @exit0 = false
|
||||||
when '-q', '--query'
|
when '-q', '--query'
|
||||||
usage 1, 'query string required' unless query = argv.shift
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
@query = AtomicVar.new query.dup
|
@query = query
|
||||||
when /^-q(.*)$/, /^--query=(.*)$/
|
when /^-q(.*)$/, /^--query=(.*)$/
|
||||||
@query = AtomicVar.new($1)
|
@query = $1
|
||||||
when '-f', '--filter'
|
when '-f', '--filter'
|
||||||
usage 1, 'query string required' unless query = argv.shift
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
@filter = query
|
@filter = query
|
||||||
when /^-f(.*)$/, /^--filter=(.*)$/
|
when /^-f(.*)$/, /^--filter=(.*)$/
|
||||||
@filter = $1
|
@filter = $1
|
||||||
when '-n', '--nth'
|
when '-n', '--nth'
|
||||||
usage 1, 'field number required' unless nth = argv.shift
|
usage 1, 'field expression required' unless nth = argv.shift
|
||||||
usage 1, 'invalid field number' if nth.to_i == 0
|
|
||||||
@nth = parse_nth nth
|
@nth = parse_nth nth
|
||||||
when /^-n([0-9,-]+)$/, /^--nth=([0-9,-]+)$/
|
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
||||||
@nth = parse_nth $1
|
@nth = parse_nth $1
|
||||||
|
when '--with-nth'
|
||||||
|
usage 1, 'field expression required' unless nth = argv.shift
|
||||||
|
@with_nth = parse_nth nth
|
||||||
|
when /^--with-nth=([0-9,-\.]+)$/
|
||||||
|
@with_nth = parse_nth $1
|
||||||
when '-d', '--delimiter'
|
when '-d', '--delimiter'
|
||||||
usage 1, 'delimiter required' unless delim = argv.shift
|
usage 1, 'delimiter required' unless delim = argv.shift
|
||||||
@delim = FZF.build_delim_regex delim
|
@delim = FZF.build_delim_regex delim
|
||||||
@@ -146,41 +191,66 @@ class FZF
|
|||||||
@sort = sort.to_i
|
@sort = sort.to_i
|
||||||
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||||
@sort = $1.to_i
|
@sort = $1.to_i
|
||||||
|
when '--prompt'
|
||||||
|
usage 1, 'prompt string required' unless prompt = argv.shift
|
||||||
|
@prompt = prompt
|
||||||
|
when /^--prompt=(.*)$/
|
||||||
|
@prompt = $1
|
||||||
|
when '--print-query' then @print_query = true
|
||||||
|
when '--no-print-query' then @print_query = false
|
||||||
when '-e', '--extended-exact' then @extended = :exact
|
when '-e', '--extended-exact' then @extended = :exact
|
||||||
when '+e', '--no-extended-exact' then @extended = nil
|
when '+e', '--no-extended-exact' then @extended = nil
|
||||||
|
when '--tac', '--sync'
|
||||||
|
# XXX
|
||||||
else
|
else
|
||||||
usage 1, "illegal option: #{o}"
|
usage 1, "illegal option: #{o}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@source = source.clone
|
@source = source.clone
|
||||||
@mtx = Mutex.new
|
@evt_mtx = Mutex.new
|
||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@pending = nil
|
@pending = nil
|
||||||
|
@rev_dir = @reverse ? -1 : 1
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
|
||||||
unless @filter
|
unless @filter
|
||||||
@query ||= AtomicVar.new('')
|
# Shared variables: needs protection
|
||||||
@cursor_x = AtomicVar.new(@query.length)
|
@query ||= ''
|
||||||
@matches = AtomicVar.new([])
|
@matches = []
|
||||||
@count = AtomicVar.new(0)
|
@count = 0
|
||||||
@vcursor = AtomicVar.new(0)
|
@xcur = @query.length
|
||||||
@vcursors = AtomicVar.new(Set.new)
|
@ycur = 0
|
||||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
@yoff = 0
|
||||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
@dirty = Set.new
|
||||||
@main = Thread.current
|
@spinner = '-\|/-\|/'.split(//)
|
||||||
@plcount = 0
|
@selects = {} # ordered >= 1.9
|
||||||
|
|
||||||
|
@main = Thread.current
|
||||||
|
@plcount = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_nth nth
|
def parse_nth nth
|
||||||
nth.split(',').map { |n|
|
ranges = nth.split(',').map { |expr|
|
||||||
ni = n.to_i
|
x = proc { usage 1, "invalid field expression: #{expr}" }
|
||||||
usage 1, "invalid field number: #{n}" if ni == 0
|
first, second = expr.split('..', 2)
|
||||||
ni
|
x.call if !first.empty? && first.to_i == 0 ||
|
||||||
|
second && !second.empty? && (second.to_i == 0 || second.include?('.'))
|
||||||
|
|
||||||
|
first = first.empty? ? 1 : first.to_i
|
||||||
|
second = case second
|
||||||
|
when nil then first
|
||||||
|
when '' then -1
|
||||||
|
else second.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
||||||
}
|
}
|
||||||
|
ranges == [0..-1] ? nil : ranges
|
||||||
end
|
end
|
||||||
|
|
||||||
def FZF.build_delim_regex delim
|
def FZF.build_delim_regex delim
|
||||||
@@ -188,21 +258,28 @@ class FZF
|
|||||||
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def burp string, orig = nil
|
||||||
|
@stdout.puts(orig || string.orig || string)
|
||||||
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
if @filter
|
if @filter
|
||||||
start_reader.join
|
start_reader.join
|
||||||
filter_list @new
|
filter_list @new
|
||||||
else
|
else
|
||||||
start_reader
|
start_reader
|
||||||
emit(:key) { q = @query.get; [q, q.length] } unless empty = @query.empty?
|
query = get(:@query)
|
||||||
|
emit(:key) { [query, query.length] } unless empty = query.empty?
|
||||||
if @select1 || @exit0
|
if @select1 || @exit0
|
||||||
start_search do |loaded, matches|
|
start_search do |loaded, matches|
|
||||||
len = empty ? @count.get : matches.length
|
len = empty ? get(:@count) : matches.length
|
||||||
if loaded
|
if loaded
|
||||||
if @select1 && len == 1
|
if @select1 && len == 1
|
||||||
puts empty ? matches.first : matches.first.first
|
puts @query if @print_query
|
||||||
|
burp(empty ? matches.first : matches.first.first)
|
||||||
exit 0
|
exit 0
|
||||||
elsif @exit0 && len == 0
|
elsif @exit0 && len == 0
|
||||||
|
puts @query if @print_query
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -223,6 +300,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_list list
|
def filter_list list
|
||||||
|
puts @filter if @print_query
|
||||||
matches = matcher.match(list, @filter, '', '')
|
matches = matcher.match(list, @filter, '', '')
|
||||||
if @sort && matches.length <= @sort
|
if @sort && matches.length <= @sort
|
||||||
matches = FZF.sort(matches)
|
matches = FZF.sort(matches)
|
||||||
@@ -274,200 +352,99 @@ class FZF
|
|||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
Search
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
search scope (positive or negative integers)
|
for limiting search scope. Each can be a non-zero
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
integer or a range expression ([BEGIN]..[END])
|
||||||
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
Search result
|
Search result
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
+c, --no-color Disable colors
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
|
--reverse Reverse orientation
|
||||||
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
-q, --query=STR Start the finder with the given query
|
-q, --query=STR Start the finder with the given query
|
||||||
-1, --select-1 Automatically select the only match
|
-1, --select-1 Automatically select the only match
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
--print-query Print query as the first line
|
||||||
|
|
||||||
Environment variables
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||||
exit x
|
exit x
|
||||||
end
|
|
||||||
|
|
||||||
case RUBY_PLATFORM
|
|
||||||
when /darwin/
|
|
||||||
module UConv
|
|
||||||
CHOSUNG = 0x1100
|
|
||||||
JUNGSUNG = 0x1161
|
|
||||||
JONGSUNG = 0x11A7
|
|
||||||
CHOSUNGS = 19
|
|
||||||
JUNGSUNGS = 21
|
|
||||||
JONGSUNGS = 28
|
|
||||||
JJCOUNT = JUNGSUNGS * JONGSUNGS
|
|
||||||
NFC_BEGIN = 0xAC00
|
|
||||||
NFC_END = NFC_BEGIN + CHOSUNGS * JUNGSUNGS * JONGSUNGS
|
|
||||||
|
|
||||||
def self.nfd str
|
|
||||||
str.split(//).map do |c|
|
|
||||||
cp = c.ord
|
|
||||||
if cp >= NFC_BEGIN && cp < NFC_END
|
|
||||||
chr = ''
|
|
||||||
idx = cp - NFC_BEGIN
|
|
||||||
cho = CHOSUNG + idx / JJCOUNT
|
|
||||||
jung = JUNGSUNG + (idx % JJCOUNT) / JONGSUNGS
|
|
||||||
jong = JONGSUNG + idx % JONGSUNGS
|
|
||||||
chr << cho << jung
|
|
||||||
chr << jong if jong != JONGSUNG
|
|
||||||
chr
|
|
||||||
else
|
|
||||||
c
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.to_nfc arr
|
|
||||||
[NFC_BEGIN + arr[0] * JJCOUNT +
|
|
||||||
(arr[1] || 0) * JONGSUNGS +
|
|
||||||
(arr[2] || 0)].pack('U*')
|
|
||||||
end
|
|
||||||
|
|
||||||
if String.method_defined?(:each_char)
|
|
||||||
def self.split str
|
|
||||||
str.each_char.to_a
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def self.split str
|
|
||||||
str.split('')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.nfc str, offsets = []
|
|
||||||
ret = ''
|
|
||||||
omap = []
|
|
||||||
pend = []
|
|
||||||
split(str).each_with_index do |c, idx|
|
|
||||||
cp =
|
|
||||||
begin
|
|
||||||
c.ord
|
|
||||||
rescue Exception
|
|
||||||
next
|
|
||||||
end
|
|
||||||
omap << ret.length
|
|
||||||
unless pend.empty?
|
|
||||||
if cp >= JUNGSUNG && cp < JUNGSUNG + JUNGSUNGS
|
|
||||||
pend << cp - JUNGSUNG
|
|
||||||
next
|
|
||||||
elsif cp >= JONGSUNG && cp < JONGSUNG + JONGSUNGS
|
|
||||||
pend << cp - JONGSUNG
|
|
||||||
next
|
|
||||||
else
|
|
||||||
omap[-1] = omap[-1] + 1
|
|
||||||
ret << to_nfc(pend)
|
|
||||||
pend.clear
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if cp >= CHOSUNG && cp < CHOSUNG + CHOSUNGS
|
|
||||||
pend << cp - CHOSUNG
|
|
||||||
else
|
|
||||||
ret << c
|
|
||||||
end
|
|
||||||
end
|
|
||||||
ret << to_nfc(pend) unless pend.empty?
|
|
||||||
return [ret,
|
|
||||||
offsets.map { |pair|
|
|
||||||
b, e = pair
|
|
||||||
[omap[b] || 0, omap[e] || ((omap.last || 0) + 1)] }]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def convert_item item
|
|
||||||
UConv.nfc(*item)
|
|
||||||
end
|
|
||||||
|
|
||||||
class Matcher
|
|
||||||
def query_chars q
|
|
||||||
UConv.nfd(q)
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize q
|
|
||||||
UConv.nfd(q).join
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def convert_item item
|
|
||||||
item
|
|
||||||
end
|
|
||||||
|
|
||||||
class Matcher
|
|
||||||
def query_chars q
|
|
||||||
q.split(//)
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize q
|
|
||||||
q
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def emit event
|
def emit event
|
||||||
@mtx.synchronize do
|
@evt_mtx.synchronize do
|
||||||
@events[event] = yield
|
@events[event] = yield
|
||||||
@cv.broadcast
|
@cv.broadcast
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def max_items; C.lines - 2; end
|
def max_items; C.lines - 2; end
|
||||||
def cursor_y; C.lines - 1; end
|
|
||||||
|
def cursor_y offset = 0
|
||||||
|
@reverse ? (offset) : (C.lines - 1 - offset)
|
||||||
|
end
|
||||||
|
|
||||||
def cprint str, col
|
def cprint str, col
|
||||||
C.attron(col) do
|
C.attron(col) do
|
||||||
addstr_safe str
|
addstr_safe str
|
||||||
end if str
|
end if str
|
||||||
end
|
end
|
||||||
def addstr_safe str
|
def addstr_safe str
|
||||||
C.addstr str.gsub("\0", '')
|
str = str.gsub("\0", '') rescue str
|
||||||
|
C.addstr str
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_input
|
def print_input
|
||||||
C.setpos cursor_y, 0
|
C.setpos cursor_y, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
cprint '> ', color(:prompt, true)
|
cprint @prompt, color(:prompt, true)
|
||||||
C.attron(C::A_BOLD) do
|
C.attron(C::A_BOLD) do
|
||||||
C.addstr @query.get
|
C.addstr get(:@query)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_info msg = nil
|
def print_info msg = nil
|
||||||
C.setpos cursor_y - 1, 0
|
C.setpos cursor_y(1), 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
|
|
||||||
prefix =
|
prefix =
|
||||||
if spinner = @spinner.first
|
if spin_char = call(:@spinner, :first)
|
||||||
cprint spinner, color(:spinner, true)
|
cprint spin_char, color(:spinner, true)
|
||||||
' '
|
' '
|
||||||
else
|
else
|
||||||
' '
|
' '
|
||||||
end
|
end
|
||||||
C.attron color(:info, false) do
|
C.attron color(:info, false) do
|
||||||
C.addstr "#{prefix}#{@matches.length}/#{@count.get}"
|
sync do
|
||||||
if (selected = @selects.length) > 0
|
C.addstr "#{prefix}#{@matches.length}/#{@count}"
|
||||||
C.addstr " (#{selected})"
|
if (selected = @selects.length) > 0
|
||||||
|
C.addstr " (#{selected})"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
C.addstr msg if msg
|
C.addstr msg if msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get])
|
query, xcur = geta(:@query, :@xcur)
|
||||||
|
C.setpos cursor_y, @prompt.length + width(query[0, xcur])
|
||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -550,7 +527,7 @@ class FZF
|
|||||||
width = width str
|
width = width str
|
||||||
diff = 0
|
diff = 0
|
||||||
while width > len
|
while width > len
|
||||||
width -= (left ? str[0, 1] : str[-1, 1]) =~ @@wrx ? 2 : 1
|
width -= ((left ? str[0, 1] : str[-1, 1]) =~ @@wrx ? 2 : 1) rescue 1
|
||||||
str = left ? str[1..-1] : str[0...-1]
|
str = left ? str[1..-1] : str[0...-1]
|
||||||
diff += 1
|
diff += 1
|
||||||
end
|
end
|
||||||
@@ -584,7 +561,6 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def init_screen
|
def init_screen
|
||||||
@stdout = $stdout.clone
|
|
||||||
$stdout.reopen($stderr)
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
C.init_screen
|
C.init_screen
|
||||||
@@ -659,14 +635,28 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
while line = stream.gets
|
if @with_nth
|
||||||
emit(:new) { @new << line.chomp }
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << transform(line) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << line.chomp }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@spinner.clear if @spinner
|
@spinner.clear if @spinner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform line
|
||||||
|
line = line.chomp
|
||||||
|
mut = (line =~ / $/ ? line : line + ' ').
|
||||||
|
tokenize(@delim, @with_nth).map { |e| e.last }.join('').sub(/ *$/, '')
|
||||||
|
mut.orig = line
|
||||||
|
mut
|
||||||
|
end
|
||||||
|
|
||||||
def start_search &callback
|
def start_search &callback
|
||||||
Thread.new do
|
Thread.new do
|
||||||
lists = []
|
lists = []
|
||||||
@@ -677,12 +667,12 @@ class FZF
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
while true
|
while true
|
||||||
@mtx.synchronize do
|
@evt_mtx.synchronize do
|
||||||
while true
|
while true
|
||||||
events.merge! @events
|
events.merge! @events
|
||||||
|
|
||||||
if @events.empty? # No new events
|
if @events.empty? # No new events
|
||||||
@cv.wait @mtx
|
@cv.wait @evt_mtx
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
@events.clear
|
@events.clear
|
||||||
@@ -691,8 +681,8 @@ class FZF
|
|||||||
|
|
||||||
if events[:new]
|
if events[:new]
|
||||||
lists << @new
|
lists << @new
|
||||||
@count.set { |c| c + @new.length }
|
set(:@count) { |c| c + @new.length }
|
||||||
@spinner.set { |spinner|
|
set(:@spinner) { |spinner|
|
||||||
if e = spinner.shift
|
if e = spinner.shift
|
||||||
spinner.push e
|
spinner.push e
|
||||||
end; spinner
|
end; spinner
|
||||||
@@ -716,17 +706,20 @@ class FZF
|
|||||||
cnt = 0
|
cnt = 0
|
||||||
lists.each do |list|
|
lists.each do |list|
|
||||||
cnt += list.length
|
cnt += list.length
|
||||||
skip = @mtx.synchronize { @events[:key] }
|
skip = @evt_mtx.synchronize { @events[:key] }
|
||||||
break if skip
|
break if skip
|
||||||
|
|
||||||
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
if !empty && (progress = 100 * cnt / get(:@count)) < 100 && Time.now - started_at > 0.5
|
||||||
render { print_info " (#{progress}%)" }
|
render { print_info " (#{progress}%)" }
|
||||||
end
|
end
|
||||||
|
|
||||||
found.concat(q.empty? ? list :
|
found.concat(q.empty? ? list :
|
||||||
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
||||||
end
|
end
|
||||||
next if skip
|
if skip
|
||||||
|
sleep 0.1
|
||||||
|
next
|
||||||
|
end
|
||||||
matches = @sort ? found : found.reverse
|
matches = @sort ? found : found.reverse
|
||||||
if !empty && @sort && matches.length <= @sort
|
if !empty && @sort && matches.length <= @sort
|
||||||
matches = FZF.sort(matches)
|
matches = FZF.sort(matches)
|
||||||
@@ -735,7 +728,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Atomic update
|
# Atomic update
|
||||||
@matches.set matches
|
set(:@matches, matches)
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
callback = nil if callback &&
|
callback = nil if callback &&
|
||||||
@@ -754,38 +747,67 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pick
|
def pick
|
||||||
items = @matches[0, max_items]
|
sync do
|
||||||
curr = [0, [@vcursor.get, items.length - 1].min].max
|
item = @matches[@ycur]
|
||||||
[*items.fetch(curr, [])][0]
|
item.is_a?(Array) ? item[0] : item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def constrain offset, cursor, count, height
|
||||||
|
original = [offset, cursor]
|
||||||
|
diffpos = cursor - offset
|
||||||
|
|
||||||
|
# Constrain cursor
|
||||||
|
cursor = [0, [cursor, count - 1].min].max
|
||||||
|
|
||||||
|
# Ceil
|
||||||
|
if cursor > offset + (height - 1)
|
||||||
|
offset = cursor - (height - 1)
|
||||||
|
# Floor
|
||||||
|
elsif offset > cursor
|
||||||
|
offset = cursor
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adjustment
|
||||||
|
if count - offset < height
|
||||||
|
offset = [0, count - height].max
|
||||||
|
cursor = [0, [offset + diffpos, count - 1].min].max
|
||||||
|
end
|
||||||
|
|
||||||
|
[[offset, cursor] != original, offset, cursor]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_list wipe
|
def update_list wipe
|
||||||
render do
|
render do
|
||||||
items = @matches[0, max_items]
|
pos, items = sync {
|
||||||
|
changed, @yoff, @ycur =
|
||||||
|
constrain(@yoff, @ycur, @matches.length, max_items)
|
||||||
|
wipe ||= changed
|
||||||
|
|
||||||
|
[@ycur - @yoff, @matches[@yoff, max_items]]
|
||||||
|
}
|
||||||
|
|
||||||
# Wipe
|
# Wipe
|
||||||
if items.length < @plcount
|
if items.length < @plcount
|
||||||
@plcount.downto(items.length) do |idx|
|
@plcount.downto(items.length) do |idx|
|
||||||
C.setpos cursor_y - idx - 2, 0
|
C.setpos cursor_y(idx + 2), 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@plcount = items.length
|
@plcount = items.length
|
||||||
|
|
||||||
maxc = C.cols - 3
|
dirty = Set[pos]
|
||||||
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }
|
set(:@dirty) do |vs|
|
||||||
cleanse = Set[vcursor]
|
dirty.merge vs
|
||||||
@vcursors.set { |vs|
|
|
||||||
cleanse.merge vs
|
|
||||||
Set.new
|
Set.new
|
||||||
}
|
end
|
||||||
items.each_with_index do |item, idx|
|
items.each_with_index do |item, idx|
|
||||||
next unless wipe || cleanse.include?(idx)
|
next unless wipe || dirty.include?(idx)
|
||||||
row = cursor_y - idx - 2
|
row = cursor_y(idx + 2)
|
||||||
chosen = idx == vcursor
|
chosen = idx == pos
|
||||||
selected = @selects.include?([*item][0])
|
selected = @selects.include?([*item][0])
|
||||||
line, offsets = convert_item item
|
line, offsets = item
|
||||||
tokens = format line, maxc, offsets
|
tokens = format line, C.cols - 3, offsets
|
||||||
print_item row, tokens, chosen, selected
|
print_item row, tokens, chosen, selected
|
||||||
end
|
end
|
||||||
print_info
|
print_info
|
||||||
@@ -814,7 +836,10 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def vselect &prc
|
def vselect &prc
|
||||||
@vcursor.set { |v| @vcursors << v; prc.call v }
|
sync do
|
||||||
|
@dirty << @ycur - @yoff
|
||||||
|
@ycur = prc.call @ycur
|
||||||
|
end
|
||||||
update_list false
|
update_list false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -871,7 +896,13 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_input actions
|
def get_input actions
|
||||||
@tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')
|
@tty ||=
|
||||||
|
begin
|
||||||
|
require 'io/console'
|
||||||
|
IO.console
|
||||||
|
rescue LoadError
|
||||||
|
IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
|
end
|
||||||
|
|
||||||
if pending = @pending
|
if pending = @pending
|
||||||
@pending = nil
|
@pending = nil
|
||||||
@@ -903,7 +934,7 @@ class FZF
|
|||||||
|
|
||||||
ord =
|
ord =
|
||||||
case read_nb(1, :esc)
|
case read_nb(1, :esc)
|
||||||
when 91
|
when 91, 79
|
||||||
case read_nb(1, nil)
|
case read_nb(1, nil)
|
||||||
when 68 then ctrl(:b)
|
when 68 then ctrl(:b)
|
||||||
when 67 then ctrl(:f)
|
when 67 then ctrl(:f)
|
||||||
@@ -918,6 +949,8 @@ class FZF
|
|||||||
case read_nbs
|
case read_nbs
|
||||||
when [59, 50, 68] then ctrl(:a)
|
when [59, 50, 68] then ctrl(:a)
|
||||||
when [59, 50, 67] then ctrl(:e)
|
when [59, 50, 67] then ctrl(:e)
|
||||||
|
when [59, 53, 68] then :alt_b
|
||||||
|
when [59, 53, 67] then :alt_f
|
||||||
when [126] then ctrl(:a)
|
when [126] then ctrl(:a)
|
||||||
end
|
end
|
||||||
when 52 then read_nb; ctrl(:e)
|
when 52 then read_nb; ctrl(:e)
|
||||||
@@ -927,8 +960,10 @@ class FZF
|
|||||||
get_mouse
|
get_mouse
|
||||||
end
|
end
|
||||||
when 'b', 98 then :alt_b
|
when 'b', 98 then :alt_b
|
||||||
|
when 'd', 100 then :alt_d
|
||||||
when 'f', 102 then :alt_f
|
when 'f', 102 then :alt_f
|
||||||
when :esc then :esc
|
when :esc then :esc
|
||||||
|
when 127 then :alt_bs
|
||||||
else next
|
else next
|
||||||
end if ord == 27
|
end if ord == 27
|
||||||
|
|
||||||
@@ -979,16 +1014,35 @@ class FZF
|
|||||||
def start_loop
|
def start_loop
|
||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
input = @query.get.dup
|
input = call(:@query, :dup)
|
||||||
cursor = input.length
|
cursor = input.length
|
||||||
yanked = ''
|
yanked = ''
|
||||||
mouse_event = MouseEvent.new
|
mouse_event = MouseEvent.new
|
||||||
backword = proc {
|
backword = proc {
|
||||||
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
cursor = (input[0, cursor].rindex(/[^[:alnum:]][[:alnum:]]/) || -1) + 1
|
||||||
|
nil
|
||||||
|
}
|
||||||
|
forward = proc {
|
||||||
|
cursor += (input[cursor..-1].index(/([[:alnum:]][^[:alnum:]])|(.$)/) || -1) + 1
|
||||||
|
nil
|
||||||
|
}
|
||||||
|
rubout = proc { |regex|
|
||||||
|
pcursor = cursor
|
||||||
|
cursor = (input[0, cursor].rindex(regex) || -1) + 1
|
||||||
|
if pcursor > cursor
|
||||||
|
yanked = input[cursor...pcursor]
|
||||||
|
input = input[0...cursor] + input[pcursor..-1]
|
||||||
|
end
|
||||||
}
|
}
|
||||||
actions = {
|
actions = {
|
||||||
:esc => proc { exit 1 },
|
:esc => proc { exit 1 },
|
||||||
ctrl(:d) => proc { exit 1 if input.empty? },
|
ctrl(:d) => proc {
|
||||||
|
if input.empty?
|
||||||
|
exit 1
|
||||||
|
elsif cursor < input.length
|
||||||
|
input = input[0...cursor] + input[(cursor + 1)..-1]
|
||||||
|
end
|
||||||
|
},
|
||||||
ctrl(:m) => proc {
|
ctrl(:m) => proc {
|
||||||
got = pick
|
got = pick
|
||||||
exit 0
|
exit 0
|
||||||
@@ -1000,40 +1054,46 @@ class FZF
|
|||||||
},
|
},
|
||||||
ctrl(:a) => proc { cursor = 0; nil },
|
ctrl(:a) => proc { cursor = 0; nil },
|
||||||
ctrl(:e) => proc { cursor = input.length; nil },
|
ctrl(:e) => proc { cursor = input.length; nil },
|
||||||
ctrl(:j) => proc { vselect { |v| v - 1 } },
|
ctrl(:j) => proc { vselect { |v| v - @rev_dir } },
|
||||||
ctrl(:k) => proc { vselect { |v| v + 1 } },
|
ctrl(:k) => proc { vselect { |v| v + @rev_dir } },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc { rubout.call /\s\S/ },
|
||||||
pcursor = cursor
|
|
||||||
backword.call
|
|
||||||
yanked = input[cursor...pcursor] if pcursor > cursor
|
|
||||||
input = input[0...cursor] + input[pcursor..-1]
|
|
||||||
},
|
|
||||||
ctrl(:y) => proc { actions[:default].call yanked },
|
ctrl(:y) => proc { actions[:default].call yanked },
|
||||||
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
ctrl(:i) => proc { |o|
|
ctrl(:i) => proc { |o|
|
||||||
if @multi && sel = pick
|
if @multi && sel = pick
|
||||||
if @selects.has_key? sel
|
sync do
|
||||||
@selects.delete sel
|
if @selects.has_key? sel
|
||||||
else
|
@selects.delete sel
|
||||||
@selects[sel] = 1
|
else
|
||||||
|
@selects[sel] = sel.orig
|
||||||
|
end
|
||||||
end
|
end
|
||||||
vselect { |v| v + case o
|
vselect { |v| v + case o
|
||||||
when :stab then 1
|
when :stab then 1
|
||||||
when :sclick then 0
|
when :sclick then 0
|
||||||
else -1
|
else -1
|
||||||
end }
|
end * @rev_dir }
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||||
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
||||||
:del => proc { input[cursor] = '' if input.length > cursor },
|
:del => proc { input[cursor] = '' if input.length > cursor },
|
||||||
:pgup => proc { vselect { |_| max_items } },
|
:pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } },
|
||||||
:pgdn => proc { vselect { |_| 0 } },
|
:pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } },
|
||||||
:alt_b => proc { backword.call; nil },
|
:alt_bs => proc { rubout.call /[^[:alnum:]][[:alnum:]]/ },
|
||||||
:alt_f => proc {
|
:alt_b => proc { backword.call },
|
||||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
:alt_d => proc {
|
||||||
nil
|
pcursor = cursor
|
||||||
|
forward.call
|
||||||
|
if cursor > pcursor
|
||||||
|
yanked = input[pcursor...cursor]
|
||||||
|
input = input[0...pcursor] + input[cursor..-1]
|
||||||
|
cursor = pcursor
|
||||||
|
end
|
||||||
|
},
|
||||||
|
:alt_f => proc {
|
||||||
|
forward.call
|
||||||
},
|
},
|
||||||
:default => proc { |val|
|
:default => proc { |val|
|
||||||
case val
|
case val
|
||||||
@@ -1045,10 +1105,11 @@ class FZF
|
|||||||
case event
|
case event
|
||||||
when :click, :release
|
when :click, :release
|
||||||
x, y, shift = val.values_at :x, :y, :shift
|
x, y, shift = val.values_at :x, :y, :shift
|
||||||
if y == cursor_y
|
y = @reverse ? (C.lines - 1 - y) : y
|
||||||
cursor = [0, [input.length, x - 2].min].max
|
if y == C.lines - 1
|
||||||
|
cursor = [0, [input.length, x - @prompt.length].min].max
|
||||||
elsif x > 1 && y <= max_items
|
elsif x > 1 && y <= max_items
|
||||||
tv = max_items - y - 1
|
tv = get(:@yoff) + max_items - y - 1
|
||||||
|
|
||||||
case event
|
case event
|
||||||
when :click
|
when :click
|
||||||
@@ -1066,6 +1127,7 @@ class FZF
|
|||||||
actions[ctrl(:i)].call(:sclick) if shift
|
actions[ctrl(:i)].call(:sclick) if shift
|
||||||
actions[ctrl(diff > 0 ? :j : :k)].call
|
actions[ctrl(diff > 0 ? :j : :k)].call
|
||||||
end
|
end
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1076,24 +1138,26 @@ class FZF
|
|||||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||||
|
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
set(:@xcur, cursor)
|
||||||
render { print_input }
|
render { print_input }
|
||||||
|
|
||||||
if key = get_input(actions)
|
if key = get_input(actions)
|
||||||
upd = actions.fetch(key, actions[:default]).call(key)
|
upd = actions.fetch(key, actions[:default]).call(key)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
emit(:key) { [set(:@query, input.dup), cursor] } if upd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
|
q, selects = geta(:@query, :@selects)
|
||||||
|
@stdout.puts q if @print_query
|
||||||
if got
|
if got
|
||||||
if @selects.empty?
|
if selects.empty?
|
||||||
@stdout.puts got
|
burp got
|
||||||
else
|
else
|
||||||
@selects.each do |sel, _|
|
selects.each do |sel, orig|
|
||||||
@stdout.puts sel
|
burp sel, orig
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1112,32 +1176,21 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize nth, delim
|
def initialize nth, delim
|
||||||
@nth = nth && nth.map { |n| n > 0 ? n - 1 : n }
|
@nth = nth
|
||||||
@delim = delim
|
@delim = delim
|
||||||
@tokens_cache = {}
|
@tokens_cache = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def tokenize str
|
def tokenize str
|
||||||
@tokens_cache[str] ||=
|
@tokens_cache[str] ||= str.tokenize(@delim, @nth)
|
||||||
unless @delim
|
|
||||||
# AWK default
|
|
||||||
prefix_length = str[/^\s+/].length rescue 0
|
|
||||||
[prefix_length, (str.strip.scan(/\S+\s*/) rescue [])]
|
|
||||||
else
|
|
||||||
prefix_length = 0
|
|
||||||
[prefix_length, (str.scan(@delim) rescue [])]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_match str, pat
|
def do_match str, pat
|
||||||
if @nth
|
if @nth
|
||||||
prefix_length, tokens = tokenize str
|
tokenize(str).each do |pair|
|
||||||
|
prefix_length, token = pair
|
||||||
@nth.each do |n|
|
if md = token.match(pat) rescue nil
|
||||||
if (token = tokens[n]) && (md = token.match(pat) rescue nil)
|
return MatchData.new(md.offset(0).map { |o| o + prefix_length })
|
||||||
prefix_length += (tokens[0...n] || []).join.length
|
|
||||||
offset = md.offset(0).map { |o| o + prefix_length }
|
|
||||||
return MatchData.new(offset)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
@@ -1168,7 +1221,7 @@ class FZF
|
|||||||
def fuzzy_regex q
|
def fuzzy_regex q
|
||||||
@regexp[q] ||= begin
|
@regexp[q] ||= begin
|
||||||
q = q.downcase if @rxflag == Regexp::IGNORECASE
|
q = q.downcase if @rxflag == Regexp::IGNORECASE
|
||||||
Regexp.new(query_chars(q).inject('') { |sum, e|
|
Regexp.new(q.split(//).inject('') { |sum, e|
|
||||||
e = Regexp.escape e
|
e = Regexp.escape e
|
||||||
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
||||||
"#{e}[^#{e}]*?")
|
"#{e}[^#{e}]*?")
|
||||||
@@ -1226,7 +1279,7 @@ class FZF
|
|||||||
when ''
|
when ''
|
||||||
nil
|
nil
|
||||||
when /^\^(.*)\$$/
|
when /^\^(.*)\$$/
|
||||||
Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag_for(w))
|
Regexp.new('^' << Regexp.escape($1) << '$', rxflag_for(w))
|
||||||
when /^'/
|
when /^'/
|
||||||
if @mode == :fuzzy && w.length > 1
|
if @mode == :fuzzy && w.length > 1
|
||||||
exact_regex w[1..-1]
|
exact_regex w[1..-1]
|
||||||
@@ -1235,10 +1288,10 @@ class FZF
|
|||||||
end
|
end
|
||||||
when /^\^/
|
when /^\^/
|
||||||
w.length > 1 ?
|
w.length > 1 ?
|
||||||
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
Regexp.new('^' << Regexp.escape(w[1..-1]), rxflag_for(w)) : nil
|
||||||
when /\$$/
|
when /\$$/
|
||||||
w.length > 1 ?
|
w.length > 1 ?
|
||||||
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag_for(w)) : nil
|
Regexp.new(Regexp.escape(w[0..-2]) << '$', rxflag_for(w)) : nil
|
||||||
else
|
else
|
||||||
@mode == :fuzzy ? fuzzy_regex(w) : exact_regex(w)
|
@mode == :fuzzy ? fuzzy_regex(w) : exact_regex(w)
|
||||||
end, invert ]
|
end, invert ]
|
||||||
@@ -1246,7 +1299,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def exact_regex w
|
def exact_regex w
|
||||||
Regexp.new(sanitize(Regexp.escape(w)), rxflag_for(w))
|
Regexp.new(Regexp.escape(w), rxflag_for(w))
|
||||||
end
|
end
|
||||||
|
|
||||||
def match list, q, prefix, suffix
|
def match list, q, prefix, suffix
|
||||||
|
@@ -8,19 +8,36 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
|
_fzf_orig_completion_filter() {
|
||||||
|
sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |
|
||||||
|
sed 's/[^a-z0-9_= ;]/_/g'
|
||||||
|
}
|
||||||
|
|
||||||
_fzf_opts_completion() {
|
_fzf_opts_completion() {
|
||||||
local cur prev opts
|
local cur opts
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
opts="
|
||||||
opts="-m --multi -x --extended -s --sort +s +i +c --no-color"
|
-x --extended
|
||||||
|
-e --extended-exact
|
||||||
case "${prev}" in
|
-i +i
|
||||||
--sort|-s)
|
-n --nth
|
||||||
COMPREPLY=( $(compgen -W "$(seq 2000 1000 10000)" -- ${cur}) )
|
-d --delimiter
|
||||||
return 0
|
+s --no-sort
|
||||||
;;
|
--tac
|
||||||
esac
|
-m --multi
|
||||||
|
--no-mouse
|
||||||
|
+c --no-color
|
||||||
|
+2 --no-256
|
||||||
|
--black
|
||||||
|
--reverse
|
||||||
|
--prompt
|
||||||
|
-q --query
|
||||||
|
-1 --select-1
|
||||||
|
-0 --exit-0
|
||||||
|
-f --filter
|
||||||
|
--print-query
|
||||||
|
--sync"
|
||||||
|
|
||||||
if [[ ${cur} =~ ^-|\+ ]]; then
|
if [[ ${cur} =~ ^-|\+ ]]; then
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
@@ -30,8 +47,26 @@ _fzf_opts_completion() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_generic_completion() {
|
_fzf_handle_dynamic_completion() {
|
||||||
local cur base dir leftover matches trigger
|
local cmd orig ret
|
||||||
|
cmd="$1"
|
||||||
|
shift
|
||||||
|
|
||||||
|
orig=$(eval "echo \$_fzf_orig_completion_$cmd")
|
||||||
|
if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then
|
||||||
|
$orig "$@"
|
||||||
|
elif [ -n "$_fzf_completion_loader" ]; then
|
||||||
|
_completion_loader "$@"
|
||||||
|
ret=$?
|
||||||
|
eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)
|
||||||
|
source $BASH_SOURCE
|
||||||
|
return $ret
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_path_completion() {
|
||||||
|
local cur base dir leftover matches trigger cmd
|
||||||
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
@@ -46,8 +81,8 @@ _fzf_generic_completion() {
|
|||||||
leftover=${leftover/#\/}
|
leftover=${leftover/#\/}
|
||||||
[ "$dir" = './' ] && dir=''
|
[ "$dir" = './' ] && dir=''
|
||||||
tput sc
|
tput sc
|
||||||
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
||||||
printf '%q ' "$item"
|
printf "%q$3 " "$item"
|
||||||
done)
|
done)
|
||||||
matches=${matches% }
|
matches=${matches% }
|
||||||
if [ -n "$matches" ]; then
|
if [ -n "$matches" ]; then
|
||||||
@@ -61,25 +96,54 @@ _fzf_generic_completion() {
|
|||||||
dir=$(dirname "$dir")
|
dir=$(dirname "$dir")
|
||||||
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
||||||
done
|
done
|
||||||
|
else
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
_fzf_handle_dynamic_completion "$cmd" "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_list_completion() {
|
||||||
|
local cur selected trigger cmd src
|
||||||
|
read -r src
|
||||||
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
if [[ ${cur} == *"$trigger" ]]; then
|
||||||
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
tput sc
|
||||||
|
selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
||||||
|
selected=${selected% }
|
||||||
|
tput rc
|
||||||
|
|
||||||
|
if [ -n "$selected" ]; then
|
||||||
|
COMPREPLY=("$selected")
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
shift
|
||||||
|
_fzf_handle_dynamic_completion "$cmd" "$@"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_all_completion() {
|
||||||
_fzf_generic_completion \
|
_fzf_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_generic_completion \
|
_fzf_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_generic_completion \
|
_fzf_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_kill_completion() {
|
||||||
@@ -97,64 +161,69 @@ _fzf_kill_completion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_telnet_completion() {
|
||||||
local cur selected trigger
|
_fzf_list_completion '+m' "$@" << "EOF"
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
EOF
|
||||||
[[ ${cur} == *"$trigger" ]] || return 1
|
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
|
||||||
|
|
||||||
tput sc
|
|
||||||
selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
|
||||||
tput rc
|
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
|
||||||
COMPREPLY=("$selected")
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_ssh_completion() {
|
||||||
local cur selected trigger
|
_fzf_list_completion '+m' "$@" << "EOF"
|
||||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts) | awk '{print $2}' | sort -u
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
EOF
|
||||||
[[ ${cur} == *"$trigger" ]] || return 1
|
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
|
||||||
|
|
||||||
tput sc
|
|
||||||
selected=$(cat \
|
|
||||||
<(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | grep -i ^host) \
|
|
||||||
<(grep -v '^\s*\(#\|$\)' /etc/hosts) | \
|
|
||||||
awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
|
||||||
tput rc
|
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
|
||||||
COMPREPLY=("$selected")
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_fzf_env_var_completion() {
|
||||||
|
_fzf_list_completion '-m' "$@" << "EOF"
|
||||||
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_alias_completion() {
|
||||||
|
_fzf_list_completion '-m' "$@" << "EOF"
|
||||||
|
alias | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# fzf options
|
||||||
complete -F _fzf_opts_completion fzf
|
complete -F _fzf_opts_completion fzf
|
||||||
|
|
||||||
# Directory
|
d_cmds="cd pushd rmdir"
|
||||||
for cmd in "cd pushd rmdir"; do
|
f_cmds="
|
||||||
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
|
||||||
done
|
|
||||||
|
|
||||||
# File
|
|
||||||
for cmd in "
|
|
||||||
awk cat diff diff3
|
awk cat diff diff3
|
||||||
emacs ex file ftp g++ gcc gvim head hg java
|
emacs ex file ftp g++ gcc gvim head hg java
|
||||||
javac ld less more mvim patch perl python ruby
|
javac ld less more mvim patch perl python ruby
|
||||||
sed sftp sort source tail tee uniq vi view vim wc"; do
|
sed sftp sort source tail tee uniq vi view vim wc"
|
||||||
|
a_cmds="
|
||||||
|
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||||
|
find git grep gunzip gzip hg jar
|
||||||
|
ln ls mv open rm rsync scp
|
||||||
|
svn tar unzip zip"
|
||||||
|
x_cmds="kill ssh telnet unset unalias export"
|
||||||
|
|
||||||
|
# Preserve existing completion
|
||||||
|
if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then
|
||||||
|
# Really wish I could use associative array but OSX comes with bash 3.2 :(
|
||||||
|
eval $(complete | \grep '\-F' | \grep -v _fzf_ |
|
||||||
|
\grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
|
||||||
|
export _fzf_completion_loaded=0.8.6-1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if type _completion_loader > /dev/null 2>&1; then
|
||||||
|
_fzf_completion_loader=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Directory
|
||||||
|
for cmd in $d_cmds; do
|
||||||
|
complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd
|
||||||
|
done
|
||||||
|
|
||||||
|
# File
|
||||||
|
for cmd in $f_cmds; do
|
||||||
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
# Anything
|
# Anything
|
||||||
for cmd in "
|
for cmd in $a_cmds; do
|
||||||
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
|
||||||
find git grep gunzip gzip hg jar
|
|
||||||
ln ls mv open rm rsync scp
|
|
||||||
svn tar unzip zip"; do
|
|
||||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -165,3 +234,9 @@ complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
|||||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
||||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
||||||
|
|
||||||
|
# Environment variables / Aliases
|
||||||
|
complete -F _fzf_env_var_completion -o default -o bashdefault unset
|
||||||
|
complete -F _fzf_env_var_completion -o default -o bashdefault export
|
||||||
|
complete -F _fzf_alias_completion -o default -o bashdefault unalias
|
||||||
|
|
||||||
|
unset cmd d_cmds f_cmds a_cmds x_cmds
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
Gem::Specification.new do |spec|
|
Gem::Specification.new do |spec|
|
||||||
spec.name = 'fzf'
|
spec.name = 'fzf'
|
||||||
spec.version = '0.8.3'
|
spec.version = '0.8.4'
|
||||||
spec.authors = ['Junegunn Choi']
|
spec.authors = ['Junegunn Choi']
|
||||||
spec.email = ['junegunn.c@gmail.com']
|
spec.email = ['junegunn.c@gmail.com']
|
||||||
spec.description = %q{Fuzzy finder for your shell}
|
spec.description = %q{Fuzzy finder for your shell}
|
||||||
|
402
install
402
install
@@ -1,71 +1,157 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
cd `dirname $BASH_SOURCE`
|
version=0.9.4
|
||||||
fzf_base=`pwd`
|
|
||||||
|
|
||||||
# ruby executable
|
cd $(dirname $BASH_SOURCE)
|
||||||
echo -n "Checking Ruby executable ... "
|
fzf_base=$(pwd)
|
||||||
ruby=`which ruby`
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "ruby executable not found!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# System ruby is preferred
|
ask() {
|
||||||
curses_check="begin; require 'curses'; rescue Exception; exit 1; end"
|
read -p "$1 ([y]/n) " -n 1 -r
|
||||||
system_ruby=/usr/bin/ruby
|
echo
|
||||||
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||||
$system_ruby --disable-gems -e "$curses_check" 2> /dev/null
|
}
|
||||||
[ $? -eq 0 ] && ruby=$system_ruby
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "OK ($ruby)"
|
check_binary() {
|
||||||
|
echo -n " - Checking fzf executable ... "
|
||||||
|
local output=$("$fzf_base"/bin/fzf --version 2>&1)
|
||||||
|
if [ "$version" = "$output" ]; then
|
||||||
|
echo "$output"
|
||||||
|
binary_error=""
|
||||||
|
else
|
||||||
|
echo "$output != $version"
|
||||||
|
rm -f "$fzf_base"/bin/fzf
|
||||||
|
binary_error="Invalid binary"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Curses-support
|
symlink() {
|
||||||
echo -n "Checking Curses support ... "
|
echo " - Creating symlink: bin/$1 -> bin/fzf"
|
||||||
"$ruby" -e "$curses_check"
|
(cd "$fzf_base"/bin &&
|
||||||
if [ $? -eq 0 ]; then
|
rm -f fzf &&
|
||||||
echo "OK"
|
ln -sf $1 fzf)
|
||||||
else
|
|
||||||
echo "Not found"
|
|
||||||
echo "Installing 'curses' gem ... "
|
|
||||||
/usr/bin/env gem install curses -v 1.0.0 --user-install
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo
|
binary_error="Failed to create symlink"
|
||||||
echo "Failed to install 'curses' gem."
|
return 1
|
||||||
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
fi
|
||||||
echo "Make sure that base-devel package group is installed."
|
}
|
||||||
fi
|
|
||||||
|
download() {
|
||||||
|
echo "Downloading bin/fzf ..."
|
||||||
|
if [[ ! $1 =~ dev && -x "$fzf_base"/bin/fzf ]]; then
|
||||||
|
echo " - Already exists"
|
||||||
|
check_binary && return
|
||||||
|
elif [ -x "$fzf_base"/bin/$1 ]; then
|
||||||
|
symlink $1 && check_binary && return
|
||||||
|
fi
|
||||||
|
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
binary_error="Failed to create bin directory"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
|
||||||
|
if which curl > /dev/null; then
|
||||||
|
curl -fL $url | tar -xz
|
||||||
|
elif which wget > /dev/null; then
|
||||||
|
wget -O - $url | tar -xz
|
||||||
|
else
|
||||||
|
binary_error="curl or wget not found"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f $1 ]; then
|
||||||
|
binary_error="Failed to download ${1}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
chmod +x $1 && symlink $1 && check_binary
|
||||||
|
}
|
||||||
|
|
||||||
|
# Try to download binary executable
|
||||||
|
archi=$(uname -sm)
|
||||||
|
binary_available=1
|
||||||
|
binary_error=""
|
||||||
|
case "$archi" in
|
||||||
|
Darwin\ x86_64) download fzf-$version-darwin_amd64 ;;
|
||||||
|
Darwin\ i*86) download fzf-$version-darwin_386 ;;
|
||||||
|
Linux\ x86_64) download fzf-$version-linux_amd64 ;;
|
||||||
|
Linux\ i*86) download fzf-$version-linux_386 ;;
|
||||||
|
*) binary_available=0 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
cd "$fzf_base"
|
||||||
|
if [ -n "$binary_error" ]; then
|
||||||
|
if [ $binary_available -eq 0 ]; then
|
||||||
|
echo "No prebuilt binary for $archi ... "
|
||||||
|
else
|
||||||
|
echo " - $binary_error !!!"
|
||||||
|
fi
|
||||||
|
echo "Installing legacy Ruby version ..."
|
||||||
|
|
||||||
|
# ruby executable
|
||||||
|
echo -n "Checking Ruby executable ... "
|
||||||
|
ruby=`which ruby`
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ruby executable not found !!!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
# Ruby version
|
# System ruby is preferred
|
||||||
echo -n "Checking Ruby version ... "
|
system_ruby=/usr/bin/ruby
|
||||||
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||||
if [ $? -eq 0 ]; then
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
||||||
echo ">= 1.9"
|
[ $? -eq 0 ] && ruby=$system_ruby
|
||||||
"$ruby" --disable-gems -e "$curses_check"
|
fi
|
||||||
|
|
||||||
|
echo "OK ($ruby)"
|
||||||
|
|
||||||
|
# Curses-support
|
||||||
|
echo -n "Checking Curses support ... "
|
||||||
|
"$ruby" -rcurses -e0 2> /dev/null
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
echo "OK"
|
||||||
else
|
else
|
||||||
|
echo "Not found"
|
||||||
|
echo "Installing 'curses' gem ... "
|
||||||
|
if (( EUID )); then
|
||||||
|
/usr/bin/env gem install curses --user-install
|
||||||
|
else
|
||||||
|
/usr/bin/env gem install curses
|
||||||
|
fi
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo
|
||||||
|
echo "Failed to install 'curses' gem."
|
||||||
|
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
||||||
|
echo "Make sure that base-devel package group is installed."
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ruby version
|
||||||
|
echo -n "Checking Ruby version ... "
|
||||||
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo ">= 1.9"
|
||||||
|
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
||||||
|
else
|
||||||
|
fzf_cmd="$ruby $fzf_base/fzf"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "< 1.9"
|
||||||
fzf_cmd="$ruby $fzf_base/fzf"
|
fzf_cmd="$ruby $fzf_base/fzf"
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
echo "< 1.9"
|
|
||||||
fzf_cmd="$ruby $fzf_base/fzf"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
ask "Do you want to add auto-completion support?"
|
||||||
echo
|
|
||||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
|
||||||
auto_completion=$?
|
auto_completion=$?
|
||||||
|
|
||||||
# Key-bindings
|
# Key-bindings
|
||||||
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
|
ask "Do you want to add key bindings?"
|
||||||
echo
|
|
||||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
|
||||||
key_bindings=$?
|
key_bindings=$?
|
||||||
|
|
||||||
echo
|
echo
|
||||||
@@ -73,12 +159,13 @@ for shell in bash zsh; do
|
|||||||
echo -n "Generate ~/.fzf.$shell ... "
|
echo -n "Generate ~/.fzf.$shell ... "
|
||||||
src=~/.fzf.${shell}
|
src=~/.fzf.${shell}
|
||||||
|
|
||||||
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/fzf-completion.${shell}\""
|
||||||
if [ $auto_completion -ne 0 ]; then
|
if [ $auto_completion -ne 0 ]; then
|
||||||
fzf_completion="# $fzf_completion"
|
fzf_completion="# $fzf_completion"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat > $src << EOF
|
if [ -n "$binary_error" ]; then
|
||||||
|
cat > $src << EOF
|
||||||
# Setup fzf function
|
# Setup fzf function
|
||||||
# ------------------
|
# ------------------
|
||||||
unalias fzf 2> /dev/null
|
unalias fzf 2> /dev/null
|
||||||
@@ -89,9 +176,25 @@ export -f fzf > /dev/null
|
|||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
# ---------------
|
# ---------------
|
||||||
[[ \$- =~ i ]] && $fzf_completion
|
$fzf_completion
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
else
|
||||||
|
cat > $src << EOF
|
||||||
|
# Setup fzf
|
||||||
|
# ---------
|
||||||
|
unalias fzf 2> /dev/null
|
||||||
|
unset fzf 2> /dev/null
|
||||||
|
if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then
|
||||||
|
export PATH="$fzf_base/bin:\$PATH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Auto-completion
|
||||||
|
# ---------------
|
||||||
|
$fzf_completion
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
if [ $key_bindings -eq 0 ]; then
|
if [ $key_bindings -eq 0 ]; then
|
||||||
if [ $shell = bash ]; then
|
if [ $shell = bash ]; then
|
||||||
@@ -99,10 +202,10 @@ EOF
|
|||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
__fsel() {
|
__fsel() {
|
||||||
find * -path '*/\.*' -prune \
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
|
||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done
|
done
|
||||||
echo
|
echo
|
||||||
@@ -123,13 +226,14 @@ __fsel_tmux() {
|
|||||||
|
|
||||||
__fcd() {
|
__fcd() {
|
||||||
local dir
|
local dir
|
||||||
dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir"
|
dir=$(command find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||||||
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m) && printf 'cd %q' "$dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
__use_tmux=0
|
__use_tmux=0
|
||||||
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
||||||
|
|
||||||
if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
if [ -z "$(set -o | \grep '^vi.*on')" ]; then
|
||||||
# Required to refresh the prompt after fzf
|
# Required to refresh the prompt after fzf
|
||||||
bind '"\er": redraw-current-line'
|
bind '"\er": redraw-current-line'
|
||||||
|
|
||||||
@@ -137,11 +241,11 @@ if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
|||||||
if [ $__use_tmux -eq 1 ]; then
|
if [ $__use_tmux -eq 1 ]; then
|
||||||
bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
|
bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
|
||||||
else
|
else
|
||||||
bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
||||||
@@ -154,31 +258,33 @@ else
|
|||||||
if [ $__use_tmux -eq 1 ]; then
|
if [ $__use_tmux -eq 1 ]; then
|
||||||
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
||||||
else
|
else
|
||||||
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r"'
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
||||||
fi
|
fi
|
||||||
|
bind -m vi-command '"\C-t": "i\C-t"'
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
||||||
|
bind -m vi-command '"\C-r": "i\C-r"'
|
||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
||||||
|
bind -m vi-command '"\ec": "i\ec"'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset __use_tmux
|
unset __use_tmux
|
||||||
|
|
||||||
fi
|
fi
|
||||||
EOFZF
|
EOFZF
|
||||||
else
|
else # zsh
|
||||||
cat >> $src << "EOFZF"
|
cat >> $src << "EOFZF"
|
||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
# 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() {
|
||||||
set -o nonomatch
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||||||
find * -path '*/\.*' -prune \
|
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
|
||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done
|
done
|
||||||
echo
|
echo
|
||||||
@@ -199,7 +305,7 @@ if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
fzf-file-widget() {
|
fzf-file-widget() {
|
||||||
LBUFFER="${LBUFFER%% #}$(__fsel)"
|
LBUFFER="${LBUFFER}$(__fsel)"
|
||||||
zle redisplay
|
zle redisplay
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
@@ -208,8 +314,8 @@ bindkey '^T' fzf-file-widget
|
|||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
fzf-cd-widget() {
|
fzf-cd-widget() {
|
||||||
cd "${$(set -o nonomatch; find * -path '*/\.*' -prune \
|
cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}"
|
||||||
zle reset-prompt
|
zle reset-prompt
|
||||||
}
|
}
|
||||||
zle -N fzf-cd-widget
|
zle -N fzf-cd-widget
|
||||||
@@ -217,7 +323,7 @@ bindkey '\ec' fzf-cd-widget
|
|||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
fzf-history-widget() {
|
fzf-history-widget() {
|
||||||
LBUFFER=$(fc -l 1 | fzf +s | sed "s/ *[0-9]* *//")
|
LBUFFER=$(fc -l 1 | fzf +s --tac +m -n2..,.. | sed "s/ *[0-9*]* *//")
|
||||||
zle redisplay
|
zle redisplay
|
||||||
}
|
}
|
||||||
zle -N fzf-history-widget
|
zle -N fzf-history-widget
|
||||||
@@ -231,29 +337,161 @@ EOFZF
|
|||||||
echo "OK"
|
echo "OK"
|
||||||
done
|
done
|
||||||
|
|
||||||
echo
|
# fish
|
||||||
for shell in bash zsh; do
|
has_fish=0
|
||||||
rc=~/.${shell}rc
|
if [ -n "$(which fish)" ]; then
|
||||||
src="source ~/.fzf.${shell}"
|
has_fish=1
|
||||||
|
echo -n "Generate ~/.config/fish/functions/fzf.fish ... "
|
||||||
echo "Update $rc:"
|
mkdir -p ~/.config/fish/functions
|
||||||
echo " - $src"
|
if [ -n "$binary_error" ]; then
|
||||||
line=$(grep -nF "$src" $rc | sed 's/:.*//')
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
||||||
if [ -n "$line" ]; then
|
function fzf
|
||||||
echo " - Already exists (line #$line)"
|
$fzf_cmd \$argv
|
||||||
|
end
|
||||||
|
EOFZF
|
||||||
else
|
else
|
||||||
echo $src >> $rc
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
||||||
echo " - Added"
|
function fzf
|
||||||
|
$fzf_base/bin/fzf \$argv
|
||||||
|
end
|
||||||
|
EOFZF
|
||||||
|
fi
|
||||||
|
echo "OK"
|
||||||
|
|
||||||
|
if [ $key_bindings -eq 0 ]; then
|
||||||
|
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
||||||
|
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
||||||
|
function fzf_key_bindings
|
||||||
|
# Due to a bug of fish, we cannot use command substitution,
|
||||||
|
# so we use temporary file instead
|
||||||
|
if [ -z "$TMPDIR" ]
|
||||||
|
set -g TMPDIR /tmp
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_list
|
||||||
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type d -print \
|
||||||
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3-
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_list_dir
|
||||||
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \
|
||||||
|
-prune -o -type d -print 2> /dev/null | sed 1d | cut -b3-
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_escape
|
||||||
|
while read item
|
||||||
|
echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_t
|
||||||
|
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
||||||
|
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
||||||
|
else
|
||||||
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||||
|
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||||
|
commandline -f repaint
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_t_tmux
|
||||||
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||||
|
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_reverse
|
||||||
|
if which tac > /dev/null
|
||||||
|
tac $argv
|
||||||
|
else
|
||||||
|
tail -r $argv
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_r
|
||||||
|
history | __fzf_reverse | fzf +s --tac +m > $TMPDIR/fzf.result
|
||||||
|
and commandline (cat $TMPDIR/fzf.result)
|
||||||
|
commandline -f repaint
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_alt_c
|
||||||
|
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
||||||
|
__fzf_list_dir | fzf +m > $TMPDIR/fzf.result
|
||||||
|
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
||||||
|
and cd (cat $TMPDIR/fzf.result)
|
||||||
|
commandline -f repaint
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_tmux_height
|
||||||
|
if set -q FZF_TMUX_HEIGHT
|
||||||
|
set height $FZF_TMUX_HEIGHT
|
||||||
|
else
|
||||||
|
set height 40%
|
||||||
|
end
|
||||||
|
if echo $height | \grep -q -E '%$'
|
||||||
|
echo "-p "(echo $height | sed 's/%$//')
|
||||||
|
else
|
||||||
|
echo "-l $height"
|
||||||
|
end
|
||||||
|
set -e height
|
||||||
|
end
|
||||||
|
|
||||||
|
bind \ct '__fzf_ctrl_t'
|
||||||
|
bind \cr '__fzf_ctrl_r'
|
||||||
|
bind \ec '__fzf_alt_c'
|
||||||
|
end
|
||||||
|
EOFZF
|
||||||
|
echo "OK"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
append_line() {
|
||||||
|
echo "Update $2:"
|
||||||
|
echo " - $1"
|
||||||
|
[ -f "$2" ] || touch "$2"
|
||||||
|
if [ $# -lt 3 ]; then
|
||||||
|
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||||
|
else
|
||||||
|
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||||
|
fi
|
||||||
|
if [ -n "$line" ]; then
|
||||||
|
echo " - Already exists: line #$line"
|
||||||
|
else
|
||||||
|
echo "$1" >> "$2"
|
||||||
|
echo " + Added"
|
||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
echo
|
||||||
|
for shell in bash zsh; do
|
||||||
|
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
||||||
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
|
append_line "fzf_key_bindings" "$bind_file"
|
||||||
|
|
||||||
|
echo ' * Due to a known bug of fish, you may have issues running fzf on fish.'
|
||||||
|
echo ' * If that happens, try the following:'
|
||||||
|
echo ' - Remove ~/.config/fish/functions/fzf.fish'
|
||||||
|
echo ' - Place fzf executable in a directory included in $PATH'
|
||||||
|
echo
|
||||||
|
fi
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Finished. Reload your .bashrc or .zshrc.
|
Finished. Restart your shell or reload config file.
|
||||||
source ~/.bashrc # bash
|
source ~/.bashrc # bash
|
||||||
source ~/.zshrc # zsh
|
source ~/.zshrc # zsh
|
||||||
|
EOF
|
||||||
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
||||||
|
|
||||||
To uninstall fzf, simply remove the added line.
|
Use uninstall script to remove fzf.
|
||||||
|
|
||||||
For more information, see: https://github.com/junegunn/fzf
|
For more information, see: https://github.com/junegunn/fzf
|
||||||
EOF
|
EOF
|
||||||
|
176
plugin/fzf.vim
176
plugin/fzf.vim
@@ -1,4 +1,4 @@
|
|||||||
" Copyright (c) 2014 Junegunn Choi
|
" Copyright (c) 2015 Junegunn Choi
|
||||||
"
|
"
|
||||||
" MIT License
|
" MIT License
|
||||||
"
|
"
|
||||||
@@ -21,32 +21,51 @@
|
|||||||
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
let s:min_tmux_height = 3
|
|
||||||
let s:default_tmux_height = '40%'
|
let s:default_tmux_height = '40%'
|
||||||
|
let s:launcher = 'xterm -e bash -ic %s'
|
||||||
|
let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'
|
||||||
|
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
||||||
|
let s:fzf_tmux = expand('<sfile>:h:h').'/bin/fzf-tmux'
|
||||||
|
|
||||||
let s:cpo_save = &cpo
|
let s:cpo_save = &cpo
|
||||||
set cpo&vim
|
set cpo&vim
|
||||||
|
|
||||||
call system('type fzf')
|
function! s:fzf_exec()
|
||||||
if v:shell_error
|
if !exists('s:exec')
|
||||||
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
if executable(s:fzf_go)
|
||||||
if executable(s:fzf_rb)
|
let s:exec = s:fzf_go
|
||||||
let s:exec = s:fzf_rb
|
else
|
||||||
|
let path = split(system('which fzf 2> /dev/null'), '\n')
|
||||||
|
if !v:shell_error && !empty(path)
|
||||||
|
let s:exec = path[0]
|
||||||
|
elseif executable(s:fzf_rb)
|
||||||
|
let s:exec = s:fzf_rb
|
||||||
|
else
|
||||||
|
call system('type fzf')
|
||||||
|
if v:shell_error
|
||||||
|
throw 'fzf executable not found'
|
||||||
|
else
|
||||||
|
let s:exec = 'fzf'
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
return s:exec
|
||||||
else
|
else
|
||||||
echoerr 'fzf executable not found'
|
return s:exec
|
||||||
finish
|
|
||||||
endif
|
endif
|
||||||
else
|
endfunction
|
||||||
let s:exec = 'fzf'
|
|
||||||
endif
|
|
||||||
|
|
||||||
function! s:tmux_enabled()
|
function! s:tmux_enabled()
|
||||||
|
if has('gui_running')
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
|
||||||
if exists('s:tmux')
|
if exists('s:tmux')
|
||||||
return s:tmux
|
return s:tmux
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let s:tmux = 0
|
let s:tmux = 0
|
||||||
if exists('$TMUX')
|
if exists('$TMUX') && executable(s:fzf_tmux)
|
||||||
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
|
||||||
@@ -61,15 +80,30 @@ function! s:escape(path)
|
|||||||
return substitute(a:path, ' ', '\\ ', 'g')
|
return substitute(a:path, ' ', '\\ ', 'g')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! fzf#run(...) abort
|
" Upgrade legacy options
|
||||||
if has('gui_running')
|
function! s:upgrade(dict)
|
||||||
echohl Error
|
let copy = copy(a:dict)
|
||||||
echo 'GVim is not supported'
|
if has_key(copy, 'tmux')
|
||||||
return []
|
let copy.down = remove(copy, 'tmux')
|
||||||
endif
|
endif
|
||||||
let dict = exists('a:1') ? a:1 : {}
|
if has_key(copy, 'tmux_height')
|
||||||
|
let copy.down = remove(copy, 'tmux_height')
|
||||||
|
endif
|
||||||
|
if has_key(copy, 'tmux_width')
|
||||||
|
let copy.right = remove(copy, 'tmux_width')
|
||||||
|
endif
|
||||||
|
return copy
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! fzf#run(...) abort
|
||||||
|
let dict = exists('a:1') ? s:upgrade(a:1) : {}
|
||||||
let temps = { 'result': tempname() }
|
let temps = { 'result': tempname() }
|
||||||
let optstr = get(dict, 'options', '')
|
let optstr = get(dict, 'options', '')
|
||||||
|
try
|
||||||
|
let fzf_exec = s:fzf_exec()
|
||||||
|
catch
|
||||||
|
throw v:exception
|
||||||
|
endtry
|
||||||
|
|
||||||
if has_key(dict, 'source')
|
if has_key(dict, 'source')
|
||||||
let source = dict.source
|
let source = dict.source
|
||||||
@@ -86,21 +120,47 @@ function! fzf#run(...) abort
|
|||||||
else
|
else
|
||||||
let prefix = ''
|
let prefix = ''
|
||||||
endif
|
endif
|
||||||
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
let split = s:tmux_enabled() && s:tmux_splittable(dict)
|
||||||
|
let command = prefix.(split ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result
|
||||||
|
|
||||||
if s:tmux_enabled() && has_key(dict, 'tmux') &&
|
if split
|
||||||
\ dict.tmux > 0 && winheight(0) >= s:min_tmux_height
|
|
||||||
return s:execute_tmux(dict, command, temps)
|
return s:execute_tmux(dict, command, temps)
|
||||||
else
|
else
|
||||||
return s:execute(dict, command, temps)
|
return s:execute(dict, command, temps)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:present(dict, ...)
|
||||||
|
for key in a:000
|
||||||
|
if !empty(get(a:dict, key, ''))
|
||||||
|
return 1
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return 0
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:fzf_tmux(dict)
|
||||||
|
let size = ''
|
||||||
|
for o in ['up', 'down', 'left', 'right']
|
||||||
|
if s:present(a:dict, o)
|
||||||
|
let size = '-'.o[0].(a:dict[o] == 1 ? '' : a:dict[o])
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return printf('LINES=%d COLUMNS=%d %s %s %s --',
|
||||||
|
\ &lines, &columns, s:fzf_tmux, size, (has_key(a:dict, 'source') ? '' : '-'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:tmux_splittable(dict)
|
||||||
|
return s:present(a:dict, 'up', 'down', 'left', 'right')
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:pushd(dict)
|
function! s:pushd(dict)
|
||||||
if has_key(a:dict, 'dir')
|
if s:present(a:dict, 'dir')
|
||||||
let a:dict.prev_dir = getcwd()
|
let a:dict.prev_dir = getcwd()
|
||||||
execute 'chdir '.s:escape(a:dict.dir)
|
execute 'chdir '.s:escape(a:dict.dir)
|
||||||
|
return 1
|
||||||
endif
|
endif
|
||||||
|
return 0
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:popd(dict)
|
function! s:popd(dict)
|
||||||
@@ -112,61 +172,49 @@ 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
|
silent !clear
|
||||||
execute 'silent !'.a:command
|
if has('gui_running')
|
||||||
|
let launcher = get(a:dict, 'launcher', get(g:, 'fzf_launcher', s:launcher))
|
||||||
|
let command = printf(launcher, "'".substitute(a:command, "'", "'\"'\"'", 'g')."'")
|
||||||
|
else
|
||||||
|
let command = a:command
|
||||||
|
endif
|
||||||
|
execute 'silent !'.command
|
||||||
redraw!
|
redraw!
|
||||||
if v:shell_error
|
if v:shell_error
|
||||||
|
" Do not print error message on exit status 1
|
||||||
|
if v:shell_error > 1
|
||||||
|
echohl ErrorMsg
|
||||||
|
echo 'Error running ' . command
|
||||||
|
endif
|
||||||
return []
|
return []
|
||||||
else
|
else
|
||||||
return s:callback(a:dict, a:temps, 0)
|
return s:callback(a:dict, a:temps)
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:env_var(name)
|
||||||
|
if exists('$'.a:name)
|
||||||
|
return a:name . "='". substitute(expand('$'.a:name), "'", "'\\\\''", 'g') . "' "
|
||||||
|
else
|
||||||
|
return ''
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute_tmux(dict, command, temps)
|
function! s:execute_tmux(dict, command, temps)
|
||||||
if has_key(a:dict, 'dir')
|
let command = a:command
|
||||||
let command = 'cd '.s:escape(a:dict.dir).' && '.a:command
|
if s:pushd(a:dict)
|
||||||
else
|
" -c '#{pane_current_path}' is only available on tmux 1.9 or above
|
||||||
let command = a:command
|
let command = 'cd '.s:escape(a:dict.dir).' && '.command
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if type(a:dict.tmux) == 1 && a:dict.tmux =~ '%$'
|
call system(command)
|
||||||
let height = '-p '.a:dict.tmux[0:-2]
|
return s:callback(a:dict, a:temps)
|
||||||
else
|
|
||||||
let height = '-l '.a:dict.tmux
|
|
||||||
endif
|
|
||||||
|
|
||||||
let s:pane = substitute(
|
|
||||||
\ system(
|
|
||||||
\ printf(
|
|
||||||
\ 'tmux split-window %s -P -F "#{pane_id}" %s',
|
|
||||||
\ height, s:shellesc(command))), '\n', '', 'g')
|
|
||||||
let s:dict = a:dict
|
|
||||||
let s:temps = a:temps
|
|
||||||
|
|
||||||
augroup fzf_tmux
|
|
||||||
autocmd!
|
|
||||||
autocmd VimResized * nested call s:tmux_check()
|
|
||||||
augroup END
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:tmux_check()
|
function! s:callback(dict, temps)
|
||||||
let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')
|
|
||||||
|
|
||||||
if index(panes, s:pane) < 0
|
|
||||||
augroup fzf_tmux
|
|
||||||
autocmd!
|
|
||||||
augroup END
|
|
||||||
|
|
||||||
call s:callback(s:dict, s:temps, 1)
|
|
||||||
redraw
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:callback(dict, temps, cd)
|
|
||||||
if !filereadable(a:temps.result)
|
if !filereadable(a:temps.result)
|
||||||
let lines = []
|
let lines = []
|
||||||
else
|
else
|
||||||
if a:cd | call s:pushd(a:dict) | endif
|
|
||||||
|
|
||||||
let lines = readfile(a:temps.result)
|
let lines = readfile(a:temps.result)
|
||||||
if has_key(a:dict, 'sink')
|
if has_key(a:dict, 'sink')
|
||||||
for line in lines
|
for line in lines
|
||||||
@@ -195,7 +243,7 @@ function! s:cmd(bang, ...) abort
|
|||||||
let opts.dir = remove(args, -1)
|
let opts.dir = remove(args, -1)
|
||||||
endif
|
endif
|
||||||
if !a:bang
|
if !a:bang
|
||||||
let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height)
|
let opts.down = get(g:, 'fzf_tmux_height', s:default_tmux_height)
|
||||||
endif
|
endif
|
||||||
call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
|
call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
|
||||||
endfunction
|
endfunction
|
||||||
|
27
src/Dockerfile.arch
Normal file
27
src/Dockerfile.arch
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
FROM base/archlinux:2014.07.03
|
||||||
|
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||||
|
|
||||||
|
# apt-get
|
||||||
|
RUN pacman-db-upgrade && pacman -Syu --noconfirm base-devel git
|
||||||
|
|
||||||
|
# Install Go 1.4
|
||||||
|
RUN cd / && curl \
|
||||||
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
|
ENV GOPATH /go
|
||||||
|
ENV GOROOT /go1.4
|
||||||
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
|
# For i386 build
|
||||||
|
RUN echo '[multilib]' >> /etc/pacman.conf && \
|
||||||
|
echo 'Include = /etc/pacman.d/mirrorlist' >> /etc/pacman.conf && \
|
||||||
|
pacman-db-upgrade && yes | pacman -Sy gcc-multilib lib32-ncurses && \
|
||||||
|
cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
|
# Volume
|
||||||
|
VOLUME /go
|
||||||
|
|
||||||
|
# Default CMD
|
||||||
|
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
||||||
|
|
21
src/Dockerfile.centos
Normal file
21
src/Dockerfile.centos
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
FROM centos:centos7
|
||||||
|
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||||
|
|
||||||
|
# yum
|
||||||
|
RUN yum install -y git gcc make tar ncurses-devel
|
||||||
|
|
||||||
|
# Install Go 1.4
|
||||||
|
RUN cd / && curl \
|
||||||
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
|
ENV GOPATH /go
|
||||||
|
ENV GOROOT /go1.4
|
||||||
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
|
# Volume
|
||||||
|
VOLUME /go
|
||||||
|
|
||||||
|
# Default CMD
|
||||||
|
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
||||||
|
|
26
src/Dockerfile.ubuntu
Normal file
26
src/Dockerfile.ubuntu
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
FROM ubuntu:14.04
|
||||||
|
MAINTAINER Junegunn Choi <junegunn.c@gmail.com>
|
||||||
|
|
||||||
|
# apt-get
|
||||||
|
RUN apt-get update && apt-get -y upgrade && \
|
||||||
|
apt-get install -y --force-yes git curl build-essential libncurses-dev
|
||||||
|
|
||||||
|
# Install Go 1.4
|
||||||
|
RUN cd / && curl \
|
||||||
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
|
ENV GOPATH /go
|
||||||
|
ENV GOROOT /go1.4
|
||||||
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
|
# For i386 build
|
||||||
|
RUN apt-get install -y lib32ncurses5-dev && \
|
||||||
|
cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
|
# Volume
|
||||||
|
VOLUME /go
|
||||||
|
|
||||||
|
# Default CMD
|
||||||
|
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
||||||
|
|
21
src/LICENSE
Normal file
21
src/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Junegunn Choi
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
74
src/Makefile
Normal file
74
src/Makefile
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
ifndef GOPATH
|
||||||
|
$(error GOPATH is undefined)
|
||||||
|
endif
|
||||||
|
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
GOOS := darwin
|
||||||
|
else ifeq ($(UNAME_S),Linux)
|
||||||
|
GOOS := linux
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifneq ($(shell uname -m),x86_64)
|
||||||
|
$(error "Build on $(UNAME_M) is not supported, yet.")
|
||||||
|
endif
|
||||||
|
|
||||||
|
SOURCES := $(wildcard *.go */*.go)
|
||||||
|
BINDIR := ../bin
|
||||||
|
|
||||||
|
BINARY32 := fzf-$(GOOS)_386
|
||||||
|
BINARY64 := fzf-$(GOOS)_amd64
|
||||||
|
VERSION = $(shell fzf/$(BINARY64) --version)
|
||||||
|
RELEASE32 = fzf-$(VERSION)-$(GOOS)_386
|
||||||
|
RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd64
|
||||||
|
|
||||||
|
all: release
|
||||||
|
|
||||||
|
release: build
|
||||||
|
cd fzf && \
|
||||||
|
cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32) && \
|
||||||
|
cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
||||||
|
rm $(RELEASE32) $(RELEASE64)
|
||||||
|
|
||||||
|
build: test fzf/$(BINARY32) fzf/$(BINARY64)
|
||||||
|
|
||||||
|
test:
|
||||||
|
go get
|
||||||
|
go test -v ./...
|
||||||
|
|
||||||
|
install: $(BINDIR)/fzf
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm -f $(BINDIR)/fzf $(BINDIR)/$(BINARY64)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cd fzf && rm -f $(BINARY32) $(BINARY64) $(RELEASE32).tgz $(RELEASE64).tgz
|
||||||
|
|
||||||
|
fzf/$(BINARY32): $(SOURCES)
|
||||||
|
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -o $(BINARY32)
|
||||||
|
|
||||||
|
fzf/$(BINARY64): $(SOURCES)
|
||||||
|
cd fzf && go build -o $(BINARY64)
|
||||||
|
|
||||||
|
$(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
||||||
|
cp -f fzf/$(BINARY64) $(BINDIR)
|
||||||
|
cd $(BINDIR) && ln -sf $(BINARY64) fzf
|
||||||
|
|
||||||
|
$(BINDIR):
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
# Linux distribution to build fzf on
|
||||||
|
DISTRO := arch
|
||||||
|
|
||||||
|
docker:
|
||||||
|
docker build -t junegunn/$(DISTRO)-sandbox - < Dockerfile.$(DISTRO)
|
||||||
|
|
||||||
|
linux: docker
|
||||||
|
docker run -i -t -v $(GOPATH):/go junegunn/$(DISTRO)-sandbox \
|
||||||
|
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make'
|
||||||
|
|
||||||
|
$(DISTRO): docker
|
||||||
|
docker run -i -t -v $(GOPATH):/go junegunn/$(DISTRO)-sandbox \
|
||||||
|
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
||||||
|
|
||||||
|
.PHONY: all build release test install uninstall clean docker linux $(DISTRO)
|
121
src/README.md
Normal file
121
src/README.md
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
fzf in Go
|
||||||
|
=========
|
||||||
|
|
||||||
|
<img src="https://cloud.githubusercontent.com/assets/700826/5725028/028ea834-9b93-11e4-9198-43088c3f295d.gif" height="463" alt="fzf in go">
|
||||||
|
|
||||||
|
This directory contains the source code for the new fzf implementation in
|
||||||
|
[Go][go].
|
||||||
|
|
||||||
|
Upgrade from Ruby version
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
The install script has been updated to download the right binary for your
|
||||||
|
system. If you already have installed fzf, simply git-pull the repository and
|
||||||
|
rerun the install script.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cd ~/.fzf
|
||||||
|
git pull
|
||||||
|
./install
|
||||||
|
```
|
||||||
|
|
||||||
|
Otherwise, follow [the instruction][install] as before. You can also install
|
||||||
|
fzf using Homebrew if you prefer that way.
|
||||||
|
|
||||||
|
Motivations
|
||||||
|
-----------
|
||||||
|
|
||||||
|
### No Ruby dependency
|
||||||
|
|
||||||
|
There have always been complaints about fzf being a Ruby script. To make
|
||||||
|
matters worse, Ruby 2.1 removed ncurses binding from its standard libary.
|
||||||
|
Because of the change, users running Ruby 2.1 or above are forced to build C
|
||||||
|
extensions of curses gem to meet the requirement of fzf. The new Go version
|
||||||
|
will be distributed as an executable binary so it will be much more accessible
|
||||||
|
and should be easier to setup.
|
||||||
|
|
||||||
|
### Performance
|
||||||
|
|
||||||
|
Many people have been surprised to see how fast fzf is even when it was
|
||||||
|
written in Ruby. It stays quite responsive even for 100k+ lines, which is
|
||||||
|
well above the size of the usual input.
|
||||||
|
|
||||||
|
The new Go version, of course, is significantly faster than that. It has all
|
||||||
|
the performance optimization techniques used in Ruby implementation and more.
|
||||||
|
It also doesn't suffer from [GIL][gil], so the search performance scales
|
||||||
|
proportional to the number of CPU cores. On my MacBook Pro (Mid 2012), the new
|
||||||
|
version was shown to be an order of magnitude faster on certain cases. It also
|
||||||
|
starts much faster though the difference may not be noticeable.
|
||||||
|
|
||||||
|
Differences with Ruby version
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
The Go version is designed to be perfectly compatible with the previous Ruby
|
||||||
|
version. The only behavioral difference is that the new version ignores the
|
||||||
|
numeric argument to `--sort=N` option and always sorts the result regardless
|
||||||
|
of the number of matches. The value was introduced to limit the response time
|
||||||
|
of the query, but the Go version is blazingly fast (almost instant response
|
||||||
|
even for 1M+ items) so I decided that it's no longer required.
|
||||||
|
|
||||||
|
System requirements
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Currently, prebuilt binaries are provided only for OS X and Linux. The install
|
||||||
|
script will fall back to the legacy Ruby version on the other systems, but if
|
||||||
|
you have Go 1.4 installed, you can try building it yourself.
|
||||||
|
|
||||||
|
However, as pointed out in [golang.org/doc/install][req], the Go version may
|
||||||
|
not run on CentOS/RHEL 5.x, and if that's the case, the install script will
|
||||||
|
choose the Ruby version instead.
|
||||||
|
|
||||||
|
The Go version depends on [ncurses][ncurses] and some Unix system calls, so it
|
||||||
|
shouldn't run natively on Windows at the moment. But it won't be impossible to
|
||||||
|
support Windows by falling back to a cross-platform alternative such as
|
||||||
|
[termbox][termbox] only on Windows. If you're interested in making fzf work on
|
||||||
|
Windows, please let me know.
|
||||||
|
|
||||||
|
Build
|
||||||
|
-----
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Build fzf executables and tarballs
|
||||||
|
make
|
||||||
|
|
||||||
|
# Install the executable to ../bin directory
|
||||||
|
make install
|
||||||
|
|
||||||
|
# Build executables and tarballs for Linux using Docker
|
||||||
|
make linux
|
||||||
|
```
|
||||||
|
|
||||||
|
Contribution
|
||||||
|
------------
|
||||||
|
|
||||||
|
For the time being, I will not add or accept any new features until we can be
|
||||||
|
sure that the implementation is stable and we have a sufficient number of test
|
||||||
|
cases. However, fixes for obvious bugs and new test cases are welcome.
|
||||||
|
|
||||||
|
I also care much about the performance of the implementation, so please make
|
||||||
|
sure that your change does not result in performance regression. And please be
|
||||||
|
noted that we don't have a quantitative measure of the performance yet.
|
||||||
|
|
||||||
|
Third-party libraries used
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
- [ncurses][ncurses]
|
||||||
|
- [mattn/go-runewidth](https://github.com/mattn/go-runewidth)
|
||||||
|
- Licensed under [MIT](http://mattn.mit-license.org/2013)
|
||||||
|
- [mattn/go-shellwords](https://github.com/mattn/go-shellwords)
|
||||||
|
- Licensed under [MIT](http://mattn.mit-license.org/2014)
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
||||||
|
|
||||||
|
[install]: https://github.com/junegunn/fzf#installation
|
||||||
|
[go]: https://golang.org/
|
||||||
|
[gil]: http://en.wikipedia.org/wiki/Global_Interpreter_Lock
|
||||||
|
[ncurses]: https://www.gnu.org/software/ncurses/
|
||||||
|
[req]: http://golang.org/doc/install
|
||||||
|
[termbox]: https://github.com/nsf/termbox-go
|
155
src/algo/algo.go
Normal file
155
src/algo/algo.go
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
package algo
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* String matching algorithms here do not use strings.ToLower to avoid
|
||||||
|
* performance penalty. And they assume pattern runes are given in lowercase
|
||||||
|
* letters when caseSensitive is false.
|
||||||
|
*
|
||||||
|
* In short: They try to do as little work as possible.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// FuzzyMatch performs fuzzy-match
|
||||||
|
func FuzzyMatch(caseSensitive bool, input *string, pattern []rune) (int, int) {
|
||||||
|
runes := []rune(*input)
|
||||||
|
|
||||||
|
// 0. (FIXME) How to find the shortest match?
|
||||||
|
// a_____b__c__abc
|
||||||
|
// ^^^^^^^^^^ ^^^
|
||||||
|
// 1. forward scan (abc)
|
||||||
|
// *-----*-----*>
|
||||||
|
// a_____b___abc__
|
||||||
|
// 2. reverse scan (cba)
|
||||||
|
// a_____b___abc__
|
||||||
|
// <***
|
||||||
|
pidx := 0
|
||||||
|
sidx := -1
|
||||||
|
eidx := -1
|
||||||
|
|
||||||
|
for index, char := range runes {
|
||||||
|
// This is considerably faster than blindly applying strings.ToLower to the
|
||||||
|
// whole string
|
||||||
|
if !caseSensitive && char >= 65 && char <= 90 {
|
||||||
|
char += 32
|
||||||
|
}
|
||||||
|
if char == pattern[pidx] {
|
||||||
|
if sidx < 0 {
|
||||||
|
sidx = index
|
||||||
|
}
|
||||||
|
if pidx++; pidx == len(pattern) {
|
||||||
|
eidx = index + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sidx >= 0 && eidx >= 0 {
|
||||||
|
pidx--
|
||||||
|
for index := eidx - 1; index >= sidx; index-- {
|
||||||
|
char := runes[index]
|
||||||
|
if !caseSensitive && char >= 65 && char <= 90 {
|
||||||
|
char += 32
|
||||||
|
}
|
||||||
|
if char == pattern[pidx] {
|
||||||
|
if pidx--; pidx < 0 {
|
||||||
|
sidx = index
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sidx, eidx
|
||||||
|
}
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExactMatchStrings performs exact-match using strings package.
|
||||||
|
// Currently not used.
|
||||||
|
func ExactMatchStrings(caseSensitive bool, input *string, pattern []rune) (int, int) {
|
||||||
|
var str string
|
||||||
|
if caseSensitive {
|
||||||
|
str = *input
|
||||||
|
} else {
|
||||||
|
str = strings.ToLower(*input)
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx := strings.Index(str, string(pattern)); idx >= 0 {
|
||||||
|
prefixRuneLen := len([]rune((*input)[:idx]))
|
||||||
|
return prefixRuneLen, prefixRuneLen + len(pattern)
|
||||||
|
}
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExactMatchNaive is a basic string searching algorithm that handles case
|
||||||
|
// sensitivity. Although naive, it still performs better than the combination
|
||||||
|
// of strings.ToLower + strings.Index for typical fzf use cases where input
|
||||||
|
// strings and patterns are not very long.
|
||||||
|
//
|
||||||
|
// We might try to implement better algorithms in the future:
|
||||||
|
// http://en.wikipedia.org/wiki/String_searching_algorithm
|
||||||
|
func ExactMatchNaive(caseSensitive bool, input *string, pattern []rune) (int, int) {
|
||||||
|
runes := []rune(*input)
|
||||||
|
numRunes := len(runes)
|
||||||
|
plen := len(pattern)
|
||||||
|
if numRunes < plen {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
pidx := 0
|
||||||
|
for index := 0; index < numRunes; index++ {
|
||||||
|
char := runes[index]
|
||||||
|
if !caseSensitive && char >= 65 && char <= 90 {
|
||||||
|
char += 32
|
||||||
|
}
|
||||||
|
if pattern[pidx] == char {
|
||||||
|
pidx++
|
||||||
|
if pidx == plen {
|
||||||
|
return index - plen + 1, index + 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
index -= pidx
|
||||||
|
pidx = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrefixMatch performs prefix-match
|
||||||
|
func PrefixMatch(caseSensitive bool, input *string, pattern []rune) (int, int) {
|
||||||
|
runes := []rune(*input)
|
||||||
|
if len(runes) < len(pattern) {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for index, r := range pattern {
|
||||||
|
char := runes[index]
|
||||||
|
if !caseSensitive && char >= 65 && char <= 90 {
|
||||||
|
char += 32
|
||||||
|
}
|
||||||
|
if char != r {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, len(pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SuffixMatch performs suffix-match
|
||||||
|
func SuffixMatch(caseSensitive bool, input *string, pattern []rune) (int, int) {
|
||||||
|
runes := []rune(strings.TrimRight(*input, " "))
|
||||||
|
trimmedLen := len(runes)
|
||||||
|
diff := trimmedLen - len(pattern)
|
||||||
|
if diff < 0 {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for index, r := range pattern {
|
||||||
|
char := runes[index+diff]
|
||||||
|
if !caseSensitive && char >= 65 && char <= 90 {
|
||||||
|
char += 32
|
||||||
|
}
|
||||||
|
if char != r {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return trimmedLen - len(pattern), trimmedLen
|
||||||
|
}
|
44
src/algo/algo_test.go
Normal file
44
src/algo/algo_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package algo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func assertMatch(t *testing.T, fun func(bool, *string, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {
|
||||||
|
if !caseSensitive {
|
||||||
|
pattern = strings.ToLower(pattern)
|
||||||
|
}
|
||||||
|
s, e := fun(caseSensitive, &input, []rune(pattern))
|
||||||
|
if s != sidx {
|
||||||
|
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
|
||||||
|
}
|
||||||
|
if e != eidx {
|
||||||
|
t.Errorf("Invalid end index: %d (expected: %d, %s / %s)", e, eidx, input, pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFuzzyMatch(t *testing.T) {
|
||||||
|
assertMatch(t, FuzzyMatch, false, "fooBarbaz", "oBZ", 2, 9)
|
||||||
|
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBZ", -1, -1)
|
||||||
|
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBz", 2, 9)
|
||||||
|
assertMatch(t, FuzzyMatch, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExactMatchNaive(t *testing.T) {
|
||||||
|
assertMatch(t, ExactMatchNaive, false, "fooBarbaz", "oBA", 2, 5)
|
||||||
|
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "oBA", -1, -1)
|
||||||
|
assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrefixMatch(t *testing.T) {
|
||||||
|
assertMatch(t, PrefixMatch, false, "fooBarbaz", "Foo", 0, 3)
|
||||||
|
assertMatch(t, PrefixMatch, true, "fooBarbaz", "Foo", -1, -1)
|
||||||
|
assertMatch(t, PrefixMatch, false, "fooBarbaz", "baz", -1, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSuffixMatch(t *testing.T) {
|
||||||
|
assertMatch(t, SuffixMatch, false, "fooBarbaz", "Foo", -1, -1)
|
||||||
|
assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9)
|
||||||
|
assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1)
|
||||||
|
}
|
53
src/cache.go
Normal file
53
src/cache.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// QueryCache associates strings to lists of items
|
||||||
|
type QueryCache map[string][]*Item
|
||||||
|
|
||||||
|
// ChunkCache associates Chunk and query string to lists of items
|
||||||
|
type ChunkCache struct {
|
||||||
|
mutex sync.Mutex
|
||||||
|
cache map[*Chunk]*QueryCache
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChunkCache returns a new ChunkCache
|
||||||
|
func NewChunkCache() ChunkCache {
|
||||||
|
return ChunkCache{sync.Mutex{}, make(map[*Chunk]*QueryCache)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds the list to the cache
|
||||||
|
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) {
|
||||||
|
if len(key) == 0 || !chunk.IsFull() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cc.mutex.Lock()
|
||||||
|
defer cc.mutex.Unlock()
|
||||||
|
|
||||||
|
qc, ok := cc.cache[chunk]
|
||||||
|
if !ok {
|
||||||
|
cc.cache[chunk] = &QueryCache{}
|
||||||
|
qc = cc.cache[chunk]
|
||||||
|
}
|
||||||
|
(*qc)[key] = list
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find is called to lookup ChunkCache
|
||||||
|
func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Item, bool) {
|
||||||
|
if len(key) == 0 || !chunk.IsFull() {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
cc.mutex.Lock()
|
||||||
|
defer cc.mutex.Unlock()
|
||||||
|
|
||||||
|
qc, ok := cc.cache[chunk]
|
||||||
|
if ok {
|
||||||
|
list, ok := (*qc)[key]
|
||||||
|
if ok {
|
||||||
|
return list, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
40
src/cache_test.go
Normal file
40
src/cache_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestChunkCache(t *testing.T) {
|
||||||
|
cache := NewChunkCache()
|
||||||
|
chunk2 := make(Chunk, ChunkSize)
|
||||||
|
chunk1p := &Chunk{}
|
||||||
|
chunk2p := &chunk2
|
||||||
|
items1 := []*Item{&Item{}}
|
||||||
|
items2 := []*Item{&Item{}, &Item{}}
|
||||||
|
cache.Add(chunk1p, "foo", items1)
|
||||||
|
cache.Add(chunk2p, "foo", items1)
|
||||||
|
cache.Add(chunk2p, "bar", items2)
|
||||||
|
|
||||||
|
{ // chunk1 is not full
|
||||||
|
cached, found := cache.Find(chunk1p, "foo")
|
||||||
|
if found {
|
||||||
|
t.Error("Cached disabled for non-empty chunks", found, cached)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
cached, found := cache.Find(chunk2p, "foo")
|
||||||
|
if !found || len(cached) != 1 {
|
||||||
|
t.Error("Expected 1 item cached", found, cached)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
cached, found := cache.Find(chunk2p, "bar")
|
||||||
|
if !found || len(cached) != 2 {
|
||||||
|
t.Error("Expected 2 items cached", found, cached)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
cached, found := cache.Find(chunk1p, "foobar")
|
||||||
|
if found {
|
||||||
|
t.Error("Expected 0 item cached", found, cached)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
88
src/chunklist.go
Normal file
88
src/chunklist.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// Capacity of each chunk
|
||||||
|
const ChunkSize int = 100
|
||||||
|
|
||||||
|
// Chunk is a list of Item pointers whose size has the upper limit of ChunkSize
|
||||||
|
type Chunk []*Item // >>> []Item
|
||||||
|
|
||||||
|
// ItemBuilder is a closure type that builds Item object from a pointer to a
|
||||||
|
// string and an integer
|
||||||
|
type ItemBuilder func(*string, int) *Item
|
||||||
|
|
||||||
|
// ChunkList is a list of Chunks
|
||||||
|
type ChunkList struct {
|
||||||
|
chunks []*Chunk
|
||||||
|
count int
|
||||||
|
mutex sync.Mutex
|
||||||
|
trans ItemBuilder
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChunkList returns a new ChunkList
|
||||||
|
func NewChunkList(trans ItemBuilder) *ChunkList {
|
||||||
|
return &ChunkList{
|
||||||
|
chunks: []*Chunk{},
|
||||||
|
count: 0,
|
||||||
|
mutex: sync.Mutex{},
|
||||||
|
trans: trans}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Chunk) push(trans ItemBuilder, data *string, index int) {
|
||||||
|
*c = append(*c, trans(data, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFull returns true if the Chunk is full
|
||||||
|
func (c *Chunk) IsFull() bool {
|
||||||
|
return len(*c) == ChunkSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ChunkList) lastChunk() *Chunk {
|
||||||
|
return cl.chunks[len(cl.chunks)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountItems returns the total number of Items
|
||||||
|
func CountItems(cs []*Chunk) int {
|
||||||
|
if len(cs) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return ChunkSize*(len(cs)-1) + len(*(cs[len(cs)-1]))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push adds the item to the list
|
||||||
|
func (cl *ChunkList) Push(data string) {
|
||||||
|
cl.mutex.Lock()
|
||||||
|
defer cl.mutex.Unlock()
|
||||||
|
|
||||||
|
if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {
|
||||||
|
newChunk := Chunk(make([]*Item, 0, ChunkSize))
|
||||||
|
cl.chunks = append(cl.chunks, &newChunk)
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.lastChunk().push(cl.trans, &data, cl.count)
|
||||||
|
cl.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot returns immutable snapshot of the ChunkList
|
||||||
|
func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
|
||||||
|
cl.mutex.Lock()
|
||||||
|
defer cl.mutex.Unlock()
|
||||||
|
|
||||||
|
ret := make([]*Chunk, len(cl.chunks))
|
||||||
|
copy(ret, cl.chunks)
|
||||||
|
|
||||||
|
// Duplicate the last chunk
|
||||||
|
if cnt := len(ret); cnt > 0 {
|
||||||
|
ret[cnt-1] = ret[cnt-1].dupe()
|
||||||
|
}
|
||||||
|
return ret, cl.count
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Chunk) dupe() *Chunk {
|
||||||
|
newChunk := make(Chunk, len(*c))
|
||||||
|
for idx, ptr := range *c {
|
||||||
|
newChunk[idx] = ptr
|
||||||
|
}
|
||||||
|
return &newChunk
|
||||||
|
}
|
74
src/chunklist_test.go
Normal file
74
src/chunklist_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChunkList(t *testing.T) {
|
||||||
|
cl := NewChunkList(func(s *string, i int) *Item {
|
||||||
|
return &Item{text: s, rank: Rank{0, 0, uint32(i * 2)}}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Snapshot
|
||||||
|
snapshot, count := cl.Snapshot()
|
||||||
|
if len(snapshot) > 0 || count > 0 {
|
||||||
|
t.Error("Snapshot should be empty now")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add some data
|
||||||
|
cl.Push("hello")
|
||||||
|
cl.Push("world")
|
||||||
|
|
||||||
|
// Previously created snapshot should remain the same
|
||||||
|
if len(snapshot) > 0 {
|
||||||
|
t.Error("Snapshot should not have changed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// But the new snapshot should contain the added items
|
||||||
|
snapshot, count = cl.Snapshot()
|
||||||
|
if len(snapshot) != 1 && count != 2 {
|
||||||
|
t.Error("Snapshot should not be empty now")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the content of the ChunkList
|
||||||
|
chunk1 := snapshot[0]
|
||||||
|
if len(*chunk1) != 2 {
|
||||||
|
t.Error("Snapshot should contain only two items")
|
||||||
|
}
|
||||||
|
if *(*chunk1)[0].text != "hello" || (*chunk1)[0].rank.index != 0 ||
|
||||||
|
*(*chunk1)[1].text != "world" || (*chunk1)[1].rank.index != 2 {
|
||||||
|
t.Error("Invalid data")
|
||||||
|
}
|
||||||
|
if chunk1.IsFull() {
|
||||||
|
t.Error("Chunk should not have been marked full yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add more data
|
||||||
|
for i := 0; i < ChunkSize*2; i++ {
|
||||||
|
cl.Push(fmt.Sprintf("item %d", i))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Previous snapshot should remain the same
|
||||||
|
if len(snapshot) != 1 {
|
||||||
|
t.Error("Snapshot should stay the same")
|
||||||
|
}
|
||||||
|
|
||||||
|
// New snapshot
|
||||||
|
snapshot, count = cl.Snapshot()
|
||||||
|
if len(snapshot) != 3 || !snapshot[0].IsFull() ||
|
||||||
|
!snapshot[1].IsFull() || snapshot[2].IsFull() || count != ChunkSize*2+2 {
|
||||||
|
t.Error("Expected two full chunks and one more chunk")
|
||||||
|
}
|
||||||
|
if len(*snapshot[2]) != 2 {
|
||||||
|
t.Error("Unexpected number of items")
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.Push("hello")
|
||||||
|
cl.Push("world")
|
||||||
|
|
||||||
|
lastChunkCount := len(*snapshot[len(snapshot)-1])
|
||||||
|
if lastChunkCount != 2 {
|
||||||
|
t.Error("Unexpected number of items:", lastChunkCount)
|
||||||
|
}
|
||||||
|
}
|
18
src/constants.go
Normal file
18
src/constants.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Current version
|
||||||
|
const Version = "0.9.4"
|
||||||
|
|
||||||
|
// fzf events
|
||||||
|
const (
|
||||||
|
EvtReadNew util.EventType = iota
|
||||||
|
EvtReadFin
|
||||||
|
EvtSearchNew
|
||||||
|
EvtSearchProgress
|
||||||
|
EvtSearchFin
|
||||||
|
EvtClose
|
||||||
|
)
|
213
src/core.go
Normal file
213
src/core.go
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
/*
|
||||||
|
Package fzf implements fzf, a command-line fuzzy finder.
|
||||||
|
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Junegunn Choi
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
||||||
|
const coordinatorDelayStep time.Duration = 10 * time.Millisecond
|
||||||
|
|
||||||
|
func initProcs() {
|
||||||
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Reader -> EvtReadFin
|
||||||
|
Reader -> EvtReadNew -> Matcher (restart)
|
||||||
|
Terminal -> EvtSearchNew -> Matcher (restart)
|
||||||
|
Matcher -> EvtSearchProgress -> Terminal (update info)
|
||||||
|
Matcher -> EvtSearchFin -> Terminal (update list)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Run starts fzf
|
||||||
|
func Run(options *Options) {
|
||||||
|
initProcs()
|
||||||
|
|
||||||
|
opts := ParseOptions()
|
||||||
|
|
||||||
|
if opts.Version {
|
||||||
|
fmt.Println(Version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event channel
|
||||||
|
eventBox := util.NewEventBox()
|
||||||
|
|
||||||
|
// Chunk list
|
||||||
|
var chunkList *ChunkList
|
||||||
|
if len(opts.WithNth) == 0 {
|
||||||
|
chunkList = NewChunkList(func(data *string, index int) *Item {
|
||||||
|
return &Item{
|
||||||
|
text: data,
|
||||||
|
index: uint32(index),
|
||||||
|
rank: Rank{0, 0, uint32(index)}}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
chunkList = NewChunkList(func(data *string, index int) *Item {
|
||||||
|
tokens := Tokenize(data, opts.Delimiter)
|
||||||
|
item := Item{
|
||||||
|
text: Transform(tokens, opts.WithNth).whole,
|
||||||
|
origText: data,
|
||||||
|
index: uint32(index),
|
||||||
|
rank: Rank{0, 0, uint32(index)}}
|
||||||
|
return &item
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader
|
||||||
|
streamingFilter := opts.Filter != nil && opts.Sort == 0 && !opts.Tac && !opts.Sync
|
||||||
|
if !streamingFilter {
|
||||||
|
reader := Reader{func(str string) { chunkList.Push(str) }, eventBox}
|
||||||
|
go reader.ReadSource()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Matcher
|
||||||
|
patternBuilder := func(runes []rune) *Pattern {
|
||||||
|
return BuildPattern(
|
||||||
|
opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)
|
||||||
|
}
|
||||||
|
matcher := NewMatcher(patternBuilder, opts.Sort > 0, opts.Tac, eventBox)
|
||||||
|
|
||||||
|
// Filtering mode
|
||||||
|
if opts.Filter != nil {
|
||||||
|
if opts.PrintQuery {
|
||||||
|
fmt.Println(*opts.Filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern := patternBuilder([]rune(*opts.Filter))
|
||||||
|
|
||||||
|
if streamingFilter {
|
||||||
|
reader := Reader{
|
||||||
|
func(str string) {
|
||||||
|
item := chunkList.trans(&str, 0)
|
||||||
|
if pattern.MatchItem(item) {
|
||||||
|
fmt.Println(*item.text)
|
||||||
|
}
|
||||||
|
}, eventBox}
|
||||||
|
reader.ReadSource()
|
||||||
|
} else {
|
||||||
|
eventBox.Unwatch(EvtReadNew)
|
||||||
|
eventBox.WaitFor(EvtReadFin)
|
||||||
|
|
||||||
|
snapshot, _ := chunkList.Snapshot()
|
||||||
|
merger, _ := matcher.scan(MatchRequest{
|
||||||
|
chunks: snapshot,
|
||||||
|
pattern: pattern})
|
||||||
|
for i := 0; i < merger.Length(); i++ {
|
||||||
|
fmt.Println(merger.Get(i).AsString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Synchronous search
|
||||||
|
if opts.Sync {
|
||||||
|
eventBox.Unwatch(EvtReadNew)
|
||||||
|
eventBox.WaitFor(EvtReadFin)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go interactive
|
||||||
|
go matcher.Loop()
|
||||||
|
|
||||||
|
// Terminal I/O
|
||||||
|
terminal := NewTerminal(opts, eventBox)
|
||||||
|
deferred := opts.Select1 || opts.Exit0
|
||||||
|
go terminal.Loop()
|
||||||
|
if !deferred {
|
||||||
|
terminal.startChan <- true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event coordination
|
||||||
|
reading := true
|
||||||
|
ticks := 0
|
||||||
|
eventBox.Watch(EvtReadNew)
|
||||||
|
for {
|
||||||
|
delay := true
|
||||||
|
ticks++
|
||||||
|
eventBox.Wait(func(events *util.Events) {
|
||||||
|
defer events.Clear()
|
||||||
|
for evt, value := range *events {
|
||||||
|
switch evt {
|
||||||
|
|
||||||
|
case EvtReadNew, EvtReadFin:
|
||||||
|
reading = reading && evt == EvtReadNew
|
||||||
|
snapshot, count := chunkList.Snapshot()
|
||||||
|
terminal.UpdateCount(count, !reading)
|
||||||
|
matcher.Reset(snapshot, terminal.Input(), false, !reading)
|
||||||
|
|
||||||
|
case EvtSearchNew:
|
||||||
|
snapshot, _ := chunkList.Snapshot()
|
||||||
|
matcher.Reset(snapshot, terminal.Input(), true, !reading)
|
||||||
|
delay = false
|
||||||
|
|
||||||
|
case EvtSearchProgress:
|
||||||
|
switch val := value.(type) {
|
||||||
|
case float32:
|
||||||
|
terminal.UpdateProgress(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
case EvtSearchFin:
|
||||||
|
switch val := value.(type) {
|
||||||
|
case *Merger:
|
||||||
|
if deferred {
|
||||||
|
count := val.Length()
|
||||||
|
if opts.Select1 && count > 1 || opts.Exit0 && !opts.Select1 && count > 0 {
|
||||||
|
deferred = false
|
||||||
|
terminal.startChan <- true
|
||||||
|
} else if val.final {
|
||||||
|
if opts.Exit0 && count == 0 || opts.Select1 && count == 1 {
|
||||||
|
if opts.PrintQuery {
|
||||||
|
fmt.Println(opts.Query)
|
||||||
|
}
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
fmt.Println(val.Get(i).AsString())
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
deferred = false
|
||||||
|
terminal.startChan <- true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
terminal.UpdateList(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if delay && reading {
|
||||||
|
dur := util.DurWithin(
|
||||||
|
time.Duration(ticks)*coordinatorDelayStep,
|
||||||
|
0, coordinatorDelayMax)
|
||||||
|
time.Sleep(dur)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
430
src/curses/curses.go
Normal file
430
src/curses/curses.go
Normal file
@@ -0,0 +1,430 @@
|
|||||||
|
package curses
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#cgo LDFLAGS: -lncurses
|
||||||
|
void swapOutput() {
|
||||||
|
FILE* temp = stdout;
|
||||||
|
stdout = stderr;
|
||||||
|
stderr = temp;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Types of user action
|
||||||
|
const (
|
||||||
|
Rune = iota
|
||||||
|
|
||||||
|
CtrlA
|
||||||
|
CtrlB
|
||||||
|
CtrlC
|
||||||
|
CtrlD
|
||||||
|
CtrlE
|
||||||
|
CtrlF
|
||||||
|
CtrlG
|
||||||
|
CtrlH
|
||||||
|
Tab
|
||||||
|
CtrlJ
|
||||||
|
CtrlK
|
||||||
|
CtrlL
|
||||||
|
CtrlM
|
||||||
|
CtrlN
|
||||||
|
CtrlO
|
||||||
|
CtrlP
|
||||||
|
CtrlQ
|
||||||
|
CtrlR
|
||||||
|
CtrlS
|
||||||
|
CtrlT
|
||||||
|
CtrlU
|
||||||
|
CtrlV
|
||||||
|
CtrlW
|
||||||
|
CtrlX
|
||||||
|
CtrlY
|
||||||
|
CtrlZ
|
||||||
|
ESC
|
||||||
|
|
||||||
|
Invalid
|
||||||
|
Mouse
|
||||||
|
|
||||||
|
BTab
|
||||||
|
|
||||||
|
Del
|
||||||
|
PgUp
|
||||||
|
PgDn
|
||||||
|
|
||||||
|
AltB
|
||||||
|
AltF
|
||||||
|
AltD
|
||||||
|
AltBS
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pallete
|
||||||
|
const (
|
||||||
|
ColNormal = iota
|
||||||
|
ColPrompt
|
||||||
|
ColMatch
|
||||||
|
ColCurrent
|
||||||
|
ColCurrentMatch
|
||||||
|
ColSpinner
|
||||||
|
ColInfo
|
||||||
|
ColCursor
|
||||||
|
ColSelected
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
doubleClickDuration = 500 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
Type int
|
||||||
|
Char rune
|
||||||
|
MouseEvent *MouseEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
type MouseEvent struct {
|
||||||
|
Y int
|
||||||
|
X int
|
||||||
|
S int
|
||||||
|
Down bool
|
||||||
|
Double bool
|
||||||
|
Mod bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_buf []byte
|
||||||
|
_in *os.File
|
||||||
|
_color func(int, bool) C.int
|
||||||
|
_prevDownTime time.Time
|
||||||
|
_prevDownY int
|
||||||
|
_clickY []int
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_prevDownTime = time.Unix(0, 0)
|
||||||
|
_clickY = []int{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func attrColored(pair int, bold bool) C.int {
|
||||||
|
var attr C.int
|
||||||
|
if pair > ColNormal {
|
||||||
|
attr = C.COLOR_PAIR(C.int(pair))
|
||||||
|
}
|
||||||
|
if bold {
|
||||||
|
attr = attr | C.A_BOLD
|
||||||
|
}
|
||||||
|
return attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func attrMono(pair int, bold bool) C.int {
|
||||||
|
var attr C.int
|
||||||
|
switch pair {
|
||||||
|
case ColCurrent:
|
||||||
|
if bold {
|
||||||
|
attr = C.A_REVERSE
|
||||||
|
}
|
||||||
|
case ColMatch:
|
||||||
|
attr = C.A_UNDERLINE
|
||||||
|
case ColCurrentMatch:
|
||||||
|
attr = C.A_UNDERLINE | C.A_REVERSE
|
||||||
|
}
|
||||||
|
if bold {
|
||||||
|
attr = attr | C.A_BOLD
|
||||||
|
}
|
||||||
|
return attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxX() int {
|
||||||
|
return int(C.COLS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxY() int {
|
||||||
|
return int(C.LINES)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getch(nonblock bool) int {
|
||||||
|
b := make([]byte, 1)
|
||||||
|
syscall.SetNonblock(int(_in.Fd()), nonblock)
|
||||||
|
_, err := _in.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return int(b[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init(color bool, color256 bool, black bool, mouse bool) {
|
||||||
|
{
|
||||||
|
in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
panic("Failed to open /dev/tty")
|
||||||
|
}
|
||||||
|
_in = in
|
||||||
|
// Break STDIN
|
||||||
|
// syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))
|
||||||
|
}
|
||||||
|
|
||||||
|
C.swapOutput()
|
||||||
|
|
||||||
|
C.setlocale(C.LC_ALL, C.CString(""))
|
||||||
|
C.initscr()
|
||||||
|
if mouse {
|
||||||
|
C.mousemask(C.ALL_MOUSE_EVENTS, nil)
|
||||||
|
}
|
||||||
|
C.cbreak()
|
||||||
|
C.noecho()
|
||||||
|
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 color {
|
||||||
|
C.start_color()
|
||||||
|
var bg C.short
|
||||||
|
if black {
|
||||||
|
bg = C.COLOR_BLACK
|
||||||
|
} else {
|
||||||
|
C.use_default_colors()
|
||||||
|
bg = -1
|
||||||
|
}
|
||||||
|
if color256 {
|
||||||
|
C.init_pair(ColPrompt, 110, bg)
|
||||||
|
C.init_pair(ColMatch, 108, bg)
|
||||||
|
C.init_pair(ColCurrent, 254, 236)
|
||||||
|
C.init_pair(ColCurrentMatch, 151, 236)
|
||||||
|
C.init_pair(ColSpinner, 148, bg)
|
||||||
|
C.init_pair(ColInfo, 144, bg)
|
||||||
|
C.init_pair(ColCursor, 161, 236)
|
||||||
|
C.init_pair(ColSelected, 168, 236)
|
||||||
|
} else {
|
||||||
|
C.init_pair(ColPrompt, C.COLOR_BLUE, bg)
|
||||||
|
C.init_pair(ColMatch, C.COLOR_GREEN, bg)
|
||||||
|
C.init_pair(ColCurrent, C.COLOR_YELLOW, C.COLOR_BLACK)
|
||||||
|
C.init_pair(ColCurrentMatch, C.COLOR_GREEN, C.COLOR_BLACK)
|
||||||
|
C.init_pair(ColSpinner, C.COLOR_GREEN, bg)
|
||||||
|
C.init_pair(ColInfo, C.COLOR_WHITE, bg)
|
||||||
|
C.init_pair(ColCursor, C.COLOR_RED, C.COLOR_BLACK)
|
||||||
|
C.init_pair(ColSelected, C.COLOR_MAGENTA, C.COLOR_BLACK)
|
||||||
|
}
|
||||||
|
_color = attrColored
|
||||||
|
} else {
|
||||||
|
_color = attrMono
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close() {
|
||||||
|
C.endwin()
|
||||||
|
C.swapOutput()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBytes() []byte {
|
||||||
|
c := getch(false)
|
||||||
|
_buf = append(_buf, byte(c))
|
||||||
|
|
||||||
|
for {
|
||||||
|
c = getch(true)
|
||||||
|
if c == -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_buf = append(_buf, byte(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
return _buf
|
||||||
|
}
|
||||||
|
|
||||||
|
// 27 (91 79) 77 type x y
|
||||||
|
func mouseSequence(sz *int) Event {
|
||||||
|
if len(_buf) < 6 {
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
*sz = 6
|
||||||
|
switch _buf[3] {
|
||||||
|
case 32, 36, 40, 48, // mouse-down / shift / cmd / ctrl
|
||||||
|
35, 39, 43, 51: // mouse-up / shift / cmd / ctrl
|
||||||
|
mod := _buf[3] >= 36
|
||||||
|
down := _buf[3]%2 == 0
|
||||||
|
x := int(_buf[4] - 33)
|
||||||
|
y := int(_buf[5] - 33)
|
||||||
|
double := false
|
||||||
|
if down {
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(_prevDownTime) < doubleClickDuration {
|
||||||
|
_clickY = append(_clickY, y)
|
||||||
|
} else {
|
||||||
|
_clickY = []int{y}
|
||||||
|
}
|
||||||
|
_prevDownTime = now
|
||||||
|
} else {
|
||||||
|
if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&
|
||||||
|
time.Now().Sub(_prevDownTime) < doubleClickDuration {
|
||||||
|
double = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, 0, down, double, mod}}
|
||||||
|
case 96, 100, 104, 112, // scroll-up / shift / cmd / ctrl
|
||||||
|
97, 101, 105, 113: // scroll-down / shift / cmd / ctrl
|
||||||
|
mod := _buf[3] >= 100
|
||||||
|
s := 1 - int(_buf[3]%2)*2
|
||||||
|
return Event{Mouse, 0, &MouseEvent{0, 0, s, false, false, mod}}
|
||||||
|
}
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func escSequence(sz *int) Event {
|
||||||
|
if len(_buf) < 2 {
|
||||||
|
return Event{ESC, 0, nil}
|
||||||
|
}
|
||||||
|
*sz = 2
|
||||||
|
switch _buf[1] {
|
||||||
|
case 98:
|
||||||
|
return Event{AltB, 0, nil}
|
||||||
|
case 100:
|
||||||
|
return Event{AltD, 0, nil}
|
||||||
|
case 102:
|
||||||
|
return Event{AltF, 0, nil}
|
||||||
|
case 127:
|
||||||
|
return Event{AltBS, 0, nil}
|
||||||
|
case 91, 79:
|
||||||
|
if len(_buf) < 3 {
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
*sz = 3
|
||||||
|
switch _buf[2] {
|
||||||
|
case 68:
|
||||||
|
return Event{CtrlB, 0, nil}
|
||||||
|
case 67:
|
||||||
|
return Event{CtrlF, 0, nil}
|
||||||
|
case 66:
|
||||||
|
return Event{CtrlJ, 0, nil}
|
||||||
|
case 65:
|
||||||
|
return Event{CtrlK, 0, nil}
|
||||||
|
case 90:
|
||||||
|
return Event{BTab, 0, nil}
|
||||||
|
case 72:
|
||||||
|
return Event{CtrlA, 0, nil}
|
||||||
|
case 70:
|
||||||
|
return Event{CtrlE, 0, nil}
|
||||||
|
case 77:
|
||||||
|
return mouseSequence(sz)
|
||||||
|
case 49, 50, 51, 52, 53, 54:
|
||||||
|
if len(_buf) < 4 {
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
*sz = 4
|
||||||
|
switch _buf[2] {
|
||||||
|
case 50:
|
||||||
|
return Event{Invalid, 0, nil} // INS
|
||||||
|
case 51:
|
||||||
|
return Event{Del, 0, nil}
|
||||||
|
case 52:
|
||||||
|
return Event{CtrlE, 0, nil}
|
||||||
|
case 53:
|
||||||
|
return Event{PgUp, 0, nil}
|
||||||
|
case 54:
|
||||||
|
return Event{PgDn, 0, nil}
|
||||||
|
case 49:
|
||||||
|
switch _buf[3] {
|
||||||
|
case 126:
|
||||||
|
return Event{CtrlA, 0, nil}
|
||||||
|
case 59:
|
||||||
|
if len(_buf) != 6 {
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
*sz = 6
|
||||||
|
switch _buf[4] {
|
||||||
|
case 50:
|
||||||
|
switch _buf[5] {
|
||||||
|
case 68:
|
||||||
|
return Event{CtrlA, 0, nil}
|
||||||
|
case 67:
|
||||||
|
return Event{CtrlE, 0, nil}
|
||||||
|
}
|
||||||
|
case 53:
|
||||||
|
switch _buf[5] {
|
||||||
|
case 68:
|
||||||
|
return Event{AltB, 0, nil}
|
||||||
|
case 67:
|
||||||
|
return Event{AltF, 0, nil}
|
||||||
|
}
|
||||||
|
} // _buf[4]
|
||||||
|
} // _buf[3]
|
||||||
|
} // _buf[2]
|
||||||
|
} // _buf[2]
|
||||||
|
} // _buf[1]
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetChar() Event {
|
||||||
|
if len(_buf) == 0 {
|
||||||
|
_buf = GetBytes()
|
||||||
|
}
|
||||||
|
if len(_buf) == 0 {
|
||||||
|
panic("Empty _buffer")
|
||||||
|
}
|
||||||
|
|
||||||
|
sz := 1
|
||||||
|
defer func() {
|
||||||
|
_buf = _buf[sz:]
|
||||||
|
}()
|
||||||
|
|
||||||
|
switch _buf[0] {
|
||||||
|
case CtrlC, CtrlG, CtrlQ:
|
||||||
|
return Event{CtrlC, 0, nil}
|
||||||
|
case 127:
|
||||||
|
return Event{CtrlH, 0, nil}
|
||||||
|
case ESC:
|
||||||
|
return escSequence(&sz)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CTRL-A ~ CTRL-Z
|
||||||
|
if _buf[0] <= CtrlZ {
|
||||||
|
return Event{int(_buf[0]), 0, nil}
|
||||||
|
}
|
||||||
|
r, rsz := utf8.DecodeRune(_buf)
|
||||||
|
sz = rsz
|
||||||
|
return Event{Rune, r, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Move(y int, x int) {
|
||||||
|
C.move(C.int(y), C.int(x))
|
||||||
|
}
|
||||||
|
|
||||||
|
func MoveAndClear(y int, x int) {
|
||||||
|
Move(y, x)
|
||||||
|
C.clrtoeol()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Print(text string) {
|
||||||
|
C.addstr(C.CString(text))
|
||||||
|
}
|
||||||
|
|
||||||
|
func CPrint(pair int, bold bool, text string) {
|
||||||
|
attr := _color(pair, bold)
|
||||||
|
C.attron(attr)
|
||||||
|
Print(text)
|
||||||
|
C.attroff(attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() {
|
||||||
|
C.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Endwin() {
|
||||||
|
C.endwin()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Refresh() {
|
||||||
|
C.refresh()
|
||||||
|
}
|
7
src/fzf/main.go
Normal file
7
src/fzf/main.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/junegunn/fzf/src"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fzf.Run(fzf.ParseOptions())
|
||||||
|
}
|
125
src/item.go
Normal file
125
src/item.go
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
||||||
|
type Offset [2]int32
|
||||||
|
|
||||||
|
// Item represents each input line
|
||||||
|
type Item struct {
|
||||||
|
text *string
|
||||||
|
origText *string
|
||||||
|
transformed *Transformed
|
||||||
|
index uint32
|
||||||
|
offsets []Offset
|
||||||
|
rank Rank
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rank is used to sort the search result
|
||||||
|
type Rank struct {
|
||||||
|
matchlen uint16
|
||||||
|
strlen uint16
|
||||||
|
index uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rank calculates rank of the Item
|
||||||
|
func (i *Item) Rank(cache bool) Rank {
|
||||||
|
if cache && (i.rank.matchlen > 0 || i.rank.strlen > 0) {
|
||||||
|
return i.rank
|
||||||
|
}
|
||||||
|
matchlen := 0
|
||||||
|
prevEnd := 0
|
||||||
|
for _, offset := range i.offsets {
|
||||||
|
begin := int(offset[0])
|
||||||
|
end := int(offset[1])
|
||||||
|
if prevEnd > begin {
|
||||||
|
begin = prevEnd
|
||||||
|
}
|
||||||
|
if end > prevEnd {
|
||||||
|
prevEnd = end
|
||||||
|
}
|
||||||
|
if end > begin {
|
||||||
|
matchlen += end - begin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rank := Rank{uint16(matchlen), uint16(len(*i.text)), i.index}
|
||||||
|
if cache {
|
||||||
|
i.rank = rank
|
||||||
|
}
|
||||||
|
return rank
|
||||||
|
}
|
||||||
|
|
||||||
|
// AsString returns the original string
|
||||||
|
func (i *Item) AsString() string {
|
||||||
|
if i.origText != nil {
|
||||||
|
return *i.origText
|
||||||
|
}
|
||||||
|
return *i.text
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByOrder is for sorting substring offsets
|
||||||
|
type ByOrder []Offset
|
||||||
|
|
||||||
|
func (a ByOrder) Len() int {
|
||||||
|
return len(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByOrder) Swap(i, j int) {
|
||||||
|
a[i], a[j] = a[j], a[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByOrder) Less(i, j int) bool {
|
||||||
|
ioff := a[i]
|
||||||
|
joff := a[j]
|
||||||
|
return (ioff[0] < joff[0]) || (ioff[0] == joff[0]) && (ioff[1] <= joff[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByRelevance is for sorting Items
|
||||||
|
type ByRelevance []*Item
|
||||||
|
|
||||||
|
func (a ByRelevance) Len() int {
|
||||||
|
return len(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByRelevance) Swap(i, j int) {
|
||||||
|
a[i], a[j] = a[j], a[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByRelevance) Less(i, j int) bool {
|
||||||
|
irank := a[i].Rank(true)
|
||||||
|
jrank := a[j].Rank(true)
|
||||||
|
|
||||||
|
return compareRanks(irank, jrank, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByRelevanceTac is for sorting Items
|
||||||
|
type ByRelevanceTac []*Item
|
||||||
|
|
||||||
|
func (a ByRelevanceTac) Len() int {
|
||||||
|
return len(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByRelevanceTac) Swap(i, j int) {
|
||||||
|
a[i], a[j] = a[j], a[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByRelevanceTac) Less(i, j int) bool {
|
||||||
|
irank := a[i].Rank(true)
|
||||||
|
jrank := a[j].Rank(true)
|
||||||
|
|
||||||
|
return compareRanks(irank, jrank, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareRanks(irank Rank, jrank Rank, tac bool) bool {
|
||||||
|
if irank.matchlen < jrank.matchlen {
|
||||||
|
return true
|
||||||
|
} else if irank.matchlen > jrank.matchlen {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if irank.strlen < jrank.strlen {
|
||||||
|
return true
|
||||||
|
} else if irank.strlen > jrank.strlen {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return (irank.index <= jrank.index) != tac
|
||||||
|
}
|
74
src/item_test.go
Normal file
74
src/item_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOffsetSort(t *testing.T) {
|
||||||
|
offsets := []Offset{
|
||||||
|
Offset{3, 5}, Offset{2, 7},
|
||||||
|
Offset{1, 3}, Offset{2, 9}}
|
||||||
|
sort.Sort(ByOrder(offsets))
|
||||||
|
|
||||||
|
if offsets[0][0] != 1 || offsets[0][1] != 3 ||
|
||||||
|
offsets[1][0] != 2 || offsets[1][1] != 7 ||
|
||||||
|
offsets[2][0] != 2 || offsets[2][1] != 9 ||
|
||||||
|
offsets[3][0] != 3 || offsets[3][1] != 5 {
|
||||||
|
t.Error("Invalid order:", offsets)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRankComparison(t *testing.T) {
|
||||||
|
if compareRanks(Rank{3, 0, 5}, Rank{2, 0, 7}, false) ||
|
||||||
|
!compareRanks(Rank{3, 0, 5}, Rank{3, 0, 6}, false) ||
|
||||||
|
!compareRanks(Rank{1, 2, 3}, Rank{1, 3, 2}, false) ||
|
||||||
|
!compareRanks(Rank{0, 0, 0}, Rank{0, 0, 0}, false) {
|
||||||
|
t.Error("Invalid order")
|
||||||
|
}
|
||||||
|
|
||||||
|
if compareRanks(Rank{3, 0, 5}, Rank{2, 0, 7}, true) ||
|
||||||
|
!compareRanks(Rank{3, 0, 5}, Rank{3, 0, 6}, false) ||
|
||||||
|
!compareRanks(Rank{1, 2, 3}, Rank{1, 3, 2}, true) ||
|
||||||
|
!compareRanks(Rank{0, 0, 0}, Rank{0, 0, 0}, false) {
|
||||||
|
t.Error("Invalid order (tac)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match length, string length, index
|
||||||
|
func TestItemRank(t *testing.T) {
|
||||||
|
strs := []string{"foo", "foobar", "bar", "baz"}
|
||||||
|
item1 := Item{text: &strs[0], index: 1, offsets: []Offset{}}
|
||||||
|
rank1 := item1.Rank(true)
|
||||||
|
if rank1.matchlen != 0 || rank1.strlen != 3 || rank1.index != 1 {
|
||||||
|
t.Error(item1.Rank(true))
|
||||||
|
}
|
||||||
|
// Only differ in index
|
||||||
|
item2 := Item{text: &strs[0], index: 0, offsets: []Offset{}}
|
||||||
|
|
||||||
|
items := []*Item{&item1, &item2}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if items[0] != &item2 || items[1] != &item1 {
|
||||||
|
t.Error(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
items = []*Item{&item2, &item1, &item1, &item2}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if items[0] != &item2 || items[1] != &item2 ||
|
||||||
|
items[2] != &item1 || items[3] != &item1 {
|
||||||
|
t.Error(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by relevance
|
||||||
|
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}}}
|
||||||
|
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}}}
|
||||||
|
items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if items[0] != &item2 || items[1] != &item1 ||
|
||||||
|
items[2] != &item6 || items[3] != &item4 ||
|
||||||
|
items[4] != &item5 || items[5] != &item3 {
|
||||||
|
t.Error(items)
|
||||||
|
}
|
||||||
|
}
|
218
src/matcher.go
Normal file
218
src/matcher.go
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MatchRequest represents a search request
|
||||||
|
type MatchRequest struct {
|
||||||
|
chunks []*Chunk
|
||||||
|
pattern *Pattern
|
||||||
|
final bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Matcher is responsible for performing search
|
||||||
|
type Matcher struct {
|
||||||
|
patternBuilder func([]rune) *Pattern
|
||||||
|
sort bool
|
||||||
|
tac bool
|
||||||
|
eventBox *util.EventBox
|
||||||
|
reqBox *util.EventBox
|
||||||
|
partitions int
|
||||||
|
mergerCache map[string]*Merger
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
reqRetry util.EventType = iota
|
||||||
|
reqReset
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
progressMinDuration = 200 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewMatcher returns a new Matcher
|
||||||
|
func NewMatcher(patternBuilder func([]rune) *Pattern,
|
||||||
|
sort bool, tac bool, eventBox *util.EventBox) *Matcher {
|
||||||
|
return &Matcher{
|
||||||
|
patternBuilder: patternBuilder,
|
||||||
|
sort: sort,
|
||||||
|
tac: tac,
|
||||||
|
eventBox: eventBox,
|
||||||
|
reqBox: util.NewEventBox(),
|
||||||
|
partitions: runtime.NumCPU(),
|
||||||
|
mergerCache: make(map[string]*Merger)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop puts Matcher in action
|
||||||
|
func (m *Matcher) Loop() {
|
||||||
|
prevCount := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
var request MatchRequest
|
||||||
|
|
||||||
|
m.reqBox.Wait(func(events *util.Events) {
|
||||||
|
for _, val := range *events {
|
||||||
|
switch val := val.(type) {
|
||||||
|
case MatchRequest:
|
||||||
|
request = val
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("Unexpected type: %T", val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
events.Clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Restart search
|
||||||
|
patternString := request.pattern.AsString()
|
||||||
|
var merger *Merger
|
||||||
|
cancelled := false
|
||||||
|
count := CountItems(request.chunks)
|
||||||
|
|
||||||
|
foundCache := false
|
||||||
|
if count == prevCount {
|
||||||
|
// Look up mergerCache
|
||||||
|
if cached, found := m.mergerCache[patternString]; found {
|
||||||
|
foundCache = true
|
||||||
|
merger = cached
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Invalidate mergerCache
|
||||||
|
prevCount = count
|
||||||
|
m.mergerCache = make(map[string]*Merger)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundCache {
|
||||||
|
merger, cancelled = m.scan(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cancelled {
|
||||||
|
m.mergerCache[patternString] = merger
|
||||||
|
merger.final = request.final
|
||||||
|
m.eventBox.Set(EvtSearchFin, merger)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {
|
||||||
|
perSlice := len(chunks) / m.partitions
|
||||||
|
|
||||||
|
// No need to parallelize
|
||||||
|
if perSlice == 0 {
|
||||||
|
return [][]*Chunk{chunks}
|
||||||
|
}
|
||||||
|
|
||||||
|
slices := make([][]*Chunk, m.partitions)
|
||||||
|
for i := 0; i < m.partitions; i++ {
|
||||||
|
start := i * perSlice
|
||||||
|
end := start + perSlice
|
||||||
|
if i == m.partitions-1 {
|
||||||
|
end = len(chunks)
|
||||||
|
}
|
||||||
|
slices[i] = chunks[start:end]
|
||||||
|
}
|
||||||
|
return slices
|
||||||
|
}
|
||||||
|
|
||||||
|
type partialResult struct {
|
||||||
|
index int
|
||||||
|
matches []*Item
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
||||||
|
startedAt := time.Now()
|
||||||
|
|
||||||
|
numChunks := len(request.chunks)
|
||||||
|
if numChunks == 0 {
|
||||||
|
return EmptyMerger, false
|
||||||
|
}
|
||||||
|
pattern := request.pattern
|
||||||
|
empty := pattern.IsEmpty()
|
||||||
|
cancelled := util.NewAtomicBool(false)
|
||||||
|
|
||||||
|
slices := m.sliceChunks(request.chunks)
|
||||||
|
numSlices := len(slices)
|
||||||
|
resultChan := make(chan partialResult, numSlices)
|
||||||
|
countChan := make(chan int, numChunks)
|
||||||
|
waitGroup := sync.WaitGroup{}
|
||||||
|
|
||||||
|
for idx, chunks := range slices {
|
||||||
|
waitGroup.Add(1)
|
||||||
|
go func(idx int, chunks []*Chunk) {
|
||||||
|
defer func() { waitGroup.Done() }()
|
||||||
|
sliceMatches := []*Item{}
|
||||||
|
for _, chunk := range chunks {
|
||||||
|
var matches []*Item
|
||||||
|
if empty {
|
||||||
|
matches = *chunk
|
||||||
|
} else {
|
||||||
|
matches = request.pattern.Match(chunk)
|
||||||
|
}
|
||||||
|
sliceMatches = append(sliceMatches, matches...)
|
||||||
|
if cancelled.Get() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
countChan <- len(matches)
|
||||||
|
}
|
||||||
|
if !empty && m.sort {
|
||||||
|
if m.tac {
|
||||||
|
sort.Sort(ByRelevanceTac(sliceMatches))
|
||||||
|
} else {
|
||||||
|
sort.Sort(ByRelevance(sliceMatches))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resultChan <- partialResult{idx, sliceMatches}
|
||||||
|
}(idx, chunks)
|
||||||
|
}
|
||||||
|
|
||||||
|
wait := func() bool {
|
||||||
|
cancelled.Set(true)
|
||||||
|
waitGroup.Wait()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
matchCount := 0
|
||||||
|
for matchesInChunk := range countChan {
|
||||||
|
count++
|
||||||
|
matchCount += matchesInChunk
|
||||||
|
|
||||||
|
if count == numChunks {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !empty && m.reqBox.Peek(reqReset) {
|
||||||
|
return nil, wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Now().Sub(startedAt) > progressMinDuration {
|
||||||
|
m.eventBox.Set(EvtSearchProgress, float32(count)/float32(numChunks))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
partialResults := make([][]*Item, numSlices)
|
||||||
|
for range slices {
|
||||||
|
partialResult := <-resultChan
|
||||||
|
partialResults[partialResult.index] = partialResult.matches
|
||||||
|
}
|
||||||
|
return NewMerger(partialResults, !empty && m.sort, m.tac), false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset is called to interrupt/signal the ongoing search
|
||||||
|
func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool) {
|
||||||
|
pattern := m.patternBuilder(patternRunes)
|
||||||
|
|
||||||
|
var event util.EventType
|
||||||
|
if cancel {
|
||||||
|
event = reqReset
|
||||||
|
} else {
|
||||||
|
event = reqRetry
|
||||||
|
}
|
||||||
|
m.reqBox.Set(event, MatchRequest{chunks, pattern, final})
|
||||||
|
}
|
90
src/merger.go
Normal file
90
src/merger.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Merger with no data
|
||||||
|
var EmptyMerger = NewMerger([][]*Item{}, false, false)
|
||||||
|
|
||||||
|
// Merger holds a set of locally sorted lists of items and provides the view of
|
||||||
|
// a single, globally-sorted list
|
||||||
|
type Merger struct {
|
||||||
|
lists [][]*Item
|
||||||
|
merged []*Item
|
||||||
|
cursors []int
|
||||||
|
sorted bool
|
||||||
|
tac bool
|
||||||
|
final bool
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMerger returns a new Merger
|
||||||
|
func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger {
|
||||||
|
mg := Merger{
|
||||||
|
lists: lists,
|
||||||
|
merged: []*Item{},
|
||||||
|
cursors: make([]int, len(lists)),
|
||||||
|
sorted: sorted,
|
||||||
|
tac: tac,
|
||||||
|
final: false,
|
||||||
|
count: 0}
|
||||||
|
|
||||||
|
for _, list := range mg.lists {
|
||||||
|
mg.count += len(list)
|
||||||
|
}
|
||||||
|
return &mg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Length returns the number of items
|
||||||
|
func (mg *Merger) Length() int {
|
||||||
|
return mg.count
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the pointer to the Item object indexed by the given integer
|
||||||
|
func (mg *Merger) Get(idx int) *Item {
|
||||||
|
if mg.sorted {
|
||||||
|
return mg.mergedGet(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mg.tac {
|
||||||
|
idx = mg.Length() - idx - 1
|
||||||
|
}
|
||||||
|
for _, list := range mg.lists {
|
||||||
|
numItems := len(list)
|
||||||
|
if idx < numItems {
|
||||||
|
return list[idx]
|
||||||
|
}
|
||||||
|
idx -= numItems
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("Index out of bounds (unsorted, %d/%d)", idx, mg.count))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mg *Merger) mergedGet(idx int) *Item {
|
||||||
|
for i := len(mg.merged); i <= idx; i++ {
|
||||||
|
minRank := Rank{0, 0, 0}
|
||||||
|
minIdx := -1
|
||||||
|
for listIdx, list := range mg.lists {
|
||||||
|
cursor := mg.cursors[listIdx]
|
||||||
|
if cursor < 0 || cursor == len(list) {
|
||||||
|
mg.cursors[listIdx] = -1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cursor >= 0 {
|
||||||
|
rank := list[cursor].Rank(false)
|
||||||
|
if minIdx < 0 || compareRanks(rank, minRank, mg.tac) {
|
||||||
|
minRank = rank
|
||||||
|
minIdx = listIdx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mg.cursors[listIdx] = cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
if minIdx >= 0 {
|
||||||
|
chosen := mg.lists[minIdx]
|
||||||
|
mg.merged = append(mg.merged, chosen[mg.cursors[minIdx]])
|
||||||
|
mg.cursors[minIdx]++
|
||||||
|
} else {
|
||||||
|
panic(fmt.Sprintf("Index out of bounds (sorted, %d/%d)", i, mg.count))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mg.merged[idx]
|
||||||
|
}
|
93
src/merger_test.go
Normal file
93
src/merger_test.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func assert(t *testing.T, cond bool, msg ...string) {
|
||||||
|
if !cond {
|
||||||
|
t.Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func randItem() *Item {
|
||||||
|
str := fmt.Sprintf("%d", rand.Uint32())
|
||||||
|
offsets := make([]Offset, rand.Int()%3)
|
||||||
|
for idx := range offsets {
|
||||||
|
sidx := int32(rand.Uint32() % 20)
|
||||||
|
eidx := sidx + int32(rand.Uint32()%20)
|
||||||
|
offsets[idx] = Offset{sidx, eidx}
|
||||||
|
}
|
||||||
|
return &Item{
|
||||||
|
text: &str,
|
||||||
|
index: rand.Uint32(),
|
||||||
|
offsets: offsets}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyMerger(t *testing.T) {
|
||||||
|
assert(t, EmptyMerger.Length() == 0, "Not empty")
|
||||||
|
assert(t, EmptyMerger.count == 0, "Invalid count")
|
||||||
|
assert(t, len(EmptyMerger.lists) == 0, "Invalid lists")
|
||||||
|
assert(t, len(EmptyMerger.merged) == 0, "Invalid merged list")
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLists(partiallySorted bool) ([][]*Item, []*Item) {
|
||||||
|
numLists := 4
|
||||||
|
lists := make([][]*Item, numLists)
|
||||||
|
cnt := 0
|
||||||
|
for i := 0; i < numLists; i++ {
|
||||||
|
numItems := rand.Int() % 20
|
||||||
|
cnt += numItems
|
||||||
|
lists[i] = make([]*Item, numItems)
|
||||||
|
for j := 0; j < numItems; j++ {
|
||||||
|
item := randItem()
|
||||||
|
lists[i][j] = item
|
||||||
|
}
|
||||||
|
if partiallySorted {
|
||||||
|
sort.Sort(ByRelevance(lists[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
items := []*Item{}
|
||||||
|
for _, list := range lists {
|
||||||
|
items = append(items, list...)
|
||||||
|
}
|
||||||
|
return lists, items
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergerUnsorted(t *testing.T) {
|
||||||
|
lists, items := buildLists(false)
|
||||||
|
cnt := len(items)
|
||||||
|
|
||||||
|
// Not sorted: same order
|
||||||
|
mg := NewMerger(lists, false, false)
|
||||||
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
||||||
|
for i := 0; i < cnt; i++ {
|
||||||
|
assert(t, items[i] == mg.Get(i), "Invalid Get")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergerSorted(t *testing.T) {
|
||||||
|
lists, items := buildLists(true)
|
||||||
|
cnt := len(items)
|
||||||
|
|
||||||
|
// Sorted sorted order
|
||||||
|
mg := NewMerger(lists, true, false)
|
||||||
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
for i := 0; i < cnt; i++ {
|
||||||
|
if items[i] != mg.Get(i) {
|
||||||
|
t.Error("Not sorted", items[i], mg.Get(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inverse order
|
||||||
|
mg2 := NewMerger(lists, true, false)
|
||||||
|
for i := cnt - 1; i >= 0; i-- {
|
||||||
|
if items[i] != mg2.Get(i) {
|
||||||
|
t.Error("Not sorted", items[i], mg2.Get(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
310
src/options.go
Normal file
310
src/options.go
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/junegunn/go-shellwords"
|
||||||
|
)
|
||||||
|
|
||||||
|
const usage = `usage: fzf [options]
|
||||||
|
|
||||||
|
Search mode
|
||||||
|
-x, --extended Extended-search mode
|
||||||
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
|
-i Case-insensitive match (default: smart-case match)
|
||||||
|
+i Case-sensitive match
|
||||||
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
|
for limiting search scope. Each can be a non-zero
|
||||||
|
integer or a range expression ([BEGIN]..[END])
|
||||||
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
|
Search result
|
||||||
|
+s, --no-sort Do not sort the result
|
||||||
|
--tac Reverse the order of the input
|
||||||
|
(e.g. 'history | fzf --tac --no-sort')
|
||||||
|
|
||||||
|
Interface
|
||||||
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
|
--no-mouse Disable mouse
|
||||||
|
+c, --no-color Disable colors
|
||||||
|
+2, --no-256 Disable 256-color
|
||||||
|
--black Use black background
|
||||||
|
--reverse Reverse orientation
|
||||||
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
|
Scripting
|
||||||
|
-q, --query=STR Start the finder with the given query
|
||||||
|
-1, --select-1 Automatically select the only match
|
||||||
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
--print-query Print query as the first line
|
||||||
|
--sync Synchronous search for multi-staged filtering
|
||||||
|
(e.g. 'fzf --multi | fzf --sync')
|
||||||
|
|
||||||
|
Environment variables
|
||||||
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
|
FZF_DEFAULT_OPTS Defaults options. (e.g. '-x -m')
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
// Mode denotes the current search mode
|
||||||
|
type Mode int
|
||||||
|
|
||||||
|
// Search modes
|
||||||
|
const (
|
||||||
|
ModeFuzzy Mode = iota
|
||||||
|
ModeExtended
|
||||||
|
ModeExtendedExact
|
||||||
|
)
|
||||||
|
|
||||||
|
// Case denotes case-sensitivity of search
|
||||||
|
type Case int
|
||||||
|
|
||||||
|
// Case-sensitivities
|
||||||
|
const (
|
||||||
|
CaseSmart Case = iota
|
||||||
|
CaseIgnore
|
||||||
|
CaseRespect
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options stores the values of command-line options
|
||||||
|
type Options struct {
|
||||||
|
Mode Mode
|
||||||
|
Case Case
|
||||||
|
Nth []Range
|
||||||
|
WithNth []Range
|
||||||
|
Delimiter *regexp.Regexp
|
||||||
|
Sort int
|
||||||
|
Tac bool
|
||||||
|
Multi bool
|
||||||
|
Mouse bool
|
||||||
|
Color bool
|
||||||
|
Color256 bool
|
||||||
|
Black bool
|
||||||
|
Reverse bool
|
||||||
|
Prompt string
|
||||||
|
Query string
|
||||||
|
Select1 bool
|
||||||
|
Exit0 bool
|
||||||
|
Filter *string
|
||||||
|
PrintQuery bool
|
||||||
|
Sync bool
|
||||||
|
Version bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() *Options {
|
||||||
|
return &Options{
|
||||||
|
Mode: ModeFuzzy,
|
||||||
|
Case: CaseSmart,
|
||||||
|
Nth: make([]Range, 0),
|
||||||
|
WithNth: make([]Range, 0),
|
||||||
|
Delimiter: nil,
|
||||||
|
Sort: 1000,
|
||||||
|
Tac: false,
|
||||||
|
Multi: false,
|
||||||
|
Mouse: true,
|
||||||
|
Color: true,
|
||||||
|
Color256: strings.Contains(os.Getenv("TERM"), "256"),
|
||||||
|
Black: false,
|
||||||
|
Reverse: false,
|
||||||
|
Prompt: "> ",
|
||||||
|
Query: "",
|
||||||
|
Select1: false,
|
||||||
|
Exit0: false,
|
||||||
|
Filter: nil,
|
||||||
|
PrintQuery: false,
|
||||||
|
Sync: false,
|
||||||
|
Version: false}
|
||||||
|
}
|
||||||
|
|
||||||
|
func help(ok int) {
|
||||||
|
os.Stderr.WriteString(usage)
|
||||||
|
os.Exit(ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorExit(msg string) {
|
||||||
|
os.Stderr.WriteString(msg + "\n")
|
||||||
|
help(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func optString(arg string, prefix string) (bool, string) {
|
||||||
|
rx, _ := regexp.Compile(fmt.Sprintf("^(?:%s)(.*)$", prefix))
|
||||||
|
matches := rx.FindStringSubmatch(arg)
|
||||||
|
if len(matches) > 1 {
|
||||||
|
return true, matches[1]
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextString(args []string, i *int, message string) string {
|
||||||
|
if len(args) > *i+1 {
|
||||||
|
*i++
|
||||||
|
} else {
|
||||||
|
errorExit(message)
|
||||||
|
}
|
||||||
|
return args[*i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func optionalNumeric(args []string, i *int) int {
|
||||||
|
if len(args) > *i+1 {
|
||||||
|
if strings.IndexAny(args[*i+1], "0123456789") == 0 {
|
||||||
|
*i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1 // Don't care
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitNth(str string) []Range {
|
||||||
|
if match, _ := regexp.MatchString("^[0-9,-.]+$", str); !match {
|
||||||
|
errorExit("invalid format: " + str)
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens := strings.Split(str, ",")
|
||||||
|
ranges := make([]Range, len(tokens))
|
||||||
|
for idx, s := range tokens {
|
||||||
|
r, ok := ParseRange(&s)
|
||||||
|
if !ok {
|
||||||
|
errorExit("invalid format: " + str)
|
||||||
|
}
|
||||||
|
ranges[idx] = r
|
||||||
|
}
|
||||||
|
return ranges
|
||||||
|
}
|
||||||
|
|
||||||
|
func delimiterRegexp(str string) *regexp.Regexp {
|
||||||
|
rx, e := regexp.Compile(str)
|
||||||
|
if e != nil {
|
||||||
|
str = regexp.QuoteMeta(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
rx, e = regexp.Compile(fmt.Sprintf("(?:.*?%s)|(?:.+?$)", str))
|
||||||
|
if e != nil {
|
||||||
|
errorExit("invalid regular expression: " + e.Error())
|
||||||
|
}
|
||||||
|
return rx
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptions(opts *Options, allArgs []string) {
|
||||||
|
for i := 0; i < len(allArgs); i++ {
|
||||||
|
arg := allArgs[i]
|
||||||
|
switch arg {
|
||||||
|
case "-h", "--help":
|
||||||
|
help(0)
|
||||||
|
case "-x", "--extended":
|
||||||
|
opts.Mode = ModeExtended
|
||||||
|
case "-e", "--extended-exact":
|
||||||
|
opts.Mode = ModeExtendedExact
|
||||||
|
case "+x", "--no-extended", "+e", "--no-extended-exact":
|
||||||
|
opts.Mode = ModeFuzzy
|
||||||
|
case "-q", "--query":
|
||||||
|
opts.Query = nextString(allArgs, &i, "query string required")
|
||||||
|
case "-f", "--filter":
|
||||||
|
filter := nextString(allArgs, &i, "query string required")
|
||||||
|
opts.Filter = &filter
|
||||||
|
case "-d", "--delimiter":
|
||||||
|
opts.Delimiter = delimiterRegexp(nextString(allArgs, &i, "delimiter required"))
|
||||||
|
case "-n", "--nth":
|
||||||
|
opts.Nth = splitNth(nextString(allArgs, &i, "nth expression required"))
|
||||||
|
case "--with-nth":
|
||||||
|
opts.WithNth = splitNth(nextString(allArgs, &i, "nth expression required"))
|
||||||
|
case "-s", "--sort":
|
||||||
|
opts.Sort = optionalNumeric(allArgs, &i)
|
||||||
|
case "+s", "--no-sort":
|
||||||
|
opts.Sort = 0
|
||||||
|
case "--tac":
|
||||||
|
opts.Tac = true
|
||||||
|
case "--no-tac":
|
||||||
|
opts.Tac = false
|
||||||
|
case "-i":
|
||||||
|
opts.Case = CaseIgnore
|
||||||
|
case "+i":
|
||||||
|
opts.Case = CaseRespect
|
||||||
|
case "-m", "--multi":
|
||||||
|
opts.Multi = true
|
||||||
|
case "+m", "--no-multi":
|
||||||
|
opts.Multi = false
|
||||||
|
case "--no-mouse":
|
||||||
|
opts.Mouse = false
|
||||||
|
case "+c", "--no-color":
|
||||||
|
opts.Color = false
|
||||||
|
case "+2", "--no-256":
|
||||||
|
opts.Color256 = false
|
||||||
|
case "--black":
|
||||||
|
opts.Black = true
|
||||||
|
case "--no-black":
|
||||||
|
opts.Black = false
|
||||||
|
case "--reverse":
|
||||||
|
opts.Reverse = true
|
||||||
|
case "--no-reverse":
|
||||||
|
opts.Reverse = false
|
||||||
|
case "-1", "--select-1":
|
||||||
|
opts.Select1 = true
|
||||||
|
case "+1", "--no-select-1":
|
||||||
|
opts.Select1 = false
|
||||||
|
case "-0", "--exit-0":
|
||||||
|
opts.Exit0 = true
|
||||||
|
case "+0", "--no-exit-0":
|
||||||
|
opts.Exit0 = false
|
||||||
|
case "--print-query":
|
||||||
|
opts.PrintQuery = true
|
||||||
|
case "--no-print-query":
|
||||||
|
opts.PrintQuery = false
|
||||||
|
case "--prompt":
|
||||||
|
opts.Prompt = nextString(allArgs, &i, "prompt string required")
|
||||||
|
case "--sync":
|
||||||
|
opts.Sync = true
|
||||||
|
case "--no-sync":
|
||||||
|
opts.Sync = false
|
||||||
|
case "--async":
|
||||||
|
opts.Sync = false
|
||||||
|
case "--version":
|
||||||
|
opts.Version = true
|
||||||
|
default:
|
||||||
|
if match, value := optString(arg, "-q|--query="); match {
|
||||||
|
opts.Query = value
|
||||||
|
} else if match, value := optString(arg, "-f|--filter="); match {
|
||||||
|
opts.Filter = &value
|
||||||
|
} else if match, value := optString(arg, "-d|--delimiter="); match {
|
||||||
|
opts.Delimiter = delimiterRegexp(value)
|
||||||
|
} else if match, value := optString(arg, "--prompt="); match {
|
||||||
|
opts.Prompt = value
|
||||||
|
} else if match, value := optString(arg, "-n|--nth="); match {
|
||||||
|
opts.Nth = splitNth(value)
|
||||||
|
} else if match, value := optString(arg, "--with-nth="); match {
|
||||||
|
opts.WithNth = splitNth(value)
|
||||||
|
} else if match, _ := optString(arg, "-s|--sort="); match {
|
||||||
|
opts.Sort = 1 // Don't care
|
||||||
|
} else {
|
||||||
|
errorExit("unknown option: " + arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're not using extended search mode, --nth option becomes irrelevant
|
||||||
|
// if it contains the whole range
|
||||||
|
if opts.Mode == ModeFuzzy || len(opts.Nth) == 1 {
|
||||||
|
for _, r := range opts.Nth {
|
||||||
|
if r.begin == rangeEllipsis && r.end == rangeEllipsis {
|
||||||
|
opts.Nth = make([]Range, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseOptions parses command-line options
|
||||||
|
func ParseOptions() *Options {
|
||||||
|
opts := defaultOptions()
|
||||||
|
|
||||||
|
// Options from Env var
|
||||||
|
words, _ := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
|
||||||
|
parseOptions(opts, words)
|
||||||
|
|
||||||
|
// Options from command-line arguments
|
||||||
|
parseOptions(opts, os.Args[1:])
|
||||||
|
return opts
|
||||||
|
}
|
67
src/options_test.go
Normal file
67
src/options_test.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestDelimiterRegex(t *testing.T) {
|
||||||
|
rx := delimiterRegexp("*")
|
||||||
|
tokens := rx.FindAllString("-*--*---**---", -1)
|
||||||
|
if tokens[0] != "-*" || tokens[1] != "--*" || tokens[2] != "---*" ||
|
||||||
|
tokens[3] != "*" || tokens[4] != "---" {
|
||||||
|
t.Errorf("%s %s %d", rx, tokens, len(tokens))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitNth(t *testing.T) {
|
||||||
|
{
|
||||||
|
ranges := splitNth("..")
|
||||||
|
if len(ranges) != 1 ||
|
||||||
|
ranges[0].begin != rangeEllipsis ||
|
||||||
|
ranges[0].end != rangeEllipsis {
|
||||||
|
t.Errorf("%s", ranges)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ranges := splitNth("..3,1..,2..3,4..-1,-3..-2,..,2,-2,2..-2,1..-1")
|
||||||
|
if len(ranges) != 10 ||
|
||||||
|
ranges[0].begin != rangeEllipsis || ranges[0].end != 3 ||
|
||||||
|
ranges[1].begin != rangeEllipsis || ranges[1].end != rangeEllipsis ||
|
||||||
|
ranges[2].begin != 2 || ranges[2].end != 3 ||
|
||||||
|
ranges[3].begin != 4 || ranges[3].end != rangeEllipsis ||
|
||||||
|
ranges[4].begin != -3 || ranges[4].end != -2 ||
|
||||||
|
ranges[5].begin != rangeEllipsis || ranges[5].end != rangeEllipsis ||
|
||||||
|
ranges[6].begin != 2 || ranges[6].end != 2 ||
|
||||||
|
ranges[7].begin != -2 || ranges[7].end != -2 ||
|
||||||
|
ranges[8].begin != 2 || ranges[8].end != -2 ||
|
||||||
|
ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis {
|
||||||
|
t.Errorf("%s", ranges)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIrrelevantNth(t *testing.T) {
|
||||||
|
{
|
||||||
|
opts := defaultOptions()
|
||||||
|
words := []string{"--nth", "..", "-x"}
|
||||||
|
parseOptions(opts, words)
|
||||||
|
if len(opts.Nth) != 0 {
|
||||||
|
t.Errorf("nth should be empty: %s", opts.Nth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, words := range [][]string{[]string{"--nth", "..,3"}, []string{"--nth", "3,1.."}, []string{"--nth", "..-1,1"}} {
|
||||||
|
{
|
||||||
|
opts := defaultOptions()
|
||||||
|
parseOptions(opts, words)
|
||||||
|
if len(opts.Nth) != 0 {
|
||||||
|
t.Errorf("nth should be empty: %s", opts.Nth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
opts := defaultOptions()
|
||||||
|
words = append(words, "-x")
|
||||||
|
parseOptions(opts, words)
|
||||||
|
if len(opts.Nth) != 2 {
|
||||||
|
t.Errorf("nth should not be empty: %s", opts.Nth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
320
src/pattern.go
Normal file
320
src/pattern.go
Normal file
@@ -0,0 +1,320 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/algo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
|
// fuzzy
|
||||||
|
// 'exact
|
||||||
|
// ^exact-prefix
|
||||||
|
// exact-suffix$
|
||||||
|
// !not-fuzzy
|
||||||
|
// !'not-exact
|
||||||
|
// !^not-exact-prefix
|
||||||
|
// !not-exact-suffix$
|
||||||
|
|
||||||
|
type termType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
termFuzzy termType = iota
|
||||||
|
termExact
|
||||||
|
termPrefix
|
||||||
|
termSuffix
|
||||||
|
)
|
||||||
|
|
||||||
|
type term struct {
|
||||||
|
typ termType
|
||||||
|
inv bool
|
||||||
|
text []rune
|
||||||
|
origText []rune
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pattern represents search pattern
|
||||||
|
type Pattern struct {
|
||||||
|
mode Mode
|
||||||
|
caseSensitive bool
|
||||||
|
text []rune
|
||||||
|
terms []term
|
||||||
|
hasInvTerm bool
|
||||||
|
delimiter *regexp.Regexp
|
||||||
|
nth []Range
|
||||||
|
procFun map[termType]func(bool, *string, []rune) (int, int)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_patternCache map[string]*Pattern
|
||||||
|
_splitRegex *regexp.Regexp
|
||||||
|
_cache ChunkCache
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// We can uniquely identify the pattern for a given string since
|
||||||
|
// mode and caseMode do not change while the program is running
|
||||||
|
_patternCache = make(map[string]*Pattern)
|
||||||
|
_splitRegex = regexp.MustCompile("\\s+")
|
||||||
|
_cache = NewChunkCache()
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearPatternCache() {
|
||||||
|
_patternCache = make(map[string]*Pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildPattern builds Pattern object from the given arguments
|
||||||
|
func BuildPattern(mode Mode, caseMode Case,
|
||||||
|
nth []Range, delimiter *regexp.Regexp, runes []rune) *Pattern {
|
||||||
|
|
||||||
|
var asString string
|
||||||
|
switch mode {
|
||||||
|
case ModeExtended, ModeExtendedExact:
|
||||||
|
asString = strings.Trim(string(runes), " ")
|
||||||
|
default:
|
||||||
|
asString = string(runes)
|
||||||
|
}
|
||||||
|
|
||||||
|
cached, found := _patternCache[asString]
|
||||||
|
if found {
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
|
||||||
|
caseSensitive, hasInvTerm := true, false
|
||||||
|
terms := []term{}
|
||||||
|
|
||||||
|
switch caseMode {
|
||||||
|
case CaseSmart:
|
||||||
|
if !strings.ContainsAny(asString, uppercaseLetters) {
|
||||||
|
runes, caseSensitive = []rune(strings.ToLower(asString)), false
|
||||||
|
}
|
||||||
|
case CaseIgnore:
|
||||||
|
runes, caseSensitive = []rune(strings.ToLower(asString)), false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch mode {
|
||||||
|
case ModeExtended, ModeExtendedExact:
|
||||||
|
terms = parseTerms(mode, string(runes))
|
||||||
|
for _, term := range terms {
|
||||||
|
if term.inv {
|
||||||
|
hasInvTerm = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr := &Pattern{
|
||||||
|
mode: mode,
|
||||||
|
caseSensitive: caseSensitive,
|
||||||
|
text: runes,
|
||||||
|
terms: terms,
|
||||||
|
hasInvTerm: hasInvTerm,
|
||||||
|
nth: nth,
|
||||||
|
delimiter: delimiter,
|
||||||
|
procFun: make(map[termType]func(bool, *string, []rune) (int, int))}
|
||||||
|
|
||||||
|
ptr.procFun[termFuzzy] = algo.FuzzyMatch
|
||||||
|
ptr.procFun[termExact] = algo.ExactMatchNaive
|
||||||
|
ptr.procFun[termPrefix] = algo.PrefixMatch
|
||||||
|
ptr.procFun[termSuffix] = algo.SuffixMatch
|
||||||
|
|
||||||
|
_patternCache[asString] = ptr
|
||||||
|
return ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseTerms(mode Mode, str string) []term {
|
||||||
|
tokens := _splitRegex.Split(str, -1)
|
||||||
|
terms := []term{}
|
||||||
|
for _, token := range tokens {
|
||||||
|
typ, inv, text := termFuzzy, false, token
|
||||||
|
origText := []rune(text)
|
||||||
|
if mode == ModeExtendedExact {
|
||||||
|
typ = termExact
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(text, "!") {
|
||||||
|
inv = true
|
||||||
|
text = text[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(text, "'") {
|
||||||
|
if mode == ModeExtended {
|
||||||
|
typ = termExact
|
||||||
|
text = text[1:]
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(text, "^") {
|
||||||
|
typ = termPrefix
|
||||||
|
text = text[1:]
|
||||||
|
} else if strings.HasSuffix(text, "$") {
|
||||||
|
typ = termSuffix
|
||||||
|
text = text[:len(text)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(text) > 0 {
|
||||||
|
terms = append(terms, term{
|
||||||
|
typ: typ,
|
||||||
|
inv: inv,
|
||||||
|
text: []rune(text),
|
||||||
|
origText: origText})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return terms
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEmpty returns true if the pattern is effectively empty
|
||||||
|
func (p *Pattern) IsEmpty() bool {
|
||||||
|
if p.mode == ModeFuzzy {
|
||||||
|
return len(p.text) == 0
|
||||||
|
}
|
||||||
|
return len(p.terms) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// AsString returns the search query in string type
|
||||||
|
func (p *Pattern) AsString() string {
|
||||||
|
return string(p.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CacheKey is used to build string to be used as the key of result cache
|
||||||
|
func (p *Pattern) CacheKey() string {
|
||||||
|
if p.mode == ModeFuzzy {
|
||||||
|
return p.AsString()
|
||||||
|
}
|
||||||
|
cacheableTerms := []string{}
|
||||||
|
for _, term := range p.terms {
|
||||||
|
if term.inv {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cacheableTerms = append(cacheableTerms, string(term.origText))
|
||||||
|
}
|
||||||
|
return strings.Join(cacheableTerms, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match returns the list of matches Items in the given Chunk
|
||||||
|
func (p *Pattern) Match(chunk *Chunk) []*Item {
|
||||||
|
space := chunk
|
||||||
|
|
||||||
|
// ChunkCache: Exact match
|
||||||
|
cacheKey := p.CacheKey()
|
||||||
|
if !p.hasInvTerm { // Because we're excluding Inv-term from cache key
|
||||||
|
if cached, found := _cache.Find(chunk, cacheKey); found {
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChunkCache: Prefix/suffix match
|
||||||
|
Loop:
|
||||||
|
for idx := 1; idx < len(cacheKey); idx++ {
|
||||||
|
// [---------| ] | [ |---------]
|
||||||
|
// [--------| ] | [ |--------]
|
||||||
|
// [-------| ] | [ |-------]
|
||||||
|
prefix := cacheKey[:len(cacheKey)-idx]
|
||||||
|
suffix := cacheKey[idx:]
|
||||||
|
for _, substr := range [2]*string{&prefix, &suffix} {
|
||||||
|
if cached, found := _cache.Find(chunk, *substr); found {
|
||||||
|
cachedChunk := Chunk(cached)
|
||||||
|
space = &cachedChunk
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := p.matchChunk(space)
|
||||||
|
|
||||||
|
if !p.hasInvTerm {
|
||||||
|
_cache.Add(chunk, cacheKey, matches)
|
||||||
|
}
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
||||||
|
matches := []*Item{}
|
||||||
|
if p.mode == ModeFuzzy {
|
||||||
|
for _, item := range *chunk {
|
||||||
|
if sidx, eidx := p.fuzzyMatch(item); sidx >= 0 {
|
||||||
|
matches = append(matches,
|
||||||
|
dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, item := range *chunk {
|
||||||
|
if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {
|
||||||
|
matches = append(matches, dupItem(item, offsets))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatchItem returns true if the Item is a match
|
||||||
|
func (p *Pattern) MatchItem(item *Item) bool {
|
||||||
|
if p.mode == ModeFuzzy {
|
||||||
|
sidx, _ := p.fuzzyMatch(item)
|
||||||
|
return sidx >= 0
|
||||||
|
}
|
||||||
|
offsets := p.extendedMatch(item)
|
||||||
|
return len(offsets) == len(p.terms)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dupItem(item *Item, offsets []Offset) *Item {
|
||||||
|
sort.Sort(ByOrder(offsets))
|
||||||
|
return &Item{
|
||||||
|
text: item.text,
|
||||||
|
origText: item.origText,
|
||||||
|
transformed: item.transformed,
|
||||||
|
index: item.index,
|
||||||
|
offsets: offsets,
|
||||||
|
rank: Rank{0, 0, item.index}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pattern) fuzzyMatch(item *Item) (int, int) {
|
||||||
|
input := p.prepareInput(item)
|
||||||
|
return p.iter(algo.FuzzyMatch, input, p.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
||||||
|
input := p.prepareInput(item)
|
||||||
|
offsets := []Offset{}
|
||||||
|
for _, term := range p.terms {
|
||||||
|
pfun := p.procFun[term.typ]
|
||||||
|
if sidx, eidx := p.iter(pfun, input, term.text); sidx >= 0 {
|
||||||
|
if term.inv {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
offsets = append(offsets, Offset{int32(sidx), int32(eidx)})
|
||||||
|
} else if term.inv {
|
||||||
|
offsets = append(offsets, Offset{0, 0})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return offsets
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pattern) prepareInput(item *Item) *Transformed {
|
||||||
|
if item.transformed != nil {
|
||||||
|
return item.transformed
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret *Transformed
|
||||||
|
if len(p.nth) > 0 {
|
||||||
|
tokens := Tokenize(item.text, p.delimiter)
|
||||||
|
ret = Transform(tokens, p.nth)
|
||||||
|
} else {
|
||||||
|
trans := Transformed{
|
||||||
|
whole: item.text,
|
||||||
|
parts: []Token{Token{text: item.text, prefixLength: 0}}}
|
||||||
|
ret = &trans
|
||||||
|
}
|
||||||
|
item.transformed = ret
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pattern) iter(pfun func(bool, *string, []rune) (int, int),
|
||||||
|
inputs *Transformed, pattern []rune) (int, int) {
|
||||||
|
for _, part := range inputs.parts {
|
||||||
|
prefixLength := part.prefixLength
|
||||||
|
if sidx, eidx := pfun(p.caseSensitive, part.text, pattern); sidx >= 0 {
|
||||||
|
return sidx + prefixLength, eidx + prefixLength
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, -1
|
||||||
|
}
|
116
src/pattern_test.go
Normal file
116
src/pattern_test.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/algo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseTermsExtended(t *testing.T) {
|
||||||
|
terms := parseTerms(ModeExtended,
|
||||||
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
||||||
|
if len(terms) != 8 ||
|
||||||
|
terms[0].typ != termFuzzy || terms[0].inv ||
|
||||||
|
terms[1].typ != termExact || terms[1].inv ||
|
||||||
|
terms[2].typ != termPrefix || terms[2].inv ||
|
||||||
|
terms[3].typ != termSuffix || terms[3].inv ||
|
||||||
|
terms[4].typ != termFuzzy || !terms[4].inv ||
|
||||||
|
terms[5].typ != termExact || !terms[5].inv ||
|
||||||
|
terms[6].typ != termPrefix || !terms[6].inv ||
|
||||||
|
terms[7].typ != termSuffix || !terms[7].inv {
|
||||||
|
t.Errorf("%s", terms)
|
||||||
|
}
|
||||||
|
for idx, term := range terms {
|
||||||
|
if len(term.text) != 3 {
|
||||||
|
t.Errorf("%s", term)
|
||||||
|
}
|
||||||
|
if idx > 0 && len(term.origText) != 4+idx/5 {
|
||||||
|
t.Errorf("%s", term)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTermsExtendedExact(t *testing.T) {
|
||||||
|
terms := parseTerms(ModeExtendedExact,
|
||||||
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
||||||
|
if len(terms) != 8 ||
|
||||||
|
terms[0].typ != termExact || terms[0].inv || len(terms[0].text) != 3 ||
|
||||||
|
terms[1].typ != termExact || terms[1].inv || len(terms[1].text) != 4 ||
|
||||||
|
terms[2].typ != termPrefix || terms[2].inv || len(terms[2].text) != 3 ||
|
||||||
|
terms[3].typ != termSuffix || terms[3].inv || len(terms[3].text) != 3 ||
|
||||||
|
terms[4].typ != termExact || !terms[4].inv || len(terms[4].text) != 3 ||
|
||||||
|
terms[5].typ != termExact || !terms[5].inv || len(terms[5].text) != 4 ||
|
||||||
|
terms[6].typ != termPrefix || !terms[6].inv || len(terms[6].text) != 3 ||
|
||||||
|
terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {
|
||||||
|
t.Errorf("%s", terms)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTermsEmpty(t *testing.T) {
|
||||||
|
terms := parseTerms(ModeExtended, "' $ ^ !' !^ !$")
|
||||||
|
if len(terms) != 0 {
|
||||||
|
t.Errorf("%s", terms)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExact(t *testing.T) {
|
||||||
|
defer clearPatternCache()
|
||||||
|
clearPatternCache()
|
||||||
|
pattern := BuildPattern(ModeExtended, CaseSmart,
|
||||||
|
[]Range{}, nil, []rune("'abc"))
|
||||||
|
str := "aabbcc abc"
|
||||||
|
sidx, eidx := algo.ExactMatchNaive(pattern.caseSensitive, &str, pattern.terms[0].text)
|
||||||
|
if sidx != 7 || eidx != 10 {
|
||||||
|
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCaseSensitivity(t *testing.T) {
|
||||||
|
defer clearPatternCache()
|
||||||
|
clearPatternCache()
|
||||||
|
pat1 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, nil, []rune("abc"))
|
||||||
|
clearPatternCache()
|
||||||
|
pat2 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, nil, []rune("Abc"))
|
||||||
|
clearPatternCache()
|
||||||
|
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, nil, []rune("abc"))
|
||||||
|
clearPatternCache()
|
||||||
|
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, nil, []rune("Abc"))
|
||||||
|
clearPatternCache()
|
||||||
|
pat5 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, nil, []rune("abc"))
|
||||||
|
clearPatternCache()
|
||||||
|
pat6 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, nil, []rune("Abc"))
|
||||||
|
|
||||||
|
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
||||||
|
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
||||||
|
string(pat3.text) != "abc" || pat3.caseSensitive != false ||
|
||||||
|
string(pat4.text) != "abc" || pat4.caseSensitive != false ||
|
||||||
|
string(pat5.text) != "abc" || pat5.caseSensitive != true ||
|
||||||
|
string(pat6.text) != "Abc" || pat6.caseSensitive != true {
|
||||||
|
t.Error("Invalid case conversion")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrigTextAndTransformed(t *testing.T) {
|
||||||
|
strptr := func(str string) *string {
|
||||||
|
return &str
|
||||||
|
}
|
||||||
|
pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, nil, []rune("jg"))
|
||||||
|
tokens := Tokenize(strptr("junegunn"), nil)
|
||||||
|
trans := Transform(tokens, []Range{Range{1, 1}})
|
||||||
|
|
||||||
|
for _, mode := range []Mode{ModeFuzzy, ModeExtended} {
|
||||||
|
chunk := Chunk{
|
||||||
|
&Item{
|
||||||
|
text: strptr("junegunn"),
|
||||||
|
origText: strptr("junegunn.choi"),
|
||||||
|
transformed: trans},
|
||||||
|
}
|
||||||
|
pattern.mode = mode
|
||||||
|
matches := pattern.matchChunk(&chunk)
|
||||||
|
if *matches[0].text != "junegunn" || *matches[0].origText != "junegunn.choi" ||
|
||||||
|
matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||
|
||||||
|
matches[0].transformed != trans {
|
||||||
|
t.Error("Invalid match result", matches)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
59
src/reader.go
Normal file
59
src/reader.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultCommand = `find * -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null`
|
||||||
|
|
||||||
|
// Reader reads from command or standard input
|
||||||
|
type Reader struct {
|
||||||
|
pusher func(string)
|
||||||
|
eventBox *util.EventBox
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadSource reads data from the default command or from standard input
|
||||||
|
func (r *Reader) ReadSource() {
|
||||||
|
if util.IsTty() {
|
||||||
|
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
|
||||||
|
if len(cmd) == 0 {
|
||||||
|
cmd = defaultCommand
|
||||||
|
}
|
||||||
|
r.readFromCommand(cmd)
|
||||||
|
} else {
|
||||||
|
r.readFromStdin()
|
||||||
|
}
|
||||||
|
r.eventBox.Set(EvtReadFin, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) feed(src io.Reader) {
|
||||||
|
if scanner := bufio.NewScanner(src); scanner != nil {
|
||||||
|
for scanner.Scan() {
|
||||||
|
r.pusher(scanner.Text())
|
||||||
|
r.eventBox.Set(EvtReadNew, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) readFromStdin() {
|
||||||
|
r.feed(os.Stdin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) readFromCommand(cmd string) {
|
||||||
|
listCommand := exec.Command("sh", "-c", cmd)
|
||||||
|
out, err := listCommand.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = listCommand.Start()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer listCommand.Wait()
|
||||||
|
r.feed(out)
|
||||||
|
}
|
56
src/reader_test.go
Normal file
56
src/reader_test.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReadFromCommand(t *testing.T) {
|
||||||
|
strs := []string{}
|
||||||
|
eb := util.NewEventBox()
|
||||||
|
reader := Reader{
|
||||||
|
pusher: func(s string) { strs = append(strs, s) },
|
||||||
|
eventBox: eb}
|
||||||
|
|
||||||
|
// Check EventBox
|
||||||
|
if eb.Peek(EvtReadNew) {
|
||||||
|
t.Error("EvtReadNew should not be set yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal command
|
||||||
|
reader.readFromCommand(`echo abc && echo def`)
|
||||||
|
if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
|
||||||
|
t.Errorf("%s", strs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check EventBox again
|
||||||
|
if !eb.Peek(EvtReadNew) {
|
||||||
|
t.Error("EvtReadNew should be set yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait should return immediately
|
||||||
|
eb.Wait(func(events *util.Events) {
|
||||||
|
if _, found := (*events)[EvtReadNew]; !found {
|
||||||
|
t.Errorf("%s", events)
|
||||||
|
}
|
||||||
|
events.Clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
// EventBox is cleared
|
||||||
|
if eb.Peek(EvtReadNew) {
|
||||||
|
t.Error("EvtReadNew should not be set yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Failing command
|
||||||
|
reader.readFromCommand(`no-such-command`)
|
||||||
|
strs = []string{}
|
||||||
|
if len(strs) > 0 {
|
||||||
|
t.Errorf("%s", strs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check EventBox again
|
||||||
|
if eb.Peek(EvtReadNew) {
|
||||||
|
t.Error("Command failed. EvtReadNew should be set")
|
||||||
|
}
|
||||||
|
}
|
709
src/terminal.go
Normal file
709
src/terminal.go
Normal file
@@ -0,0 +1,709 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
C "github.com/junegunn/fzf/src/curses"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
|
||||||
|
"github.com/junegunn/go-runewidth"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Terminal represents terminal input/output
|
||||||
|
type Terminal struct {
|
||||||
|
prompt string
|
||||||
|
reverse bool
|
||||||
|
cx int
|
||||||
|
cy int
|
||||||
|
offset int
|
||||||
|
yanked []rune
|
||||||
|
input []rune
|
||||||
|
multi bool
|
||||||
|
printQuery bool
|
||||||
|
count int
|
||||||
|
progress int
|
||||||
|
reading bool
|
||||||
|
merger *Merger
|
||||||
|
selected map[*string]selectedItem
|
||||||
|
reqBox *util.EventBox
|
||||||
|
eventBox *util.EventBox
|
||||||
|
mutex sync.Mutex
|
||||||
|
initFunc func()
|
||||||
|
suppress bool
|
||||||
|
startChan chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type selectedItem struct {
|
||||||
|
at time.Time
|
||||||
|
text *string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ByTimeOrder []selectedItem
|
||||||
|
|
||||||
|
func (a ByTimeOrder) Len() int {
|
||||||
|
return len(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByTimeOrder) Swap(i, j int) {
|
||||||
|
a[i], a[j] = a[j], a[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ByTimeOrder) Less(i, j int) bool {
|
||||||
|
return a[i].at.Before(a[j].at)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
|
||||||
|
var _runeWidths = make(map[rune]int)
|
||||||
|
|
||||||
|
const (
|
||||||
|
reqPrompt util.EventType = iota
|
||||||
|
reqInfo
|
||||||
|
reqList
|
||||||
|
reqRefresh
|
||||||
|
reqRedraw
|
||||||
|
reqClose
|
||||||
|
reqQuit
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
initialDelay = 100 * time.Millisecond
|
||||||
|
spinnerDuration = 200 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTerminal returns new Terminal object
|
||||||
|
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
||||||
|
input := []rune(opts.Query)
|
||||||
|
return &Terminal{
|
||||||
|
prompt: opts.Prompt,
|
||||||
|
reverse: opts.Reverse,
|
||||||
|
cx: len(input),
|
||||||
|
cy: 0,
|
||||||
|
offset: 0,
|
||||||
|
yanked: []rune{},
|
||||||
|
input: input,
|
||||||
|
multi: opts.Multi,
|
||||||
|
printQuery: opts.PrintQuery,
|
||||||
|
merger: EmptyMerger,
|
||||||
|
selected: make(map[*string]selectedItem),
|
||||||
|
reqBox: util.NewEventBox(),
|
||||||
|
eventBox: eventBox,
|
||||||
|
mutex: sync.Mutex{},
|
||||||
|
suppress: true,
|
||||||
|
startChan: make(chan bool, 1),
|
||||||
|
initFunc: func() {
|
||||||
|
C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse)
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input returns current query string
|
||||||
|
func (t *Terminal) Input() []rune {
|
||||||
|
t.mutex.Lock()
|
||||||
|
defer t.mutex.Unlock()
|
||||||
|
return copySlice(t.input)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCount updates the count information
|
||||||
|
func (t *Terminal) UpdateCount(cnt int, final bool) {
|
||||||
|
t.mutex.Lock()
|
||||||
|
t.count = cnt
|
||||||
|
t.reading = !final
|
||||||
|
t.mutex.Unlock()
|
||||||
|
t.reqBox.Set(reqInfo, nil)
|
||||||
|
if final {
|
||||||
|
t.reqBox.Set(reqRefresh, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateProgress updates the search progress
|
||||||
|
func (t *Terminal) UpdateProgress(progress float32) {
|
||||||
|
t.mutex.Lock()
|
||||||
|
newProgress := int(progress * 100)
|
||||||
|
changed := t.progress != newProgress
|
||||||
|
t.progress = newProgress
|
||||||
|
t.mutex.Unlock()
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
t.reqBox.Set(reqInfo, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateList updates Merger to display the list
|
||||||
|
func (t *Terminal) UpdateList(merger *Merger) {
|
||||||
|
t.mutex.Lock()
|
||||||
|
t.progress = 100
|
||||||
|
t.merger = merger
|
||||||
|
t.mutex.Unlock()
|
||||||
|
t.reqBox.Set(reqInfo, nil)
|
||||||
|
t.reqBox.Set(reqList, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) output() {
|
||||||
|
if t.printQuery {
|
||||||
|
fmt.Println(string(t.input))
|
||||||
|
}
|
||||||
|
if len(t.selected) == 0 {
|
||||||
|
cnt := t.merger.Length()
|
||||||
|
if cnt > 0 && cnt > t.cy {
|
||||||
|
fmt.Println(t.merger.Get(t.cy).AsString())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sels := make([]selectedItem, 0, len(t.selected))
|
||||||
|
for _, sel := range t.selected {
|
||||||
|
sels = append(sels, sel)
|
||||||
|
}
|
||||||
|
sort.Sort(ByTimeOrder(sels))
|
||||||
|
for _, sel := range sels {
|
||||||
|
fmt.Println(*sel.text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runeWidth(r rune, prefixWidth int) int {
|
||||||
|
if r == '\t' {
|
||||||
|
return 8 - prefixWidth%8
|
||||||
|
} else if w, found := _runeWidths[r]; found {
|
||||||
|
return w
|
||||||
|
} else {
|
||||||
|
w := runewidth.RuneWidth(r)
|
||||||
|
_runeWidths[r] = w
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayWidth(runes []rune) int {
|
||||||
|
l := 0
|
||||||
|
for _, r := range runes {
|
||||||
|
l += runeWidth(r, l)
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) move(y int, x int, clear bool) {
|
||||||
|
maxy := C.MaxY()
|
||||||
|
if !t.reverse {
|
||||||
|
y = maxy - y - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if clear {
|
||||||
|
C.MoveAndClear(y, x)
|
||||||
|
} else {
|
||||||
|
C.Move(y, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) placeCursor() {
|
||||||
|
t.move(0, len(t.prompt)+displayWidth(t.input[:t.cx]), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) printPrompt() {
|
||||||
|
t.move(0, 0, true)
|
||||||
|
C.CPrint(C.ColPrompt, true, t.prompt)
|
||||||
|
C.CPrint(C.ColNormal, true, string(t.input))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) printInfo() {
|
||||||
|
t.move(1, 0, true)
|
||||||
|
if t.reading {
|
||||||
|
duration := int64(spinnerDuration)
|
||||||
|
idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration
|
||||||
|
C.CPrint(C.ColSpinner, true, _spinner[idx])
|
||||||
|
}
|
||||||
|
|
||||||
|
t.move(1, 2, false)
|
||||||
|
output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count)
|
||||||
|
if t.multi && len(t.selected) > 0 {
|
||||||
|
output += fmt.Sprintf(" (%d)", len(t.selected))
|
||||||
|
}
|
||||||
|
if t.progress > 0 && t.progress < 100 {
|
||||||
|
output += fmt.Sprintf(" (%d%%)", t.progress)
|
||||||
|
}
|
||||||
|
C.CPrint(C.ColInfo, false, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) printList() {
|
||||||
|
t.constrain()
|
||||||
|
|
||||||
|
maxy := maxItems()
|
||||||
|
count := t.merger.Length() - t.offset
|
||||||
|
for i := 0; i < maxy; i++ {
|
||||||
|
t.move(i+2, 0, true)
|
||||||
|
if i < count {
|
||||||
|
t.printItem(t.merger.Get(i+t.offset), i == t.cy-t.offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) printItem(item *Item, current bool) {
|
||||||
|
_, selected := t.selected[item.text]
|
||||||
|
if current {
|
||||||
|
C.CPrint(C.ColCursor, true, ">")
|
||||||
|
if selected {
|
||||||
|
C.CPrint(C.ColCurrent, true, ">")
|
||||||
|
} else {
|
||||||
|
C.CPrint(C.ColCurrent, true, " ")
|
||||||
|
}
|
||||||
|
t.printHighlighted(item, true, C.ColCurrent, C.ColCurrentMatch)
|
||||||
|
} else {
|
||||||
|
C.CPrint(C.ColCursor, true, " ")
|
||||||
|
if selected {
|
||||||
|
C.CPrint(C.ColSelected, true, ">")
|
||||||
|
} else {
|
||||||
|
C.Print(" ")
|
||||||
|
}
|
||||||
|
t.printHighlighted(item, false, 0, C.ColMatch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimRight(runes []rune, width int) ([]rune, int) {
|
||||||
|
// We start from the beginning to handle tab characters
|
||||||
|
l := 0
|
||||||
|
for idx, r := range runes {
|
||||||
|
l += runeWidth(r, l)
|
||||||
|
if idx > 0 && l > width {
|
||||||
|
return runes[:idx], len(runes) - idx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return runes, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayWidthWithLimit(runes []rune, prefixWidth int, limit int) int {
|
||||||
|
l := 0
|
||||||
|
for _, r := range runes {
|
||||||
|
l += runeWidth(r, l+prefixWidth)
|
||||||
|
if l > limit {
|
||||||
|
// Early exit
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimLeft(runes []rune, width int) ([]rune, int32) {
|
||||||
|
currentWidth := displayWidth(runes)
|
||||||
|
var trimmed int32
|
||||||
|
|
||||||
|
for currentWidth > width && len(runes) > 0 {
|
||||||
|
runes = runes[1:]
|
||||||
|
trimmed++
|
||||||
|
currentWidth = displayWidthWithLimit(runes, 2, width)
|
||||||
|
}
|
||||||
|
return runes, trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int) {
|
||||||
|
var maxe int32
|
||||||
|
for _, offset := range item.offsets {
|
||||||
|
if offset[1] > maxe {
|
||||||
|
maxe = offset[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overflow
|
||||||
|
text := []rune(*item.text)
|
||||||
|
offsets := item.offsets
|
||||||
|
maxWidth := C.MaxX() - 3
|
||||||
|
fullWidth := displayWidth(text)
|
||||||
|
if fullWidth > maxWidth {
|
||||||
|
// Stri..
|
||||||
|
matchEndWidth := displayWidth(text[:maxe])
|
||||||
|
if matchEndWidth <= maxWidth-2 {
|
||||||
|
text, _ = trimRight(text, maxWidth-2)
|
||||||
|
text = append(text, []rune("..")...)
|
||||||
|
} else {
|
||||||
|
// Stri..
|
||||||
|
if matchEndWidth < fullWidth-2 {
|
||||||
|
text = append(text[:maxe], []rune("..")...)
|
||||||
|
}
|
||||||
|
// ..ri..
|
||||||
|
var diff int32
|
||||||
|
text, diff = trimLeft(text, maxWidth-2)
|
||||||
|
|
||||||
|
// Transform offsets
|
||||||
|
offsets = make([]Offset, len(item.offsets))
|
||||||
|
for idx, offset := range item.offsets {
|
||||||
|
b, e := offset[0], offset[1]
|
||||||
|
b += 2 - diff
|
||||||
|
e += 2 - diff
|
||||||
|
b = util.Max32(b, 2)
|
||||||
|
if b < e {
|
||||||
|
offsets[idx] = Offset{b, e}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
text = append([]rune(".."), text...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(ByOrder(offsets))
|
||||||
|
var index int32
|
||||||
|
var substr string
|
||||||
|
var prefixWidth int
|
||||||
|
for _, offset := range offsets {
|
||||||
|
b := util.Max32(index, offset[0])
|
||||||
|
e := util.Max32(index, offset[1])
|
||||||
|
|
||||||
|
substr, prefixWidth = processTabs(text[index:b], prefixWidth)
|
||||||
|
C.CPrint(col1, bold, substr)
|
||||||
|
|
||||||
|
substr, prefixWidth = processTabs(text[b:e], prefixWidth)
|
||||||
|
C.CPrint(col2, bold, substr)
|
||||||
|
|
||||||
|
index = e
|
||||||
|
}
|
||||||
|
if index < int32(len(text)) {
|
||||||
|
substr, _ = processTabs(text[index:], prefixWidth)
|
||||||
|
C.CPrint(col1, bold, substr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processTabs(runes []rune, prefixWidth int) (string, int) {
|
||||||
|
var strbuf bytes.Buffer
|
||||||
|
l := prefixWidth
|
||||||
|
for _, r := range runes {
|
||||||
|
w := runeWidth(r, l)
|
||||||
|
l += w
|
||||||
|
if r == '\t' {
|
||||||
|
strbuf.WriteString(strings.Repeat(" ", w))
|
||||||
|
} else {
|
||||||
|
strbuf.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strbuf.String(), l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) printAll() {
|
||||||
|
t.printList()
|
||||||
|
t.printInfo()
|
||||||
|
t.printPrompt()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) refresh() {
|
||||||
|
if !t.suppress {
|
||||||
|
C.Refresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) delChar() bool {
|
||||||
|
if len(t.input) > 0 && t.cx < len(t.input) {
|
||||||
|
t.input = append(t.input[:t.cx], t.input[t.cx+1:]...)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func findLastMatch(pattern string, str string) int {
|
||||||
|
rx, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
locs := rx.FindAllStringIndex(str, -1)
|
||||||
|
if locs == nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return locs[len(locs)-1][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func findFirstMatch(pattern string, str string) int {
|
||||||
|
rx, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
loc := rx.FindStringIndex(str)
|
||||||
|
if loc == nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return loc[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func copySlice(slice []rune) []rune {
|
||||||
|
ret := make([]rune, len(slice))
|
||||||
|
copy(ret, slice)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) rubout(pattern string) {
|
||||||
|
pcx := t.cx
|
||||||
|
after := t.input[t.cx:]
|
||||||
|
t.cx = findLastMatch(pattern, string(t.input[:t.cx])) + 1
|
||||||
|
t.yanked = copySlice(t.input[t.cx:pcx])
|
||||||
|
t.input = append(t.input[:t.cx], after...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop is called to start Terminal I/O
|
||||||
|
func (t *Terminal) Loop() {
|
||||||
|
<-t.startChan
|
||||||
|
{ // Late initialization
|
||||||
|
t.mutex.Lock()
|
||||||
|
t.initFunc()
|
||||||
|
t.printPrompt()
|
||||||
|
t.placeCursor()
|
||||||
|
C.Refresh()
|
||||||
|
t.printInfo()
|
||||||
|
t.mutex.Unlock()
|
||||||
|
go func() {
|
||||||
|
timer := time.NewTimer(initialDelay)
|
||||||
|
<-timer.C
|
||||||
|
t.reqBox.Set(reqRefresh, nil)
|
||||||
|
}()
|
||||||
|
|
||||||
|
resizeChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(resizeChan, syscall.SIGWINCH)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
<-resizeChan
|
||||||
|
t.reqBox.Set(reqRedraw, nil)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
t.reqBox.Wait(func(events *util.Events) {
|
||||||
|
defer events.Clear()
|
||||||
|
t.mutex.Lock()
|
||||||
|
for req := range *events {
|
||||||
|
switch req {
|
||||||
|
case reqPrompt:
|
||||||
|
t.printPrompt()
|
||||||
|
case reqInfo:
|
||||||
|
t.printInfo()
|
||||||
|
case reqList:
|
||||||
|
t.printList()
|
||||||
|
case reqRefresh:
|
||||||
|
t.suppress = false
|
||||||
|
case reqRedraw:
|
||||||
|
C.Clear()
|
||||||
|
C.Endwin()
|
||||||
|
C.Refresh()
|
||||||
|
t.printAll()
|
||||||
|
case reqClose:
|
||||||
|
C.Close()
|
||||||
|
t.output()
|
||||||
|
os.Exit(0)
|
||||||
|
case reqQuit:
|
||||||
|
C.Close()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.placeCursor()
|
||||||
|
t.mutex.Unlock()
|
||||||
|
})
|
||||||
|
t.refresh()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
looping := true
|
||||||
|
for looping {
|
||||||
|
event := C.GetChar()
|
||||||
|
|
||||||
|
t.mutex.Lock()
|
||||||
|
previousInput := t.input
|
||||||
|
events := []util.EventType{reqPrompt}
|
||||||
|
req := func(evts ...util.EventType) {
|
||||||
|
for _, event := range evts {
|
||||||
|
events = append(events, event)
|
||||||
|
if event == reqClose || event == reqQuit {
|
||||||
|
looping = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toggle := func() {
|
||||||
|
if t.cy < t.merger.Length() {
|
||||||
|
item := t.merger.Get(t.cy)
|
||||||
|
if _, found := t.selected[item.text]; !found {
|
||||||
|
var strptr *string
|
||||||
|
if item.origText != nil {
|
||||||
|
strptr = item.origText
|
||||||
|
} else {
|
||||||
|
strptr = item.text
|
||||||
|
}
|
||||||
|
t.selected[item.text] = selectedItem{time.Now(), strptr}
|
||||||
|
} else {
|
||||||
|
delete(t.selected, item.text)
|
||||||
|
}
|
||||||
|
req(reqInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch event.Type {
|
||||||
|
case C.Invalid:
|
||||||
|
t.mutex.Unlock()
|
||||||
|
continue
|
||||||
|
case C.CtrlA:
|
||||||
|
t.cx = 0
|
||||||
|
case C.CtrlB:
|
||||||
|
if t.cx > 0 {
|
||||||
|
t.cx--
|
||||||
|
}
|
||||||
|
case C.CtrlC, C.CtrlG, C.CtrlQ, C.ESC:
|
||||||
|
req(reqQuit)
|
||||||
|
case C.CtrlD:
|
||||||
|
if !t.delChar() && t.cx == 0 {
|
||||||
|
req(reqQuit)
|
||||||
|
}
|
||||||
|
case C.CtrlE:
|
||||||
|
t.cx = len(t.input)
|
||||||
|
case C.CtrlF:
|
||||||
|
if t.cx < len(t.input) {
|
||||||
|
t.cx++
|
||||||
|
}
|
||||||
|
case C.CtrlH:
|
||||||
|
if t.cx > 0 {
|
||||||
|
t.input = append(t.input[:t.cx-1], t.input[t.cx:]...)
|
||||||
|
t.cx--
|
||||||
|
}
|
||||||
|
case C.Tab:
|
||||||
|
if t.multi && t.merger.Length() > 0 {
|
||||||
|
toggle()
|
||||||
|
t.vmove(-1)
|
||||||
|
req(reqList)
|
||||||
|
}
|
||||||
|
case C.BTab:
|
||||||
|
if t.multi && t.merger.Length() > 0 {
|
||||||
|
toggle()
|
||||||
|
t.vmove(1)
|
||||||
|
req(reqList)
|
||||||
|
}
|
||||||
|
case C.CtrlJ, C.CtrlN:
|
||||||
|
t.vmove(-1)
|
||||||
|
req(reqList)
|
||||||
|
case C.CtrlK, C.CtrlP:
|
||||||
|
t.vmove(1)
|
||||||
|
req(reqList)
|
||||||
|
case C.CtrlM:
|
||||||
|
req(reqClose)
|
||||||
|
case C.CtrlL:
|
||||||
|
req(reqRedraw)
|
||||||
|
case C.CtrlU:
|
||||||
|
if t.cx > 0 {
|
||||||
|
t.yanked = copySlice(t.input[:t.cx])
|
||||||
|
t.input = t.input[t.cx:]
|
||||||
|
t.cx = 0
|
||||||
|
}
|
||||||
|
case C.CtrlW:
|
||||||
|
if t.cx > 0 {
|
||||||
|
t.rubout("\\s\\S")
|
||||||
|
}
|
||||||
|
case C.AltBS:
|
||||||
|
if t.cx > 0 {
|
||||||
|
t.rubout("[^[:alnum:]][[:alnum:]]")
|
||||||
|
}
|
||||||
|
case C.CtrlY:
|
||||||
|
suffix := copySlice(t.input[t.cx:])
|
||||||
|
t.input = append(append(t.input[:t.cx], t.yanked...), suffix...)
|
||||||
|
t.cx += len(t.yanked)
|
||||||
|
case C.Del:
|
||||||
|
t.delChar()
|
||||||
|
case C.PgUp:
|
||||||
|
t.vmove(maxItems() - 1)
|
||||||
|
req(reqList)
|
||||||
|
case C.PgDn:
|
||||||
|
t.vmove(-(maxItems() - 1))
|
||||||
|
req(reqList)
|
||||||
|
case C.AltB:
|
||||||
|
t.cx = findLastMatch("[^[:alnum:]][[:alnum:]]", string(t.input[:t.cx])) + 1
|
||||||
|
case C.AltF:
|
||||||
|
t.cx += findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 1
|
||||||
|
case C.AltD:
|
||||||
|
ncx := t.cx +
|
||||||
|
findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 1
|
||||||
|
if ncx > t.cx {
|
||||||
|
t.yanked = copySlice(t.input[t.cx:ncx])
|
||||||
|
t.input = append(t.input[:t.cx], t.input[ncx:]...)
|
||||||
|
}
|
||||||
|
case C.Rune:
|
||||||
|
prefix := copySlice(t.input[:t.cx])
|
||||||
|
t.input = append(append(prefix, event.Char), t.input[t.cx:]...)
|
||||||
|
t.cx++
|
||||||
|
case C.Mouse:
|
||||||
|
me := event.MouseEvent
|
||||||
|
mx, my := util.Constrain(me.X-len(t.prompt), 0, len(t.input)), me.Y
|
||||||
|
if !t.reverse {
|
||||||
|
my = C.MaxY() - my - 1
|
||||||
|
}
|
||||||
|
if me.S != 0 {
|
||||||
|
// Scroll
|
||||||
|
if t.merger.Length() > 0 {
|
||||||
|
if t.multi && me.Mod {
|
||||||
|
toggle()
|
||||||
|
}
|
||||||
|
t.vmove(me.S)
|
||||||
|
req(reqList)
|
||||||
|
}
|
||||||
|
} else if me.Double {
|
||||||
|
// Double-click
|
||||||
|
if my >= 2 {
|
||||||
|
if t.vset(my-2) && t.cy < t.merger.Length() {
|
||||||
|
req(reqClose)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if me.Down {
|
||||||
|
if my == 0 && mx >= 0 {
|
||||||
|
// Prompt
|
||||||
|
t.cx = mx
|
||||||
|
} else if my >= 2 {
|
||||||
|
// List
|
||||||
|
if t.vset(t.offset+my-2) && t.multi && me.Mod {
|
||||||
|
toggle()
|
||||||
|
}
|
||||||
|
req(reqList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
changed := string(previousInput) != string(t.input)
|
||||||
|
t.mutex.Unlock() // Must be unlocked before touching reqBox
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
t.eventBox.Set(EvtSearchNew, nil)
|
||||||
|
}
|
||||||
|
for _, event := range events {
|
||||||
|
t.reqBox.Set(event, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) constrain() {
|
||||||
|
count := t.merger.Length()
|
||||||
|
height := C.MaxY() - 2
|
||||||
|
diffpos := t.cy - t.offset
|
||||||
|
|
||||||
|
t.cy = util.Constrain(t.cy, 0, count-1)
|
||||||
|
|
||||||
|
if t.cy > t.offset+(height-1) {
|
||||||
|
// Ceil
|
||||||
|
t.offset = t.cy - (height - 1)
|
||||||
|
} else if t.offset > t.cy {
|
||||||
|
// Floor
|
||||||
|
t.offset = t.cy
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjustment
|
||||||
|
if count-t.offset < height {
|
||||||
|
t.offset = util.Max(0, count-height)
|
||||||
|
t.cy = util.Constrain(t.offset+diffpos, 0, count-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) vmove(o int) {
|
||||||
|
if t.reverse {
|
||||||
|
t.vset(t.cy - o)
|
||||||
|
} else {
|
||||||
|
t.vset(t.cy + o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Terminal) vset(o int) bool {
|
||||||
|
t.cy = util.Constrain(o, 0, t.merger.Length()-1)
|
||||||
|
return t.cy == o
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxItems() int {
|
||||||
|
return C.MaxY() - 2
|
||||||
|
}
|
214
src/tokenizer.go
Normal file
214
src/tokenizer.go
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const rangeEllipsis = 0
|
||||||
|
|
||||||
|
// Range represents nth-expression
|
||||||
|
type Range struct {
|
||||||
|
begin int
|
||||||
|
end int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transformed holds the result of tokenization and transformation
|
||||||
|
type Transformed struct {
|
||||||
|
whole *string
|
||||||
|
parts []Token
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token contains the tokenized part of the strings and its prefix length
|
||||||
|
type Token struct {
|
||||||
|
text *string
|
||||||
|
prefixLength int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRange(begin int, end int) Range {
|
||||||
|
if begin == 1 {
|
||||||
|
begin = rangeEllipsis
|
||||||
|
}
|
||||||
|
if end == -1 {
|
||||||
|
end = rangeEllipsis
|
||||||
|
}
|
||||||
|
return Range{begin, end}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseRange parses nth-expression and returns the corresponding Range object
|
||||||
|
func ParseRange(str *string) (Range, bool) {
|
||||||
|
if (*str) == ".." {
|
||||||
|
return newRange(rangeEllipsis, rangeEllipsis), true
|
||||||
|
} else if strings.HasPrefix(*str, "..") {
|
||||||
|
end, err := strconv.Atoi((*str)[2:])
|
||||||
|
if err != nil || end == 0 {
|
||||||
|
return Range{}, false
|
||||||
|
}
|
||||||
|
return newRange(rangeEllipsis, end), true
|
||||||
|
} else if strings.HasSuffix(*str, "..") {
|
||||||
|
begin, err := strconv.Atoi((*str)[:len(*str)-2])
|
||||||
|
if err != nil || begin == 0 {
|
||||||
|
return Range{}, false
|
||||||
|
}
|
||||||
|
return newRange(begin, rangeEllipsis), true
|
||||||
|
} else if strings.Contains(*str, "..") {
|
||||||
|
ns := strings.Split(*str, "..")
|
||||||
|
if len(ns) != 2 {
|
||||||
|
return Range{}, false
|
||||||
|
}
|
||||||
|
begin, err1 := strconv.Atoi(ns[0])
|
||||||
|
end, err2 := strconv.Atoi(ns[1])
|
||||||
|
if err1 != nil || err2 != nil || begin == 0 || end == 0 {
|
||||||
|
return Range{}, false
|
||||||
|
}
|
||||||
|
return newRange(begin, end), true
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := strconv.Atoi(*str)
|
||||||
|
if err != nil || n == 0 {
|
||||||
|
return Range{}, false
|
||||||
|
}
|
||||||
|
return newRange(n, n), true
|
||||||
|
}
|
||||||
|
|
||||||
|
func withPrefixLengths(tokens []string, begin int) []Token {
|
||||||
|
ret := make([]Token, len(tokens))
|
||||||
|
|
||||||
|
prefixLength := begin
|
||||||
|
for idx, token := range tokens {
|
||||||
|
// Need to define a new local variable instead of the reused token to take
|
||||||
|
// the pointer to it
|
||||||
|
str := token
|
||||||
|
ret[idx] = Token{text: &str, prefixLength: prefixLength}
|
||||||
|
prefixLength += len([]rune(token))
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
awkNil = iota
|
||||||
|
awkBlack
|
||||||
|
awkWhite
|
||||||
|
)
|
||||||
|
|
||||||
|
func awkTokenizer(input *string) ([]string, int) {
|
||||||
|
// 9, 32
|
||||||
|
ret := []string{}
|
||||||
|
str := []rune{}
|
||||||
|
prefixLength := 0
|
||||||
|
state := awkNil
|
||||||
|
for _, r := range []rune(*input) {
|
||||||
|
white := r == 9 || r == 32
|
||||||
|
switch state {
|
||||||
|
case awkNil:
|
||||||
|
if white {
|
||||||
|
prefixLength++
|
||||||
|
} else {
|
||||||
|
state = awkBlack
|
||||||
|
str = append(str, r)
|
||||||
|
}
|
||||||
|
case awkBlack:
|
||||||
|
str = append(str, r)
|
||||||
|
if white {
|
||||||
|
state = awkWhite
|
||||||
|
}
|
||||||
|
case awkWhite:
|
||||||
|
if white {
|
||||||
|
str = append(str, r)
|
||||||
|
} else {
|
||||||
|
ret = append(ret, string(str))
|
||||||
|
state = awkBlack
|
||||||
|
str = []rune{r}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(str) > 0 {
|
||||||
|
ret = append(ret, string(str))
|
||||||
|
}
|
||||||
|
return ret, prefixLength
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tokenize tokenizes the given string with the delimiter
|
||||||
|
func Tokenize(str *string, delimiter *regexp.Regexp) []Token {
|
||||||
|
if delimiter == nil {
|
||||||
|
// AWK-style (\S+\s*)
|
||||||
|
tokens, prefixLength := awkTokenizer(str)
|
||||||
|
return withPrefixLengths(tokens, prefixLength)
|
||||||
|
}
|
||||||
|
tokens := delimiter.FindAllString(*str, -1)
|
||||||
|
return withPrefixLengths(tokens, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinTokens(tokens []Token) string {
|
||||||
|
ret := ""
|
||||||
|
for _, token := range tokens {
|
||||||
|
ret += *token.text
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform is used to transform the input when --with-nth option is given
|
||||||
|
func Transform(tokens []Token, withNth []Range) *Transformed {
|
||||||
|
transTokens := make([]Token, len(withNth))
|
||||||
|
numTokens := len(tokens)
|
||||||
|
whole := ""
|
||||||
|
for idx, r := range withNth {
|
||||||
|
part := ""
|
||||||
|
minIdx := 0
|
||||||
|
if r.begin == r.end {
|
||||||
|
idx := r.begin
|
||||||
|
if idx == rangeEllipsis {
|
||||||
|
part += joinTokens(tokens)
|
||||||
|
} else {
|
||||||
|
if idx < 0 {
|
||||||
|
idx += numTokens + 1
|
||||||
|
}
|
||||||
|
if idx >= 1 && idx <= numTokens {
|
||||||
|
minIdx = idx - 1
|
||||||
|
part += *tokens[idx-1].text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var begin, end int
|
||||||
|
if r.begin == rangeEllipsis { // ..N
|
||||||
|
begin, end = 1, r.end
|
||||||
|
if end < 0 {
|
||||||
|
end += numTokens + 1
|
||||||
|
}
|
||||||
|
} else if r.end == rangeEllipsis { // N..
|
||||||
|
begin, end = r.begin, numTokens
|
||||||
|
if begin < 0 {
|
||||||
|
begin += numTokens + 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
begin, end = r.begin, r.end
|
||||||
|
if begin < 0 {
|
||||||
|
begin += numTokens + 1
|
||||||
|
}
|
||||||
|
if end < 0 {
|
||||||
|
end += numTokens + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
minIdx = util.Max(0, begin-1)
|
||||||
|
for idx := begin; idx <= end; idx++ {
|
||||||
|
if idx >= 1 && idx <= numTokens {
|
||||||
|
part += *tokens[idx-1].text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
whole += part
|
||||||
|
var prefixLength int
|
||||||
|
if minIdx < numTokens {
|
||||||
|
prefixLength = tokens[minIdx].prefixLength
|
||||||
|
} else {
|
||||||
|
prefixLength = 0
|
||||||
|
}
|
||||||
|
transTokens[idx] = Token{&part, prefixLength}
|
||||||
|
}
|
||||||
|
return &Transformed{
|
||||||
|
whole: &whole,
|
||||||
|
parts: transTokens}
|
||||||
|
}
|
101
src/tokenizer_test.go
Normal file
101
src/tokenizer_test.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestParseRange(t *testing.T) {
|
||||||
|
{
|
||||||
|
i := ".."
|
||||||
|
r, _ := ParseRange(&i)
|
||||||
|
if r.begin != rangeEllipsis || r.end != rangeEllipsis {
|
||||||
|
t.Errorf("%s", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
i := "3.."
|
||||||
|
r, _ := ParseRange(&i)
|
||||||
|
if r.begin != 3 || r.end != rangeEllipsis {
|
||||||
|
t.Errorf("%s", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
i := "3..5"
|
||||||
|
r, _ := ParseRange(&i)
|
||||||
|
if r.begin != 3 || r.end != 5 {
|
||||||
|
t.Errorf("%s", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
i := "-3..-5"
|
||||||
|
r, _ := ParseRange(&i)
|
||||||
|
if r.begin != -3 || r.end != -5 {
|
||||||
|
t.Errorf("%s", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
i := "3"
|
||||||
|
r, _ := ParseRange(&i)
|
||||||
|
if r.begin != 3 || r.end != 3 {
|
||||||
|
t.Errorf("%s", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTokenize(t *testing.T) {
|
||||||
|
// AWK-style
|
||||||
|
input := " abc: def: ghi "
|
||||||
|
tokens := Tokenize(&input, nil)
|
||||||
|
if *tokens[0].text != "abc: " || tokens[0].prefixLength != 2 {
|
||||||
|
t.Errorf("%s", tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
// With delimiter
|
||||||
|
tokens = Tokenize(&input, delimiterRegexp(":"))
|
||||||
|
if *tokens[0].text != " abc:" || tokens[0].prefixLength != 0 {
|
||||||
|
t.Errorf("%s", tokens)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTransform(t *testing.T) {
|
||||||
|
input := " abc: def: ghi: jkl"
|
||||||
|
{
|
||||||
|
tokens := Tokenize(&input, nil)
|
||||||
|
{
|
||||||
|
ranges := splitNth("1,2,3")
|
||||||
|
tx := Transform(tokens, ranges)
|
||||||
|
if *tx.whole != "abc: def: ghi: " {
|
||||||
|
t.Errorf("%s", *tx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ranges := splitNth("1..2,3,2..,1")
|
||||||
|
tx := Transform(tokens, ranges)
|
||||||
|
if *tx.whole != "abc: def: ghi: def: ghi: jklabc: " ||
|
||||||
|
len(tx.parts) != 4 ||
|
||||||
|
*tx.parts[0].text != "abc: def: " || tx.parts[0].prefixLength != 2 ||
|
||||||
|
*tx.parts[1].text != "ghi: " || tx.parts[1].prefixLength != 14 ||
|
||||||
|
*tx.parts[2].text != "def: ghi: jkl" || tx.parts[2].prefixLength != 8 ||
|
||||||
|
*tx.parts[3].text != "abc: " || tx.parts[3].prefixLength != 2 {
|
||||||
|
t.Errorf("%s", *tx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
tokens := Tokenize(&input, delimiterRegexp(":"))
|
||||||
|
{
|
||||||
|
ranges := splitNth("1..2,3,2..,1")
|
||||||
|
tx := Transform(tokens, ranges)
|
||||||
|
if *tx.whole != " abc: def: ghi: def: ghi: jkl abc:" ||
|
||||||
|
len(tx.parts) != 4 ||
|
||||||
|
*tx.parts[0].text != " abc: def:" || tx.parts[0].prefixLength != 0 ||
|
||||||
|
*tx.parts[1].text != " ghi:" || tx.parts[1].prefixLength != 12 ||
|
||||||
|
*tx.parts[2].text != " def: ghi: jkl" || tx.parts[2].prefixLength != 6 ||
|
||||||
|
*tx.parts[3].text != " abc:" || tx.parts[3].prefixLength != 0 {
|
||||||
|
t.Errorf("%s", *tx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTransformIndexOutOfBounds(t *testing.T) {
|
||||||
|
Transform([]Token{}, splitNth("1"))
|
||||||
|
}
|
42
src/update_assets.rb
Executable file
42
src/update_assets.rb
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
# http://www.rubydoc.info/github/rest-client/rest-client/RestClient
|
||||||
|
require 'rest_client'
|
||||||
|
|
||||||
|
if ARGV.length < 3
|
||||||
|
puts "usage: #$0 <token> <version> <files...>"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
token, version, *files = ARGV
|
||||||
|
base = "https://api.github.com/repos/junegunn/fzf-bin/releases"
|
||||||
|
|
||||||
|
# List releases
|
||||||
|
rels = JSON.parse(RestClient.get(base, :authorization => "token #{token}"))
|
||||||
|
rel = rels.find { |r| r['tag_name'] == version }
|
||||||
|
unless rel
|
||||||
|
puts "#{version} not found"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
# List assets
|
||||||
|
assets = Hash[rel['assets'].map { |a| a.values_at *%w[name id] }]
|
||||||
|
|
||||||
|
files.select { |f| File.exists? f }.each do |file|
|
||||||
|
name = File.basename file
|
||||||
|
|
||||||
|
if asset_id = assets[name]
|
||||||
|
puts "#{name} found. Deleting asset id #{asset_id}."
|
||||||
|
RestClient.delete "#{base}/assets/#{asset_id}",
|
||||||
|
:authorization => "token #{token}"
|
||||||
|
else
|
||||||
|
puts "#{name} not found"
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Uploading #{name}"
|
||||||
|
RestClient.post(
|
||||||
|
"#{base.sub 'api', 'uploads'}/#{rel['id']}/assets?name=#{name}",
|
||||||
|
File.read(file),
|
||||||
|
:authorization => "token #{token}",
|
||||||
|
:content_type => "application/octet-stream")
|
||||||
|
end
|
32
src/util/atomicbool.go
Normal file
32
src/util/atomicbool.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// AtomicBool is a boxed-class that provides synchronized access to the
|
||||||
|
// underlying boolean value
|
||||||
|
type AtomicBool struct {
|
||||||
|
mutex sync.Mutex
|
||||||
|
state bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAtomicBool returns a new AtomicBool
|
||||||
|
func NewAtomicBool(initialState bool) *AtomicBool {
|
||||||
|
return &AtomicBool{
|
||||||
|
mutex: sync.Mutex{},
|
||||||
|
state: initialState}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the current boolean value synchronously
|
||||||
|
func (a *AtomicBool) Get() bool {
|
||||||
|
a.mutex.Lock()
|
||||||
|
defer a.mutex.Unlock()
|
||||||
|
return a.state
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set updates the boolean value synchronously
|
||||||
|
func (a *AtomicBool) Set(newState bool) bool {
|
||||||
|
a.mutex.Lock()
|
||||||
|
defer a.mutex.Unlock()
|
||||||
|
a.state = newState
|
||||||
|
return a.state
|
||||||
|
}
|
17
src/util/atomicbool_test.go
Normal file
17
src/util/atomicbool_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestAtomicBool(t *testing.T) {
|
||||||
|
if !NewAtomicBool(true).Get() || NewAtomicBool(false).Get() {
|
||||||
|
t.Error("Invalid initial value")
|
||||||
|
}
|
||||||
|
|
||||||
|
ab := NewAtomicBool(true)
|
||||||
|
if ab.Set(false) {
|
||||||
|
t.Error("Invalid return value")
|
||||||
|
}
|
||||||
|
if ab.Get() {
|
||||||
|
t.Error("Invalid state")
|
||||||
|
}
|
||||||
|
}
|
95
src/util/eventbox.go
Normal file
95
src/util/eventbox.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// EventType is the type for fzf events
|
||||||
|
type EventType int
|
||||||
|
|
||||||
|
// Events is a type that associates EventType to any data
|
||||||
|
type Events map[EventType]interface{}
|
||||||
|
|
||||||
|
// EventBox is used for coordinating events
|
||||||
|
type EventBox struct {
|
||||||
|
events Events
|
||||||
|
cond *sync.Cond
|
||||||
|
ignore map[EventType]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEventBox returns a new EventBox
|
||||||
|
func NewEventBox() *EventBox {
|
||||||
|
return &EventBox{
|
||||||
|
events: make(Events),
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
|
ignore: make(map[EventType]bool)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait blocks the goroutine until signaled
|
||||||
|
func (b *EventBox) Wait(callback func(*Events)) {
|
||||||
|
b.cond.L.Lock()
|
||||||
|
defer b.cond.L.Unlock()
|
||||||
|
|
||||||
|
if len(b.events) == 0 {
|
||||||
|
b.cond.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(&b.events)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set turns on the event type on the box
|
||||||
|
func (b *EventBox) Set(event EventType, value interface{}) {
|
||||||
|
b.cond.L.Lock()
|
||||||
|
defer b.cond.L.Unlock()
|
||||||
|
b.events[event] = value
|
||||||
|
if _, found := b.ignore[event]; !found {
|
||||||
|
b.cond.Broadcast()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear clears the events
|
||||||
|
// Unsynchronized; should be called within Wait routine
|
||||||
|
func (events *Events) Clear() {
|
||||||
|
for event := range *events {
|
||||||
|
delete(*events, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Peek peeks at the event box if the given event is set
|
||||||
|
func (b *EventBox) Peek(event EventType) bool {
|
||||||
|
b.cond.L.Lock()
|
||||||
|
defer b.cond.L.Unlock()
|
||||||
|
_, ok := b.events[event]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch deletes the events from the ignore list
|
||||||
|
func (b *EventBox) Watch(events ...EventType) {
|
||||||
|
b.cond.L.Lock()
|
||||||
|
defer b.cond.L.Unlock()
|
||||||
|
for _, event := range events {
|
||||||
|
delete(b.ignore, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unwatch adds the events to the ignore list
|
||||||
|
func (b *EventBox) Unwatch(events ...EventType) {
|
||||||
|
b.cond.L.Lock()
|
||||||
|
defer b.cond.L.Unlock()
|
||||||
|
for _, event := range events {
|
||||||
|
b.ignore[event] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *EventBox) WaitFor(event EventType) {
|
||||||
|
looping := true
|
||||||
|
for looping {
|
||||||
|
b.Wait(func(events *Events) {
|
||||||
|
for evt := range *events {
|
||||||
|
switch evt {
|
||||||
|
case event:
|
||||||
|
looping = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
61
src/util/eventbox_test.go
Normal file
61
src/util/eventbox_test.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// fzf events
|
||||||
|
const (
|
||||||
|
EvtReadNew EventType = iota
|
||||||
|
EvtReadFin
|
||||||
|
EvtSearchNew
|
||||||
|
EvtSearchProgress
|
||||||
|
EvtSearchFin
|
||||||
|
EvtClose
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEventBox(t *testing.T) {
|
||||||
|
eb := NewEventBox()
|
||||||
|
|
||||||
|
// Wait should return immediately
|
||||||
|
ch := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
eb.Set(EvtReadNew, 10)
|
||||||
|
ch <- true
|
||||||
|
<-ch
|
||||||
|
eb.Set(EvtSearchNew, 10)
|
||||||
|
eb.Set(EvtSearchNew, 15)
|
||||||
|
eb.Set(EvtSearchNew, 20)
|
||||||
|
eb.Set(EvtSearchProgress, 30)
|
||||||
|
ch <- true
|
||||||
|
<-ch
|
||||||
|
eb.Set(EvtSearchFin, 40)
|
||||||
|
ch <- true
|
||||||
|
<-ch
|
||||||
|
}()
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
sum := 0
|
||||||
|
looping := true
|
||||||
|
for looping {
|
||||||
|
<-ch
|
||||||
|
eb.Wait(func(events *Events) {
|
||||||
|
for _, value := range *events {
|
||||||
|
switch val := value.(type) {
|
||||||
|
case int:
|
||||||
|
sum += val
|
||||||
|
looping = sum < 100
|
||||||
|
}
|
||||||
|
}
|
||||||
|
events.Clear()
|
||||||
|
})
|
||||||
|
ch <- true
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
if count != 3 {
|
||||||
|
t.Error("Invalid number of events", count)
|
||||||
|
}
|
||||||
|
if sum != 100 {
|
||||||
|
t.Error("Invalid sum", sum)
|
||||||
|
}
|
||||||
|
}
|
56
src/util/util.go
Normal file
56
src/util/util.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// #include <unistd.h>
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Max returns the largest integer
|
||||||
|
func Max(first int, items ...int) int {
|
||||||
|
max := first
|
||||||
|
for _, item := range items {
|
||||||
|
if item > max {
|
||||||
|
max = item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max32 returns the largest 32-bit integer
|
||||||
|
func Max32(first int32, second int32) int32 {
|
||||||
|
if first > second {
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
return second
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constrain limits the given integer with the upper and lower bounds
|
||||||
|
func Constrain(val int, min int, max int) int {
|
||||||
|
if val < min {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
if val > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurWithin limits the given time.Duration with the upper and lower bounds
|
||||||
|
func DurWithin(
|
||||||
|
val time.Duration, min time.Duration, max time.Duration) time.Duration {
|
||||||
|
if val < min {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
if val > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsTty returns true is stdin is a terminal
|
||||||
|
func IsTty() bool {
|
||||||
|
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
||||||
|
}
|
22
src/util/util_test.go
Normal file
22
src/util/util_test.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
if Max(-2, 5, 1, 4, 3) != 5 {
|
||||||
|
t.Error("Invalid result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContrain(t *testing.T) {
|
||||||
|
if Constrain(-3, -1, 3) != -1 {
|
||||||
|
t.Error("Expected", -1)
|
||||||
|
}
|
||||||
|
if Constrain(2, -1, 3) != 2 {
|
||||||
|
t.Error("Expected", 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if Constrain(5, -1, 3) != 3 {
|
||||||
|
t.Error("Expected", 3)
|
||||||
|
}
|
||||||
|
}
|
@@ -7,16 +7,16 @@ Execute (fzf#run with dir option):
|
|||||||
AssertEqual ['fzf.vader'], result
|
AssertEqual ['fzf.vader'], result
|
||||||
|
|
||||||
let result = sort(fzf#run({ 'options': '--filter e', 'dir': g:dir }))
|
let result = sort(fzf#run({ 'options': '--filter e', 'dir': g:dir }))
|
||||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
AssertEqual ['fzf.vader', 'test_go.rb', 'test_ruby.rb'], result
|
||||||
|
|
||||||
Execute (fzf#run with Funcref command):
|
Execute (fzf#run with Funcref command):
|
||||||
let g:ret = []
|
let g:ret = []
|
||||||
function! g:proc(e)
|
function! g:FzfTest(e)
|
||||||
call add(g:ret, a:e)
|
call add(g:ret, a:e)
|
||||||
endfunction
|
endfunction
|
||||||
let result = sort(fzf#run({ 'sink': function('g:proc'), 'options': '--filter e', 'dir': g:dir }))
|
let result = sort(fzf#run({ 'sink': function('g:FzfTest'), 'options': '--filter e', 'dir': g:dir }))
|
||||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
AssertEqual ['fzf.vader', 'test_go.rb', 'test_ruby.rb'], result
|
||||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], sort(g:ret)
|
AssertEqual ['fzf.vader', 'test_go.rb', 'test_ruby.rb'], sort(g:ret)
|
||||||
|
|
||||||
Execute (fzf#run with string source):
|
Execute (fzf#run with string source):
|
||||||
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
||||||
|
577
test/test_go.rb
Normal file
577
test/test_go.rb
Normal file
@@ -0,0 +1,577 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'minitest/autorun'
|
||||||
|
require 'fileutils'
|
||||||
|
|
||||||
|
class NilClass
|
||||||
|
def include? str
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_with? str
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def end_with? str
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Temp
|
||||||
|
def readonce
|
||||||
|
name = self.class::TEMPNAME
|
||||||
|
waited = 0
|
||||||
|
while waited < 5
|
||||||
|
begin
|
||||||
|
data = `cat #{name}`
|
||||||
|
return data unless data.empty?
|
||||||
|
rescue
|
||||||
|
sleep 0.1
|
||||||
|
waited += 0.1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
raise "failed to read tempfile"
|
||||||
|
ensure
|
||||||
|
while File.exists? name
|
||||||
|
File.unlink name rescue nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Shell
|
||||||
|
class << self
|
||||||
|
def bash
|
||||||
|
'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.bash'
|
||||||
|
end
|
||||||
|
|
||||||
|
def zsh
|
||||||
|
FileUtils.mkdir_p '/tmp/fzf-zsh'
|
||||||
|
FileUtils.cp File.expand_path('~/.fzf.zsh'), '/tmp/fzf-zsh/.zshrc'
|
||||||
|
'PS1= PROMPT_COMMAND= HISTSIZE=100 ZDOTDIR=/tmp/fzf-zsh zsh'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Tmux
|
||||||
|
include Temp
|
||||||
|
|
||||||
|
TEMPNAME = '/tmp/fzf-test.txt'
|
||||||
|
|
||||||
|
attr_reader :win
|
||||||
|
|
||||||
|
def initialize shell = :bash
|
||||||
|
@win =
|
||||||
|
case shell
|
||||||
|
when :bash
|
||||||
|
go("new-window -d -P -F '#I' '#{Shell.bash}'").first
|
||||||
|
when :zsh
|
||||||
|
go("new-window -d -P -F '#I' '#{Shell.zsh}'").first
|
||||||
|
when :fish
|
||||||
|
go("new-window -d -P -F '#I' 'fish'").first
|
||||||
|
else
|
||||||
|
raise "Unknown shell: #{shell}"
|
||||||
|
end
|
||||||
|
@lines = `tput lines`.chomp.to_i
|
||||||
|
|
||||||
|
if shell == :fish
|
||||||
|
send_keys('function fish_prompt; end; clear', :Enter)
|
||||||
|
self.until { |lines| lines.empty? }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def closed?
|
||||||
|
!go("list-window -F '#I'").include?(win)
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
send_keys 'C-c', 'C-u', 'exit', :Enter
|
||||||
|
wait { closed? }
|
||||||
|
end
|
||||||
|
|
||||||
|
def kill
|
||||||
|
go("kill-window -t #{win} 2> /dev/null")
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_keys *args
|
||||||
|
target =
|
||||||
|
if args.last.is_a?(Hash)
|
||||||
|
hash = args.pop
|
||||||
|
go("select-window -t #{win}")
|
||||||
|
"#{win}.#{hash[:pane]}"
|
||||||
|
else
|
||||||
|
win
|
||||||
|
end
|
||||||
|
args = args.map { |a| %{"#{a}"} }.join ' '
|
||||||
|
go("send-keys -t #{target} #{args}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def capture opts = {}
|
||||||
|
timeout, pane = defaults(opts).values_at(:timeout, :pane)
|
||||||
|
waited = 0
|
||||||
|
loop do
|
||||||
|
go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME}")
|
||||||
|
break if $?.exitstatus == 0
|
||||||
|
|
||||||
|
if waited > timeout
|
||||||
|
raise "Window not found"
|
||||||
|
end
|
||||||
|
waited += 0.1
|
||||||
|
sleep 0.1
|
||||||
|
end
|
||||||
|
readonce.split($/)[0, @lines].reverse.drop_while(&:empty?).reverse
|
||||||
|
end
|
||||||
|
|
||||||
|
def until opts = {}
|
||||||
|
lines = nil
|
||||||
|
wait(opts) do
|
||||||
|
yield lines = capture(opts)
|
||||||
|
end
|
||||||
|
lines
|
||||||
|
end
|
||||||
|
|
||||||
|
def prepare
|
||||||
|
self.send_keys 'echo hello', :Enter
|
||||||
|
self.until { |lines| lines[-1].start_with?('hello') }
|
||||||
|
self.send_keys 'clear', :Enter
|
||||||
|
self.until { |lines| lines.empty? }
|
||||||
|
end
|
||||||
|
private
|
||||||
|
def defaults opts
|
||||||
|
{ timeout: 10, pane: 0 }.merge(opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def wait opts = {}
|
||||||
|
timeout, pane = defaults(opts).values_at(:timeout, :pane)
|
||||||
|
waited = 0
|
||||||
|
until yield
|
||||||
|
if waited > timeout
|
||||||
|
hl = '=' * 10
|
||||||
|
puts hl
|
||||||
|
capture(opts).each_with_index do |line, idx|
|
||||||
|
puts [idx.to_s.rjust(2), line].join(': ')
|
||||||
|
end
|
||||||
|
puts hl
|
||||||
|
raise "timeout"
|
||||||
|
end
|
||||||
|
waited += 0.1
|
||||||
|
sleep 0.1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def go *args
|
||||||
|
%x[tmux #{args.join ' '}].split($/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestBase < Minitest::Test
|
||||||
|
include Temp
|
||||||
|
|
||||||
|
FIN = 'FIN'
|
||||||
|
TEMPNAME = '/tmp/output'
|
||||||
|
|
||||||
|
attr_reader :tmux
|
||||||
|
|
||||||
|
def setup
|
||||||
|
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||||
|
ENV.delete 'FZF_DEFAULT_COMMAND'
|
||||||
|
end
|
||||||
|
|
||||||
|
def fzf(*opts)
|
||||||
|
fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def fzf!(*opts)
|
||||||
|
opts = opts.map { |o|
|
||||||
|
case o
|
||||||
|
when Symbol
|
||||||
|
o = o.to_s
|
||||||
|
o.length > 1 ? "--#{o.gsub('_', '-')}" : "-#{o}"
|
||||||
|
when String, Numeric
|
||||||
|
o.to_s
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
}.compact
|
||||||
|
"fzf #{opts.join ' '}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestGoFZF < TestBase
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@tmux = Tmux.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
@tmux.kill
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_vanilla
|
||||||
|
tmux.send_keys "seq 1 100000 | #{fzf}", :Enter
|
||||||
|
tmux.until(timeout: 20) { |lines|
|
||||||
|
lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }
|
||||||
|
lines = tmux.capture
|
||||||
|
assert_equal ' 2', lines[-4]
|
||||||
|
assert_equal '> 1', lines[-3]
|
||||||
|
assert_equal ' 100000/100000', lines[-2]
|
||||||
|
assert_equal '>', lines[-1]
|
||||||
|
|
||||||
|
# Testing basic key bindings
|
||||||
|
tmux.send_keys '99', 'C-a', '1', 'C-f', '3', 'C-b', 'C-h', 'C-u', 'C-e', 'C-y', 'C-k', 'Tab', 'BTab'
|
||||||
|
tmux.until { |lines| lines[-2] == ' 856/100000' }
|
||||||
|
lines = tmux.capture
|
||||||
|
assert_equal '> 1391', lines[-4]
|
||||||
|
assert_equal ' 391', lines[-3]
|
||||||
|
assert_equal ' 856/100000', lines[-2]
|
||||||
|
assert_equal '> 391', lines[-1]
|
||||||
|
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.close
|
||||||
|
assert_equal '1391', readonce.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fzf_default_command
|
||||||
|
tmux.send_keys "FZF_DEFAULT_COMMAND='echo hello' #{fzf}", :Enter
|
||||||
|
tmux.until { |lines| lines.last =~ /^>/ }
|
||||||
|
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.close
|
||||||
|
assert_equal 'hello', readonce.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_key_bindings
|
||||||
|
tmux.send_keys "fzf -q 'foo bar foo-bar'", :Enter
|
||||||
|
tmux.until { |lines| lines.last =~ /^>/ }
|
||||||
|
|
||||||
|
# CTRL-A
|
||||||
|
tmux.send_keys "C-A", "("
|
||||||
|
tmux.until { |lines| lines.last == '> (foo bar foo-bar' }
|
||||||
|
|
||||||
|
# META-F
|
||||||
|
tmux.send_keys :Escape, :f, ")"
|
||||||
|
tmux.until { |lines| lines.last == '> (foo) bar foo-bar' }
|
||||||
|
|
||||||
|
# CTRL-B
|
||||||
|
tmux.send_keys "C-B", "var"
|
||||||
|
tmux.until { |lines| lines.last == '> (foovar) bar foo-bar' }
|
||||||
|
|
||||||
|
# Left, CTRL-D
|
||||||
|
tmux.send_keys :Left, :Left, "C-D"
|
||||||
|
tmux.until { |lines| lines.last == '> (foovr) bar foo-bar' }
|
||||||
|
|
||||||
|
# META-BS
|
||||||
|
tmux.send_keys :Escape, :BSpace
|
||||||
|
tmux.until { |lines| lines.last == '> (r) bar foo-bar' }
|
||||||
|
|
||||||
|
# CTRL-Y
|
||||||
|
tmux.send_keys "C-Y", "C-Y"
|
||||||
|
tmux.until { |lines| lines.last == '> (foovfoovr) bar foo-bar' }
|
||||||
|
|
||||||
|
# META-B
|
||||||
|
tmux.send_keys :Escape, :b, :Space, :Space
|
||||||
|
tmux.until { |lines| lines.last == '> ( foovfoovr) bar foo-bar' }
|
||||||
|
|
||||||
|
# CTRL-F / Right
|
||||||
|
tmux.send_keys 'C-F', :Right, '/'
|
||||||
|
tmux.until { |lines| lines.last == '> ( fo/ovfoovr) bar foo-bar' }
|
||||||
|
|
||||||
|
# CTRL-H / BS
|
||||||
|
tmux.send_keys 'C-H', :BSpace
|
||||||
|
tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-bar' }
|
||||||
|
|
||||||
|
# CTRL-E
|
||||||
|
tmux.send_keys "C-E", 'baz'
|
||||||
|
tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-barbaz' }
|
||||||
|
|
||||||
|
# CTRL-U
|
||||||
|
tmux.send_keys "C-U"
|
||||||
|
tmux.until { |lines| lines.last == '>' }
|
||||||
|
|
||||||
|
# CTRL-Y
|
||||||
|
tmux.send_keys "C-Y"
|
||||||
|
tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-barbaz' }
|
||||||
|
|
||||||
|
# CTRL-W
|
||||||
|
tmux.send_keys "C-W", "bar-foo"
|
||||||
|
tmux.until { |lines| lines.last == '> ( fovfoovr) bar bar-foo' }
|
||||||
|
|
||||||
|
# META-D
|
||||||
|
tmux.send_keys :Escape, :b, :Escape, :b, :Escape, :d, "C-A", "C-Y"
|
||||||
|
tmux.until { |lines| lines.last == '> bar( fovfoovr) bar -foo' }
|
||||||
|
|
||||||
|
# CTRL-M
|
||||||
|
tmux.send_keys "C-M"
|
||||||
|
tmux.until { |lines| lines.last !~ /^>/ }
|
||||||
|
tmux.close
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_multi_order
|
||||||
|
tmux.send_keys "seq 1 10 | #{fzf :multi}", :Enter
|
||||||
|
tmux.until { |lines| lines.last =~ /^>/ }
|
||||||
|
|
||||||
|
tmux.send_keys :Tab, :Up, :Up, :Tab, :Tab, :Tab, # 3, 2
|
||||||
|
'C-K', 'C-K', 'C-K', 'C-K', :BTab, :BTab, # 5, 6
|
||||||
|
:PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7
|
||||||
|
tmux.until { |lines| lines[-2].include? '(6)' }
|
||||||
|
tmux.send_keys "C-M"
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal %w[3 2 5 6 8 7], readonce.split($/)
|
||||||
|
tmux.close
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth
|
||||||
|
[true, false].each do |multi|
|
||||||
|
tmux.send_keys "(echo ' 1st 2nd 3rd/';
|
||||||
|
echo ' first second third/') |
|
||||||
|
#{fzf multi && :multi, :x, :nth, 2, :with_nth, '2,-1,1'}",
|
||||||
|
:Enter
|
||||||
|
tmux.until { |lines| lines[-2].include?('2/2') }
|
||||||
|
|
||||||
|
# Transformed list
|
||||||
|
lines = tmux.capture
|
||||||
|
assert_equal ' second third/first', lines[-4]
|
||||||
|
assert_equal '> 2nd 3rd/1st', lines[-3]
|
||||||
|
|
||||||
|
# However, the output must not be transformed
|
||||||
|
if multi
|
||||||
|
tmux.send_keys :BTab, :BTab, :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.split($/)
|
||||||
|
else
|
||||||
|
tmux.send_keys '^', '3'
|
||||||
|
tmux.until { |lines| lines[-2].include?('1/2') }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal [' 1st 2nd 3rd/'], readonce.split($/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_scroll
|
||||||
|
[true, false].each do |rev|
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf rev && :reverse}", :Enter
|
||||||
|
tmux.until { |lines| lines.include? ' 100/100' }
|
||||||
|
tmux.send_keys *110.times.map { rev ? :Down : :Up }
|
||||||
|
tmux.until { |lines| lines.include? '> 100' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal '100', readonce.chomp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select_1
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 5555, :'1'}", :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal ['5555', '55'], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exit_0
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 555555, :'0'}", :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal ['555555'], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select_1_exit_0_fail
|
||||||
|
[:'0', :'1', [:'1', :'0']].each do |opt|
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf :print_query, :multi, :q, 5, *opt}", :Enter
|
||||||
|
tmux.until { |lines| lines.last =~ /^> 5/ }
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal ['5', '5', '15', '25'], readonce.split($/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_query_unicode
|
||||||
|
tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter
|
||||||
|
tmux.until { |lines| lines.last.start_with? '>' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include?(FIN) }
|
||||||
|
assert_equal ['가나다'], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_sync
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf! :multi} | awk '{print \\$1 \\$1}' | #{fzf :sync}", :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == '>' }
|
||||||
|
tmux.send_keys 9
|
||||||
|
tmux.until { |lines| lines[-2] == ' 19/100' }
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == '>' }
|
||||||
|
tmux.send_keys 'C-K', :Enter
|
||||||
|
assert_equal ['1919'], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tac
|
||||||
|
tmux.send_keys "seq 1 1000 | #{fzf :tac, :multi}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '1000/1000' }
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
|
assert_equal %w[1000 999 998], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tac_sort
|
||||||
|
tmux.send_keys "seq 1 1000 | #{fzf :tac, :multi}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '1000/1000' }
|
||||||
|
tmux.send_keys '99'
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
|
assert_equal %w[99 999 998], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tac_nosort
|
||||||
|
tmux.send_keys "seq 1 1000 | #{fzf :tac, :no_sort, :multi}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '1000/1000' }
|
||||||
|
tmux.send_keys '00'
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
|
assert_equal %w[1000 900 800], readonce.split($/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module TestShell
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
@tmux.kill
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ctrl_t
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'C-t', pane: 0
|
||||||
|
lines = tmux.until(pane: 1) { |lines| lines[-1].start_with? '>' }
|
||||||
|
expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')
|
||||||
|
tmux.send_keys :BTab, :BTab, :Enter, pane: 1
|
||||||
|
tmux.until(pane: 0) { |lines| lines[-1].include? expected }
|
||||||
|
tmux.send_keys 'C-c'
|
||||||
|
|
||||||
|
# FZF_TMUX=0
|
||||||
|
new_shell
|
||||||
|
tmux.send_keys 'C-t', pane: 0
|
||||||
|
lines = tmux.until(pane: 0) { |lines| lines[-1].start_with? '>' }
|
||||||
|
expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')
|
||||||
|
tmux.send_keys :BTab, :BTab, :Enter, pane: 0
|
||||||
|
tmux.until(pane: 0) { |lines| lines[-1].include? expected }
|
||||||
|
tmux.send_keys 'C-c', 'C-d'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_alt_c
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys :Escape, :c
|
||||||
|
lines = tmux.until { |lines| lines[-1].start_with? '>' }
|
||||||
|
expected = lines[-3][2..-1]
|
||||||
|
p expected
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys :pwd, :Enter
|
||||||
|
tmux.until { |lines| p lines; lines[-1].end_with?(expected) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ctrl_r
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'echo 1st', :Enter; tmux.prepare
|
||||||
|
tmux.send_keys 'echo 2nd', :Enter; tmux.prepare
|
||||||
|
tmux.send_keys 'echo 3d', :Enter; tmux.prepare
|
||||||
|
tmux.send_keys 'echo 3rd', :Enter; tmux.prepare
|
||||||
|
tmux.send_keys 'echo 4th', :Enter; tmux.prepare
|
||||||
|
tmux.send_keys 'C-r'
|
||||||
|
tmux.until { |lines| lines[-1].start_with? '>' }
|
||||||
|
tmux.send_keys '3d'
|
||||||
|
tmux.until { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == 'echo 3rd' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == '3rd' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestBash < TestBase
|
||||||
|
include TestShell
|
||||||
|
|
||||||
|
def new_shell
|
||||||
|
tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter
|
||||||
|
tmux.prepare
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@tmux = Tmux.new :bash
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_file_completion
|
||||||
|
tmux.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab
|
||||||
|
tmux.until { |lines| lines[-1].start_with? '>' }
|
||||||
|
tmux.send_keys :BTab, :BTab, :Enter
|
||||||
|
tmux.until { |lines|
|
||||||
|
lines[-1].include?('/tmp/fzf-test/10') &&
|
||||||
|
lines[-1].include?('/tmp/fzf-test/100')
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_dir_completion
|
||||||
|
tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'cd /tmp/fzf-test/**', :Tab
|
||||||
|
tmux.until { |lines| lines[-1].start_with? '>' }
|
||||||
|
tmux.send_keys :BTab, :BTab # BTab does not work here
|
||||||
|
tmux.send_keys 55
|
||||||
|
tmux.until { |lines| lines[-2].start_with? ' 1/' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/' }
|
||||||
|
tmux.send_keys :xx
|
||||||
|
tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }
|
||||||
|
|
||||||
|
# Should not match regular files
|
||||||
|
tmux.send_keys :Tab
|
||||||
|
tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/xx' }
|
||||||
|
|
||||||
|
# Fail back to plusdirs
|
||||||
|
tmux.send_keys :BSpace, :BSpace, :BSpace
|
||||||
|
tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55' }
|
||||||
|
tmux.send_keys :Tab
|
||||||
|
tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55/' }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_process_completion
|
||||||
|
tmux.send_keys 'sleep 12345 &', :Enter
|
||||||
|
lines = tmux.until { |lines| lines[-1].start_with? '[1]' }
|
||||||
|
pid = lines[-1].split.last
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'kill ', :Tab
|
||||||
|
tmux.until { |lines| lines[-1].start_with? '>' }
|
||||||
|
tmux.send_keys 'sleep12345'
|
||||||
|
tmux.until { |lines| lines[-3].include? 'sleep 12345' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == "kill #{pid}" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestZsh < TestBase
|
||||||
|
include TestShell
|
||||||
|
|
||||||
|
def new_shell
|
||||||
|
tmux.send_keys "FZF_TMUX=0 #{Shell.zsh}", :Enter
|
||||||
|
tmux.prepare
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@tmux = Tmux.new :zsh
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestFish < TestBase
|
||||||
|
include TestShell
|
||||||
|
|
||||||
|
def new_shell
|
||||||
|
tmux.send_keys 'env FZF_TMUX=0 fish', :Enter
|
||||||
|
tmux.send_keys 'function fish_prompt; end; clear', :Enter
|
||||||
|
tmux.until { |lines| lines.empty? }
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@tmux = Tmux.new :fish
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@@ -1,15 +1,60 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'rubygems'
|
||||||
require 'curses'
|
require 'curses'
|
||||||
require 'timeout'
|
require 'timeout'
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
|
require 'tempfile'
|
||||||
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||||
ENV['FZF_EXECUTABLE'] = '0'
|
ENV['FZF_EXECUTABLE'] = '0'
|
||||||
load 'fzf'
|
load 'fzf'
|
||||||
|
|
||||||
class TestFZF < MiniTest::Unit::TestCase
|
class MockTTY
|
||||||
|
def initialize
|
||||||
|
@buffer = ''
|
||||||
|
@mutex = Mutex.new
|
||||||
|
@condv = ConditionVariable.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_nonblock sz
|
||||||
|
@mutex.synchronize do
|
||||||
|
take sz
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def take sz
|
||||||
|
if @buffer.length >= sz
|
||||||
|
ret = @buffer[0, sz]
|
||||||
|
@buffer = @buffer[sz..-1]
|
||||||
|
ret
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def getc
|
||||||
|
sleep 0.1
|
||||||
|
while true
|
||||||
|
@mutex.synchronize do
|
||||||
|
if char = take(1)
|
||||||
|
return char
|
||||||
|
else
|
||||||
|
@condv.wait(@mutex)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def << str
|
||||||
|
@mutex.synchronize do
|
||||||
|
@buffer << str
|
||||||
|
@condv.broadcast
|
||||||
|
end
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestRubyFZF < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
ENV.delete 'FZF_DEFAULT_SORT'
|
ENV.delete 'FZF_DEFAULT_SORT'
|
||||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||||
@@ -24,14 +69,18 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.rxflag
|
assert_equal nil, fzf.rxflag
|
||||||
assert_equal true, fzf.mouse
|
assert_equal true, fzf.mouse
|
||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal true, fzf.ansi256
|
assert_equal true, fzf.ansi256
|
||||||
assert_equal '', fzf.query.get
|
assert_equal '', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal nil, fzf.filter
|
assert_equal nil, fzf.filter
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
|
assert_equal false, fzf.reverse
|
||||||
|
assert_equal '> ', fzf.prompt
|
||||||
|
assert_equal false, fzf.print_query
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_environment_variables
|
def test_environment_variables
|
||||||
@@ -42,12 +91,12 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
|
||||||
ENV['FZF_DEFAULT_OPTS'] =
|
ENV['FZF_DEFAULT_OPTS'] =
|
||||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +
|
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
||||||
'--no-mouse -f "goodbye world" --black --nth=3,-1,2'
|
'--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 10000, fzf.sort
|
assert_equal 10000, fzf.sort
|
||||||
assert_equal ' hello world ',
|
assert_equal ' hello world ',
|
||||||
fzf.query.get
|
fzf.query
|
||||||
assert_equal 'goodbye world',
|
assert_equal 'goodbye world',
|
||||||
fzf.filter
|
fzf.filter
|
||||||
assert_equal :fuzzy, fzf.extended
|
assert_equal :fuzzy, fzf.extended
|
||||||
@@ -58,14 +107,18 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.mouse
|
assert_equal false, fzf.mouse
|
||||||
assert_equal true, fzf.select1
|
assert_equal true, fzf.select1
|
||||||
assert_equal true, fzf.exit0
|
assert_equal true, fzf.exit0
|
||||||
assert_equal [3, -1, 2], fzf.nth
|
assert_equal true, fzf.reverse
|
||||||
|
assert_equal true, fzf.print_query
|
||||||
|
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
||||||
|
assert_equal [2..2, -3..-1, 1..1], fzf.with_nth
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_option_parser
|
def test_option_parser
|
||||||
# Long opts
|
# Long opts
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||||
--exit-0 --filter=howdy --extended-exact
|
--exit-0 --filter=howdy --extended-exact
|
||||||
--no-mouse --no-256 --nth=1]
|
--no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)
|
||||||
|
--print-query]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
@@ -73,18 +126,24 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal false, fzf.mouse
|
assert_equal false, fzf.mouse
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal true, fzf.select1
|
assert_equal true, fzf.select1
|
||||||
assert_equal true, fzf.exit0
|
assert_equal true, fzf.exit0
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :exact, fzf.extended
|
assert_equal :exact, fzf.extended
|
||||||
assert_equal [1], fzf.nth
|
assert_equal [0..0], fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
|
assert_equal true, fzf.reverse
|
||||||
|
assert_equal '(hi)', fzf.prompt
|
||||||
|
assert_equal true, fzf.print_query
|
||||||
|
|
||||||
# Long opts (left-to-right)
|
# Long opts (left-to-right)
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||||
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
||||||
--select-1 --exit-0 --no-select-1 --no-exit-0
|
--select-1 --exit-0 --no-select-1 --no-exit-0
|
||||||
--no-sort -i --color --no-multi --256]
|
--no-sort -i --color --no-multi --256
|
||||||
|
--reverse --no-reverse --prompt (hi) --prompt=(HI)
|
||||||
|
--print-query --no-print-query]
|
||||||
assert_equal nil, fzf.sort
|
assert_equal nil, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
@@ -93,11 +152,14 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.mouse
|
assert_equal true, fzf.mouse
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'b', fzf.filter
|
assert_equal 'b', fzf.filter
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal [-2], fzf.nth
|
assert_equal [-2..-2], fzf.nth
|
||||||
|
assert_equal false, fzf.reverse
|
||||||
|
assert_equal '(HI)', fzf.prompt
|
||||||
|
assert_equal false, fzf.print_query
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||||
@@ -106,10 +168,10 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
assert_equal false, fzf.ansi256
|
assert_equal false, fzf.ansi256
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :fuzzy, fzf.extended
|
assert_equal :fuzzy, fzf.extended
|
||||||
assert_equal [3], fzf.nth
|
assert_equal [2..2], fzf.nth
|
||||||
assert_equal true, fzf.select1
|
assert_equal true, fzf.select1
|
||||||
assert_equal true, fzf.exit0
|
assert_equal true, fzf.exit0
|
||||||
|
|
||||||
@@ -124,37 +186,41 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.ansi256
|
assert_equal true, fzf.ansi256
|
||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'world', fzf.query.get
|
assert_equal 'world', fzf.query
|
||||||
assert_equal false, fzf.select1
|
assert_equal false, fzf.select1
|
||||||
assert_equal false, fzf.exit0
|
assert_equal false, fzf.exit0
|
||||||
assert_equal 'world', fzf.filter
|
assert_equal 'world', fzf.filter
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal [4, 5], fzf.nth
|
assert_equal [3..3, 4..4], fzf.nth
|
||||||
rescue SystemExit => e
|
rescue SystemExit => e
|
||||||
assert false, "Exited"
|
assert false, "Exited"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_option
|
def test_invalid_option
|
||||||
[%w[--unknown], %w[yo dawg]].each do |argv|
|
[
|
||||||
|
%w[--unknown],
|
||||||
|
%w[yo dawg],
|
||||||
|
%w[--nth=0],
|
||||||
|
%w[-n 0],
|
||||||
|
%w[-n 1..2..3],
|
||||||
|
%w[-n 1....],
|
||||||
|
%w[-n ....3],
|
||||||
|
%w[-n 1....3],
|
||||||
|
%w[-n 1..0],
|
||||||
|
%w[--nth ..0],
|
||||||
|
].each do |argv|
|
||||||
assert_raises(SystemExit) do
|
assert_raises(SystemExit) do
|
||||||
fzf = FZF.new argv
|
fzf = FZF.new argv
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
assert_raises(SystemExit) do
|
|
||||||
fzf = FZF.new %w[--nth=0]
|
|
||||||
end
|
|
||||||
assert_raises(SystemExit) do
|
|
||||||
fzf = FZF.new %w[-n 0]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME Only on 1.9 or above
|
|
||||||
def test_width
|
def test_width
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 5, fzf.width('abcde')
|
assert_equal 5, fzf.width('abcde')
|
||||||
assert_equal 4, fzf.width('한글')
|
assert_equal 4, fzf.width('한글')
|
||||||
assert_equal 5, fzf.width('한글.')
|
assert_equal 5, fzf.width('한글.')
|
||||||
end
|
end if RUBY_VERSION >= '1.9'
|
||||||
|
|
||||||
def test_trim
|
def test_trim
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
@@ -167,7 +233,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
||||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
||||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
||||||
end
|
end if RUBY_VERSION >= '1.9'
|
||||||
|
|
||||||
def test_format
|
def test_format
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
@@ -445,58 +511,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal 2, exact.match(list, "-fuzzy", '', '').length
|
assert_equal 2, exact.match(list, "-fuzzy", '', '').length
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_PLATFORM =~ /darwin/
|
|
||||||
NFD = '한글'
|
|
||||||
def test_nfc
|
|
||||||
assert_equal 6, NFD.length
|
|
||||||
assert_equal ["한글", [[0, 1], [1, 2]]],
|
|
||||||
FZF::UConv.nfc(NFD, [[0, 3], [3, 6]])
|
|
||||||
|
|
||||||
nfd2 = 'before' + NFD + 'after'
|
|
||||||
assert_equal 6 + 6 + 5, nfd2.length
|
|
||||||
|
|
||||||
nfc, offsets = FZF::UConv.nfc(nfd2, [[4, 14], [9, 13]])
|
|
||||||
o1, o2 = offsets
|
|
||||||
assert_equal 'before한글after', nfc
|
|
||||||
assert_equal 're한글af', nfc[(o1.first...o1.last)]
|
|
||||||
assert_equal '글a', nfc[(o2.first...o2.last)]
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_nfd
|
|
||||||
nfc = '한글'
|
|
||||||
nfd = FZF::UConv.nfd(nfc)
|
|
||||||
assert_equal 2, nfd.length
|
|
||||||
assert_equal 6, nfd.join.length
|
|
||||||
assert_equal NFD, nfd.join
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_nfd_fuzzy_matcher
|
|
||||||
matcher = FZF::FuzzyMatcher.new 0
|
|
||||||
assert_equal [], matcher.match([NFD + NFD], '할', '', '')
|
|
||||||
match = matcher.match([NFD + NFD], '글글', '', '')
|
|
||||||
assert_equal [[NFD + NFD, [[3, 12]]]], match
|
|
||||||
assert_equal ['한글한글', [[1, 4]]], FZF::UConv.nfc(*match.first)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_nfd_extended_fuzzy_matcher
|
|
||||||
matcher = FZF::ExtendedFuzzyMatcher.new 0
|
|
||||||
assert_equal [], matcher.match([NFD], "'글글", '', '')
|
|
||||||
match = matcher.match([NFD], "'한글", '', '')
|
|
||||||
assert_equal [[NFD, [[0, 6]]]], match
|
|
||||||
assert_equal ['한글', [[0, 2]]], FZF::UConv.nfc(*match.first)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_split
|
|
||||||
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
|
||||||
FZF::UConv.split("abc\xFFdef")
|
|
||||||
end
|
|
||||||
|
|
||||||
# ^$ -> matches empty item
|
# ^$ -> matches empty item
|
||||||
def test_format_empty_item
|
def test_format_empty_item
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
item = ['', [[0, 0]]]
|
item = ['', [[0, 0]]]
|
||||||
line, offsets = fzf.convert_item item
|
line, offsets = item
|
||||||
tokens = fzf.format line, 80, offsets
|
tokens = fzf.format line, 80, offsets
|
||||||
assert_equal [], tokens
|
assert_equal [], tokens
|
||||||
end
|
end
|
||||||
@@ -529,126 +548,176 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
[list[0], [[2, 5]]],
|
[list[0], [[2, 5]]],
|
||||||
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1]
|
||||||
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2]
|
||||||
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
|
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
# Comma-separated
|
# Comma-separated
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3, 1]
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2, 0..0]
|
||||||
assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
# Ordered
|
# Ordered
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1, 3]
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0, 2..2]
|
||||||
assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
regex = FZF.build_delim_regex "\t"
|
regex = FZF.build_delim_regex "\t"
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex
|
||||||
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex
|
||||||
assert_equal [], matcher.match(list, 'r', '', '')
|
assert_equal [], matcher.match(list, 'r', '', '')
|
||||||
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
# Negative indexing
|
# Negative indexing
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1], regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1..-1], regex
|
||||||
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
||||||
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
# Regex delimiter
|
# Regex delimiter
|
||||||
regex = FZF.build_delim_regex "[ \t]+"
|
regex = FZF.build_delim_regex "[ \t]+"
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex
|
||||||
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex
|
||||||
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
end
|
end
|
||||||
|
|
||||||
def stream_for str
|
def test_nth_match_range
|
||||||
|
list = [
|
||||||
|
' first second third',
|
||||||
|
'fourth fifth sixth',
|
||||||
|
]
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..2]
|
||||||
|
assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')
|
||||||
|
assert_equal [], matcher.match(list, 'fo', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..-1, 0..0]
|
||||||
|
assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')
|
||||||
|
assert_equal [[list[1], [[0, 2]]]], matcher.match(list, 'fo', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..0, 1..2]
|
||||||
|
assert_equal [], matcher.match(list, '^t', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..1, 2..2]
|
||||||
|
assert_equal [[list[0], [[16, 17]]]], matcher.match(list, '^t', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [1..-1]
|
||||||
|
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')
|
||||||
|
end
|
||||||
|
|
||||||
|
def stream_for str, delay = 0
|
||||||
StringIO.new(str).tap do |sio|
|
StringIO.new(str).tap do |sio|
|
||||||
sio.instance_eval do
|
sio.instance_eval do
|
||||||
alias org_gets gets
|
alias org_gets gets
|
||||||
|
|
||||||
def gets
|
def gets
|
||||||
org_gets.tap { |e| sleep 0.5 unless e.nil? }
|
org_gets.tap { |e| sleep(@delay) unless e.nil? }
|
||||||
|
end
|
||||||
|
|
||||||
|
def reopen _
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
sio.instance_variable_set :@delay, delay
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_fzf_output opts, given, expected
|
||||||
|
stream = stream_for given
|
||||||
|
output = stream_for ''
|
||||||
|
|
||||||
|
def sorted_lines line
|
||||||
|
line.split($/).sort
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
tty = MockTTY.new
|
||||||
|
$stdout = output
|
||||||
|
fzf = FZF.new(opts, stream)
|
||||||
|
fzf.instance_variable_set :@tty, tty
|
||||||
|
thr = block_given? && Thread.new { yield tty }
|
||||||
|
fzf.start
|
||||||
|
thr && thr.join
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal sorted_lines(expected), sorted_lines(output.string)
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filter
|
||||||
|
{
|
||||||
|
%w[--filter=ol] => 'World',
|
||||||
|
%w[--filter=ol --print-query] => "ol\nWorld",
|
||||||
|
}.each do |opts, expected|
|
||||||
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1
|
def test_select_1
|
||||||
stream = stream_for "Hello\nWorld"
|
{
|
||||||
output = StringIO.new
|
%w[--query=ol --select-1] => 'World',
|
||||||
|
%w[--query=ol --select-1 --print-query] => "ol\nWorld",
|
||||||
begin
|
}.each do |opts, expected|
|
||||||
$stdout = output
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
FZF.new(%w[--query=ol --select-1], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal 'World', output.string.chomp
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1_without_query
|
def test_select_1_without_query
|
||||||
stream = stream_for "Hello World"
|
assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'
|
||||||
output = StringIO.new
|
|
||||||
|
|
||||||
begin
|
|
||||||
$stdout = output
|
|
||||||
FZF.new(%w[--select-1], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal 'Hello World', output.string.chomp
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1_ambiguity
|
def test_select_1_ambiguity
|
||||||
stream = stream_for "Hello\nWorld"
|
|
||||||
begin
|
begin
|
||||||
Timeout::timeout(3) do
|
Timeout::timeout(0.5) do
|
||||||
FZF.new(%w[--query=o --select-1], stream).start
|
assert_fzf_output %w[--query=o --select-1], "hello\nworld", "should not match"
|
||||||
end
|
end
|
||||||
flunk 'Should not reach here'
|
rescue Timeout::Error
|
||||||
rescue Exception => e
|
|
||||||
Curses.close_screen
|
Curses.close_screen
|
||||||
assert_instance_of Timeout::Error, e
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0
|
def test_exit_0
|
||||||
stream = stream_for "Hello\nWorld"
|
{
|
||||||
output = StringIO.new
|
%w[--query=zz --exit-0] => '',
|
||||||
|
%w[--query=zz --exit-0 --print-query] => 'zz',
|
||||||
begin
|
}.each do |opts, expected|
|
||||||
$stdout = output
|
assert_fzf_output opts, "Hello\nWorld", expected
|
||||||
FZF.new(%w[--query=zz --exit-0], stream).start
|
|
||||||
rescue SystemExit => e
|
|
||||||
assert_equal 0, e.status
|
|
||||||
assert_equal '', output.string
|
|
||||||
ensure
|
|
||||||
$stdout = STDOUT
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0_without_query
|
def test_exit_0_without_query
|
||||||
stream = stream_for ""
|
assert_fzf_output %w[--exit-0], '', ''
|
||||||
output = StringIO.new
|
end
|
||||||
|
|
||||||
begin
|
def test_with_nth
|
||||||
$stdout = output
|
source = "hello world\nbatman"
|
||||||
FZF.new(%w[--exit-0], stream).start
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],
|
||||||
rescue SystemExit => e
|
source, 'hello world'
|
||||||
assert_equal 0, e.status
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],
|
||||||
assert_equal '', output.string
|
source, 'hello world'
|
||||||
ensure
|
assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],
|
||||||
$stdout = STDOUT
|
source, ''
|
||||||
end
|
assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],
|
||||||
|
source, 'hello world'
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],
|
||||||
|
source, 'batman'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_transform
|
||||||
|
fzf = FZF.new %w[--with-nth 2..,1]
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world ')
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--with-nth 2,-1,2]
|
||||||
|
assert_equal 'my world my', fzf.transform('hello my world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ranking_overlap_match_regions
|
def test_ranking_overlap_match_regions
|
||||||
@@ -661,5 +730,121 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
['1 3 4 2', [[0, 24], [12, 17]]],
|
['1 3 4 2', [[0, 24], [12, 17]]],
|
||||||
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_constrain
|
||||||
|
fzf = FZF.new []
|
||||||
|
|
||||||
|
# [#**** ]
|
||||||
|
assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)
|
||||||
|
|
||||||
|
# *****[**#** ... ] => [**#******* ... ]
|
||||||
|
assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)
|
||||||
|
|
||||||
|
# [**********]**#** => ***[*********#]**
|
||||||
|
assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)
|
||||||
|
|
||||||
|
# *****[**#** ] => ***[**#****]
|
||||||
|
assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)
|
||||||
|
|
||||||
|
# *****[**#** ] => ****[**#***]
|
||||||
|
assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)
|
||||||
|
|
||||||
|
# ***** [#] => ****[#]
|
||||||
|
assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)
|
||||||
|
|
||||||
|
# [ ] #**** => [#]****
|
||||||
|
assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)
|
||||||
|
|
||||||
|
# [ ] **#** => **[#]**
|
||||||
|
assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)
|
||||||
|
|
||||||
|
# [***** #] => [****# ]
|
||||||
|
assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)
|
||||||
|
|
||||||
|
# **[***** #] => [******# ]
|
||||||
|
assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_utf8
|
||||||
|
tmp = Tempfile.new('fzf')
|
||||||
|
tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*')
|
||||||
|
tmp.close
|
||||||
|
begin
|
||||||
|
Timeout::timeout(0.5) do
|
||||||
|
FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start
|
||||||
|
end
|
||||||
|
rescue Timeout::Error
|
||||||
|
Curses.close_screen
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
tmp.unlink
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_mock_tty
|
||||||
|
# Manual selection with input
|
||||||
|
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||||
|
tty << "world"
|
||||||
|
tty << "hell"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection without input
|
||||||
|
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection with input and --multi
|
||||||
|
lines = "hello world\ngoodbye world"
|
||||||
|
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||||
|
tty << "o"
|
||||||
|
tty << "\e[Z\e[Z"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Manual selection without input and --multi
|
||||||
|
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||||
|
tty << "\e[Z\e[Z"
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# ALT-D
|
||||||
|
assert_fzf_output %w[--print-query], "", "hello baby = world" do |tty|
|
||||||
|
tty << "hello world baby"
|
||||||
|
tty << alt(:b) << alt(:b) << alt(:d)
|
||||||
|
tty << ctrl(:e) << " = " << ctrl(:y)
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# ALT-BACKSPACE
|
||||||
|
assert_fzf_output %w[--print-query], "", "hello baby = world " do |tty|
|
||||||
|
tty << "hello world baby"
|
||||||
|
tty << alt(:b) << alt(127.chr)
|
||||||
|
tty << ctrl(:e) << " = " << ctrl(:y)
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Word-movements
|
||||||
|
assert_fzf_output %w[--print-query], "", "ello!_orld!~ foo=?" do |tty|
|
||||||
|
tty << "hello_world==baby?"
|
||||||
|
tty << alt(:b) << ctrl(:d)
|
||||||
|
tty << alt(:b) << ctrl(:d)
|
||||||
|
tty << alt(:b) << ctrl(:d)
|
||||||
|
tty << alt(:f) << '!'
|
||||||
|
tty << alt(:f) << '!'
|
||||||
|
tty << alt(:d) << '~'
|
||||||
|
tty << " foo=bar foo=bar"
|
||||||
|
tty << ctrl(:w)
|
||||||
|
tty << alt(127.chr)
|
||||||
|
tty << "\r"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def alt chr
|
||||||
|
"\e#{chr}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def ctrl char
|
||||||
|
char.to_s.ord - 'a'.ord + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
78
uninstall
Executable file
78
uninstall
Executable file
@@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
confirm() {
|
||||||
|
while [ 1 ]; do
|
||||||
|
read -p "$1" -n 1 -r
|
||||||
|
echo
|
||||||
|
if [[ "$REPLY" =~ ^[Yy] ]]; then
|
||||||
|
return 0
|
||||||
|
elif [[ "$REPLY" =~ ^[Nn] ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
remove() {
|
||||||
|
echo "Remove $1"
|
||||||
|
rm -f "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_line() {
|
||||||
|
src=$(readlink "$1")
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Remove from $1 ($src):"
|
||||||
|
else
|
||||||
|
src=$1
|
||||||
|
echo "Remove from $1:"
|
||||||
|
fi
|
||||||
|
|
||||||
|
shift
|
||||||
|
line_no=1
|
||||||
|
match=0
|
||||||
|
while [ -n "$1" ]; do
|
||||||
|
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
shift
|
||||||
|
line_no=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
|
||||||
|
content=$(sed 's/^[0-9]*://' <<< "$line")
|
||||||
|
match=1
|
||||||
|
echo " - Line #$line_no: $content"
|
||||||
|
[ "$content" = "$1" ] || confirm " - Remove (y/n) ? "
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
|
||||||
|
mv "$src.bak" "$src" || break
|
||||||
|
echo " - Removed"
|
||||||
|
else
|
||||||
|
echo " - Skipped"
|
||||||
|
line_no=$(( line_no + 1 ))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
[ $match -eq 0 ] && echo " - Nothing found"
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
for shell in bash zsh; do
|
||||||
|
remove ~/.fzf.${shell}
|
||||||
|
remove_line ~/.${shell}rc \
|
||||||
|
"[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" \
|
||||||
|
"source ~/.fzf.${shell}"
|
||||||
|
done
|
||||||
|
|
||||||
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
|
if [ -f "$bind_file" ]; then
|
||||||
|
remove_line "$bind_file" "fzf_key_bindings"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -d ~/.config/fish/functions ]; then
|
||||||
|
remove ~/.config/fish/functions/fzf.fish
|
||||||
|
|
||||||
|
if [ "$(ls -A ~/.config/fish/functions)" ]; then
|
||||||
|
echo "Can't delete non-empty directory: \"~/.config/fish/functions\""
|
||||||
|
else
|
||||||
|
rmdir ~/.config/fish/functions
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
Reference in New Issue
Block a user