mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-14 14:23:47 -05:00
Compare commits
301 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a65006f55 | ||
|
|
d75ed841a9 | ||
|
|
3cd2547e91 | ||
|
|
8c661d4e8c | ||
|
|
4b332d831e | ||
|
|
22487810ba | ||
|
|
c49e65d926 | ||
|
|
2e8814bb57 | ||
|
|
dc557c0d4c | ||
|
|
a2beb159f1 | ||
|
|
7ce427ff47 | ||
|
|
a221c672fb | ||
|
|
f87d382ec8 | ||
|
|
3dfc020fac | ||
|
|
2d87896939 | ||
|
|
2192d8d816 | ||
|
|
d206949f62 | ||
|
|
4accc69022 | ||
|
|
898d8d94c8 | ||
|
|
26895da969 | ||
|
|
0c573b3dff | ||
|
|
2cff00dce2 | ||
|
|
06a6ad8bca | ||
|
|
02c6ad0e59 | ||
|
|
9f321cbe13 | ||
|
|
9f30ca2923 | ||
|
|
37f2d8f795 | ||
|
|
400e443a0a | ||
|
|
0a8d2996dc | ||
|
|
cfdb00b971 | ||
|
|
9b9ad39143 | ||
|
|
0541c0dbcf | ||
|
|
47b11cb8b4 | ||
|
|
d3da310b92 | ||
|
|
93e0a6a9de | ||
|
|
ac549a853a | ||
|
|
053af9a1c8 | ||
|
|
60112def02 | ||
|
|
2134c0c8a9 | ||
|
|
3222d62ddf | ||
|
|
aeb957a285 | ||
|
|
154cf22ffa | ||
|
|
51f532697e | ||
|
|
01b88539ba | ||
|
|
3066b206af | ||
|
|
04492bab10 | ||
|
|
8b0d0342d4 | ||
|
|
957c12e7d7 | ||
|
|
3b5ae0f8a2 | ||
|
|
1fc5659842 | ||
|
|
1acd2adce2 | ||
|
|
1bc223d4b3 | ||
|
|
bef405bfa5 | ||
|
|
0612074abe | ||
|
|
3bf51d8362 | ||
|
|
2c8479a7c5 | ||
|
|
8c8b5b313e | ||
|
|
66d55fd893 | ||
|
|
7fa5e6c861 | ||
|
|
00f96aae76 | ||
|
|
a749e6bd16 | ||
|
|
791076d366 | ||
|
|
37f43fbb35 | ||
|
|
401a5fd5ff | ||
|
|
1854922f0c | ||
|
|
2fc7c18747 | ||
|
|
8ef2420677 | ||
|
|
cf6f4d74c4 | ||
|
|
f44d40f6b4 | ||
|
|
1c81a58127 | ||
|
|
9baf7c4874 | ||
|
|
22b089e47e | ||
|
|
b166f18220 | ||
|
|
68600f6ecf | ||
|
|
4d4447779f | ||
|
|
639de4c27b | ||
|
|
d87390934e | ||
|
|
411ec2e557 | ||
|
|
f025602841 | ||
|
|
f958c9daf5 | ||
|
|
b86838c2b0 | ||
|
|
1f7d1f9b15 | ||
|
|
f8fdf9618a | ||
|
|
827a83efbc | ||
|
|
3e88849386 | ||
|
|
608c416207 | ||
|
|
62f6ff9d6c | ||
|
|
37dc273148 | ||
|
|
f7f01d109e | ||
|
|
01ee335521 | ||
|
|
0e0de29b87 | ||
|
|
babf877fd6 | ||
|
|
935272824e | ||
|
|
3a9532c8fd | ||
|
|
c4c92142a6 | ||
|
|
d4b6338102 | ||
|
|
8df7d962e6 | ||
|
|
41e916a511 | ||
|
|
d9c8a9a880 | ||
|
|
ddc7bb9064 | ||
|
|
1d4057c209 | ||
|
|
822b86942c | ||
|
|
1e74dbb937 | ||
|
|
7cef92fffe | ||
|
|
42e4992f06 | ||
|
|
a6066175c6 | ||
|
|
27444d6b1e | ||
|
|
d6a99c0391 | ||
|
|
f787f7e651 | ||
|
|
a7c9c08371 | ||
|
|
fccc93176b | ||
|
|
6439a138fe | ||
|
|
a9a29dff4f | ||
|
|
6a52f8b8dd | ||
|
|
a1049328d6 | ||
|
|
5c2b96bd00 | ||
|
|
c36413fdf6 | ||
|
|
52cf5af91c | ||
|
|
3a4e053af7 | ||
|
|
049bc9ec68 | ||
|
|
b461a555b8 | ||
|
|
0f87b2d1e1 | ||
|
|
0fb5b76c0d | ||
|
|
0c918dd87a | ||
|
|
05299a0fee | ||
|
|
b36b0a91f5 | ||
|
|
6081eac58a | ||
|
|
942ba749c7 | ||
|
|
f941012687 | ||
|
|
fed5e5d5af | ||
|
|
b864885753 | ||
|
|
64747c2324 | ||
|
|
34965edcda | ||
|
|
bd4377084d | ||
|
|
38a2076b89 | ||
|
|
5759d50d4a | ||
|
|
e455836cc9 | ||
|
|
8a90f26c8a | ||
|
|
24e1fabf2e | ||
|
|
c39c039e15 | ||
|
|
07f176f426 | ||
|
|
19339e3a6d | ||
|
|
3e1d6a7bcf | ||
|
|
2bbc12063c | ||
|
|
b8737b724b | ||
|
|
d91c3a2f5e | ||
|
|
fe5db5aadc | ||
|
|
cf9c957c66 | ||
|
|
68b60c6d19 | ||
|
|
3a644b16a4 | ||
|
|
95b34de339 | ||
|
|
6a431cbf49 | ||
|
|
56fb2f00b3 | ||
|
|
1c86aaf342 | ||
|
|
cfc0b18eaa | ||
|
|
412c211655 | ||
|
|
923feb69ab | ||
|
|
92dba7035a | ||
|
|
b8a3ba16a2 | ||
|
|
cd5e4d9402 | ||
|
|
f074709fc9 | ||
|
|
e0b29e437b | ||
|
|
bdb94fba7d | ||
|
|
2f364c62f4 | ||
|
|
7ed9f83662 | ||
|
|
f498a9b3fb | ||
|
|
13330738b8 | ||
|
|
e53535cc61 | ||
|
|
c62fc5e75c | ||
|
|
70245ad98c | ||
|
|
6d235bceee | ||
|
|
4adebfc856 | ||
|
|
faccc0a410 | ||
|
|
9078688baf | ||
|
|
9bd8b1d25f | ||
|
|
dd4be1da38 | ||
|
|
66f86e1870 | ||
|
|
4ab75b68dc | ||
|
|
73cb70dbb3 | ||
|
|
d082cccb6d | ||
|
|
88a80e3c2c | ||
|
|
24516bcf4d | ||
|
|
b4c4a642ed | ||
|
|
0231617857 | ||
|
|
7f64fba80f | ||
|
|
633aec38f5 | ||
|
|
d1b402a23c | ||
|
|
35a9aff8e1 | ||
|
|
988c9bd9be | ||
|
|
095f31b316 | ||
|
|
d86cee2a69 | ||
|
|
e986f20a85 | ||
|
|
c727ba1d99 | ||
|
|
bb70923cd8 | ||
|
|
772fa42dcb | ||
|
|
85ef3263fc | ||
|
|
4bde8de63f | ||
|
|
654a7df9b0 | ||
|
|
c3aa836ec0 | ||
|
|
95764bef6f | ||
|
|
63dbf48546 | ||
|
|
e2401350a3 | ||
|
|
e867355b2a | ||
|
|
b28c14b93a | ||
|
|
879ead210f | ||
|
|
2f6d23b91e | ||
|
|
5f63a7b587 | ||
|
|
d9ce797d88 | ||
|
|
12230f8043 | ||
|
|
0c8de1ca44 | ||
|
|
89687105f4 | ||
|
|
74d1694be9 | ||
|
|
935e986be5 | ||
|
|
e5ac2ebd7c | ||
|
|
8d6e13bf94 | ||
|
|
2ca704405a | ||
|
|
802c1c2937 | ||
|
|
3cb5fef6b6 | ||
|
|
6da2e0aa1e | ||
|
|
24f3ec7f33 | ||
|
|
a57b375b41 | ||
|
|
6cc9d53978 | ||
|
|
df32c05833 | ||
|
|
c0652adf4c | ||
|
|
6ea760a336 | ||
|
|
f704b94603 | ||
|
|
444a67cafa | ||
|
|
f91cbd5688 | ||
|
|
3073ca3e5a | ||
|
|
b47ab633e2 | ||
|
|
09a2ab39fe | ||
|
|
6cf54833f7 | ||
|
|
2ccdf21a1f | ||
|
|
cf8afc527e | ||
|
|
1d6f05f974 | ||
|
|
85751966e9 | ||
|
|
a7bc9d5351 | ||
|
|
42c006d07c | ||
|
|
1b9ca314b8 | ||
|
|
e72a360337 | ||
|
|
45108ddd53 | ||
|
|
e3401a0645 | ||
|
|
26b9100709 | ||
|
|
a568120e42 | ||
|
|
e57182c658 | ||
|
|
6354dbbbdf | ||
|
|
2b3e740569 | ||
|
|
40d934e378 | ||
|
|
e95d82748f | ||
|
|
30bd0b53db | ||
|
|
1893eca41a | ||
|
|
82067463b8 | ||
|
|
ce9c51d399 | ||
|
|
96176476f3 | ||
|
|
68c84264af | ||
|
|
69438a55ca | ||
|
|
8695b5e319 | ||
|
|
95970164ad | ||
|
|
f6c6e59a50 | ||
|
|
45143f9541 | ||
|
|
23244bb410 | ||
|
|
edb647667e | ||
|
|
8d3a302a17 | ||
|
|
1d2d32c847 | ||
|
|
d635b3fd3c | ||
|
|
0f281ef894 | ||
|
|
b18db4733c | ||
|
|
6e08fe337c | ||
|
|
2a2c0a0957 | ||
|
|
4230b6f3c9 | ||
|
|
aa171b45cb | ||
|
|
661d06c90a | ||
|
|
a9aa263d3a | ||
|
|
6208fc9cfd | ||
|
|
e1dd798482 | ||
|
|
c8a3f6f06a | ||
|
|
3b9984379c | ||
|
|
a1b60b1d42 | ||
|
|
b5850ebd4c | ||
|
|
ac0a62e494 | ||
|
|
54b4b0c56f | ||
|
|
033afde3b5 | ||
|
|
a07944a5bb | ||
|
|
32010055e1 | ||
|
|
971ea2217c | ||
|
|
d513a210c6 | ||
|
|
a1db64e7b1 | ||
|
|
0b9c4e1e74 | ||
|
|
248320fa55 | ||
|
|
d4e26707c7 | ||
|
|
99ea1056ac | ||
|
|
7bcf4effa5 | ||
|
|
e1df876b61 | ||
|
|
28ffb9638d | ||
|
|
1c20255504 | ||
|
|
1fd884b34f | ||
|
|
701687faab | ||
|
|
bbc3055feb | ||
|
|
95c69083c7 | ||
|
|
57a37b5832 | ||
|
|
d29ae1c462 |
30
.github/ISSUE_TEMPLATE.md
vendored
Normal file
30
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<!-- Check all that apply [x] -->
|
||||||
|
- Category
|
||||||
|
- [ ] fzf binary
|
||||||
|
- [ ] fzf-tmux script
|
||||||
|
- [ ] Key bindings
|
||||||
|
- [ ] Completion
|
||||||
|
- [ ] Vim
|
||||||
|
- [ ] Neovim
|
||||||
|
- [ ] Etc.
|
||||||
|
- OS
|
||||||
|
- [ ] Linux
|
||||||
|
- [ ] Mac OS X
|
||||||
|
- [ ] Windows
|
||||||
|
- [ ] Windows Subsystem for Linux
|
||||||
|
- [ ] Etc.
|
||||||
|
- Shell
|
||||||
|
- [ ] bash
|
||||||
|
- [ ] zsh
|
||||||
|
- [ ] fish
|
||||||
|
|
||||||
|
<!--
|
||||||
|
### Before submitting
|
||||||
|
|
||||||
|
- Make sure that you have the latest version of fzf
|
||||||
|
- If you use tmux, make sure $TERM is set to screen or screen-256color
|
||||||
|
- For more Vim stuff, check out https://github.com/junegunn/fzf.vim
|
||||||
|
|
||||||
|
Describe your problem or suggestion from here ...
|
||||||
|
-->
|
||||||
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
bin
|
bin
|
||||||
src/fzf/fzf_*
|
src/fzf/fzf-*
|
||||||
|
gopath
|
||||||
pkg
|
pkg
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
rvm:
|
matrix:
|
||||||
- 2.2.0
|
include:
|
||||||
|
- env: TAGS=
|
||||||
|
rvm: 2.2.0
|
||||||
|
# - env: TAGS=tcell
|
||||||
|
# rvm: 2.2.0
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- sudo apt-get update
|
- sudo apt-get update
|
||||||
|
|||||||
171
CHANGELOG.md
171
CHANGELOG.md
@@ -1,6 +1,177 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.15.8
|
||||||
|
------
|
||||||
|
- Updated ANSI processor to handle more VT-100 escape sequences
|
||||||
|
- Added `--no-bold` (and `--bold`) option
|
||||||
|
- Improved escape sequence processing for WSL
|
||||||
|
- Added support for `alt-[0-9]`, `f11`, and `f12` for `--bind` and `--expect`
|
||||||
|
|
||||||
|
0.15.7
|
||||||
|
------
|
||||||
|
- Fixed panic when color is disabled and header lines contain ANSI colors
|
||||||
|
|
||||||
|
0.15.6
|
||||||
|
------
|
||||||
|
- Windows binaries! (@kelleyma49)
|
||||||
|
- Fixed the bug where header lines are cleared when preview window is toggled
|
||||||
|
- Fixed not to display ^N and ^O on screen
|
||||||
|
- Fixed cursor keys (or any key sequence that starts with ESC) on WSL by
|
||||||
|
making fzf wait for additional keystrokes after ESC for up to 100ms
|
||||||
|
|
||||||
|
0.15.5
|
||||||
|
------
|
||||||
|
- Setting foreground color will no longer set background color to black
|
||||||
|
- e.g. `fzf --color fg:153`
|
||||||
|
- `--tiebreak=end` will consider relative position instead of absolute distance
|
||||||
|
- Updated `fzf#wrap` function to respect `g:fzf_colors`
|
||||||
|
|
||||||
|
0.15.4
|
||||||
|
------
|
||||||
|
- Added support for range expression in preview and execute action
|
||||||
|
- e.g. `ls -l | fzf --preview="echo user={3} when={-4..-2}; cat {-1}" --header-lines=1`
|
||||||
|
- `{q}` will be replaced to the single-quoted string of the current query
|
||||||
|
- Fixed to properly handle unicode whitespace characters
|
||||||
|
- Display scroll indicator in preview window
|
||||||
|
- Inverse search term will use exact matcher by default
|
||||||
|
- This is a breaking change, but I believe it makes much more sense. It is
|
||||||
|
almost impossible to predict which entries will be filtered out due to
|
||||||
|
a fuzzy inverse term. You can still perform inverse-fuzzy-match by
|
||||||
|
prepending `!'` to the term.
|
||||||
|
|
||||||
|
0.15.3
|
||||||
|
------
|
||||||
|
- Added support for more ANSI attributes: dim, underline, blink, and reverse
|
||||||
|
- Fixed race condition in `toggle-preview`
|
||||||
|
|
||||||
|
0.15.2
|
||||||
|
------
|
||||||
|
- Preview window is now scrollable
|
||||||
|
- With mouse scroll or with bindable actions
|
||||||
|
- `preview-up`
|
||||||
|
- `preview-down`
|
||||||
|
- `preview-page-up`
|
||||||
|
- `preview-page-down`
|
||||||
|
- Updated ANSI processor to support high intensity colors and ignore
|
||||||
|
some VT100-related escape sequences
|
||||||
|
|
||||||
|
0.15.1
|
||||||
|
------
|
||||||
|
- Fixed panic when the pattern occurs after 2^15-th column
|
||||||
|
- Fixed rendering delay when displaying extremely long lines
|
||||||
|
|
||||||
|
0.15.0
|
||||||
|
------
|
||||||
|
- Improved fuzzy search algorithm
|
||||||
|
- Added `--algo=[v1|v2]` option so one can still choose the old algorithm
|
||||||
|
which values the search performance over the quality of the result
|
||||||
|
- Advanced scoring criteria
|
||||||
|
- `--read0` to read input delimited by ASCII NUL character
|
||||||
|
- `--print0` to print output delimited by ASCII NUL character
|
||||||
|
|
||||||
|
0.13.5
|
||||||
|
------
|
||||||
|
- Memory and performance optimization
|
||||||
|
- Up to 2x performance with half the amount of memory
|
||||||
|
|
||||||
|
0.13.4
|
||||||
|
------
|
||||||
|
- Performance optimization
|
||||||
|
- Memory footprint for ascii string is reduced by 60%
|
||||||
|
- 15 to 20% improvement of query performance
|
||||||
|
- Up to 45% better performance of `--nth` with non-regex delimiters
|
||||||
|
- Fixed invalid handling of `hidden` property of `--preview-window`
|
||||||
|
|
||||||
|
0.13.3
|
||||||
|
------
|
||||||
|
- Fixed duplicate rendering of the last line in preview window
|
||||||
|
|
||||||
|
0.13.2
|
||||||
|
------
|
||||||
|
- Fixed race condition where preview window is not properly cleared
|
||||||
|
|
||||||
|
0.13.1
|
||||||
|
------
|
||||||
|
- Fixed UI issue with large `--preview` output with many ANSI codes
|
||||||
|
|
||||||
|
0.13.0
|
||||||
|
------
|
||||||
|
- Added preview feature
|
||||||
|
- `--preview CMD`
|
||||||
|
- `--preview-window POS[:SIZE][:hidden]`
|
||||||
|
- `{}` in execute action is now replaced to the single-quoted (instead of
|
||||||
|
double-quoted) string of the current line
|
||||||
|
- Fixed to ignore control characters for bracketed paste mode
|
||||||
|
|
||||||
|
0.12.2
|
||||||
|
------
|
||||||
|
|
||||||
|
- 256-color capability detection does not require `256` in `$TERM`
|
||||||
|
- Added `print-query` action
|
||||||
|
- More named keys for binding; <kbd>F1</kbd> ~ <kbd>F10</kbd>,
|
||||||
|
<kbd>ALT-/</kbd>, <kbd>ALT-space</kbd>, and <kbd>ALT-enter</kbd>
|
||||||
|
- Added `jump` and `jump-accept` actions that implement [EasyMotion][em]-like
|
||||||
|
movement
|
||||||
|
![][jump]
|
||||||
|
|
||||||
|
[em]: https://github.com/easymotion/vim-easymotion
|
||||||
|
[jump]: https://cloud.githubusercontent.com/assets/700826/15367574/b3999dc4-1d64-11e6-85da-28ceeb1a9bc2.png
|
||||||
|
|
||||||
|
0.12.1
|
||||||
|
------
|
||||||
|
|
||||||
|
- Ranking algorithm introduced in 0.12.0 is now universally applied
|
||||||
|
- Fixed invalid cache reference in exact mode
|
||||||
|
- Fixes and improvements in Vim plugin and shell extensions
|
||||||
|
|
||||||
|
0.12.0
|
||||||
|
------
|
||||||
|
|
||||||
|
- Enhanced ranking algorithm
|
||||||
|
- Minor bug fixes
|
||||||
|
|
||||||
|
0.11.4
|
||||||
|
------
|
||||||
|
|
||||||
|
- Added `--hscroll-off=COL` option (default: 10) (#513)
|
||||||
|
- Some fixes in Vim plugin and shell extensions
|
||||||
|
|
||||||
|
0.11.3
|
||||||
|
------
|
||||||
|
|
||||||
|
- Graceful exit on SIGTERM (#482)
|
||||||
|
- `$SHELL` instead of `sh` for `execute` action and `$FZF_DEFAULT_COMMAND` (#481)
|
||||||
|
- Changes in fuzzy completion API
|
||||||
|
- [`_fzf_compgen_{path,dir}`](https://github.com/junegunn/fzf/commit/9617647)
|
||||||
|
- [`_fzf_complete_COMMAND_post`](https://github.com/junegunn/fzf/commit/8206746)
|
||||||
|
for post-processing
|
||||||
|
|
||||||
|
0.11.2
|
||||||
|
------
|
||||||
|
|
||||||
|
- `--tiebreak` now accepts comma-separated list of sort criteria
|
||||||
|
- Each criterion should appear only once in the list
|
||||||
|
- `index` is only allowed at the end of the list
|
||||||
|
- `index` is implicitly appended to the list when not specified
|
||||||
|
- Default is `length` (or equivalently `length,index`)
|
||||||
|
- `begin` criterion will ignore leading whitespaces when calculating the index
|
||||||
|
- Added `toggle-in` and `toggle-out` actions
|
||||||
|
- Switch direction depending on `--reverse`-ness
|
||||||
|
- `export FZF_DEFAULT_OPTS="--bind tab:toggle-out,shift-tab:toggle-in"`
|
||||||
|
- Reduced the initial delay when `--tac` is not given
|
||||||
|
- fzf defers the initial rendering of the screen up to 100ms if the input
|
||||||
|
stream is ongoing to prevent unnecessary redraw during the initial
|
||||||
|
phase. However, 100ms delay is quite noticeable and might give the
|
||||||
|
impression that fzf is not snappy enough. This commit reduces the
|
||||||
|
maximum delay down to 20ms when `--tac` is not specified, in which case
|
||||||
|
the input list quickly fills the entire screen.
|
||||||
|
|
||||||
|
0.11.1
|
||||||
|
------
|
||||||
|
|
||||||
|
- Added `--tabstop=SPACES` option
|
||||||
|
|
||||||
0.11.0
|
0.11.0
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2015 Junegunn Choi
|
Copyright (c) 2016 Junegunn Choi
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
161
README.md
161
README.md
@@ -10,18 +10,15 @@ Pros
|
|||||||
|
|
||||||
- No dependencies
|
- No dependencies
|
||||||
- Blazingly fast
|
- Blazingly fast
|
||||||
- e.g. `locate / | fzf`
|
|
||||||
- Flexible layout
|
|
||||||
- Runs in fullscreen or in horizontal/vertical split using tmux
|
|
||||||
- The most comprehensive feature set
|
- The most comprehensive feature set
|
||||||
- Try `fzf --help` and be surprised
|
- Flexible layout using tmux panes
|
||||||
- Batteries included
|
- Batteries included
|
||||||
- Vim/Neovim plugin, key bindings and fuzzy auto-completion
|
- Vim/Neovim plugin, key bindings and fuzzy auto-completion
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
fzf project consists of the followings:
|
fzf project consists of the following components:
|
||||||
|
|
||||||
- `fzf` executable
|
- `fzf` executable
|
||||||
- `fzf-tmux` script for launching fzf in a tmux pane
|
- `fzf-tmux` script for launching fzf in a tmux pane
|
||||||
@@ -30,12 +27,12 @@ fzf project consists of the followings:
|
|||||||
- Fuzzy auto-completion (bash, zsh)
|
- Fuzzy auto-completion (bash, zsh)
|
||||||
- Vim/Neovim plugin
|
- Vim/Neovim plugin
|
||||||
|
|
||||||
You can [download fzf executable][bin] alone, but it's recommended that you
|
You can [download fzf executable][bin] alone if you don't need the extra
|
||||||
install the extra stuff using the attached install script.
|
stuff.
|
||||||
|
|
||||||
[bin]: https://github.com/junegunn/fzf-bin/releases
|
[bin]: https://github.com/junegunn/fzf-bin/releases
|
||||||
|
|
||||||
#### Using git (recommended)
|
### Using git
|
||||||
|
|
||||||
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.
|
||||||
@@ -45,7 +42,7 @@ git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
|
|||||||
~/.fzf/install
|
~/.fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Using Homebrew
|
### Using Homebrew
|
||||||
|
|
||||||
On OS X, you can use [Homebrew](http://brew.sh/) to install fzf.
|
On OS X, you can use [Homebrew](http://brew.sh/) to install fzf.
|
||||||
|
|
||||||
@@ -56,31 +53,44 @@ brew install fzf
|
|||||||
/usr/local/opt/fzf/install
|
/usr/local/opt/fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Install as Vim plugin
|
### Vim plugin
|
||||||
|
|
||||||
Once you have cloned the repository, add the following line to your .vimrc.
|
You can manually add the directory to `&runtimepath` as follows,
|
||||||
|
|
||||||
```vim
|
```vim
|
||||||
|
" If installed using git
|
||||||
set rtp+=~/.fzf
|
set rtp+=~/.fzf
|
||||||
|
|
||||||
|
" If installed using Homebrew
|
||||||
|
set rtp+=/usr/local/opt/fzf
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf
|
But it's recommended that you use a plugin manager like
|
||||||
(recommended):
|
[vim-plug](https://github.com/junegunn/vim-plug).
|
||||||
|
|
||||||
```vim
|
```vim
|
||||||
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
|
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Upgrading fzf
|
### Upgrading fzf
|
||||||
|
|
||||||
fzf is being actively developed and you might want to upgrade it once in a
|
fzf is being actively developed and you might want to upgrade it once in a
|
||||||
while. Please follow the instruction below depending on the installation
|
while. Please follow the instruction below depending on the installation
|
||||||
method.
|
method used.
|
||||||
|
|
||||||
- git: `cd ~/.fzf && git pull && ./install`
|
- git: `cd ~/.fzf && git pull && ./install`
|
||||||
- brew: `brew update; brew reinstall fzf`
|
- brew: `brew update; brew reinstall fzf`
|
||||||
- vim-plug: `:PlugUpdate fzf`
|
- vim-plug: `:PlugUpdate fzf`
|
||||||
|
|
||||||
|
### Windows
|
||||||
|
|
||||||
|
Pre-built binaries for Windows can be downloaded [here][bin]. However, other
|
||||||
|
components of the project may not work on Windows. You might want to consider
|
||||||
|
installing fzf on [Windows Subsystem for Linux][wsl] where everything runs
|
||||||
|
flawlessly.
|
||||||
|
|
||||||
|
[wsl]: https://blogs.msdn.microsoft.com/wsl/
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
@@ -101,7 +111,7 @@ vim $(fzf)
|
|||||||
|
|
||||||
#### Using the finder
|
#### Using the finder
|
||||||
|
|
||||||
- `CTRL-J` / `CTRL-K` (or `CTRL-N` / `CTRL-P)` to move cursor up and down
|
- `CTRL-J` / `CTRL-K` (or `CTRL-N` / `CTRL-P`) to move cursor up and down
|
||||||
- `Enter` key to select the item, `CTRL-C` / `CTRL-G` / `ESC` to exit
|
- `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
|
- On multi-select mode (`-m`), `TAB` and `Shift-TAB` to mark multiple items
|
||||||
- Emacs style key bindings
|
- Emacs style key bindings
|
||||||
@@ -112,16 +122,16 @@ vim $(fzf)
|
|||||||
|
|
||||||
Unless otherwise specified, fzf starts in "extended-search mode" where you can
|
Unless otherwise specified, fzf starts in "extended-search mode" where you can
|
||||||
type in multiple search terms delimited by spaces. e.g. `^music .mp3$ sbtrkt
|
type in multiple search terms delimited by spaces. e.g. `^music .mp3$ sbtrkt
|
||||||
!rmx`
|
!fire`
|
||||||
|
|
||||||
| Token | Match type | Description |
|
| Token | Match type | Description |
|
||||||
| -------- | -------------------- | -------------------------------- |
|
| -------- | -------------------------- | --------------------------------- |
|
||||||
| `sbtrkt` | fuzzy-match | Items that match `sbtrkt` |
|
| `sbtrkt` | fuzzy-match | Items that match `sbtrkt` |
|
||||||
| `^music` | prefix-exact-match | Items that start with `music` |
|
| `^music` | prefix-exact-match | Items that start with `music` |
|
||||||
| `.mp3$` | suffix-exact-match | Items that end with `.mp3` |
|
| `.mp3$` | suffix-exact-match | Items that end with `.mp3` |
|
||||||
| `'wild` | exact-match (quoted) | Items that include `wild` |
|
| `'wild` | exact-match (quoted) | Items that include `wild` |
|
||||||
| `!rmx` | inverse-fuzzy-match | Items that do not match `rmx` |
|
| `!fire` | inverse-exact-match | Items that do not include `fire` |
|
||||||
| `!'fire` | inverse-exact-match | Items that do not include `fire` |
|
| `!.mp3$` | inverse-suffix-exact-match | Items that do not end with `.mp3` |
|
||||||
|
|
||||||
If you don't prefer fuzzy matching and do not wish to "quote" every word,
|
If you don't prefer fuzzy matching and do not wish to "quote" every word,
|
||||||
start fzf with `-e` or `--exact` option. Note that when `--exact` is set,
|
start fzf with `-e` or `--exact` option. Note that when `--exact` is set,
|
||||||
@@ -144,6 +154,10 @@ or `py`.
|
|||||||
- Default options
|
- Default options
|
||||||
- e.g. `export FZF_DEFAULT_OPTS="--reverse --inline-info"`
|
- e.g. `export FZF_DEFAULT_OPTS="--reverse --inline-info"`
|
||||||
|
|
||||||
|
#### Options
|
||||||
|
|
||||||
|
See the man page (`man fzf`) for the full list of options.
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
|
|
||||||
@@ -151,27 +165,6 @@ Many useful examples can be found on [the wiki
|
|||||||
page](https://github.com/junegunn/fzf/wiki/examples). Feel free to add your
|
page](https://github.com/junegunn/fzf/wiki/examples). Feel free to add your
|
||||||
own as well.
|
own as well.
|
||||||
|
|
||||||
Key bindings for command line
|
|
||||||
-----------------------------
|
|
||||||
|
|
||||||
The install script will setup the following key bindings for bash, zsh, and
|
|
||||||
fish.
|
|
||||||
|
|
||||||
- `CTRL-T` - Paste the selected files and directories onto the command line
|
|
||||||
- Set `FZF_CTRL_T_COMMAND` to override the default command
|
|
||||||
- `CTRL-R` - Paste the selected command from history onto the command line
|
|
||||||
- Sort is disabled by default to respect chronological ordering
|
|
||||||
- Press `CTRL-R` again to toggle sort
|
|
||||||
- `ALT-C` - cd into the selected directory
|
|
||||||
|
|
||||||
If you're on a tmux session, fzf will start in a split pane. You may disable
|
|
||||||
this tmux integration by setting `FZF_TMUX` to 0, or change the height of the
|
|
||||||
pane with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
`fzf-tmux` script
|
`fzf-tmux` script
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
@@ -191,6 +184,33 @@ 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]`
|
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.
|
options, so you can invariably use `fzf-tmux` in your scripts.
|
||||||
|
|
||||||
|
Key bindings for command line
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
The install script will setup the following key bindings for bash, zsh, and
|
||||||
|
fish.
|
||||||
|
|
||||||
|
- `CTRL-T` - Paste the selected files and directories onto the command line
|
||||||
|
- Set `FZF_CTRL_T_COMMAND` to override the default command
|
||||||
|
- Set `FZF_CTRL_T_OPTS` to pass additional options
|
||||||
|
- `CTRL-R` - Paste the selected command from history onto the command line
|
||||||
|
- Sort is disabled by default to respect chronological ordering
|
||||||
|
- Press `CTRL-R` again to toggle sort
|
||||||
|
- Set `FZF_CTRL_R_OPTS` to pass additional options
|
||||||
|
- `ALT-C` - cd into the selected directory
|
||||||
|
- Set `FZF_ALT_C_COMMAND` to override the default command
|
||||||
|
- Set `FZF_ALT_C_OPTS` to pass additional options
|
||||||
|
|
||||||
|
If you're on a tmux session, fzf will start in a split pane. You may disable
|
||||||
|
this tmux integration by setting `FZF_TMUX` to 0, or change the height of the
|
||||||
|
pane with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
More tips can be found on [the wiki page](https://github.com/junegunn/fzf/wiki/Configuring-shell-key-bindings).
|
||||||
|
|
||||||
Fuzzy completion for bash and zsh
|
Fuzzy completion for bash and zsh
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
@@ -259,6 +279,25 @@ export FZF_COMPLETION_TRIGGER='~~'
|
|||||||
|
|
||||||
# Options to fzf command
|
# Options to fzf command
|
||||||
export FZF_COMPLETION_OPTS='+c -x'
|
export FZF_COMPLETION_OPTS='+c -x'
|
||||||
|
|
||||||
|
# Use ag instead of the default find command for listing candidates.
|
||||||
|
# - The first argument to the function is the base path to start traversal
|
||||||
|
# - Note that ag only lists files not directories
|
||||||
|
# - See the source code (completion.{bash,zsh}) for the details.
|
||||||
|
_fzf_compgen_path() {
|
||||||
|
ag -g "" "$1"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Supported commands
|
||||||
|
|
||||||
|
On bash, fuzzy completion is enabled only for a predefined set of commands
|
||||||
|
(`complete | grep _fzf` to see the list). But you can enable it for other
|
||||||
|
commands as well like follows.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# There are also _fzf_path_completion and _fzf_dir_completion
|
||||||
|
complete -F _fzf_file_completion -o default -o bashdefault doge
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage as Vim plugin
|
Usage as Vim plugin
|
||||||
@@ -281,7 +320,7 @@ If you have set up fzf for Vim, `:FZF` command will be added.
|
|||||||
:FZF ~
|
:FZF ~
|
||||||
|
|
||||||
" With options
|
" With options
|
||||||
:FZF --no-sort -m /tmp
|
:FZF --no-sort --reverse --inline-info /tmp
|
||||||
|
|
||||||
" Bang version starts in fullscreen instead of using tmux pane or Neovim split
|
" Bang version starts in fullscreen instead of using tmux pane or Neovim split
|
||||||
:FZF!
|
:FZF!
|
||||||
@@ -295,12 +334,12 @@ Note that the environment variables `FZF_DEFAULT_COMMAND` and
|
|||||||
`FZF_DEFAULT_OPTS` also apply here. Refer to [the wiki page][fzf-config] for
|
`FZF_DEFAULT_OPTS` also apply here. Refer to [the wiki page][fzf-config] for
|
||||||
customization.
|
customization.
|
||||||
|
|
||||||
[fzf-config]: https://github.com/junegunn/fzf/wiki/Configuring-FZF-command-(vim)
|
[fzf-config]: https://github.com/junegunn/fzf/wiki/Configuring-Vim-plugin
|
||||||
|
|
||||||
#### `fzf#run([options])`
|
#### `fzf#run`
|
||||||
|
|
||||||
For more advanced uses, you can use `fzf#run()` function with the following
|
For more advanced uses, you can use `fzf#run([options])` function with the
|
||||||
options.
|
following options.
|
||||||
|
|
||||||
| Option name | Type | Description |
|
| Option name | Type | Description |
|
||||||
| -------------------------- | ------------- | ---------------------------------------------------------------- |
|
| -------------------------- | ------------- | ---------------------------------------------------------------- |
|
||||||
@@ -319,12 +358,24 @@ options.
|
|||||||
Examples can be found on [the wiki
|
Examples can be found on [the wiki
|
||||||
page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).
|
page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).
|
||||||
|
|
||||||
|
#### `fzf#wrap`
|
||||||
|
|
||||||
|
`fzf#wrap([name string,] [opts dict,] [fullscreen boolean])` is a helper
|
||||||
|
function that decorates the options dictionary so that it understands
|
||||||
|
`g:fzf_layout`, `g:fzf_action`, `g:fzf_colors`, and `g:fzf_history_dir` like
|
||||||
|
`:FZF`.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
command! -bang MyStuff
|
||||||
|
\ call fzf#run(fzf#wrap('my-stuff', {'dir': '~/my-stuff'}, <bang>0))
|
||||||
|
```
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
----
|
----
|
||||||
|
|
||||||
#### Rendering issues
|
#### Rendering issues
|
||||||
|
|
||||||
If you have any rendering issues, check the followings:
|
If you have any rendering issues, check the following:
|
||||||
|
|
||||||
1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
|
1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
|
||||||
contains `256` (e.g. `xterm-256color`)
|
contains `256` (e.g. `xterm-256color`)
|
||||||
@@ -356,6 +407,12 @@ fzf
|
|||||||
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
|
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you don't want to exclude hidden files, use the following command:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export FZF_DEFAULT_COMMAND='ag --hidden --ignore .git -g ""'
|
||||||
|
```
|
||||||
|
|
||||||
#### `git ls-tree` for fast traversal
|
#### `git ls-tree` for fast traversal
|
||||||
|
|
||||||
If you're running fzf in a large git repository, `git ls-tree` can boost up the
|
If you're running fzf in a large git repository, `git ls-tree` can boost up the
|
||||||
|
|||||||
118
bin/fzf-tmux
118
bin/fzf-tmux
@@ -2,24 +2,52 @@
|
|||||||
# fzf-tmux: starts fzf in a tmux pane
|
# fzf-tmux: starts fzf in a tmux pane
|
||||||
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
||||||
|
|
||||||
|
fail() {
|
||||||
|
>&2 echo "$1"
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
fzf="$(command -v fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
||||||
|
[[ -x "$fzf" ]] || fail 'fzf executable not found'
|
||||||
|
|
||||||
args=()
|
args=()
|
||||||
opt=""
|
opt=""
|
||||||
skip=""
|
skip=""
|
||||||
swap=""
|
swap=""
|
||||||
close=""
|
close=""
|
||||||
term=""
|
term=""
|
||||||
while [ $# -gt 0 ]; do
|
[[ -n "$LINES" ]] && lines=$LINES || lines=$(tput lines)
|
||||||
|
[[ -n "$COLUMNS" ]] && columns=$COLUMNS || columns=$(tput cols)
|
||||||
|
|
||||||
|
help() {
|
||||||
|
>&2 echo 'usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
||||||
|
|
||||||
|
Layout
|
||||||
|
-u [HEIGHT[%]] Split above (up)
|
||||||
|
-d [HEIGHT[%]] Split below (down)
|
||||||
|
-l [WIDTH[%]] Split left
|
||||||
|
-r [WIDTH[%]] Split right
|
||||||
|
|
||||||
|
(default: -d 50%)
|
||||||
|
'
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
arg="$1"
|
arg="$1"
|
||||||
case "$arg" in
|
shift
|
||||||
|
[[ -z "$skip" ]] && case "$arg" in
|
||||||
-)
|
-)
|
||||||
term=1
|
term=1
|
||||||
;;
|
;;
|
||||||
|
--help)
|
||||||
|
help
|
||||||
|
;;
|
||||||
|
--version)
|
||||||
|
echo "fzf-tmux (with fzf $("$fzf" --version))"
|
||||||
|
exit
|
||||||
|
;;
|
||||||
-w*|-h*|-d*|-u*|-r*|-l*)
|
-w*|-h*|-d*|-u*|-r*|-l*)
|
||||||
if [ -n "$skip" ]; then
|
|
||||||
args+=("$1")
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
if [[ "$arg" =~ ^.[lrw] ]]; then
|
if [[ "$arg" =~ ^.[lrw] ]]; then
|
||||||
opt="-h"
|
opt="-h"
|
||||||
if [[ "$arg" =~ ^.l ]]; then
|
if [[ "$arg" =~ ^.l ]]; then
|
||||||
@@ -35,35 +63,33 @@ while [ $# -gt 0 ]; do
|
|||||||
close="; tmux swap-pane -D"
|
close="; tmux swap-pane -D"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
if [ ${#arg} -gt 2 ]; then
|
if [[ ${#arg} -gt 2 ]]; then
|
||||||
size="${arg:2}"
|
size="${arg:2}"
|
||||||
else
|
else
|
||||||
shift
|
|
||||||
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
|
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
|
||||||
size="$1"
|
size="$1"
|
||||||
else
|
|
||||||
[ -n "$1" -a "$1" != "--" ] && args+=("$1")
|
|
||||||
shift
|
shift
|
||||||
|
else
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$size" =~ %$ ]]; then
|
if [[ "$size" =~ %$ ]]; then
|
||||||
size=${size:0:((${#size}-1))}
|
size=${size:0:((${#size}-1))}
|
||||||
if [ -n "$swap" ]; then
|
if [[ -n "$swap" ]]; then
|
||||||
opt="$opt -p $(( 100 - size ))"
|
opt="$opt -p $(( 100 - size ))"
|
||||||
else
|
else
|
||||||
opt="$opt -p $size"
|
opt="$opt -p $size"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if [ -n "$swap" ]; then
|
if [[ -n "$swap" ]]; then
|
||||||
if [[ "$arg" =~ ^.l ]]; then
|
if [[ "$arg" =~ ^.l ]]; then
|
||||||
[ -n "$COLUMNS" ] && max=$COLUMNS || max=$(tput cols)
|
max=$columns
|
||||||
else
|
else
|
||||||
[ -n "$LINES" ] && max=$LINES || max=$(tput lines)
|
max=$lines
|
||||||
fi
|
fi
|
||||||
size=$(( max - size ))
|
size=$(( max - size ))
|
||||||
[ $size -lt 0 ] && size=0
|
[[ $size -lt 0 ]] && size=0
|
||||||
opt="$opt -l $size"
|
opt="$opt -l $size"
|
||||||
else
|
else
|
||||||
opt="$opt -l $size"
|
opt="$opt -l $size"
|
||||||
@@ -74,19 +100,28 @@ while [ $# -gt 0 ]; do
|
|||||||
# "--" can be used to separate fzf-tmux options from fzf options to
|
# "--" can be used to separate fzf-tmux options from fzf options to
|
||||||
# avoid conflicts
|
# avoid conflicts
|
||||||
skip=1
|
skip=1
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
args+=("$1")
|
args+=("$arg")
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
shift
|
[[ -n "$skip" ]] && args+=("$arg")
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z "$TMUX_PANE" ] || tmux list-panes -F '#F' | grep -q Z; then
|
if [[ -z "$TMUX" || "$opt" =~ ^-h && "$columns" -le 40 || ! "$opt" =~ ^-h && "$lines" -le 15 ]]; then
|
||||||
fzf "${args[@]}"
|
"$fzf" "${args[@]}"
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Handle zoomed tmux pane by moving it to a temp window
|
||||||
|
if tmux list-panes -F '#F' | grep -q Z; then
|
||||||
|
zoomed=1
|
||||||
|
original_window=$(tmux display-message -p "#{window_id}")
|
||||||
|
tmp_window=$(tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - \\\\; do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'")
|
||||||
|
tmux swap-pane -t $tmp_window \; select-window -t $tmp_window
|
||||||
|
fi
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Clean up named pipes on exit
|
# Clean up named pipes on exit
|
||||||
@@ -97,40 +132,47 @@ fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id"
|
|||||||
fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"
|
fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"
|
||||||
cleanup() {
|
cleanup() {
|
||||||
rm -f $argsf $fifo1 $fifo2 $fifo3
|
rm -f $argsf $fifo1 $fifo2 $fifo3
|
||||||
|
|
||||||
|
# Remove temp window if we were zoomed
|
||||||
|
if [[ -n "$zoomed" ]]; then
|
||||||
|
tmux swap-pane -t $original_window \; \
|
||||||
|
select-window -t $original_window \; \
|
||||||
|
kill-window -t $tmp_window \; \
|
||||||
|
resize-pane -Z
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
trap cleanup EXIT SIGINT SIGTERM
|
trap cleanup EXIT SIGINT SIGTERM
|
||||||
|
|
||||||
fail() {
|
|
||||||
>&2 echo "$1"
|
|
||||||
exit 2
|
|
||||||
}
|
|
||||||
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
|
||||||
[ -x "$fzf" ] || fail "fzf executable not found"
|
|
||||||
|
|
||||||
envs="env TERM=$TERM "
|
envs="env TERM=$TERM "
|
||||||
[ -n "$FZF_DEFAULT_OPTS" ] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
|
[[ -n "$FZF_DEFAULT_OPTS" ]] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
|
||||||
[ -n "$FZF_DEFAULT_COMMAND" ] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
|
[[ -n "$FZF_DEFAULT_COMMAND" ]] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
|
||||||
|
|
||||||
mkfifo $fifo2
|
mkfifo -m o+w $fifo2
|
||||||
mkfifo $fifo3
|
mkfifo -m o+w $fifo3
|
||||||
|
|
||||||
# Build arguments to fzf
|
# Build arguments to fzf
|
||||||
opts=""
|
opts=""
|
||||||
for arg in "${args[@]}"; do
|
for arg in "${args[@]}"; do
|
||||||
|
arg="${arg//\\/\\\\}"
|
||||||
arg="${arg//\"/\\\"}"
|
arg="${arg//\"/\\\"}"
|
||||||
arg="${arg//\`/\\\`}"
|
arg="${arg//\`/\\\`}"
|
||||||
|
arg="${arg//$/\\$}"
|
||||||
opts="$opts \"$arg\""
|
opts="$opts \"$arg\""
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -n "$term" -o -t 0 ]; then
|
if [[ -n "$term" ]] || [[ -t 0 ]]; then
|
||||||
cat <<< "$fzf $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
||||||
tmux set-window-option -q synchronize-panes off \;\
|
TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\
|
||||||
split-window $opt "cd $(printf %q "$PWD");$envs bash $argsf" $swap
|
set-window-option remain-on-exit off \;\
|
||||||
|
split-window $opt "cd $(printf %q "$PWD");$envs bash $argsf" $swap \
|
||||||
|
> /dev/null 2>&1
|
||||||
else
|
else
|
||||||
mkfifo $fifo1
|
mkfifo $fifo1
|
||||||
cat <<< "$fzf $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
||||||
tmux set-window-option -q synchronize-panes off \;\
|
TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\
|
||||||
split-window $opt "$envs bash $argsf" $swap
|
set-window-option remain-on-exit off \;\
|
||||||
|
split-window $opt "$envs bash $argsf" $swap \
|
||||||
|
> /dev/null 2>&1
|
||||||
cat <&0 > $fifo1 &
|
cat <&0 > $fifo1 &
|
||||||
fi
|
fi
|
||||||
cat $fifo2
|
cat $fifo2
|
||||||
|
|||||||
4
fzf
4
fzf
@@ -370,7 +370,7 @@ class FZF
|
|||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END]).
|
||||||
--with-nth=N[,..] Transform the item using index expressions for search
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
@@ -396,7 +396,7 @@ class FZF
|
|||||||
|
|
||||||
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 Default options (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||||
exit x
|
exit x
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
157
install
157
install
@@ -2,29 +2,33 @@
|
|||||||
|
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
[[ "$@" =~ --pre ]] && version=0.11.0 pre=1 ||
|
[[ "$@" =~ --pre ]] && version=0.15.8 pre=1 ||
|
||||||
version=0.11.0 pre=0
|
version=0.15.8 pre=0
|
||||||
|
|
||||||
auto_completion=
|
auto_completion=
|
||||||
key_bindings=
|
key_bindings=
|
||||||
update_config=1
|
update_config=2
|
||||||
|
binary_arch=
|
||||||
|
allow_legacy=
|
||||||
|
|
||||||
help() {
|
help() {
|
||||||
cat << EOF
|
cat << EOF
|
||||||
usage: $0 [OPTIONS]
|
usage: $0 [OPTIONS]
|
||||||
|
|
||||||
--help Show this message
|
--help Show this message
|
||||||
--bin Download fzf binary only
|
--bin Download fzf binary only; Do not generate ~/.fzf.{bash,zsh}
|
||||||
--all Download fzf binary and update configuration files
|
--all Download fzf binary and update configuration files
|
||||||
to enable key bindings and fuzzy completion
|
to enable key bindings and fuzzy completion
|
||||||
--[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C)
|
--[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C)
|
||||||
--[no-]completion Enable/disable fuzzy completion (bash & zsh)
|
--[no-]completion Enable/disable fuzzy completion (bash & zsh)
|
||||||
--[no-]update-rc Whether or not to update shell configuration files
|
--[no-]update-rc Whether or not to update shell configuration files
|
||||||
|
|
||||||
|
--32 Download 32-bit binary
|
||||||
|
--64 Download 64-bit binary
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
for opt in $@; do
|
for opt in "$@"; do
|
||||||
case $opt in
|
case $opt in
|
||||||
--help)
|
--help)
|
||||||
help
|
help
|
||||||
@@ -34,6 +38,7 @@ for opt in $@; do
|
|||||||
auto_completion=1
|
auto_completion=1
|
||||||
key_bindings=1
|
key_bindings=1
|
||||||
update_config=1
|
update_config=1
|
||||||
|
allow_legacy=1
|
||||||
;;
|
;;
|
||||||
--key-bindings) key_bindings=1 ;;
|
--key-bindings) key_bindings=1 ;;
|
||||||
--no-key-bindings) key_bindings=0 ;;
|
--no-key-bindings) key_bindings=0 ;;
|
||||||
@@ -41,7 +46,9 @@ for opt in $@; do
|
|||||||
--no-completion) auto_completion=0 ;;
|
--no-completion) auto_completion=0 ;;
|
||||||
--update-rc) update_config=1 ;;
|
--update-rc) update_config=1 ;;
|
||||||
--no-update-rc) update_config=0 ;;
|
--no-update-rc) update_config=0 ;;
|
||||||
--bin) ;;
|
--32) binary_arch=386 ;;
|
||||||
|
--64) binary_arch=amd64 ;;
|
||||||
|
--bin|--pre) ;;
|
||||||
*)
|
*)
|
||||||
echo "unknown option: $opt"
|
echo "unknown option: $opt"
|
||||||
help
|
help
|
||||||
@@ -50,17 +57,14 @@ for opt in $@; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
cd $(dirname $BASH_SOURCE)
|
cd "$(dirname "${BASH_SOURCE[0]}")"
|
||||||
fzf_base="$(pwd)"
|
fzf_base="$(pwd)"
|
||||||
|
|
||||||
# If stdin is a tty, we are "interactive".
|
|
||||||
interactive=
|
|
||||||
[ -t 0 ] && interactive=yes
|
|
||||||
|
|
||||||
ask() {
|
ask() {
|
||||||
|
# If stdin is a tty, we are "interactive".
|
||||||
# non-interactive shell: wait for a linefeed
|
# non-interactive shell: wait for a linefeed
|
||||||
# interactive shell: continue after a single keypress
|
# interactive shell: continue after a single keypress
|
||||||
[ -n "$interactive" ] && read_n='-n 1' || read_n=
|
read_n=$([ -t 0 ] && echo "-n 1")
|
||||||
|
|
||||||
read -p "$1 ([y]/n) " $read_n -r
|
read -p "$1 ([y]/n) " $read_n -r
|
||||||
echo
|
echo
|
||||||
@@ -97,6 +101,24 @@ symlink() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
link_fzf_in_path() {
|
||||||
|
if which_fzf="$(command -v fzf)"; then
|
||||||
|
echo " - Found in \$PATH"
|
||||||
|
echo " - Creating symlink: $which_fzf -> bin/fzf"
|
||||||
|
(cd "$fzf_base"/bin && rm -f fzf && ln -sf "$which_fzf" fzf)
|
||||||
|
check_binary && return
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
try_curl() {
|
||||||
|
command -v curl > /dev/null && curl -fL $1 | tar -xz
|
||||||
|
}
|
||||||
|
|
||||||
|
try_wget() {
|
||||||
|
command -v wget > /dev/null && wget -O - $1 | tar -xz
|
||||||
|
}
|
||||||
|
|
||||||
download() {
|
download() {
|
||||||
echo "Downloading bin/fzf ..."
|
echo "Downloading bin/fzf ..."
|
||||||
if [ $pre = 0 ]; then
|
if [ $pre = 0 ]; then
|
||||||
@@ -107,12 +129,7 @@ download() {
|
|||||||
if [ -x "$fzf_base"/bin/$1 ]; then
|
if [ -x "$fzf_base"/bin/$1 ]; then
|
||||||
symlink $1 && check_binary && return
|
symlink $1 && check_binary && return
|
||||||
fi
|
fi
|
||||||
if which_fzf="$(which fzf 2> /dev/null)"; then
|
link_fzf_in_path && return
|
||||||
echo " - Found in \$PATH"
|
|
||||||
echo " - Creating symlink: $which_fzf -> bin/fzf"
|
|
||||||
(cd "$fzf_base"/bin && rm -f fzf && ln -sf "$which_fzf" fzf)
|
|
||||||
check_binary && return
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
@@ -121,14 +138,13 @@ download() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
|
local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
|
||||||
if which curl > /dev/null; then
|
set -o pipefail
|
||||||
curl -fL $url | tar -xz
|
if ! (try_curl $url || try_wget $url); then
|
||||||
elif which wget > /dev/null; then
|
set +o pipefail
|
||||||
wget -O - $url | tar -xz
|
binary_error="Failed to download with curl and wget"
|
||||||
else
|
|
||||||
binary_error="curl or wget not found"
|
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
set +o pipefail
|
||||||
|
|
||||||
if [ ! -f $1 ]; then
|
if [ ! -f $1 ]; then
|
||||||
binary_error="Failed to download ${1}"
|
binary_error="Failed to download ${1}"
|
||||||
@@ -143,26 +159,22 @@ archi=$(uname -sm)
|
|||||||
binary_available=1
|
binary_available=1
|
||||||
binary_error=""
|
binary_error=""
|
||||||
case "$archi" in
|
case "$archi" in
|
||||||
Darwin\ x86_64) download fzf-$version-darwin_amd64 ;;
|
Darwin\ x86_64) download fzf-$version-darwin_${binary_arch:-amd64} ;;
|
||||||
Darwin\ i*86) download fzf-$version-darwin_386 ;;
|
Darwin\ i*86) download fzf-$version-darwin_${binary_arch:-386} ;;
|
||||||
Linux\ x86_64) download fzf-$version-linux_amd64 ;;
|
Linux\ x86_64) download fzf-$version-linux_${binary_arch:-amd64} ;;
|
||||||
Linux\ i*86) download fzf-$version-linux_386 ;;
|
Linux\ i*86) download fzf-$version-linux_${binary_arch:-386} ;;
|
||||||
*) binary_available=0 binary_error=1 ;;
|
*) binary_available=0 binary_error=1 ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
cd "$fzf_base"
|
install_ruby_fzf() {
|
||||||
if [ -n "$binary_error" ]; then
|
if [ -z "$allow_legacy" ]; then
|
||||||
if [ $binary_available -eq 0 ]; then
|
ask "Do you want to install legacy Ruby version instead?" && exit 1
|
||||||
echo "No prebuilt binary for $archi ... "
|
|
||||||
else
|
|
||||||
echo " - $binary_error !!!"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
echo "Installing legacy Ruby version ..."
|
echo "Installing legacy Ruby version ..."
|
||||||
|
|
||||||
# ruby executable
|
# ruby executable
|
||||||
echo -n "Checking Ruby executable ... "
|
echo -n "Checking Ruby executable ... "
|
||||||
ruby=`which ruby`
|
ruby=$(command -v ruby)
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "ruby executable not found !!!"
|
echo "ruby executable not found !!!"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -170,7 +182,7 @@ if [ -n "$binary_error" ]; then
|
|||||||
|
|
||||||
# System ruby is preferred
|
# System ruby is preferred
|
||||||
system_ruby=/usr/bin/ruby
|
system_ruby=/usr/bin/ruby
|
||||||
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
if [ -x $system_ruby ] && [ $system_ruby != "$ruby" ]; then
|
||||||
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
||||||
[ $? -eq 0 ] && ruby=$system_ruby
|
[ $? -eq 0 ] && ruby=$system_ruby
|
||||||
fi
|
fi
|
||||||
@@ -223,6 +235,32 @@ if [ -n "$binary_error" ]; then
|
|||||||
echo "$fzf_cmd \"\$@\"" >> "$fzf_base"/bin/fzf
|
echo "$fzf_cmd \"\$@\"" >> "$fzf_base"/bin/fzf
|
||||||
chmod +x "$fzf_base"/bin/fzf
|
chmod +x "$fzf_base"/bin/fzf
|
||||||
echo "OK"
|
echo "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
if command -v go > /dev/null; then
|
||||||
|
echo -n "Building binary (go get -u github.com/junegunn/fzf/src/fzf) ... "
|
||||||
|
if [ -z "${GOPATH-}" ]; then
|
||||||
|
export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"
|
||||||
|
mkdir -p "$GOPATH"
|
||||||
|
fi
|
||||||
|
if go get -u github.com/junegunn/fzf/src/fzf; then
|
||||||
|
echo "OK"
|
||||||
|
cp "$GOPATH/bin/fzf" "$fzf_base/bin/"
|
||||||
|
else
|
||||||
|
echo "Failed to build binary ..."
|
||||||
|
install_ruby_fzf
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "go executable not found. Cannot build binary ..."
|
||||||
|
install_ruby_fzf
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ "$*" =~ "--bin" ]] && exit 0
|
[[ "$*" =~ "--bin" ]] && exit 0
|
||||||
@@ -240,7 +278,9 @@ if [ -z "$key_bindings" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo
|
echo
|
||||||
for shell in bash zsh; do
|
has_zsh=$(command -v zsh > /dev/null && echo 1 || echo 0)
|
||||||
|
shells=$([ $has_zsh -eq 1 ] && echo "bash zsh" || echo "bash")
|
||||||
|
for shell in $shells; do
|
||||||
echo -n "Generate ~/.fzf.$shell ... "
|
echo -n "Generate ~/.fzf.$shell ... "
|
||||||
src=~/.fzf.${shell}
|
src=~/.fzf.${shell}
|
||||||
|
|
||||||
@@ -280,9 +320,8 @@ EOF
|
|||||||
done
|
done
|
||||||
|
|
||||||
# fish
|
# fish
|
||||||
has_fish=0
|
has_fish=$(command -v fish > /dev/null && echo 1 || echo 0)
|
||||||
if [ -n "$(which fish 2> /dev/null)" ]; then
|
if [ $has_fish -eq 1 ]; then
|
||||||
has_fish=1
|
|
||||||
echo -n "Update fish_user_paths ... "
|
echo -n "Update fish_user_paths ... "
|
||||||
fish << EOF
|
fish << EOF
|
||||||
echo \$fish_user_paths | grep $fzf_base/bin > /dev/null
|
echo \$fish_user_paths | grep $fzf_base/bin > /dev/null
|
||||||
@@ -311,8 +350,8 @@ fi
|
|||||||
append_line() {
|
append_line() {
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
local skip line file pat lno
|
local update line file pat lno
|
||||||
skip="$1"
|
update="$1"
|
||||||
line="$2"
|
line="$2"
|
||||||
file="$3"
|
file="$3"
|
||||||
pat="${4:-}"
|
pat="${4:-}"
|
||||||
@@ -328,7 +367,7 @@ append_line() {
|
|||||||
if [ -n "$lno" ]; then
|
if [ -n "$lno" ]; then
|
||||||
echo " - Already exists: line #$lno"
|
echo " - Already exists: line #$lno"
|
||||||
else
|
else
|
||||||
if [ $skip -eq 1 ]; then
|
if [ $update -eq 1 ]; then
|
||||||
echo >> "$file"
|
echo >> "$file"
|
||||||
echo "$line" >> "$file"
|
echo "$line" >> "$file"
|
||||||
echo " + Added"
|
echo " + Added"
|
||||||
@@ -340,26 +379,30 @@ append_line() {
|
|||||||
set +e
|
set +e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if [ $update_config -eq 2 ]; then
|
||||||
|
echo
|
||||||
|
ask "Do you want to update your shell configuration files?"
|
||||||
|
update_config=$?
|
||||||
|
fi
|
||||||
echo
|
echo
|
||||||
for shell in bash zsh; do
|
for shell in $shells; do
|
||||||
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
|
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
|
||||||
append_line $update_config "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" "$dest" "~/.fzf.${shell}"
|
append_line $update_config "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" "$dest" "~/.fzf.${shell}"
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $key_bindings -eq 1 -a $has_fish -eq 1 ]; then
|
if [ $key_bindings -eq 1 ] && [ $has_fish -eq 1 ]; then
|
||||||
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
append_line $update_config "fzf_key_bindings" "$bind_file"
|
append_line $update_config "fzf_key_bindings" "$bind_file"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat << EOF
|
if [ $update_config -eq 1 ]; then
|
||||||
Finished. Restart your shell or reload config file.
|
echo 'Finished. Restart your shell or reload config file.'
|
||||||
source ~/.bashrc # bash
|
echo ' source ~/.bashrc # bash'
|
||||||
source ${ZDOTDIR:-~}/.zshrc # zsh
|
[ $has_zsh -eq 1 ] && echo " source ${ZDOTDIR:-~}/.zshrc # zsh"
|
||||||
EOF
|
[ $has_fish -eq 1 ] && [ $key_bindings -eq 1 ] && echo ' fzf_key_bindings # fish'
|
||||||
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
echo
|
||||||
|
echo 'Use uninstall script to remove fzf.'
|
||||||
Use uninstall script to remove fzf.
|
echo
|
||||||
|
fi
|
||||||
For more information, see: https://github.com/junegunn/fzf
|
echo 'For more information, see: https://github.com/junegunn/fzf'
|
||||||
EOF
|
|
||||||
|
|
||||||
|
|||||||
54
man/man1/fzf-tmux.1
Normal file
54
man/man1/fzf-tmux.1
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
.ig
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 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.
|
||||||
|
..
|
||||||
|
.TH fzf-tmux 1 "Nov 2016" "fzf 0.15.8" "fzf-tmux - open fzf in tmux split pane"
|
||||||
|
|
||||||
|
.SH NAME
|
||||||
|
fzf-tmux - open fzf in tmux split pane
|
||||||
|
|
||||||
|
.SH SYNOPSIS
|
||||||
|
.B fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
||||||
|
|
||||||
|
.SH DESCRIPTION
|
||||||
|
fzf-tmux is a wrapper script for fzf that opens fzf in a tmux split pane. It is
|
||||||
|
designed to work just like fzf except that it does not take up the whole
|
||||||
|
screen. You can safely use fzf-tmux instead of fzf in your scripts as the extra
|
||||||
|
options will be silently ignored if you're not on tmux.
|
||||||
|
|
||||||
|
.SH OPTIONS
|
||||||
|
.SS Layout
|
||||||
|
|
||||||
|
(default: \fB-d 50%\fR)
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B "-u [height[%]]"
|
||||||
|
Split above (up)
|
||||||
|
.TP
|
||||||
|
.B "-d [height[%]]"
|
||||||
|
Split below (down)
|
||||||
|
.TP
|
||||||
|
.B "-l [width[%]]"
|
||||||
|
Split left
|
||||||
|
.TP
|
||||||
|
.B "-r [width[%]]"
|
||||||
|
Split right
|
||||||
461
man/man1/fzf.1
461
man/man1/fzf.1
@@ -1,7 +1,7 @@
|
|||||||
.ig
|
.ig
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2015 Junegunn Choi
|
Copyright (c) 2016 Junegunn Choi
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
..
|
..
|
||||||
.TH fzf 1 "Nov 2015" "fzf 0.11.0" "fzf - a command-line fuzzy finder"
|
.TH fzf 1 "Nov 2016" "fzf 0.15.8" "fzf - a command-line fuzzy finder"
|
||||||
|
|
||||||
.SH NAME
|
.SH NAME
|
||||||
fzf - a command-line fuzzy finder
|
fzf - a command-line fuzzy finder
|
||||||
@@ -47,13 +47,23 @@ Case-insensitive match (default: smart-case match)
|
|||||||
.TP
|
.TP
|
||||||
.B "+i"
|
.B "+i"
|
||||||
Case-sensitive match
|
Case-sensitive match
|
||||||
|
.TP
|
||||||
|
.BI "--algo=" TYPE
|
||||||
|
Fuzzy matching algorithm (default: v2)
|
||||||
|
|
||||||
|
.br
|
||||||
|
.BR v2 " Optimal scoring algorithm (quality)"
|
||||||
|
.br
|
||||||
|
.BR v1 " Faster but not guaranteed to find the optimal result (performance)"
|
||||||
|
.br
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "-n, --nth=" "N[,..]"
|
.BI "-n, --nth=" "N[,..]"
|
||||||
Comma-separated list of field index expressions for limiting search scope.
|
Comma-separated list of field index expressions for limiting search scope.
|
||||||
See \fBFIELD INDEX EXPRESSION\fR for details.
|
See \fBFIELD INDEX EXPRESSION\fR for the details.
|
||||||
.TP
|
.TP
|
||||||
.BI "--with-nth=" "N[,..]"
|
.BI "--with-nth=" "N[,..]"
|
||||||
Transform each item using index expressions within finder
|
Transform the presentation of each line using field index expressions
|
||||||
.TP
|
.TP
|
||||||
.BI "-d, --delimiter=" "STR"
|
.BI "-d, --delimiter=" "STR"
|
||||||
Field delimiter regex for \fB--nth\fR and \fB--with-nth\fR (default: AWK-style)
|
Field delimiter regex for \fB--nth\fR and \fB--with-nth\fR (default: AWK-style)
|
||||||
@@ -64,33 +74,113 @@ Do not sort the result
|
|||||||
.TP
|
.TP
|
||||||
.B "--tac"
|
.B "--tac"
|
||||||
Reverse the order of the input
|
Reverse the order of the input
|
||||||
|
|
||||||
.RS
|
.RS
|
||||||
e.g. \fBhistory | fzf --tac --no-sort\fR
|
e.g. \fBhistory | fzf --tac --no-sort\fR
|
||||||
.RE
|
.RE
|
||||||
.TP
|
.TP
|
||||||
.BI "--tiebreak=" "CRI"
|
.BI "--tiebreak=" "CRI[,..]"
|
||||||
Sort criterion to use when the scores are tied
|
Comma-separated list of sort criteria to apply when the scores are tied.
|
||||||
.br
|
.br
|
||||||
.R ""
|
|
||||||
.br
|
.br
|
||||||
.BR length " Prefers item with shorter length"
|
.BR length " Prefers line with shorter length"
|
||||||
.br
|
.br
|
||||||
.BR begin " Prefers item with matched substring closer to the beginning"
|
.BR begin " Prefers line with matched substring closer to the beginning"
|
||||||
.br
|
.br
|
||||||
.BR end " Prefers item with matched substring closer to the end"
|
.BR end " Prefers line with matched substring closer to the end"
|
||||||
.br
|
.br
|
||||||
.BR index " Prefers item that appeared earlier in the input stream"
|
.BR index " Prefers line that appeared earlier in the input stream"
|
||||||
.br
|
.br
|
||||||
|
|
||||||
|
.br
|
||||||
|
- Each criterion should appear only once in the list
|
||||||
|
.br
|
||||||
|
- \fBindex\fR is only allowed at the end of the list
|
||||||
|
.br
|
||||||
|
- \fBindex\fR is implicitly appended to the list when not specified
|
||||||
|
.br
|
||||||
|
- Default is \fBlength\fR (or equivalently \fBlength\fR,index)
|
||||||
|
.br
|
||||||
|
- If \fBend\fR is found in the list, fzf will scan each line backwards
|
||||||
.SS Interface
|
.SS Interface
|
||||||
.TP
|
.TP
|
||||||
.B "-m, --multi"
|
.B "-m, --multi"
|
||||||
Enable multi-select with tab/shift-tab
|
Enable multi-select with tab/shift-tab
|
||||||
.TP
|
.TP
|
||||||
|
.B "--no-mouse"
|
||||||
|
Disable mouse
|
||||||
|
.TP
|
||||||
|
.BI "--bind=" "KEYBINDS"
|
||||||
|
Comma-separated list of custom key bindings. See \fBKEY BINDINGS\fR for the
|
||||||
|
details.
|
||||||
|
.TP
|
||||||
|
.B "--cycle"
|
||||||
|
Enable cyclic scroll
|
||||||
|
.TP
|
||||||
|
.B "--no-hscroll"
|
||||||
|
Disable horizontal scroll
|
||||||
|
.TP
|
||||||
|
.BI "--hscroll-off=" "COL"
|
||||||
|
Number of screen columns to keep to the right of the highlighted substring
|
||||||
|
(default: 10). Setting it to a large value will cause the text to be positioned
|
||||||
|
on the center of the screen.
|
||||||
|
.TP
|
||||||
|
.BI "--jump-labels=" "CHARS"
|
||||||
|
Label characters for \fBjump\fR and \fBjump-accept\fR
|
||||||
|
.SS Layout
|
||||||
|
.TP
|
||||||
|
.B "--reverse"
|
||||||
|
Reverse orientation
|
||||||
|
.TP
|
||||||
|
.BI "--margin=" MARGIN
|
||||||
|
Comma-separated expression for margins around the finder.
|
||||||
|
.br
|
||||||
|
|
||||||
|
.br
|
||||||
|
.RS
|
||||||
|
.BR TRBL " Same margin for top, right, bottom, and left"
|
||||||
|
.br
|
||||||
|
.BR TB,RL " Vertical, horizontal margin"
|
||||||
|
.br
|
||||||
|
.BR T,RL,B " Top, horizontal, bottom margin"
|
||||||
|
.br
|
||||||
|
.BR T,R,B,L " Top, right, bottom, left margin"
|
||||||
|
.br
|
||||||
|
|
||||||
|
.br
|
||||||
|
Each part can be given in absolute number or in percentage relative to the
|
||||||
|
terminal size with \fB%\fR suffix.
|
||||||
|
.br
|
||||||
|
|
||||||
|
.br
|
||||||
|
e.g. \fBfzf --margin 10%\fR
|
||||||
|
\fBfzf --margin 1,5%\fR
|
||||||
|
.RE
|
||||||
|
.TP
|
||||||
|
.B "--inline-info"
|
||||||
|
Display finder info inline with the query
|
||||||
|
.TP
|
||||||
|
.BI "--prompt=" "STR"
|
||||||
|
Input prompt (default: '> ')
|
||||||
|
.TP
|
||||||
|
.BI "--header=" "STR"
|
||||||
|
The given string will be printed as the sticky header. The lines are displayed
|
||||||
|
in the given order from top to bottom regardless of \fB--reverse\fR option, and
|
||||||
|
are not affected by \fB--with-nth\fR. ANSI color codes are processed even when
|
||||||
|
\fB--ansi\fR is not set.
|
||||||
|
.TP
|
||||||
|
.BI "--header-lines=" "N"
|
||||||
|
The first N lines of the input are treated as the sticky header. When
|
||||||
|
\fB--with-nth\fR is set, the lines are transformed just like the other
|
||||||
|
lines that follow.
|
||||||
|
.SS Display
|
||||||
|
.TP
|
||||||
.B "--ansi"
|
.B "--ansi"
|
||||||
Enable processing of ANSI color codes
|
Enable processing of ANSI color codes
|
||||||
.TP
|
.TP
|
||||||
.B "--no-mouse"
|
.BI "--tabstop=" SPACES
|
||||||
Disable mouse
|
Number of spaces for a tab character (default: 8)
|
||||||
.TP
|
.TP
|
||||||
.BI "--color=" "[BASE_SCHEME][,COLOR:ANSI]"
|
.BI "--color=" "[BASE_SCHEME][,COLOR:ANSI]"
|
||||||
Color configuration. The name of the base color scheme is followed by custom
|
Color configuration. The name of the base color scheme is followed by custom
|
||||||
@@ -126,164 +216,12 @@ e.g. \fBfzf --color=bg+:24\fR
|
|||||||
\fBheader \fRHeader
|
\fBheader \fRHeader
|
||||||
.RE
|
.RE
|
||||||
.TP
|
.TP
|
||||||
|
.B "--no-bold"
|
||||||
|
Do not use bold text
|
||||||
|
.TP
|
||||||
.B "--black"
|
.B "--black"
|
||||||
Use black background
|
Use black background
|
||||||
.TP
|
.SS History
|
||||||
.B "--reverse"
|
|
||||||
Reverse orientation
|
|
||||||
.TP
|
|
||||||
.BI "--margin=" MARGIN
|
|
||||||
Comma-separated expression for margins around the finder.
|
|
||||||
.br
|
|
||||||
.R ""
|
|
||||||
.br
|
|
||||||
.RS
|
|
||||||
.BR TRBL " Same margin for top, right, bottom, and left"
|
|
||||||
.br
|
|
||||||
.BR TB,RL " Vertical, horizontal margin"
|
|
||||||
.br
|
|
||||||
.BR T,RL,B " Top, horizontal, bottom margin"
|
|
||||||
.br
|
|
||||||
.BR T,R,B,L " Top, right, bottom, left margin"
|
|
||||||
.br
|
|
||||||
.R ""
|
|
||||||
.br
|
|
||||||
Each part can be given in absolute number or in percentage relative to the
|
|
||||||
terminal size with \fB%\fR suffix.
|
|
||||||
.br
|
|
||||||
.R ""
|
|
||||||
.br
|
|
||||||
e.g. \fBfzf --margin 10%\fR
|
|
||||||
\fBfzf --margin 1,5%\fR
|
|
||||||
.RE
|
|
||||||
.TP
|
|
||||||
.B "--cycle"
|
|
||||||
Enable cyclic scroll
|
|
||||||
.TP
|
|
||||||
.B "--no-hscroll"
|
|
||||||
Disable horizontal scroll
|
|
||||||
.TP
|
|
||||||
.B "--inline-info"
|
|
||||||
Display finder info inline with the query
|
|
||||||
.TP
|
|
||||||
.BI "--prompt=" "STR"
|
|
||||||
Input prompt (default: '> ')
|
|
||||||
.TP
|
|
||||||
.BI "--toggle-sort=" "KEY"
|
|
||||||
Key to toggle sort. For the list of the allowed key names, see \fB--bind\fR.
|
|
||||||
.TP
|
|
||||||
.BI "--bind=" "KEYBINDS"
|
|
||||||
Comma-separated list of custom key bindings. Each key binding expression
|
|
||||||
follows the following format: \fBKEY:ACTION\fR
|
|
||||||
.RS
|
|
||||||
e.g. \fBfzf --bind=ctrl-j:accept,ctrl-k:kill-line\fR
|
|
||||||
.RE
|
|
||||||
|
|
||||||
.RS
|
|
||||||
.B AVAILABLE KEYS: (SYNONYMS)
|
|
||||||
\fIctrl-[a-z]\fR
|
|
||||||
\fIalt-[a-z]\fR
|
|
||||||
\fIf[1-4]\fR
|
|
||||||
\fIenter\fR (\fIreturn\fR \fIctrl-m\fR)
|
|
||||||
\fIspace\fR
|
|
||||||
\fIbspace\fR (\fIbs\fR)
|
|
||||||
\fIalt-bspace\fR (\fIalt-bs\fR)
|
|
||||||
\fItab\fR
|
|
||||||
\fIbtab\fR (\fIshift-tab\fR)
|
|
||||||
\fIesc\fR
|
|
||||||
\fIdel\fR
|
|
||||||
\fIup\fR
|
|
||||||
\fIdown\fR
|
|
||||||
\fIleft\fR
|
|
||||||
\fIright\fR
|
|
||||||
\fIhome\fR
|
|
||||||
\fIend\fR
|
|
||||||
\fIpgup\fR (\fIpage-up\fR)
|
|
||||||
\fIpgdn\fR (\fIpage-down\fR)
|
|
||||||
\fIshift-left\fR
|
|
||||||
\fIshift-right\fR
|
|
||||||
\fIdouble-click\fR
|
|
||||||
or any single character
|
|
||||||
.RE
|
|
||||||
|
|
||||||
.RS
|
|
||||||
\fBACTION: DEFAULT BINDINGS:
|
|
||||||
\fBabort\fR \fIctrl-c ctrl-g ctrl-q esc\fR
|
|
||||||
\fBaccept\fR \fIenter double-click\fR
|
|
||||||
\fBbackward-char\fR \fIctrl-b left\fR
|
|
||||||
\fBbackward-delete-char\fR \fIctrl-h bspace\fR
|
|
||||||
\fBbackward-kill-word\fR \fIalt-bs\fR
|
|
||||||
\fBbackward-word\fR \fIalt-b shift-left\fR
|
|
||||||
\fBbeginning-of-line\fR \fIctrl-a home\fR
|
|
||||||
\fBcancel\fR
|
|
||||||
\fBclear-screen\fR \fIctrl-l\fR
|
|
||||||
\fBdelete-char\fR \fIdel\fR
|
|
||||||
\fBdelete-char/eof\fR \fIctrl-d\fR
|
|
||||||
\fBdeselect-all\fR
|
|
||||||
\fBdown\fR \fIctrl-j ctrl-n down\fR
|
|
||||||
\fBend-of-line\fR \fIctrl-e end\fR
|
|
||||||
\fBexecute(...)\fR (see below for the details)
|
|
||||||
\fBexecute-multi(...)\fR (see below for the details)
|
|
||||||
\fBforward-char\fR \fIctrl-f right\fR
|
|
||||||
\fBforward-word\fR \fIalt-f shift-right\fR
|
|
||||||
\fBignore\fR
|
|
||||||
\fBkill-line\fR
|
|
||||||
\fBkill-word\fR \fIalt-d\fR
|
|
||||||
\fBnext-history\fR (\fIctrl-n\fR on \fB--history\fR)
|
|
||||||
\fBpage-down\fR \fIpgdn\fR
|
|
||||||
\fBpage-up\fR \fIpgup\fR
|
|
||||||
\fBprevious-history\fR (\fIctrl-p\fR on \fB--history\fR)
|
|
||||||
\fBselect-all\fR
|
|
||||||
\fBtoggle\fR
|
|
||||||
\fBtoggle-all\fR
|
|
||||||
\fBtoggle-down\fR \fIctrl-i (tab)\fR
|
|
||||||
\fBtoggle-sort\fR (equivalent to \fB--toggle-sort\fR)
|
|
||||||
\fBtoggle-up\fR \fIbtab (shift-tab)\fR
|
|
||||||
\fBunix-line-discard\fR \fIctrl-u\fR
|
|
||||||
\fBunix-word-rubout\fR \fIctrl-w\fR
|
|
||||||
\fBup\fR \fIctrl-k ctrl-p up\fR
|
|
||||||
\fByank\fR \fIctrl-y\fR
|
|
||||||
.RE
|
|
||||||
|
|
||||||
.RS
|
|
||||||
With \fBexecute(...)\fR action, you can execute arbitrary commands without
|
|
||||||
leaving fzf. For example, you can turn fzf into a simple file browser by
|
|
||||||
binding \fBenter\fR key to \fBless\fR command like follows.
|
|
||||||
|
|
||||||
.RS
|
|
||||||
\fBfzf --bind "enter:execute(less {})"\fR
|
|
||||||
.RE
|
|
||||||
|
|
||||||
\fB{}\fR is the placeholder for the double-quoted string of the current line.
|
|
||||||
If the command contains parentheses, you can use any of the following
|
|
||||||
alternative notations to avoid parse errors.
|
|
||||||
|
|
||||||
\fBexecute[...]\fR
|
|
||||||
\fBexecute~...~\fR
|
|
||||||
\fBexecute!...!\fR
|
|
||||||
\fBexecute@...@\fR
|
|
||||||
\fBexecute#...#\fR
|
|
||||||
\fBexecute$...$\fR
|
|
||||||
\fBexecute%...%\fR
|
|
||||||
\fBexecute^...^\fR
|
|
||||||
\fBexecute&...&\fR
|
|
||||||
\fBexecute*...*\fR
|
|
||||||
\fBexecute;...;\fR
|
|
||||||
\fBexecute/.../\fR
|
|
||||||
\fBexecute|...|\fR
|
|
||||||
\fBexecute:...\fR
|
|
||||||
.RS
|
|
||||||
This is the special form that frees you from parse errors as it does not expect
|
|
||||||
the closing character. The catch is that it should be the last one in the
|
|
||||||
comma-separated list.
|
|
||||||
.RE
|
|
||||||
|
|
||||||
\fBexecute-multi(...)\fR is an alternative action that executes the command
|
|
||||||
with the selected entries when multi-select is enabled (\fB--multi\fR). With
|
|
||||||
this action, \fB{}\fR is replaced with the double-quoted strings of the
|
|
||||||
selected entries separated by spaces.
|
|
||||||
|
|
||||||
.RE
|
|
||||||
.TP
|
.TP
|
||||||
.BI "--history=" "HISTORY_FILE"
|
.BI "--history=" "HISTORY_FILE"
|
||||||
Load search history from the specified file and update the file on completion.
|
Load search history from the specified file and update the file on completion.
|
||||||
@@ -293,17 +231,40 @@ When enabled, \fBCTRL-N\fR and \fBCTRL-P\fR are automatically remapped to
|
|||||||
.BI "--history-size=" "N"
|
.BI "--history-size=" "N"
|
||||||
Maximum number of entries in the history file (default: 1000). The file is
|
Maximum number of entries in the history file (default: 1000). The file is
|
||||||
automatically truncated when the number of the lines exceeds the value.
|
automatically truncated when the number of the lines exceeds the value.
|
||||||
|
.SS Preview
|
||||||
.TP
|
.TP
|
||||||
.BI "--header=" "STR"
|
.BI "--preview=" "COMMAND"
|
||||||
The given string will be printed as the sticky header. The lines are displayed
|
Execute the given command for the current line and display the result on the
|
||||||
in the given order from top to bottom regardless of \fB--reverse\fR option, and
|
preview window. \fB{}\fR in the command is the placeholder that is replaced to
|
||||||
are not affected by \fB--with-nth\fR. ANSI color codes are processed even when
|
the single-quoted string of the current line. To transform the replacement
|
||||||
\fB--ansi\fR is not set.
|
string, specify field index expressions between the braces (See \fBFIELD INDEX
|
||||||
|
EXPRESSION\fR for the details). Also, \fB{q}\fR is replaced to the current
|
||||||
|
query string.
|
||||||
|
|
||||||
|
.RS
|
||||||
|
e.g. \fBfzf --preview="head -$LINES {}"\fR
|
||||||
|
\fBls -l | fzf --preview="echo user={3} when={-4..-2}; cat {-1}" --header-lines=1\fR
|
||||||
|
|
||||||
|
Note that you can escape a placeholder pattern by prepending a backslash.
|
||||||
|
.RE
|
||||||
.TP
|
.TP
|
||||||
.BI "--header-lines=" "N"
|
.BI "--preview-window=" "[POSITION][:SIZE[%]][:hidden]"
|
||||||
The first N lines of the input are treated as the sticky header. When
|
Determine the layout of the preview window. If the argument ends with
|
||||||
\fB--with-nth\fR is set, the lines are transformed just like the other
|
\fB:hidden\fR, the preview window will be hidden by default until
|
||||||
lines that follow.
|
\fBtoggle-preview\fR action is triggered.
|
||||||
|
|
||||||
|
.RS
|
||||||
|
.B POSITION: (default: right)
|
||||||
|
\fBup
|
||||||
|
\fBdown
|
||||||
|
\fBleft
|
||||||
|
\fBright
|
||||||
|
.RE
|
||||||
|
|
||||||
|
.RS
|
||||||
|
e.g. \fBfzf --preview="head {}" --preview-window=up:30%\fR
|
||||||
|
\fBfzf --preview="file {}" --preview-window=down:1\fR
|
||||||
|
.RE
|
||||||
.SS Scripting
|
.SS Scripting
|
||||||
.TP
|
.TP
|
||||||
.BI "-q, --query=" "STR"
|
.BI "-q, --query=" "STR"
|
||||||
@@ -328,18 +289,26 @@ the default enter key. When this option is set, fzf will print the name of the
|
|||||||
key pressed as the first line of its output (or as the second line if
|
key pressed as the first line of its output (or as the second line if
|
||||||
\fB--print-query\fR is also used). The line will be empty if fzf is completed
|
\fB--print-query\fR is also used). The line will be empty if fzf is completed
|
||||||
with the default enter key.
|
with the default enter key.
|
||||||
|
|
||||||
.RS
|
.RS
|
||||||
e.g. \fBfzf --expect=ctrl-v,ctrl-t,alt-s,f1,f2,~,@\fR
|
e.g. \fBfzf --expect=ctrl-v,ctrl-t,alt-s,f1,f2,~,@\fR
|
||||||
.RE
|
.RE
|
||||||
.TP
|
.TP
|
||||||
|
.B "--read0"
|
||||||
|
Read input delimited by ASCII NUL character instead of newline character
|
||||||
|
.TP
|
||||||
|
.B "--print0"
|
||||||
|
Print output delimited by ASCII NUL character instead of newline character
|
||||||
|
.TP
|
||||||
.B "--sync"
|
.B "--sync"
|
||||||
Synchronous search for multi-staged filtering. If specified, fzf will launch
|
Synchronous search for multi-staged filtering. If specified, fzf will launch
|
||||||
ncurses finder only after the input stream is complete.
|
ncurses finder only after the input stream is complete.
|
||||||
|
|
||||||
.RS
|
.RS
|
||||||
e.g. \fBfzf --multi | fzf --sync\fR
|
e.g. \fBfzf --multi | fzf --sync\fR
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
.SH ENVIRONMENT
|
.SH ENVIRONMENT VARIABLES
|
||||||
.TP
|
.TP
|
||||||
.B FZF_DEFAULT_COMMAND
|
.B FZF_DEFAULT_COMMAND
|
||||||
Default command to use when input is tty
|
Default command to use when input is tty
|
||||||
@@ -350,7 +319,11 @@ Default options. e.g. \fBexport FZF_DEFAULT_OPTS="--extended --cycle"\fR
|
|||||||
.SH EXIT STATUS
|
.SH EXIT STATUS
|
||||||
.BR 0 " Normal exit"
|
.BR 0 " Normal exit"
|
||||||
.br
|
.br
|
||||||
.BR 1 " Interrupted with \fBCTRL-C\fR or \fBESC\fR"
|
.BR 1 " No match"
|
||||||
|
.br
|
||||||
|
.BR 2 " Error"
|
||||||
|
.br
|
||||||
|
.BR 130 " Interrupted with \fBCTRL-C\fR or \fBESC\fR"
|
||||||
|
|
||||||
.SH FIELD INDEX EXPRESSION
|
.SH FIELD INDEX EXPRESSION
|
||||||
|
|
||||||
@@ -389,12 +362,12 @@ occurrences of the string.
|
|||||||
|
|
||||||
.SS Anchored-match
|
.SS Anchored-match
|
||||||
A term can be prefixed by \fB^\fR, or suffixed by \fB$\fR to become an
|
A term can be prefixed by \fB^\fR, or suffixed by \fB$\fR to become an
|
||||||
anchored-match term. Then fzf will search for the items that start with or end
|
anchored-match term. Then fzf will search for the lines that start with or end
|
||||||
with the given string. An anchored-match term is also an exact-match term.
|
with the given string. An anchored-match term is also an exact-match term.
|
||||||
|
|
||||||
.SS Negation
|
.SS Negation
|
||||||
If a term is prefixed by \fB!\fR, fzf will exclude the items that satisfy the
|
If a term is prefixed by \fB!\fR, fzf will exclude the lines that satisfy the
|
||||||
term from the result.
|
term from the result. In this case, fzf performs exact match by default.
|
||||||
|
|
||||||
.SS Exact-match by default
|
.SS Exact-match by default
|
||||||
If you don't prefer fuzzy matching and do not wish to "quote" (prefixing with
|
If you don't prefer fuzzy matching and do not wish to "quote" (prefixing with
|
||||||
@@ -408,6 +381,126 @@ query matches entries that start with \fBcore\fR and end with either \fBgo\fR,
|
|||||||
|
|
||||||
e.g. \fB^core go$ | rb$ | py$\fR
|
e.g. \fB^core go$ | rb$ | py$\fR
|
||||||
|
|
||||||
|
.SH KEY BINDINGS
|
||||||
|
You can customize key bindings of fzf with \fB--bind\fR option which takes
|
||||||
|
a comma-separated list of key binding expressions. Each key binding expression
|
||||||
|
follows the following format: \fBKEY:ACTION\fR
|
||||||
|
|
||||||
|
e.g. \fBfzf --bind=ctrl-j:accept,ctrl-k:kill-line\fR
|
||||||
|
|
||||||
|
.B AVAILABLE KEYS: (SYNONYMS)
|
||||||
|
\fIctrl-[a-z]\fR
|
||||||
|
\fIalt-[a-z]\fR
|
||||||
|
\fIalt-[0-9]\fR
|
||||||
|
\fIf[1-12]\fR
|
||||||
|
\fIenter\fR (\fIreturn\fR \fIctrl-m\fR)
|
||||||
|
\fIspace\fR
|
||||||
|
\fIbspace\fR (\fIbs\fR)
|
||||||
|
\fIalt-enter\fR
|
||||||
|
\fIalt-space\fR
|
||||||
|
\fIalt-bspace\fR (\fIalt-bs\fR)
|
||||||
|
\fIalt-/\fR
|
||||||
|
\fItab\fR
|
||||||
|
\fIbtab\fR (\fIshift-tab\fR)
|
||||||
|
\fIesc\fR
|
||||||
|
\fIdel\fR
|
||||||
|
\fIup\fR
|
||||||
|
\fIdown\fR
|
||||||
|
\fIleft\fR
|
||||||
|
\fIright\fR
|
||||||
|
\fIhome\fR
|
||||||
|
\fIend\fR
|
||||||
|
\fIpgup\fR (\fIpage-up\fR)
|
||||||
|
\fIpgdn\fR (\fIpage-down\fR)
|
||||||
|
\fIshift-left\fR
|
||||||
|
\fIshift-right\fR
|
||||||
|
\fIdouble-click\fR
|
||||||
|
or any single character
|
||||||
|
|
||||||
|
\fBACTION: DEFAULT BINDINGS (NOTES):
|
||||||
|
\fBabort\fR \fIctrl-c ctrl-g ctrl-q esc\fR
|
||||||
|
\fBaccept\fR \fIenter double-click\fR
|
||||||
|
\fBbackward-char\fR \fIctrl-b left\fR
|
||||||
|
\fBbackward-delete-char\fR \fIctrl-h bspace\fR
|
||||||
|
\fBbackward-kill-word\fR \fIalt-bs\fR
|
||||||
|
\fBbackward-word\fR \fIalt-b shift-left\fR
|
||||||
|
\fBbeginning-of-line\fR \fIctrl-a home\fR
|
||||||
|
\fBcancel\fR
|
||||||
|
\fBclear-screen\fR \fIctrl-l\fR
|
||||||
|
\fBdelete-char\fR \fIdel\fR
|
||||||
|
\fBdelete-char/eof\fR \fIctrl-d\fR
|
||||||
|
\fBdeselect-all\fR
|
||||||
|
\fBdown\fR \fIctrl-j ctrl-n down\fR
|
||||||
|
\fBend-of-line\fR \fIctrl-e end\fR
|
||||||
|
\fBexecute(...)\fR (see below for the details)
|
||||||
|
\fBexecute-multi(...)\fR (see below for the details)
|
||||||
|
\fBforward-char\fR \fIctrl-f right\fR
|
||||||
|
\fBforward-word\fR \fIalt-f shift-right\fR
|
||||||
|
\fBignore\fR
|
||||||
|
\fBjump\fR (EasyMotion-like 2-keystroke movement)
|
||||||
|
\fBjump-accept\fR (jump and accept)
|
||||||
|
\fBkill-line\fR
|
||||||
|
\fBkill-word\fR \fIalt-d\fR
|
||||||
|
\fBnext-history\fR (\fIctrl-n\fR on \fB--history\fR)
|
||||||
|
\fBpage-down\fR \fIpgdn\fR
|
||||||
|
\fBpage-up\fR \fIpgup\fR
|
||||||
|
\fBpreview-down\fR
|
||||||
|
\fBpreview-up\fR
|
||||||
|
\fBpreview-page-down\fR
|
||||||
|
\fBpreview-page-up\fR
|
||||||
|
\fBprevious-history\fR (\fIctrl-p\fR on \fB--history\fR)
|
||||||
|
\fBprint-query\fR (print query and exit)
|
||||||
|
\fBselect-all\fR
|
||||||
|
\fBtoggle\fR
|
||||||
|
\fBtoggle-all\fR
|
||||||
|
\fBtoggle-down\fR \fIctrl-i (tab)\fR
|
||||||
|
\fBtoggle-in\fR (\fB--reverse\fR ? \fBtoggle-up\fR : \fBtoggle-down\fR)
|
||||||
|
\fBtoggle-out\fR (\fB--reverse\fR ? \fBtoggle-down\fR : \fBtoggle-up\fR)
|
||||||
|
\fBtoggle-preview\fR
|
||||||
|
\fBtoggle-sort\fR (equivalent to \fB--toggle-sort\fR)
|
||||||
|
\fBtoggle-up\fR \fIbtab (shift-tab)\fR
|
||||||
|
\fBunix-line-discard\fR \fIctrl-u\fR
|
||||||
|
\fBunix-word-rubout\fR \fIctrl-w\fR
|
||||||
|
\fBup\fR \fIctrl-k ctrl-p up\fR
|
||||||
|
\fByank\fR \fIctrl-y\fR
|
||||||
|
|
||||||
|
With \fBexecute(...)\fR action, you can execute arbitrary commands without
|
||||||
|
leaving fzf. For example, you can turn fzf into a simple file browser by
|
||||||
|
binding \fBenter\fR key to \fBless\fR command like follows.
|
||||||
|
|
||||||
|
\fBfzf --bind "enter:execute(less {})"\fR
|
||||||
|
|
||||||
|
You can use the same placeholder expressions as in \fB--preview\fR.
|
||||||
|
|
||||||
|
If the command contains parentheses, fzf may fail to parse the expression. In
|
||||||
|
that case, you can use any of the following alternative notations to avoid
|
||||||
|
parse errors.
|
||||||
|
|
||||||
|
\fBexecute[...]\fR
|
||||||
|
\fBexecute~...~\fR
|
||||||
|
\fBexecute!...!\fR
|
||||||
|
\fBexecute@...@\fR
|
||||||
|
\fBexecute#...#\fR
|
||||||
|
\fBexecute$...$\fR
|
||||||
|
\fBexecute%...%\fR
|
||||||
|
\fBexecute^...^\fR
|
||||||
|
\fBexecute&...&\fR
|
||||||
|
\fBexecute*...*\fR
|
||||||
|
\fBexecute;...;\fR
|
||||||
|
\fBexecute/.../\fR
|
||||||
|
\fBexecute|...|\fR
|
||||||
|
\fBexecute:...\fR
|
||||||
|
.RS
|
||||||
|
This is the special form that frees you from parse errors as it does not expect
|
||||||
|
the closing character. The catch is that it should be the last one in the
|
||||||
|
comma-separated list of key-action pairs.
|
||||||
|
.RE
|
||||||
|
|
||||||
|
\fBexecute-multi(...)\fR is an alternative action that executes the command
|
||||||
|
with the selected entries when multi-select is enabled (\fB--multi\fR). With
|
||||||
|
this action, \fB{}\fR is replaced with the quoted strings of the selected
|
||||||
|
entries separated by spaces.
|
||||||
|
|
||||||
.SH AUTHOR
|
.SH AUTHOR
|
||||||
Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
||||||
|
|
||||||
@@ -417,7 +510,7 @@ Junegunn Choi (\fIjunegunn.c@gmail.com\fR)
|
|||||||
.I https://github.com/junegunn/fzf
|
.I https://github.com/junegunn/fzf
|
||||||
.RE
|
.RE
|
||||||
.br
|
.br
|
||||||
.R ""
|
|
||||||
.br
|
.br
|
||||||
.B Extra Vim plugin:
|
.B Extra Vim plugin:
|
||||||
.RS
|
.RS
|
||||||
|
|||||||
433
plugin/fzf.vim
433
plugin/fzf.vim
@@ -1,4 +1,4 @@
|
|||||||
" Copyright (c) 2015 Junegunn Choi
|
" Copyright (c) 2016 Junegunn Choi
|
||||||
"
|
"
|
||||||
" MIT License
|
" MIT License
|
||||||
"
|
"
|
||||||
@@ -21,7 +21,13 @@
|
|||||||
" 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:default_height = '40%'
|
if exists('g:loaded_fzf')
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
let g:loaded_fzf = 1
|
||||||
|
|
||||||
|
let s:default_layout = { 'down': '~40%' }
|
||||||
|
let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
|
||||||
let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'
|
let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'
|
||||||
let s:install = expand('<sfile>:h:h').'/install'
|
let s:install = expand('<sfile>:h:h').'/install'
|
||||||
let s:installed = 0
|
let s:installed = 0
|
||||||
@@ -49,11 +55,7 @@ function! s:fzf_exec()
|
|||||||
throw 'fzf executable not found'
|
throw 'fzf executable not found'
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
return s:exec
|
return s:shellesc(s:exec)
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:tmux_not_zoomed()
|
|
||||||
return system('tmux list-panes -F "#F"') !~# 'Z'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:tmux_enabled()
|
function! s:tmux_enabled()
|
||||||
@@ -62,7 +64,7 @@ function! s:tmux_enabled()
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
if exists('s:tmux')
|
if exists('s:tmux')
|
||||||
return s:tmux && s:tmux_not_zoomed()
|
return s:tmux
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let s:tmux = 0
|
let s:tmux = 0
|
||||||
@@ -70,7 +72,7 @@ function! s:tmux_enabled()
|
|||||||
let output = system('tmux -V')
|
let output = system('tmux -V')
|
||||||
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
||||||
endif
|
endif
|
||||||
return s:tmux && s:tmux_not_zoomed()
|
return s:tmux
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:shellesc(arg)
|
function! s:shellesc(arg)
|
||||||
@@ -78,7 +80,7 @@ function! s:shellesc(arg)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:escape(path)
|
function! s:escape(path)
|
||||||
return escape(a:path, ' %#''"\')
|
return escape(a:path, ' $%#''"\')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Upgrade legacy options
|
" Upgrade legacy options
|
||||||
@@ -108,11 +110,132 @@ function! s:warn(msg)
|
|||||||
echohl None
|
echohl None
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:has_any(dict, keys)
|
||||||
|
for key in a:keys
|
||||||
|
if has_key(a:dict, key)
|
||||||
|
return 1
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return 0
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:open(cmd, target)
|
||||||
|
if stridx('edit', a:cmd) == 0 && fnamemodify(a:target, ':p') ==# expand('%:p')
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
execute a:cmd s:escape(a:target)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:common_sink(action, lines) abort
|
||||||
|
if len(a:lines) < 2
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
let key = remove(a:lines, 0)
|
||||||
|
let cmd = get(a:action, key, 'e')
|
||||||
|
if len(a:lines) > 1
|
||||||
|
augroup fzf_swap
|
||||||
|
autocmd SwapExists * let v:swapchoice='o'
|
||||||
|
\| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))
|
||||||
|
augroup END
|
||||||
|
endif
|
||||||
|
try
|
||||||
|
let empty = empty(expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified
|
||||||
|
let autochdir = &autochdir
|
||||||
|
set noautochdir
|
||||||
|
for item in a:lines
|
||||||
|
if empty
|
||||||
|
execute 'e' s:escape(item)
|
||||||
|
let empty = 0
|
||||||
|
else
|
||||||
|
call s:open(cmd, item)
|
||||||
|
endif
|
||||||
|
if exists('#BufEnter') && isdirectory(item)
|
||||||
|
doautocmd BufEnter
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
finally
|
||||||
|
let &autochdir = autochdir
|
||||||
|
silent! autocmd! fzf_swap
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:get_color(attr, ...)
|
||||||
|
for group in a:000
|
||||||
|
let code = synIDattr(synIDtrans(hlID(group)), a:attr, 'cterm')
|
||||||
|
if code =~ '^[0-9]\+$'
|
||||||
|
return code
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return ''
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:defaults()
|
||||||
|
let rules = copy(get(g:, 'fzf_colors', {}))
|
||||||
|
let colors = join(map(items(filter(map(rules, 'call("s:get_color", v:val)'), '!empty(v:val)')), 'join(v:val, ":")'), ',')
|
||||||
|
return empty(colors) ? '' : ('--color='.colors)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" [name string,] [opts dict,] [fullscreen boolean]
|
||||||
|
function! fzf#wrap(...)
|
||||||
|
let args = ['', {}, 0]
|
||||||
|
let expects = map(copy(args), 'type(v:val)')
|
||||||
|
let tidx = 0
|
||||||
|
for arg in copy(a:000)
|
||||||
|
let tidx = index(expects, type(arg), tidx)
|
||||||
|
if tidx < 0
|
||||||
|
throw 'invalid arguments (expected: [name string] [opts dict] [fullscreen boolean])'
|
||||||
|
endif
|
||||||
|
let args[tidx] = arg
|
||||||
|
let tidx += 1
|
||||||
|
unlet arg
|
||||||
|
endfor
|
||||||
|
let [name, opts, bang] = args
|
||||||
|
|
||||||
|
" Layout: g:fzf_layout (and deprecated g:fzf_height)
|
||||||
|
if bang
|
||||||
|
for key in s:layout_keys
|
||||||
|
if has_key(opts, key)
|
||||||
|
call remove(opts, key)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
elseif !s:has_any(opts, s:layout_keys)
|
||||||
|
if !exists('g:fzf_layout') && exists('g:fzf_height')
|
||||||
|
let opts.down = g:fzf_height
|
||||||
|
else
|
||||||
|
let opts = extend(opts, get(g:, 'fzf_layout', s:default_layout))
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Colors: g:fzf_colors
|
||||||
|
let opts.options = s:defaults() .' '. get(opts, 'options', '')
|
||||||
|
|
||||||
|
" History: g:fzf_history_dir
|
||||||
|
if len(name) && len(get(g:, 'fzf_history_dir', ''))
|
||||||
|
let dir = expand(g:fzf_history_dir)
|
||||||
|
if !isdirectory(dir)
|
||||||
|
call mkdir(dir, 'p')
|
||||||
|
endif
|
||||||
|
let opts.options = join(['--history', s:escape(dir.'/'.name), opts.options])
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Action: g:fzf_action
|
||||||
|
if !s:has_any(opts, ['sink', 'sink*'])
|
||||||
|
let opts._action = get(g:, 'fzf_action', s:default_action)
|
||||||
|
let opts.options .= ' --expect='.join(keys(opts._action), ',')
|
||||||
|
function! opts.sink(lines) abort
|
||||||
|
return s:common_sink(self._action, a:lines)
|
||||||
|
endfunction
|
||||||
|
let opts['sink*'] = remove(opts, 'sink')
|
||||||
|
endif
|
||||||
|
|
||||||
|
return opts
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! fzf#run(...) abort
|
function! fzf#run(...) abort
|
||||||
try
|
try
|
||||||
let oshell = &shell
|
let oshell = &shell
|
||||||
set shell=sh
|
set shell=sh
|
||||||
if has('nvim') && bufexists('term://*:FZF')
|
if has('nvim') && len(filter(range(1, bufnr('$')), 'bufname(v:val) =~# ";#FZF"'))
|
||||||
call s:warn('FZF is already running!')
|
call s:warn('FZF is already running!')
|
||||||
return []
|
return []
|
||||||
endif
|
endif
|
||||||
@@ -125,6 +248,12 @@ try
|
|||||||
throw v:exception
|
throw v:exception
|
||||||
endtry
|
endtry
|
||||||
|
|
||||||
|
if !has_key(dict, 'source') && !empty($FZF_DEFAULT_COMMAND)
|
||||||
|
let temps.source = tempname()
|
||||||
|
call writefile(split($FZF_DEFAULT_COMMAND, "\n"), temps.source)
|
||||||
|
let dict.source = (empty($SHELL) ? 'sh' : $SHELL) . ' ' . s:shellesc(temps.source)
|
||||||
|
endif
|
||||||
|
|
||||||
if has_key(dict, 'source')
|
if has_key(dict, 'source')
|
||||||
let source = dict.source
|
let source = dict.source
|
||||||
let type = type(source)
|
let type = type(source)
|
||||||
@@ -135,25 +264,21 @@ try
|
|||||||
call writefile(source, temps.input)
|
call writefile(source, temps.input)
|
||||||
let prefix = 'cat '.s:shellesc(temps.input).'|'
|
let prefix = 'cat '.s:shellesc(temps.input).'|'
|
||||||
else
|
else
|
||||||
throw 'Invalid source type'
|
throw 'invalid source type'
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
let prefix = ''
|
let prefix = ''
|
||||||
endif
|
endif
|
||||||
let tmux = !has('nvim') && s:tmux_enabled() && s:splittable(dict)
|
let tmux = (!has('nvim') || get(g:, 'fzf_prefer_tmux', 0)) && s:tmux_enabled() && s:splittable(dict)
|
||||||
let command = prefix.(tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result
|
let command = prefix.(tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result
|
||||||
|
|
||||||
try
|
if has('nvim') && !tmux
|
||||||
if tmux
|
return s:execute_term(dict, command, temps)
|
||||||
return s:execute_tmux(dict, command, temps)
|
endif
|
||||||
elseif has('nvim')
|
|
||||||
return s:execute_term(dict, command, temps)
|
let lines = tmux ? s:execute_tmux(dict, command, temps) : s:execute(dict, command, temps)
|
||||||
else
|
call s:callback(dict, lines)
|
||||||
return s:execute(dict, command, temps)
|
return lines
|
||||||
endif
|
|
||||||
finally
|
|
||||||
call s:popd(dict)
|
|
||||||
endtry
|
|
||||||
finally
|
finally
|
||||||
let &shell = oshell
|
let &shell = oshell
|
||||||
endtry
|
endtry
|
||||||
@@ -174,20 +299,21 @@ function! s:fzf_tmux(dict)
|
|||||||
if s:present(a:dict, o)
|
if s:present(a:dict, o)
|
||||||
let spec = a:dict[o]
|
let spec = a:dict[o]
|
||||||
if (o == 'up' || o == 'down') && spec[0] == '~'
|
if (o == 'up' || o == 'down') && spec[0] == '~'
|
||||||
let size = '-'.o[0].s:calc_size(&lines, spec[1:], a:dict)
|
let size = '-'.o[0].s:calc_size(&lines, spec, a:dict)
|
||||||
else
|
else
|
||||||
" Legacy boolean option
|
" Legacy boolean option
|
||||||
let size = '-'.o[0].(spec == 1 ? '' : spec)
|
let size = '-'.o[0].(spec == 1 ? '' : substitute(spec, '^\~', '', ''))
|
||||||
endif
|
endif
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
return printf('LINES=%d COLUMNS=%d %s %s %s --',
|
return printf('LINES=%d COLUMNS=%d %s %s %s --',
|
||||||
\ &lines, &columns, s:fzf_tmux, size, (has_key(a:dict, 'source') ? '' : '-'))
|
\ &lines, &columns, s:shellesc(s:fzf_tmux), size, (has_key(a:dict, 'source') ? '' : '-'))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:splittable(dict)
|
function! s:splittable(dict)
|
||||||
return s:present(a:dict, 'up', 'down', 'left', 'right')
|
return s:present(a:dict, 'up', 'down') && &lines > 15 ||
|
||||||
|
\ s:present(a:dict, 'left', 'right') && &columns > 40
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:pushd(dict)
|
function! s:pushd(dict)
|
||||||
@@ -197,17 +323,24 @@ function! s:pushd(dict)
|
|||||||
return 1
|
return 1
|
||||||
endif
|
endif
|
||||||
let a:dict.prev_dir = cwd
|
let a:dict.prev_dir = cwd
|
||||||
execute 'chdir '.s:escape(a:dict.dir)
|
execute 'lcd' s:escape(a:dict.dir)
|
||||||
let a:dict.dir = getcwd()
|
let a:dict.dir = getcwd()
|
||||||
return 1
|
return 1
|
||||||
endif
|
endif
|
||||||
return 0
|
return 0
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:popd(dict)
|
augroup fzf_popd
|
||||||
if has_key(a:dict, 'prev_dir')
|
autocmd!
|
||||||
execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))
|
autocmd WinEnter * call s:dopopd()
|
||||||
|
augroup END
|
||||||
|
|
||||||
|
function! s:dopopd()
|
||||||
|
if !exists('w:fzf_prev_dir') || exists('*haslocaldir') && !haslocaldir()
|
||||||
|
return
|
||||||
endif
|
endif
|
||||||
|
execute 'lcd' s:escape(w:fzf_prev_dir)
|
||||||
|
unlet w:fzf_prev_dir
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:xterm_launcher()
|
function! s:xterm_launcher()
|
||||||
@@ -235,7 +368,7 @@ function! s:exit_handler(code, command, ...)
|
|||||||
return 1
|
return 1
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute(dict, command, temps)
|
function! s:execute(dict, command, temps) abort
|
||||||
call s:pushd(a:dict)
|
call s:pushd(a:dict)
|
||||||
silent! !clear 2> /dev/null
|
silent! !clear 2> /dev/null
|
||||||
let escaped = escape(substitute(a:command, '\n', '\\n', 'g'), '%#')
|
let escaped = escape(substitute(a:command, '\n', '\\n', 'g'), '%#')
|
||||||
@@ -247,11 +380,12 @@ function! s:execute(dict, command, temps)
|
|||||||
let command = escaped
|
let command = escaped
|
||||||
endif
|
endif
|
||||||
execute 'silent !'.command
|
execute 'silent !'.command
|
||||||
|
let exit_status = v:shell_error
|
||||||
redraw!
|
redraw!
|
||||||
return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []
|
return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute_tmux(dict, command, temps)
|
function! s:execute_tmux(dict, command, temps) abort
|
||||||
let command = a:command
|
let command = a:command
|
||||||
if s:pushd(a:dict)
|
if s:pushd(a:dict)
|
||||||
" -c '#{pane_current_path}' is only available on tmux 1.9 or above
|
" -c '#{pane_current_path}' is only available on tmux 1.9 or above
|
||||||
@@ -259,15 +393,17 @@ function! s:execute_tmux(dict, command, temps)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
call system(command)
|
call system(command)
|
||||||
|
let exit_status = v:shell_error
|
||||||
redraw!
|
redraw!
|
||||||
return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []
|
return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:calc_size(max, val, dict)
|
function! s:calc_size(max, val, dict)
|
||||||
if a:val =~ '%$'
|
let val = substitute(a:val, '^\~', '', '')
|
||||||
let size = a:max * str2nr(a:val[:-2]) / 100
|
if val =~ '%$'
|
||||||
|
let size = a:max * str2nr(val[:-2]) / 100
|
||||||
else
|
else
|
||||||
let size = min([a:max, str2nr(a:val)])
|
let size = min([a:max, str2nr(val)])
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let srcsz = -1
|
let srcsz = -1
|
||||||
@@ -277,11 +413,12 @@ function! s:calc_size(max, val, dict)
|
|||||||
|
|
||||||
let opts = get(a:dict, 'options', '').$FZF_DEFAULT_OPTS
|
let opts = get(a:dict, 'options', '').$FZF_DEFAULT_OPTS
|
||||||
let margin = stridx(opts, '--inline-info') > stridx(opts, '--no-inline-info') ? 1 : 2
|
let margin = stridx(opts, '--inline-info') > stridx(opts, '--no-inline-info') ? 1 : 2
|
||||||
|
let margin += stridx(opts, '--header') > stridx(opts, '--no-header')
|
||||||
return srcsz >= 0 ? min([srcsz + margin, size]) : size
|
return srcsz >= 0 ? min([srcsz + margin, size]) : size
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:getpos()
|
function! s:getpos()
|
||||||
return {'tab': tabpagenr(), 'win': winnr(), 'cnt': winnr('$')}
|
return {'tab': tabpagenr(), 'win': winnr(), 'cnt': winnr('$'), 'tcnt': tabpagenr('$')}
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:split(dict)
|
function! s:split(dict)
|
||||||
@@ -290,86 +427,132 @@ function! s:split(dict)
|
|||||||
\ 'down': ['botright', 'resize', &lines],
|
\ 'down': ['botright', 'resize', &lines],
|
||||||
\ 'left': ['vertical topleft', 'vertical resize', &columns],
|
\ 'left': ['vertical topleft', 'vertical resize', &columns],
|
||||||
\ 'right': ['vertical botright', 'vertical resize', &columns] }
|
\ 'right': ['vertical botright', 'vertical resize', &columns] }
|
||||||
let s:ppos = s:getpos()
|
let ppos = s:getpos()
|
||||||
try
|
try
|
||||||
for [dir, triple] in items(directions)
|
|
||||||
let val = get(a:dict, dir, '')
|
|
||||||
if !empty(val)
|
|
||||||
let [cmd, resz, max] = triple
|
|
||||||
if (dir == 'up' || dir == 'down') && val[0] == '~'
|
|
||||||
let sz = s:calc_size(max, val[1:], a:dict)
|
|
||||||
else
|
|
||||||
let sz = s:calc_size(max, val, {})
|
|
||||||
endif
|
|
||||||
execute cmd sz.'new'
|
|
||||||
execute resz sz
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
if s:present(a:dict, 'window')
|
if s:present(a:dict, 'window')
|
||||||
execute a:dict.window
|
execute a:dict.window
|
||||||
|
elseif !s:splittable(a:dict)
|
||||||
|
execute (tabpagenr()-1).'tabnew'
|
||||||
else
|
else
|
||||||
tabnew
|
for [dir, triple] in items(directions)
|
||||||
|
let val = get(a:dict, dir, '')
|
||||||
|
if !empty(val)
|
||||||
|
let [cmd, resz, max] = triple
|
||||||
|
if (dir == 'up' || dir == 'down') && val[0] == '~'
|
||||||
|
let sz = s:calc_size(max, val, a:dict)
|
||||||
|
else
|
||||||
|
let sz = s:calc_size(max, val, {})
|
||||||
|
endif
|
||||||
|
execute cmd sz.'new'
|
||||||
|
execute resz sz
|
||||||
|
return [ppos, {}]
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
endif
|
endif
|
||||||
|
return [ppos, { '&l:wfw': &l:wfw, '&l:wfh': &l:wfh }]
|
||||||
finally
|
finally
|
||||||
setlocal winfixwidth winfixheight buftype=nofile bufhidden=wipe nobuflisted
|
setlocal winfixwidth winfixheight
|
||||||
endtry
|
endtry
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:execute_term(dict, command, temps)
|
function! s:execute_term(dict, command, temps) abort
|
||||||
call s:split(a:dict)
|
let winrest = winrestcmd()
|
||||||
call s:pushd(a:dict)
|
let [ppos, winopts] = s:split(a:dict)
|
||||||
|
let fzf = { 'buf': bufnr('%'), 'ppos': ppos, 'dict': a:dict, 'temps': a:temps,
|
||||||
let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps, 'name': 'FZF' }
|
\ 'winopts': winopts, 'winrest': winrest, 'lines': &lines,
|
||||||
let s:command = a:command
|
\ 'columns': &columns, 'command': a:command }
|
||||||
|
function! fzf.switch_back(inplace)
|
||||||
|
if a:inplace && bufnr('') == self.buf
|
||||||
|
" FIXME: Can't re-enter normal mode from terminal mode
|
||||||
|
" execute "normal! \<c-^>"
|
||||||
|
b #
|
||||||
|
" No other listed buffer
|
||||||
|
if bufnr('') == self.buf
|
||||||
|
enew
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
function! fzf.on_exit(id, code)
|
function! fzf.on_exit(id, code)
|
||||||
let pos = s:getpos()
|
if s:getpos() == self.ppos " {'window': 'enew'}
|
||||||
let inplace = pos == s:ppos " {'window': 'enew'}
|
for [opt, val] in items(self.winopts)
|
||||||
if !inplace
|
execute 'let' opt '=' val
|
||||||
|
endfor
|
||||||
|
call self.switch_back(1)
|
||||||
|
else
|
||||||
if bufnr('') == self.buf
|
if bufnr('') == self.buf
|
||||||
" We use close instead of bd! since Vim does not close the split when
|
" We use close instead of bd! since Vim does not close the split when
|
||||||
" there's no other listed buffer (nvim +'set nobuflisted')
|
" there's no other listed buffer (nvim +'set nobuflisted')
|
||||||
close
|
close
|
||||||
endif
|
endif
|
||||||
if pos.tab == s:ppos.tab
|
execute 'tabnext' self.ppos.tab
|
||||||
wincmd p
|
execute self.ppos.win.'wincmd w'
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if !s:exit_handler(a:code, s:command, 1)
|
if bufexists(self.buf)
|
||||||
|
execute 'bd!' self.buf
|
||||||
|
endif
|
||||||
|
|
||||||
|
if &lines == self.lines && &columns == self.columns && s:getpos() == self.ppos
|
||||||
|
execute self.winrest
|
||||||
|
endif
|
||||||
|
|
||||||
|
if !s:exit_handler(a:code, self.command, 1)
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
call s:pushd(self.dict)
|
call s:pushd(self.dict)
|
||||||
try
|
let lines = s:collect(self.temps)
|
||||||
call s:callback(self.dict, self.temps)
|
call s:callback(self.dict, lines)
|
||||||
|
call self.switch_back(s:getpos() == self.ppos)
|
||||||
if inplace && bufnr('') == self.buf
|
|
||||||
execute "normal! \<c-^>"
|
|
||||||
" No other listed buffer
|
|
||||||
if bufnr('') == self.buf
|
|
||||||
bd!
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
finally
|
|
||||||
call s:popd(self.dict)
|
|
||||||
endtry
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
call termopen(a:command, fzf)
|
try
|
||||||
|
if s:present(a:dict, 'dir')
|
||||||
|
execute 'lcd' s:escape(a:dict.dir)
|
||||||
|
endif
|
||||||
|
call termopen(a:command . ';#FZF', fzf)
|
||||||
|
finally
|
||||||
|
if s:present(a:dict, 'dir')
|
||||||
|
lcd -
|
||||||
|
endif
|
||||||
|
endtry
|
||||||
|
setlocal nospell bufhidden=wipe nobuflisted
|
||||||
setf fzf
|
setf fzf
|
||||||
startinsert
|
startinsert
|
||||||
return []
|
return []
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:callback(dict, temps)
|
function! s:collect(temps) abort
|
||||||
try
|
try
|
||||||
if !filereadable(a:temps.result)
|
return filereadable(a:temps.result) ? readfile(a:temps.result) : []
|
||||||
let lines = []
|
finally
|
||||||
else
|
for tf in values(a:temps)
|
||||||
let lines = readfile(a:temps.result)
|
silent! call delete(tf)
|
||||||
|
endfor
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:callback(dict, lines) abort
|
||||||
|
" Since anything can be done in the sink function, there is no telling that
|
||||||
|
" the change of the working directory was made by &autochdir setting.
|
||||||
|
"
|
||||||
|
" We use the following heuristic to determine whether to restore CWD:
|
||||||
|
" - Always restore the current directory when &autochdir is disabled.
|
||||||
|
" FIXME This makes it impossible to change directory from inside the sink
|
||||||
|
" function when &autochdir is not used.
|
||||||
|
" - In case of an error or an interrupt, a:lines will be empty.
|
||||||
|
" And it will be an array of a single empty string when fzf was finished
|
||||||
|
" without a match. In these cases, we presume that the change of the
|
||||||
|
" directory is not expected and should be undone.
|
||||||
|
let popd = has_key(a:dict, 'prev_dir') &&
|
||||||
|
\ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))
|
||||||
|
if popd
|
||||||
|
let w:fzf_prev_dir = a:dict.prev_dir
|
||||||
|
endif
|
||||||
|
|
||||||
|
try
|
||||||
if has_key(a:dict, 'sink')
|
if has_key(a:dict, 'sink')
|
||||||
for line in lines
|
for line in a:lines
|
||||||
if type(a:dict.sink) == 2
|
if type(a:dict.sink) == 2
|
||||||
call a:dict.sink(line)
|
call a:dict.sink(line)
|
||||||
else
|
else
|
||||||
@@ -378,70 +561,40 @@ try
|
|||||||
endfor
|
endfor
|
||||||
endif
|
endif
|
||||||
if has_key(a:dict, 'sink*')
|
if has_key(a:dict, 'sink*')
|
||||||
call a:dict['sink*'](lines)
|
call a:dict['sink*'](a:lines)
|
||||||
endif
|
endif
|
||||||
endif
|
catch
|
||||||
|
if stridx(v:exception, ':E325:') < 0
|
||||||
|
echoerr v:exception
|
||||||
|
endif
|
||||||
|
endtry
|
||||||
|
|
||||||
for tf in values(a:temps)
|
" We may have opened a new window or tab
|
||||||
silent! call delete(tf)
|
if popd
|
||||||
endfor
|
let w:fzf_prev_dir = a:dict.prev_dir
|
||||||
|
call s:dopopd()
|
||||||
return lines
|
|
||||||
catch
|
|
||||||
if stridx(v:exception, ':E325:') < 0
|
|
||||||
echoerr v:exception
|
|
||||||
endif
|
endif
|
||||||
endtry
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:default_action = {
|
let s:default_action = {
|
||||||
\ 'ctrl-m': 'e',
|
|
||||||
\ 'ctrl-t': 'tab split',
|
\ 'ctrl-t': 'tab split',
|
||||||
\ 'ctrl-x': 'split',
|
\ 'ctrl-x': 'split',
|
||||||
\ 'ctrl-v': 'vsplit' }
|
\ 'ctrl-v': 'vsplit' }
|
||||||
|
|
||||||
function! s:cmd_callback(lines) abort
|
|
||||||
if empty(a:lines)
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
let key = remove(a:lines, 0)
|
|
||||||
let cmd = get(s:action, key, 'e')
|
|
||||||
if len(a:lines) > 1
|
|
||||||
augroup fzf_swap
|
|
||||||
autocmd SwapExists * let v:swapchoice='o'
|
|
||||||
\| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))
|
|
||||||
augroup END
|
|
||||||
endif
|
|
||||||
try
|
|
||||||
let autochdir = &autochdir
|
|
||||||
set noautochdir
|
|
||||||
for item in a:lines
|
|
||||||
execute cmd s:escape(item)
|
|
||||||
if exists('#BufEnter') && isdirectory(item)
|
|
||||||
doautocmd BufEnter
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
finally
|
|
||||||
let &autochdir = autochdir
|
|
||||||
silent! autocmd! fzf_swap
|
|
||||||
endtry
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:cmd(bang, ...) abort
|
function! s:cmd(bang, ...) abort
|
||||||
let s:action = get(g:, 'fzf_action', s:default_action)
|
let args = copy(a:000)
|
||||||
let args = extend(['--expect='.join(keys(s:action), ',')], a:000)
|
let opts = { 'options': '--multi ' }
|
||||||
let opts = {}
|
if len(args) && isdirectory(expand(args[-1]))
|
||||||
if len(args) > 0 && isdirectory(expand(args[-1]))
|
let opts.dir = substitute(substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g'), '/*$', '/', '')
|
||||||
let opts.dir = substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g')
|
let opts.options .= ' --prompt '.shellescape(opts.dir)
|
||||||
|
else
|
||||||
|
let opts.options .= ' --prompt '.shellescape(pathshorten(getcwd()).'/')
|
||||||
endif
|
endif
|
||||||
if !a:bang
|
let opts.options .= ' '.join(args)
|
||||||
let opts.down = get(g:, 'fzf_height', get(g:, 'fzf_tmux_height', s:default_height))
|
call fzf#run(fzf#wrap('FZF', opts, a:bang))
|
||||||
endif
|
|
||||||
call fzf#run(extend({'options': join(args), 'sink*': function('<sid>cmd_callback')}, opts))
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)
|
command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)
|
||||||
|
|
||||||
let &cpo = s:cpo_save
|
let &cpo = s:cpo_save
|
||||||
unlet s:cpo_save
|
unlet s:cpo_save
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,29 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
|
# To use custom commands instead of find, override _fzf_compgen_{path,dir}
|
||||||
|
if ! declare -f _fzf_compgen_path > /dev/null; then
|
||||||
|
_fzf_compgen_path() {
|
||||||
|
echo "$1"
|
||||||
|
command find -L "$1" \
|
||||||
|
-name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
|
||||||
|
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! declare -f _fzf_compgen_dir > /dev/null; then
|
||||||
|
_fzf_compgen_dir() {
|
||||||
|
command find -L "$1" \
|
||||||
|
-name .git -prune -o -name .svn -prune -o -type d \
|
||||||
|
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
###########################################################
|
||||||
|
|
||||||
_fzf_orig_completion_filter() {
|
_fzf_orig_completion_filter() {
|
||||||
sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |
|
sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |
|
||||||
awk -F= '{gsub(/[^a-z0-9_= ;]/, "_", $1); print $1"="$2}'
|
awk -F= '{gsub(/[^A-Za-z0-9_= ;]/, "_", $1); print $1"="$2}'
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_opts_completion() {
|
_fzf_opts_completion() {
|
||||||
@@ -55,11 +75,11 @@ _fzf_opts_completion() {
|
|||||||
|
|
||||||
case "${prev}" in
|
case "${prev}" in
|
||||||
--tiebreak)
|
--tiebreak)
|
||||||
COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--color)
|
--color)
|
||||||
COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--history)
|
--history)
|
||||||
@@ -68,8 +88,8 @@ _fzf_opts_completion() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ ${cur} =~ ^-|\+ ]]; then
|
if [[ "$cur" =~ ^-|\+ ]]; then
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -88,31 +108,32 @@ _fzf_handle_dynamic_completion() {
|
|||||||
elif [ -n "$_fzf_completion_loader" ]; then
|
elif [ -n "$_fzf_completion_loader" ]; then
|
||||||
_completion_loader "$@"
|
_completion_loader "$@"
|
||||||
ret=$?
|
ret=$?
|
||||||
eval $(complete | \grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)
|
eval "$(complete | command grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)"
|
||||||
source $BASH_SOURCE
|
source "${BASH_SOURCE[0]}"
|
||||||
return $ret
|
return $ret
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
__fzf_generic_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local cur base dir leftover matches trigger cmd fzf
|
local cur base dir leftover matches trigger cmd fzf
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
if [[ ${cur} == *"$trigger" ]]; then
|
if [[ "$cur" == *"$trigger" ]]; then
|
||||||
base=${cur:0:${#cur}-${#trigger}}
|
base=${cur:0:${#cur}-${#trigger}}
|
||||||
eval base=$base
|
eval "base=$base"
|
||||||
|
|
||||||
dir="$base"
|
dir="$base"
|
||||||
while [ 1 ]; do
|
while true; do
|
||||||
if [ -z "$dir" -o -d "$dir" ]; then
|
if [ -z "$dir" ] || [ -d "$dir" ]; then
|
||||||
leftover=${base/#"$dir"}
|
leftover=${base/#"$dir"}
|
||||||
leftover=${leftover/#\/}
|
leftover=${leftover/#\/}
|
||||||
[ "$dir" = './' ] && dir=''
|
[ -z "$dir" ] && dir='.'
|
||||||
|
[ "$dir" != "/" ] && dir="${dir/%\//}"
|
||||||
tput sc
|
tput sc
|
||||||
matches=$(find -L "$dir"* $1 2> /dev/null | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
matches=$(eval "$1 $(printf %q "$dir")" | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read -r item; do
|
||||||
printf "%q$3 " "$item"
|
printf "%q$3 " "$item"
|
||||||
done)
|
done)
|
||||||
matches=${matches% }
|
matches=${matches% }
|
||||||
@@ -135,29 +156,22 @@ __fzf_generic_path_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_feed_fifo() (
|
|
||||||
rm -f "$fifo"
|
|
||||||
mkfifo "$fifo"
|
|
||||||
cat <&0 > "$fifo" &
|
|
||||||
)
|
|
||||||
|
|
||||||
_fzf_complete() {
|
_fzf_complete() {
|
||||||
local fifo cur selected trigger cmd fzf
|
local cur selected trigger cmd fzf post
|
||||||
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
post="$(caller 0 | awk '{print $2}')_post"
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
type -t "$post" > /dev/null 2>&1 || post=cat
|
||||||
|
[ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
|
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"
|
||||||
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
if [[ ${cur} == *"$trigger" ]]; then
|
if [[ "$cur" == *"$trigger" ]]; then
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
_fzf_feed_fifo "$fifo"
|
|
||||||
tput sc
|
tput sc
|
||||||
selected=$(eval "cat '$fifo' | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
selected=$(cat | $fzf $FZF_COMPLETION_OPTS $1 -q "$cur" | $post | tr '\n' ' ')
|
||||||
selected=${selected% } # Strip trailing space not to repeat "-o nospace"
|
selected=${selected% } # Strip trailing space not to repeat "-o nospace"
|
||||||
tput rc
|
tput rc
|
||||||
rm -f "$fifo"
|
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
if [ -n "$selected" ]; then
|
||||||
COMPREPLY=("$selected")
|
COMPREPLY=("$selected")
|
||||||
@@ -170,28 +184,23 @@ _fzf_complete() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_fzf_path_completion() {
|
_fzf_path_completion() {
|
||||||
__fzf_generic_path_completion \
|
__fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
|
||||||
"-m" "" "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Deprecated. No file only completion.
|
||||||
_fzf_file_completion() {
|
_fzf_file_completion() {
|
||||||
__fzf_generic_path_completion \
|
_fzf_path_completion "$@"
|
||||||
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
|
||||||
"-m" "" "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
__fzf_generic_path_completion \
|
__fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
|
||||||
"" "/" "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_complete_kill() {
|
_fzf_complete_kill() {
|
||||||
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||||
|
|
||||||
local selected fzf
|
local selected fzf
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
tput sc
|
tput sc
|
||||||
selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
|
selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
|
||||||
tput rc
|
tput rc
|
||||||
@@ -204,15 +213,16 @@ _fzf_complete_kill() {
|
|||||||
|
|
||||||
_fzf_complete_telnet() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_complete '+m' "$@" < <(
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |
|
||||||
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_complete_ssh() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_complete '+m' "$@" < <(
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \
|
||||||
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
<(command grep -oE '^[^ ]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \
|
||||||
|
<(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |
|
||||||
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -238,13 +248,12 @@ _fzf_complete_unalias() {
|
|||||||
# fzf options
|
# fzf options
|
||||||
complete -o default -F _fzf_opts_completion fzf
|
complete -o default -F _fzf_opts_completion fzf
|
||||||
|
|
||||||
d_cmds="cd pushd rmdir"
|
d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"
|
||||||
f_cmds="
|
|
||||||
awk cat diff diff3
|
|
||||||
emacs ex file ftp g++ gcc gvim head hg java
|
|
||||||
javac ld less more mvim nvim patch perl python ruby
|
|
||||||
sed sftp sort source tail tee uniq vi view vim wc"
|
|
||||||
a_cmds="
|
a_cmds="
|
||||||
|
awk cat diff diff3
|
||||||
|
emacs emacsclient ex file ftp g++ gcc gvim head hg java
|
||||||
|
javac ld less more mvim nvim patch perl python ruby
|
||||||
|
sed sftp sort source tail tee uniq vi view vim wc xdg-open
|
||||||
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||||
find git grep gunzip gzip hg jar
|
find git grep gunzip gzip hg jar
|
||||||
ln ls mv open rm rsync scp
|
ln ls mv open rm rsync scp
|
||||||
@@ -252,11 +261,11 @@ a_cmds="
|
|||||||
x_cmds="kill ssh telnet unset unalias export"
|
x_cmds="kill ssh telnet unset unalias export"
|
||||||
|
|
||||||
# Preserve existing completion
|
# Preserve existing completion
|
||||||
if [ "$_fzf_completion_loaded" != '0.10.8' ]; then
|
if [ "$_fzf_completion_loaded" != '0.11.3' ]; then
|
||||||
# Really wish I could use associative array but OSX comes with bash 3.2 :(
|
# Really wish I could use associative array but OSX comes with bash 3.2 :(
|
||||||
eval $(complete | \grep '\-F' | \grep -v _fzf_ |
|
eval $(complete | command grep '\-F' | command grep -v _fzf_ |
|
||||||
\grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
|
command grep -E " ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
|
||||||
export _fzf_completion_loaded=0.10.8
|
export _fzf_completion_loaded=0.11.3
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if type _completion_loader > /dev/null 2>&1; then
|
if type _completion_loader > /dev/null 2>&1; then
|
||||||
@@ -264,34 +273,30 @@ if type _completion_loader > /dev/null 2>&1; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
_fzf_defc() {
|
_fzf_defc() {
|
||||||
local cmd func opts orig_var orig
|
local cmd func opts orig_var orig def
|
||||||
cmd="$1"
|
cmd="$1"
|
||||||
func="$2"
|
func="$2"
|
||||||
opts="$3"
|
opts="$3"
|
||||||
orig_var="_fzf_orig_completion_$cmd"
|
orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
|
||||||
orig="${!orig_var}"
|
orig="${!orig_var}"
|
||||||
if [ -n "$orig" ]; then
|
if [ -n "$orig" ]; then
|
||||||
eval "$(printf "$orig" "$func")"
|
printf -v def "$orig" "$func"
|
||||||
|
eval "$def"
|
||||||
else
|
else
|
||||||
complete -F "$func" $opts "$cmd"
|
complete -F "$func" $opts "$cmd"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Directory
|
|
||||||
for cmd in $d_cmds; do
|
|
||||||
_fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"
|
|
||||||
done
|
|
||||||
|
|
||||||
# File
|
|
||||||
for cmd in $f_cmds; do
|
|
||||||
_fzf_defc "$cmd" _fzf_file_completion "-o default -o bashdefault"
|
|
||||||
done
|
|
||||||
|
|
||||||
# Anything
|
# Anything
|
||||||
for cmd in $a_cmds; do
|
for cmd in $a_cmds; do
|
||||||
_fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
|
_fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Directory
|
||||||
|
for cmd in $d_cmds; do
|
||||||
|
_fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"
|
||||||
|
done
|
||||||
|
|
||||||
unset _fzf_defc
|
unset _fzf_defc
|
||||||
|
|
||||||
# Kill completion
|
# Kill completion
|
||||||
@@ -306,4 +311,4 @@ complete -F _fzf_complete_unset -o default -o bashdefault unset
|
|||||||
complete -F _fzf_complete_export -o default -o bashdefault export
|
complete -F _fzf_complete_export -o default -o bashdefault export
|
||||||
complete -F _fzf_complete_unalias -o default -o bashdefault unalias
|
complete -F _fzf_complete_unalias -o default -o bashdefault unalias
|
||||||
|
|
||||||
unset cmd d_cmds f_cmds a_cmds x_cmds
|
unset cmd d_cmds a_cmds x_cmds
|
||||||
|
|||||||
@@ -10,89 +10,110 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
|
# To use custom commands instead of find, override _fzf_compgen_{path,dir}
|
||||||
|
if ! declare -f _fzf_compgen_path > /dev/null; then
|
||||||
|
_fzf_compgen_path() {
|
||||||
|
echo "$1"
|
||||||
|
command find -L "$1" \
|
||||||
|
-name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
|
||||||
|
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! declare -f _fzf_compgen_dir > /dev/null; then
|
||||||
|
_fzf_compgen_dir() {
|
||||||
|
command find -L "$1" \
|
||||||
|
-name .git -prune -o -name .svn -prune -o -type d \
|
||||||
|
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
###########################################################
|
||||||
|
|
||||||
__fzf_generic_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm
|
local base lbuf compgen fzf_opts suffix tail fzf dir leftover matches
|
||||||
# (Q) flag removes a quoting level: "foo\ bar" => "foo bar"
|
# (Q) flag removes a quoting level: "foo\ bar" => "foo bar"
|
||||||
base=${(Q)1}
|
base=${(Q)1}
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
find_opts=$3
|
compgen=$3
|
||||||
fzf_opts=$4
|
fzf_opts=$4
|
||||||
suffix=$5
|
suffix=$5
|
||||||
tail=$6
|
tail=$6
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
|
|
||||||
if ! setopt | grep nonomatch > /dev/null; then
|
setopt localoptions nonomatch
|
||||||
nnm=1
|
|
||||||
setopt nonomatch
|
|
||||||
fi
|
|
||||||
dir="$base"
|
dir="$base"
|
||||||
while [ 1 ]; do
|
while [ 1 ]; do
|
||||||
if [ -z "$dir" -o -d ${~dir} ]; then
|
if [[ -z "$dir" || -d ${~dir} ]]; then
|
||||||
leftover=${base/#"$dir"}
|
leftover=${base/#"$dir"}
|
||||||
leftover=${leftover/#\/}
|
leftover=${leftover/#\/}
|
||||||
[ "$dir" = './' ] && dir=''
|
[ -z "$dir" ] && dir='.'
|
||||||
|
[ "$dir" != "/" ] && dir="${dir/%\//}"
|
||||||
dir=${~dir}
|
dir=${~dir}
|
||||||
matches=$(\find -L $dir* ${=find_opts} 2> /dev/null | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$leftover" | while read item; do
|
matches=$(eval "$compgen $(printf %q "$dir")" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$leftover" | while read item; do
|
||||||
printf "%q$suffix " "$item"
|
echo -n "${(q)item}$suffix "
|
||||||
done)
|
done)
|
||||||
matches=${matches% }
|
matches=${matches% }
|
||||||
if [ -n "$matches" ]; then
|
if [ -n "$matches" ]; then
|
||||||
LBUFFER="$lbuf$matches$tail"
|
LBUFFER="$lbuf$matches$tail"
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
dir=$(dirname "$dir")
|
dir=$(dirname "$dir")
|
||||||
dir=${dir%/}/
|
dir=${dir%/}/
|
||||||
done
|
done
|
||||||
[ -n "$nnm" ] && unsetopt nonomatch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_path_completion() {
|
_fzf_path_completion() {
|
||||||
__fzf_generic_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" _fzf_compgen_path \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
|
||||||
"-m" "" " "
|
"-m" "" " "
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
__fzf_generic_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" _fzf_compgen_dir \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
|
||||||
"" "/" ""
|
"" "/" ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_feed_fifo() (
|
_fzf_feed_fifo() (
|
||||||
rm -f "$fifo"
|
command rm -f "$1"
|
||||||
mkfifo "$fifo"
|
mkfifo "$1"
|
||||||
cat <&0 > "$fifo" &
|
cat <&0 > "$1" &
|
||||||
)
|
)
|
||||||
|
|
||||||
_fzf_complete() {
|
_fzf_complete() {
|
||||||
local fifo fzf_opts lbuf fzf matches
|
local fifo fzf_opts lbuf fzf matches post
|
||||||
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
||||||
fzf_opts=$1
|
fzf_opts=$1
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
|
post="${funcstack[2]}_post"
|
||||||
|
type $post > /dev/null 2>&1 || post=cat
|
||||||
|
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
|
|
||||||
_fzf_feed_fifo "$fifo"
|
_fzf_feed_fifo "$fifo"
|
||||||
matches=$(cat "$fifo" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "${(Q)prefix}" | tr '\n' ' ')
|
matches=$(cat "$fifo" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "${(Q)prefix}" | $post | tr '\n' ' ')
|
||||||
if [ -n "$matches" ]; then
|
if [ -n "$matches" ]; then
|
||||||
LBUFFER="$lbuf$matches"
|
LBUFFER="$lbuf$matches"
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
rm -f "$fifo"
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
|
command rm -f "$fifo"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_complete_telnet() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_complete '+m' "$@" < <(
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |
|
||||||
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_complete_ssh() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_complete '+m' "$@" < <(
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \
|
||||||
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
<(command grep -oE '^[^ ]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \
|
||||||
|
<(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |
|
||||||
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -116,17 +137,14 @@ _fzf_complete_unalias() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fzf-completion() {
|
fzf-completion() {
|
||||||
local tokens cmd prefix trigger tail fzf matches lbuf d_cmds sws
|
local tokens cmd prefix trigger tail fzf matches lbuf d_cmds
|
||||||
if setopt | grep shwordsplit > /dev/null; then
|
setopt localoptions noshwordsplit noksh_arrays
|
||||||
sws=1
|
|
||||||
unsetopt shwordsplit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# http://zsh.sourceforge.net/FAQ/zshfaq03.html
|
# http://zsh.sourceforge.net/FAQ/zshfaq03.html
|
||||||
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
||||||
tokens=(${(z)LBUFFER})
|
tokens=(${(z)LBUFFER})
|
||||||
if [ ${#tokens} -lt 1 ]; then
|
if [ ${#tokens} -lt 1 ]; then
|
||||||
eval "zle ${fzf_default_completion:-expand-or-complete}"
|
zle ${fzf_default_completion:-expand-or-complete}
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -145,9 +163,10 @@ fzf-completion() {
|
|||||||
LBUFFER="$LBUFFER$matches"
|
LBUFFER="$LBUFFER$matches"
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
# Trigger sequence given
|
# Trigger sequence given
|
||||||
elif [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then
|
elif [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then
|
||||||
d_cmds=(cd pushd rmdir)
|
d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir})
|
||||||
|
|
||||||
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
||||||
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
||||||
@@ -161,14 +180,15 @@ fzf-completion() {
|
|||||||
fi
|
fi
|
||||||
# Fall back to default completion
|
# Fall back to default completion
|
||||||
else
|
else
|
||||||
eval "zle ${fzf_default_completion:-expand-or-complete}"
|
zle ${fzf_default_completion:-expand-or-complete}
|
||||||
fi
|
fi
|
||||||
[ -n "$sws" ] && setopt shwordsplit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -z "$fzf_default_completion" ] &&
|
[ -z "$fzf_default_completion" ] && {
|
||||||
fzf_default_completion=$(bindkey '^I' | grep -v undefined-key | awk '{print $2}')
|
binding=$(bindkey '^I')
|
||||||
|
[[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2]
|
||||||
|
unset binding
|
||||||
|
}
|
||||||
|
|
||||||
zle -N fzf-completion
|
zle -N fzf-completion
|
||||||
bindkey '^I' fzf-completion
|
bindkey '^I' fzf-completion
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
__fzf_select__() {
|
__fzf_select__() {
|
||||||
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -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 | sed 1d | cut -b3-"}"
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3-"}"
|
||||||
eval "$cmd" | fzf -m | while read item; do
|
eval "$cmd | fzf -m $FZF_CTRL_T_OPTS" | while read -r item; do
|
||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done
|
done
|
||||||
echo
|
echo
|
||||||
@@ -14,7 +14,7 @@ __fzf_select__() {
|
|||||||
if [[ $- =~ i ]]; then
|
if [[ $- =~ i ]]; then
|
||||||
|
|
||||||
__fzfcmd() {
|
__fzfcmd() {
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
|
[ "${FZF_TMUX:-1}" != 0 ] && echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
|
||||||
}
|
}
|
||||||
|
|
||||||
__fzf_select_tmux__() {
|
__fzf_select_tmux__() {
|
||||||
@@ -25,14 +25,25 @@ __fzf_select_tmux__() {
|
|||||||
else
|
else
|
||||||
height="-l $height"
|
height="-l $height"
|
||||||
fi
|
fi
|
||||||
tmux split-window $height "cd $(printf %q "$PWD"); FZF_CTRL_T_COMMAND=$(printf %q "$FZF_CTRL_T_COMMAND") bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fzf_select__)\"'"
|
|
||||||
|
tmux split-window $height "cd $(printf %q "$PWD"); FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") PATH=$(printf %q "$PATH") FZF_CTRL_T_COMMAND=$(printf %q "$FZF_CTRL_T_COMMAND") FZF_CTRL_T_OPTS=$(printf %q "$FZF_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__fzf_select__)\"; tmux setb -b fzf \"\$RESULT\" \\; pasteb -b fzf -t $TMUX_PANE \\; deleteb -b fzf || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'"
|
||||||
|
}
|
||||||
|
|
||||||
|
fzf-file-widget() {
|
||||||
|
if __fzf_use_tmux__; then
|
||||||
|
__fzf_select_tmux__
|
||||||
|
else
|
||||||
|
local selected="$(__fzf_select__)"
|
||||||
|
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
|
||||||
|
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
__fzf_cd__() {
|
__fzf_cd__() {
|
||||||
local cmd dir
|
local cmd dir
|
||||||
cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
|
||||||
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
|
||||||
dir=$(eval "$cmd" | $(__fzfcmd) +m) && printf 'cd %q' "$dir"
|
dir=$(eval "$cmd | $(__fzfcmd) +m $FZF_ALT_C_OPTS") && printf 'cd %q' "$dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
__fzf_history__() (
|
__fzf_history__() (
|
||||||
@@ -40,8 +51,8 @@ __fzf_history__() (
|
|||||||
shopt -u nocaseglob nocasematch
|
shopt -u nocaseglob nocasematch
|
||||||
line=$(
|
line=$(
|
||||||
HISTTIMEFORMAT= history |
|
HISTTIMEFORMAT= history |
|
||||||
$(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r |
|
eval "$(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS" |
|
||||||
\grep '^ *[0-9]') &&
|
command grep '^ *[0-9]') &&
|
||||||
if [[ $- =~ H ]]; then
|
if [[ $- =~ H ]]; then
|
||||||
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
|
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
|
||||||
else
|
else
|
||||||
@@ -49,49 +60,66 @@ __fzf_history__() (
|
|||||||
fi
|
fi
|
||||||
)
|
)
|
||||||
|
|
||||||
__use_tmux=0
|
__fzf_use_tmux__() {
|
||||||
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-1}" != 0 ] && [ ${LINES:-40} -gt 15 ]
|
||||||
|
}
|
||||||
|
|
||||||
if [ -z "$(set -o | \grep '^vi.*on')" ]; then
|
[ $BASH_VERSINFO -gt 3 ] && __use_bind_x=1 || __use_bind_x=0
|
||||||
|
__fzf_use_tmux__ && __use_tmux=1 || __use_tmux=0
|
||||||
|
|
||||||
|
if [[ ! -o vi ]]; 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'
|
||||||
bind '"\e^": history-expand-line'
|
bind '"\e^": history-expand-line'
|
||||||
|
|
||||||
# CTRL-T - Paste the selected file path into the command line
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
if [ $__use_tmux -eq 1 ]; then
|
if [ $__use_bind_x -eq 1 ]; then
|
||||||
|
bind -x '"\C-t": "fzf-file-widget"'
|
||||||
|
elif [ $__use_tmux -eq 1 ]; then
|
||||||
bind '"\C-t": " \C-u \C-a\C-k$(__fzf_select_tmux__)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
|
bind '"\C-t": " \C-u \C-a\C-k$(__fzf_select_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$(__fzf_select__)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'
|
bind '"\C-t": " \C-u \C-a\C-k$(__fzf_select__)\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$(__fzf_history__)\e\C-e\e^\er"'
|
bind '"\C-r": " \C-e\C-u`__fzf_history__`\e\C-e\e^\er"'
|
||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
bind '"\ec": " \C-e\C-u$(__fzf_cd__)\e\C-e\er\C-m"'
|
bind '"\ec": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"'
|
||||||
else
|
else
|
||||||
|
# We'd usually use "\e" to enter vi-movement-mode so we can do our magic,
|
||||||
|
# but this incurs a very noticeable delay of a half second or so,
|
||||||
|
# because many other commands start with "\e".
|
||||||
|
# Instead, we bind an unused key, "\C-x\C-a",
|
||||||
|
# to also enter vi-movement-mode,
|
||||||
|
# and then use that thereafter.
|
||||||
|
# (We imagine that "\C-x\C-a" is relatively unlikely to be in use.)
|
||||||
|
bind '"\C-x\C-a": vi-movement-mode'
|
||||||
|
|
||||||
bind '"\C-x\C-e": shell-expand-line'
|
bind '"\C-x\C-e": shell-expand-line'
|
||||||
bind '"\C-x\C-r": redraw-current-line'
|
bind '"\C-x\C-r": redraw-current-line'
|
||||||
bind '"\C-x^": history-expand-line'
|
bind '"\C-x^": history-expand-line'
|
||||||
|
|
||||||
# CTRL-T - Paste the selected file path into the command line
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
# - FIXME: Selected items are attached to the end regardless of cursor position
|
# - FIXME: Selected items are attached to the end regardless of cursor position
|
||||||
if [ $__use_tmux -eq 1 ]; then
|
if [ $__use_bind_x -eq 1 ]; then
|
||||||
bind '"\C-t": "\e$a \eddi$(__fzf_select_tmux__)\C-x\C-e\e0P$xa"'
|
bind -x '"\C-t": "fzf-file-widget"'
|
||||||
|
elif [ $__use_tmux -eq 1 ]; then
|
||||||
|
bind '"\C-t": "\C-x\C-a$a \C-x\C-addi$(__fzf_select_tmux__)\C-x\C-e\C-x\C-a0P$xa"'
|
||||||
else
|
else
|
||||||
bind '"\C-t": "\e$a \eddi$(__fzf_select__)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
bind '"\C-t": "\C-x\C-a$a \C-x\C-addi$(__fzf_select__)\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
|
||||||
fi
|
fi
|
||||||
bind -m vi-command '"\C-t": "i\C-t"'
|
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$(__fzf_history__)\C-x\C-e\C-x^\e$a\C-x\C-r"'
|
bind '"\C-r": "\C-x\C-addi$(__fzf_history__)\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r"'
|
||||||
bind -m vi-command '"\C-r": "i\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$(__fzf_cd__)\C-x\C-e\C-x\C-r\C-m"'
|
bind '"\ec": "\C-x\C-addi$(__fzf_cd__)\C-x\C-e\C-x\C-r\C-m"'
|
||||||
bind -m vi-command '"\ec": "i\ec"'
|
bind -m vi-command '"\ec": "ddi$(__fzf_cd__)\C-x\C-e\C-x\C-r\C-m"'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset __use_tmux
|
unset -v __use_tmux __use_bind_x
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -13,31 +13,31 @@ function fzf_key_bindings
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_ctrl_t
|
function fzf-file-widget
|
||||||
set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "
|
set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "
|
||||||
command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -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 | sed 1d | cut -b3-"
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3-"
|
||||||
eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m > $TMPDIR/fzf.result"
|
eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m $FZF_CTRL_T_OPTS > $TMPDIR/fzf.result"
|
||||||
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
and for i in (seq 20); commandline -i (cat $TMPDIR/fzf.result | __fzf_escape) 2> /dev/null; and break; sleep 0.1; end
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_ctrl_r
|
function fzf-history-widget
|
||||||
history | eval (__fzfcmd) +s +m --tiebreak=index --toggle-sort=ctrl-r > $TMPDIR/fzf.result
|
history | eval (__fzfcmd) +s +m --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q '(commandline)' > $TMPDIR/fzf.result
|
||||||
and commandline (cat $TMPDIR/fzf.result)
|
and commandline -- (cat $TMPDIR/fzf.result)
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
rm -f $TMPDIR/fzf.result
|
rm -f $TMPDIR/fzf.result
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fzf_alt_c
|
function fzf-cd-widget
|
||||||
set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "
|
set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "
|
||||||
command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
|
||||||
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"
|
||||||
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
||||||
eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m > $TMPDIR/fzf.result"
|
eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m $FZF_ALT_C_OPTS > $TMPDIR/fzf.result"
|
||||||
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
||||||
and cd (cat $TMPDIR/fzf.result)
|
and cd (cat $TMPDIR/fzf.result)
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
@@ -58,14 +58,14 @@ function fzf_key_bindings
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
bind \ct '__fzf_ctrl_t'
|
bind \ct fzf-file-widget
|
||||||
bind \cr '__fzf_ctrl_r'
|
bind \cr fzf-history-widget
|
||||||
bind \ec '__fzf_alt_c'
|
bind \ec fzf-cd-widget
|
||||||
|
|
||||||
if bind -M insert > /dev/null 2>&1
|
if bind -M insert > /dev/null 2>&1
|
||||||
bind -M insert \ct '__fzf_ctrl_t'
|
bind -M insert \ct fzf-file-widget
|
||||||
bind -M insert \cr '__fzf_ctrl_r'
|
bind -M insert \cr fzf-history-widget
|
||||||
bind -M insert \ec '__fzf_alt_c'
|
bind -M insert \ec fzf-cd-widget
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,17 @@ if [[ $- == *i* ]]; then
|
|||||||
|
|
||||||
# CTRL-T - Paste the selected file path(s) into the command line
|
# CTRL-T - Paste the selected file path(s) into the command line
|
||||||
__fsel() {
|
__fsel() {
|
||||||
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -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 | sed 1d | cut -b3-"}"
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3-"}"
|
||||||
eval "$cmd" | $(__fzfcmd) -m | while read item; do
|
setopt localoptions pipefail 2> /dev/null
|
||||||
printf '%q ' "$item"
|
eval "$cmd | $(__fzfcmd) -m $FZF_CTRL_T_OPTS" | while read item; do
|
||||||
|
echo -n "${(q)item} "
|
||||||
done
|
done
|
||||||
|
local ret=$?
|
||||||
echo
|
echo
|
||||||
|
return $ret
|
||||||
}
|
}
|
||||||
|
|
||||||
__fzfcmd() {
|
__fzfcmd() {
|
||||||
@@ -20,17 +23,24 @@ __fzfcmd() {
|
|||||||
|
|
||||||
fzf-file-widget() {
|
fzf-file-widget() {
|
||||||
LBUFFER="${LBUFFER}$(__fsel)"
|
LBUFFER="${LBUFFER}$(__fsel)"
|
||||||
|
local ret=$?
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
|
return $ret
|
||||||
}
|
}
|
||||||
zle -N fzf-file-widget
|
zle -N fzf-file-widget
|
||||||
bindkey '^T' fzf-file-widget
|
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() {
|
||||||
local cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
|
local cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
|
||||||
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
|
||||||
cd "${$(eval "$cmd" | $(__fzfcmd) +m):-.}"
|
setopt localoptions pipefail 2> /dev/null
|
||||||
|
cd "${$(eval "$cmd | $(__fzfcmd) +m $FZF_ALT_C_OPTS"):-.}"
|
||||||
|
local ret=$?
|
||||||
zle reset-prompt
|
zle reset-prompt
|
||||||
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
|
return $ret
|
||||||
}
|
}
|
||||||
zle -N fzf-cd-widget
|
zle -N fzf-cd-widget
|
||||||
bindkey '\ec' fzf-cd-widget
|
bindkey '\ec' fzf-cd-widget
|
||||||
@@ -38,7 +48,9 @@ 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() {
|
||||||
local selected num
|
local selected num
|
||||||
selected=( $(fc -l 1 | $(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r -q "${LBUFFER//$/\\$}") )
|
setopt localoptions noglobsubst pipefail 2> /dev/null
|
||||||
|
selected=( $(fc -l 1 | eval "$(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q ${(q)LBUFFER}") )
|
||||||
|
local ret=$?
|
||||||
if [ -n "$selected" ]; then
|
if [ -n "$selected" ]; then
|
||||||
num=$selected[1]
|
num=$selected[1]
|
||||||
if [ -n "$num" ]; then
|
if [ -n "$num" ]; then
|
||||||
@@ -46,6 +58,8 @@ fzf-history-widget() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
typeset -f zle-line-init >/dev/null && zle zle-line-init
|
||||||
|
return $ret
|
||||||
}
|
}
|
||||||
zle -N fzf-history-widget
|
zle -N fzf-history-widget
|
||||||
bindkey '^R' fzf-history-widget
|
bindkey '^R' fzf-history-widget
|
||||||
|
|||||||
40
src/Dockerfile.android
Normal file
40
src/Dockerfile.android
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
# 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 && \
|
||||||
|
sed -i 's@#define PTHREAD_KEYS_MAX 128@@' /go1.4/src/runtime/cgo/gcc_android_arm.c
|
||||||
|
|
||||||
|
ENV GOROOT /go1.4
|
||||||
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
|
RUN cd / && \
|
||||||
|
curl -O http://dl.google.com/android/ndk/android-ndk-r10e-linux-x86_64.bin && \
|
||||||
|
chmod 755 /android-ndk* && /android-ndk-r10e-linux-x86_64.bin && \
|
||||||
|
mv android-ndk-r10e /android-ndk
|
||||||
|
|
||||||
|
RUN cd /android-ndk && bash ./build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=/ndk --arch=arm
|
||||||
|
|
||||||
|
ENV NDK_CC /ndk/bin/arm-linux-androideabi-gcc
|
||||||
|
|
||||||
|
RUN cd $GOROOT/src && \
|
||||||
|
CC_FOR_TARGET=$NDK_CC GOOS=android GOARCH=arm GOARM=7 ./make.bash
|
||||||
|
|
||||||
|
RUN cd / && curl \
|
||||||
|
http://ftp.gnu.org/gnu/ncurses/ncurses-5.9.tar.gz | \
|
||||||
|
tar -xz && cd /ncurses-5.9 && \
|
||||||
|
./configure CC=$NDK_CC CFLAGS="-fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch" LDFLAGS="-march=armv7-a -Wl,--no-warn-mismatch" --host=arm-linux --enable-overwrite --enable-const --without-cxx-binding --without-shared --without-debug --enable-widec --enable-ext-colors --enable-ext-mouse --enable-pc-files --with-pkg-config-libdir=$PKG_CONFIG_LIBDIR --without-manpages --without-ada --disable-shared --without-tests --prefix=/ndk/sysroot/usr --with-default-terminfo-dirs=/usr/share/terminfo --with-terminfo-dirs=/usr/share/terminfo ac_cv_header_locale_h=n ac_cv_func_getpwent=no ac_cv_func_getpwnam=no ac_cv_func_getpwuid=no && \
|
||||||
|
sed -i 's@#define HAVE_LOCALE_H 1@/* #undef HAVE_LOCALE_H */@' include/ncurses_cfg.h && \
|
||||||
|
make && \
|
||||||
|
sed -i '0,/echo.*/{s/echo.*/exit 0/}' misc/run_tic.sh && \
|
||||||
|
make install && \
|
||||||
|
mv /ndk/sysroot/usr/lib/libncursesw.a /ndk/sysroot/usr/lib/libncurses.a
|
||||||
|
|
||||||
|
# Default CMD
|
||||||
|
CMD cd /fzf/src && /bin/bash
|
||||||
@@ -10,7 +10,6 @@ RUN cd / && curl \
|
|||||||
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
tar -xz && mv go go1.4
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
ENV GOPATH /go
|
|
||||||
ENV GOROOT /go1.4
|
ENV GOROOT /go1.4
|
||||||
ENV PATH /go1.4/bin:$PATH
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
@@ -20,9 +19,6 @@ RUN echo '[multilib]' >> /etc/pacman.conf && \
|
|||||||
pacman-db-upgrade && yes | pacman -Sy gcc-multilib lib32-ncurses && \
|
pacman-db-upgrade && yes | pacman -Sy gcc-multilib lib32-ncurses && \
|
||||||
cd $GOROOT/src && GOARCH=386 ./make.bash
|
cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
# Volume
|
|
||||||
VOLUME /go
|
|
||||||
|
|
||||||
# Default CMD
|
# Default CMD
|
||||||
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
CMD cd /fzf/src && /bin/bash
|
||||||
|
|
||||||
|
|||||||
@@ -11,16 +11,22 @@ RUN cd / && curl \
|
|||||||
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
tar -xz && mv go go1.4
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
ENV GOPATH /go
|
# Install Go 1.7
|
||||||
ENV GOROOT /go1.4
|
RUN cd / && curl \
|
||||||
ENV PATH /go1.4/bin:$PATH
|
https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz | \
|
||||||
|
tar -xz && mv go go1.7
|
||||||
|
|
||||||
|
# Install RPMs for building static 32-bit binary
|
||||||
|
RUN curl ftp://ftp.pbone.net/mirror/ftp.centos.org/6.8/os/i386/Packages/ncurses-static-5.7-4.20090207.el6.i686.rpm -o rpm && rpm -i rpm && \
|
||||||
|
curl ftp://ftp.pbone.net/mirror/ftp.centos.org/6.8/os/i386/Packages/gpm-static-1.20.6-12.el6.i686.rpm -o rpm && rpm -i rpm
|
||||||
|
|
||||||
|
ENV GOROOT_BOOTSTRAP /go1.4
|
||||||
|
ENV GOROOT /go1.7
|
||||||
|
ENV PATH /go1.7/bin:$PATH
|
||||||
|
|
||||||
# For i386 build
|
# For i386 build
|
||||||
RUN cd $GOROOT/src && GOARCH=386 ./make.bash
|
RUN cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
# Volume
|
|
||||||
VOLUME /go
|
|
||||||
|
|
||||||
# Default CMD
|
# Default CMD
|
||||||
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
CMD cd /fzf/src && /bin/bash
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ RUN cd / && curl \
|
|||||||
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz | \
|
||||||
tar -xz && mv go go1.4
|
tar -xz && mv go go1.4
|
||||||
|
|
||||||
ENV GOPATH /go
|
|
||||||
ENV GOROOT /go1.4
|
ENV GOROOT /go1.4
|
||||||
ENV PATH /go1.4/bin:$PATH
|
ENV PATH /go1.4/bin:$PATH
|
||||||
|
|
||||||
@@ -18,9 +17,6 @@ ENV PATH /go1.4/bin:$PATH
|
|||||||
RUN apt-get install -y lib32ncurses5-dev && \
|
RUN apt-get install -y lib32ncurses5-dev && \
|
||||||
cd $GOROOT/src && GOARCH=386 ./make.bash
|
cd $GOROOT/src && GOARCH=386 ./make.bash
|
||||||
|
|
||||||
# Volume
|
|
||||||
VOLUME /go
|
|
||||||
|
|
||||||
# Default CMD
|
# Default CMD
|
||||||
CMD cd /go/src/github.com/junegunn/fzf/src && /bin/bash
|
CMD cd /fzf/src && /bin/bash
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2015 Junegunn Choi
|
Copyright (c) 2016 Junegunn Choi
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
114
src/Makefile
114
src/Makefile
@@ -1,57 +1,83 @@
|
|||||||
ifndef GOPATH
|
ifndef GOOS
|
||||||
$(error GOPATH is undefined)
|
|
||||||
endif
|
|
||||||
|
|
||||||
UNAME_S := $(shell uname -s)
|
UNAME_S := $(shell uname -s)
|
||||||
ifeq ($(UNAME_S),Darwin)
|
ifeq ($(UNAME_S),Darwin)
|
||||||
GOOS := darwin
|
GOOS := darwin
|
||||||
else ifeq ($(UNAME_S),Linux)
|
else ifeq ($(UNAME_S),Linux)
|
||||||
GOOS := linux
|
GOOS := linux
|
||||||
endif
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
ifneq ($(shell uname -m),x86_64)
|
SOURCES := $(wildcard *.go */*.go)
|
||||||
|
ROOTDIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||||
|
BINDIR := $(shell dirname $(ROOTDIR))/bin
|
||||||
|
GOPATH := $(shell dirname $(ROOTDIR))/gopath
|
||||||
|
SRCDIR := $(GOPATH)/src/github.com/junegunn/fzf/src
|
||||||
|
DOCKEROPTS := -i -t -v $(ROOTDIR):/fzf/src
|
||||||
|
BINARY32 := fzf-$(GOOS)_386
|
||||||
|
BINARY64 := fzf-$(GOOS)_amd64
|
||||||
|
BINARYARM7 := fzf-$(GOOS)_arm7
|
||||||
|
VERSION := $(shell awk -F= '/version =/ {print $$2}' constants.go | tr -d "\" ")
|
||||||
|
RELEASE32 := fzf-$(VERSION)-$(GOOS)_386
|
||||||
|
RELEASE64 := fzf-$(VERSION)-$(GOOS)_amd64
|
||||||
|
RELEASEARM7 := fzf-$(VERSION)-$(GOOS)_arm7
|
||||||
|
export GOPATH
|
||||||
|
|
||||||
|
UNAME_M := $(shell uname -m)
|
||||||
|
ifeq ($(UNAME_M),x86_64)
|
||||||
|
BINARY := $(BINARY64)
|
||||||
|
else ifeq ($(UNAME_M),i686)
|
||||||
|
BINARY := $(BINARY32)
|
||||||
|
else
|
||||||
$(error "Build on $(UNAME_M) is not supported, yet.")
|
$(error "Build on $(UNAME_M) is not supported, yet.")
|
||||||
endif
|
endif
|
||||||
|
|
||||||
SOURCES := $(wildcard *.go */*.go)
|
all: fzf/$(BINARY)
|
||||||
BINDIR := ../bin
|
|
||||||
|
|
||||||
BINARY32 := fzf-$(GOOS)_386
|
ifeq ($(GOOS),windows)
|
||||||
BINARY64 := fzf-$(GOOS)_amd64
|
release: fzf/$(BINARY32) fzf/$(BINARY64)
|
||||||
VERSION = $(shell fzf/$(BINARY64) --version)
|
-cd fzf && cp $(BINARY32) $(RELEASE32).exe && zip $(RELEASE32).zip $(RELEASE32).exe
|
||||||
RELEASE32 = fzf-$(VERSION)-$(GOOS)_386
|
cd fzf && cp $(BINARY64) $(RELEASE64).exe && zip $(RELEASE64).zip $(RELEASE64).exe && \
|
||||||
RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd64
|
rm -f $(RELEASE32).exe $(RELEASE64).exe
|
||||||
|
else
|
||||||
all: release
|
release: test fzf/$(BINARY32) fzf/$(BINARY64)
|
||||||
|
|
||||||
release: build
|
|
||||||
-cd fzf && cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32)
|
-cd fzf && cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32)
|
||||||
cd fzf && cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
cd fzf && cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \
|
||||||
rm -f $(RELEASE32) $(RELEASE64)
|
rm -f $(RELEASE32) $(RELEASE64)
|
||||||
|
endif
|
||||||
|
|
||||||
build: test fzf/$(BINARY32) fzf/$(BINARY64)
|
$(SRCDIR):
|
||||||
|
mkdir -p $(shell dirname $(SRCDIR))
|
||||||
|
ln -s $(ROOTDIR) $(SRCDIR)
|
||||||
|
|
||||||
test:
|
deps: $(SRCDIR) $(SOURCES)
|
||||||
go get
|
cd $(SRCDIR) && go get -tags "$(TAGS)"
|
||||||
go test -v ./...
|
|
||||||
|
android-build: $(SRCDIR)
|
||||||
|
cd $(SRCDIR) && GOARCH=arm GOARM=7 CGO_ENABLED=1 go get
|
||||||
|
cd $(SRCDIR)/fzf && GOARCH=arm GOARM=7 CGO_ENABLED=1 go build -a -ldflags="-w -extldflags=-pie" -o $(BINARYARM7)
|
||||||
|
cd $(SRCDIR)/fzf && cp $(BINARYARM7) $(RELEASEARM7) && tar -czf $(RELEASEARM7).tgz $(RELEASEARM7) && \
|
||||||
|
rm -f $(RELEASEARM7)
|
||||||
|
|
||||||
|
test: deps
|
||||||
|
SHELL=/bin/sh GOOS=$(GOOS) go test -v -tags "$(TAGS)" ./...
|
||||||
|
|
||||||
install: $(BINDIR)/fzf
|
install: $(BINDIR)/fzf
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f $(BINDIR)/fzf $(BINDIR)/$(BINARY64)
|
rm -f $(BINDIR)/fzf $(BINDIR)/$(BINARY)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
cd fzf && rm -f fzf-*
|
cd fzf && rm -f fzf-*
|
||||||
|
|
||||||
fzf/$(BINARY32): $(SOURCES)
|
fzf/$(BINARY32): deps
|
||||||
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -a -o $(BINARY32)
|
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -a -ldflags -w -tags "$(TAGS)" -o $(BINARY32)
|
||||||
|
|
||||||
fzf/$(BINARY64): $(SOURCES)
|
fzf/$(BINARY64): deps
|
||||||
cd fzf && go build -a -tags "$(TAGS)" -o $(BINARY64)
|
cd fzf && go build -a -ldflags -w -tags "$(TAGS)" -o $(BINARY64)
|
||||||
|
|
||||||
$(BINDIR)/fzf: fzf/$(BINARY64) | $(BINDIR)
|
$(BINDIR)/fzf: fzf/$(BINARY) | $(BINDIR)
|
||||||
cp -f fzf/$(BINARY64) $(BINDIR)
|
cp -f fzf/$(BINARY) $(BINDIR)
|
||||||
cd $(BINDIR) && ln -sf $(BINARY64) fzf
|
cd $(BINDIR) && ln -sf $(BINARY) fzf
|
||||||
|
|
||||||
$(BINDIR):
|
$(BINDIR):
|
||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
@@ -65,21 +91,33 @@ docker-ubuntu:
|
|||||||
docker-centos:
|
docker-centos:
|
||||||
docker build -t junegunn/centos-sandbox - < Dockerfile.centos
|
docker build -t junegunn/centos-sandbox - < Dockerfile.centos
|
||||||
|
|
||||||
|
docker-android:
|
||||||
|
docker build -t junegunn/android-sandbox - < Dockerfile.android
|
||||||
|
|
||||||
arch: docker-arch
|
arch: docker-arch
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
docker run $(DOCKEROPTS) junegunn/$@-sandbox \
|
||||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
sh -c 'cd /fzf/src; /bin/bash'
|
||||||
|
|
||||||
ubuntu: docker-ubuntu
|
ubuntu: docker-ubuntu
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
docker run $(DOCKEROPTS) junegunn/$@-sandbox \
|
||||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
sh -c 'cd /fzf/src; /bin/bash'
|
||||||
|
|
||||||
centos: docker-centos
|
centos: docker-centos
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/$@-sandbox \
|
docker run $(DOCKEROPTS) junegunn/$@-sandbox \
|
||||||
sh -c 'cd /go/src/github.com/junegunn/fzf/src; /bin/bash'
|
sh -c 'cd /fzf/src; /bin/bash'
|
||||||
|
|
||||||
linux: docker-centos
|
linux: docker-centos
|
||||||
docker run -i -t -v $(GOPATH):/go junegunn/centos-sandbox \
|
docker run $(DOCKEROPTS) junegunn/centos-sandbox \
|
||||||
/bin/bash -ci 'cd /go/src/github.com/junegunn/fzf/src; make TAGS=static'
|
/bin/bash -ci 'cd /fzf/src; make TAGS=static release'
|
||||||
|
|
||||||
.PHONY: all build release test install uninstall clean docker \
|
ubuntu-android: docker-android
|
||||||
linux arch ubuntu centos docker-arch docker-ubuntu docker-centos
|
docker run $(DOCKEROPTS) junegunn/android-sandbox \
|
||||||
|
sh -c 'cd /fzf/src; /bin/bash'
|
||||||
|
|
||||||
|
android: docker-android
|
||||||
|
docker run $(DOCKEROPTS) junegunn/android-sandbox \
|
||||||
|
/bin/bash -ci 'cd /fzf/src; GOOS=android make android-build'
|
||||||
|
|
||||||
|
.PHONY: all deps release test install uninstall clean \
|
||||||
|
linux arch ubuntu centos docker-arch docker-ubuntu docker-centos \
|
||||||
|
android-build docker-android ubuntu-android android
|
||||||
|
|||||||
@@ -47,39 +47,12 @@ 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
|
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.
|
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
|
Build
|
||||||
-----
|
-----
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Build fzf executables and tarballs
|
# Build fzf executables and tarballs
|
||||||
make
|
make release
|
||||||
|
|
||||||
# Install the executable to ../bin directory
|
# Install the executable to ../bin directory
|
||||||
make install
|
make install
|
||||||
@@ -88,25 +61,33 @@ make install
|
|||||||
make linux
|
make linux
|
||||||
```
|
```
|
||||||
|
|
||||||
Contribution
|
Test
|
||||||
------------
|
----
|
||||||
|
|
||||||
For the time being, I will not add or accept any new features until we can be
|
Unit tests can be run with `make test`. Integration tests are written in Ruby
|
||||||
sure that the implementation is stable and we have a sufficient number of test
|
script that should be run on tmux.
|
||||||
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
|
```sh
|
||||||
sure that your change does not result in performance regression. And please be
|
# Unit tests
|
||||||
noted that we don't have a quantitative measure of the performance yet.
|
make test
|
||||||
|
|
||||||
|
# Install the executable to ../bin directory
|
||||||
|
make install
|
||||||
|
|
||||||
|
# Integration tests
|
||||||
|
ruby ../test/test_go.rb
|
||||||
|
```
|
||||||
|
|
||||||
Third-party libraries used
|
Third-party libraries used
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
- [ncurses][ncurses]
|
- [ncurses][ncurses]
|
||||||
- [mattn/go-runewidth](https://github.com/mattn/go-runewidth)
|
- [mattn/go-runewidth](https://github.com/mattn/go-runewidth)
|
||||||
- Licensed under [MIT](http://mattn.mit-license.org/2013)
|
- Licensed under [MIT](http://mattn.mit-license.org)
|
||||||
- [mattn/go-shellwords](https://github.com/mattn/go-shellwords)
|
- [mattn/go-shellwords](https://github.com/mattn/go-shellwords)
|
||||||
- Licensed under [MIT](http://mattn.mit-license.org/2014)
|
- Licensed under [MIT](http://mattn.mit-license.org)
|
||||||
|
- [mattn/go-isatty](https://github.com/mattn/go-isatty)
|
||||||
|
- Licensed under [MIT](http://mattn.mit-license.org)
|
||||||
|
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|||||||
631
src/algo/algo.go
631
src/algo/algo.go
@@ -1,51 +1,507 @@
|
|||||||
package algo
|
package algo
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Algorithm
|
||||||
|
---------
|
||||||
|
|
||||||
|
FuzzyMatchV1 finds the first "fuzzy" occurrence of the pattern within the given
|
||||||
|
text in O(n) time where n is the length of the text. Once the position of the
|
||||||
|
last character is located, it traverses backwards to see if there's a shorter
|
||||||
|
substring that matches the pattern.
|
||||||
|
|
||||||
|
a_____b___abc__ To find "abc"
|
||||||
|
*-----*-----*> 1. Forward scan
|
||||||
|
<*** 2. Backward scan
|
||||||
|
|
||||||
|
The algorithm is simple and fast, but as it only sees the first occurrence,
|
||||||
|
it is not guaranteed to find the occurrence with the highest score.
|
||||||
|
|
||||||
|
a_____b__c__abc
|
||||||
|
*-----*--* ***
|
||||||
|
|
||||||
|
FuzzyMatchV2 implements a modified version of Smith-Waterman algorithm to find
|
||||||
|
the optimal solution (highest score) according to the scoring criteria. Unlike
|
||||||
|
the original algorithm, omission or mismatch of a character in the pattern is
|
||||||
|
not allowed.
|
||||||
|
|
||||||
|
Performance
|
||||||
|
-----------
|
||||||
|
|
||||||
|
The new V2 algorithm is slower than V1 as it examines all occurrences of the
|
||||||
|
pattern instead of stopping immediately after finding the first one. The time
|
||||||
|
complexity of the algorithm is O(nm) if a match is found and O(n) otherwise
|
||||||
|
where n is the length of the item and m is the length of the pattern. Thus, the
|
||||||
|
performance overhead may not be noticeable for a query with high selectivity.
|
||||||
|
However, if the performance is more important than the quality of the result,
|
||||||
|
you can still choose v1 algorithm with --algo=v1.
|
||||||
|
|
||||||
|
Scoring criteria
|
||||||
|
----------------
|
||||||
|
|
||||||
|
- We prefer matches at special positions, such as the start of a word, or
|
||||||
|
uppercase character in camelCase words.
|
||||||
|
|
||||||
|
- That is, we prefer an occurrence of the pattern with more characters
|
||||||
|
matching at special positions, even if the total match length is longer.
|
||||||
|
e.g. "fuzzyfinder" vs. "fuzzy-finder" on "ff"
|
||||||
|
````````````
|
||||||
|
- Also, if the first character in the pattern appears at one of the special
|
||||||
|
positions, the bonus point for the position is multiplied by a constant
|
||||||
|
as it is extremely likely that the first character in the typed pattern
|
||||||
|
has more significance than the rest.
|
||||||
|
e.g. "fo-bar" vs. "foob-r" on "br"
|
||||||
|
``````
|
||||||
|
- But since fzf is still a fuzzy finder, not an acronym finder, we should also
|
||||||
|
consider the total length of the matched substring. This is why we have the
|
||||||
|
gap penalty. The gap penalty increases as the length of the gap (distance
|
||||||
|
between the matching characters) increases, so the effect of the bonus is
|
||||||
|
eventually cancelled at some point.
|
||||||
|
e.g. "fuzzyfinder" vs. "fuzzy-blurry-finder" on "ff"
|
||||||
|
```````````
|
||||||
|
- Consequently, it is crucial to find the right balance between the bonus
|
||||||
|
and the gap penalty. The parameters were chosen that the bonus is cancelled
|
||||||
|
when the gap size increases beyond 8 characters.
|
||||||
|
|
||||||
|
- The bonus mechanism can have the undesirable side effect where consecutive
|
||||||
|
matches are ranked lower than the ones with gaps.
|
||||||
|
e.g. "foobar" vs. "foo-bar" on "foob"
|
||||||
|
```````
|
||||||
|
- To correct this anomaly, we also give extra bonus point to each character
|
||||||
|
in a consecutive matching chunk.
|
||||||
|
e.g. "foobar" vs. "foo-bar" on "foob"
|
||||||
|
``````
|
||||||
|
- The amount of consecutive bonus is primarily determined by the bonus of the
|
||||||
|
first character in the chunk.
|
||||||
|
e.g. "foobar" vs. "out-of-bound" on "oob"
|
||||||
|
````````````
|
||||||
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/util"
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
var DEBUG bool
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
func runeAt(runes []rune, index int, max int, forward bool) rune {
|
func indexAt(index int, max int, forward bool) int {
|
||||||
if forward {
|
if forward {
|
||||||
return runes[index]
|
return index
|
||||||
}
|
}
|
||||||
return runes[max-index-1]
|
return max - index - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// FuzzyMatch performs fuzzy-match
|
// Result contains the results of running a match function.
|
||||||
func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
type Result struct {
|
||||||
if len(pattern) == 0 {
|
// TODO int32 should suffice
|
||||||
return 0, 0
|
Start int
|
||||||
|
End int
|
||||||
|
Score int
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
scoreMatch = 16
|
||||||
|
scoreGapStart = -3
|
||||||
|
scoreGapExtention = -1
|
||||||
|
|
||||||
|
// We prefer matches at the beginning of a word, but the bonus should not be
|
||||||
|
// too great to prevent the longer acronym matches from always winning over
|
||||||
|
// shorter fuzzy matches. The bonus point here was specifically chosen that
|
||||||
|
// the bonus is cancelled when the gap between the acronyms grows over
|
||||||
|
// 8 characters, which is approximately the average length of the words found
|
||||||
|
// in web2 dictionary and my file system.
|
||||||
|
bonusBoundary = scoreMatch / 2
|
||||||
|
|
||||||
|
// Although bonus point for non-word characters is non-contextual, we need it
|
||||||
|
// for computing bonus points for consecutive chunks starting with a non-word
|
||||||
|
// character.
|
||||||
|
bonusNonWord = scoreMatch / 2
|
||||||
|
|
||||||
|
// Edge-triggered bonus for matches in camelCase words.
|
||||||
|
// Compared to word-boundary case, they don't accompany single-character gaps
|
||||||
|
// (e.g. FooBar vs. foo-bar), so we deduct bonus point accordingly.
|
||||||
|
bonusCamel123 = bonusBoundary + scoreGapExtention
|
||||||
|
|
||||||
|
// Minimum bonus point given to characters in consecutive chunks.
|
||||||
|
// Note that bonus points for consecutive matches shouldn't have needed if we
|
||||||
|
// used fixed match score as in the original algorithm.
|
||||||
|
bonusConsecutive = -(scoreGapStart + scoreGapExtention)
|
||||||
|
|
||||||
|
// The first character in the typed pattern usually has more significance
|
||||||
|
// than the rest so it's important that it appears at special positions where
|
||||||
|
// bonus points are given. e.g. "to-go" vs. "ongoing" on "og" or on "ogo".
|
||||||
|
// The amount of the extra bonus should be limited so that the gap penalty is
|
||||||
|
// still respected.
|
||||||
|
bonusFirstCharMultiplier = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
type charClass int
|
||||||
|
|
||||||
|
const (
|
||||||
|
charNonWord charClass = iota
|
||||||
|
charLower
|
||||||
|
charUpper
|
||||||
|
charLetter
|
||||||
|
charNumber
|
||||||
|
)
|
||||||
|
|
||||||
|
func posArray(withPos bool, len int) *[]int {
|
||||||
|
if withPos {
|
||||||
|
pos := make([]int, 0, len)
|
||||||
|
return &pos
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func alloc16(offset int, slab *util.Slab, size int, clear bool) (int, []int16) {
|
||||||
|
if slab != nil && cap(slab.I16) > offset+size {
|
||||||
|
slice := slab.I16[offset : offset+size]
|
||||||
|
if clear {
|
||||||
|
for idx := range slice {
|
||||||
|
slice[idx] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return offset + size, slice
|
||||||
|
}
|
||||||
|
return offset, make([]int16, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func alloc32(offset int, slab *util.Slab, size int, clear bool) (int, []int32) {
|
||||||
|
if slab != nil && cap(slab.I32) > offset+size {
|
||||||
|
slice := slab.I32[offset : offset+size]
|
||||||
|
if clear {
|
||||||
|
for idx := range slice {
|
||||||
|
slice[idx] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return offset + size, slice
|
||||||
|
}
|
||||||
|
return offset, make([]int32, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func charClassOfAscii(char rune) charClass {
|
||||||
|
if char >= 'a' && char <= 'z' {
|
||||||
|
return charLower
|
||||||
|
} else if char >= 'A' && char <= 'Z' {
|
||||||
|
return charUpper
|
||||||
|
} else if char >= '0' && char <= '9' {
|
||||||
|
return charNumber
|
||||||
|
}
|
||||||
|
return charNonWord
|
||||||
|
}
|
||||||
|
|
||||||
|
func charClassOfNonAscii(char rune) charClass {
|
||||||
|
if unicode.IsLower(char) {
|
||||||
|
return charLower
|
||||||
|
} else if unicode.IsUpper(char) {
|
||||||
|
return charUpper
|
||||||
|
} else if unicode.IsNumber(char) {
|
||||||
|
return charNumber
|
||||||
|
} else if unicode.IsLetter(char) {
|
||||||
|
return charLetter
|
||||||
|
}
|
||||||
|
return charNonWord
|
||||||
|
}
|
||||||
|
|
||||||
|
func charClassOf(char rune) charClass {
|
||||||
|
if char <= unicode.MaxASCII {
|
||||||
|
return charClassOfAscii(char)
|
||||||
|
}
|
||||||
|
return charClassOfNonAscii(char)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bonusFor(prevClass charClass, class charClass) int16 {
|
||||||
|
if prevClass == charNonWord && class != charNonWord {
|
||||||
|
// Word boundary
|
||||||
|
return bonusBoundary
|
||||||
|
} else if prevClass == charLower && class == charUpper ||
|
||||||
|
prevClass != charNumber && class == charNumber {
|
||||||
|
// camelCase letter123
|
||||||
|
return bonusCamel123
|
||||||
|
} else if class == charNonWord {
|
||||||
|
return bonusNonWord
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func bonusAt(input util.Chars, idx int) int16 {
|
||||||
|
if idx == 0 {
|
||||||
|
return bonusBoundary
|
||||||
|
}
|
||||||
|
return bonusFor(charClassOf(input.Get(idx-1)), charClassOf(input.Get(idx)))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Algo func(caseSensitive bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)
|
||||||
|
|
||||||
|
func FuzzyMatchV2(caseSensitive bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
|
// Assume that pattern is given in lowercase if case-insensitive.
|
||||||
|
// First check if there's a match and calculate bonus for each position.
|
||||||
|
// If the input string is too long, consider finding the matching chars in
|
||||||
|
// this phase as well (non-optimal alignment).
|
||||||
|
N := input.Length()
|
||||||
|
M := len(pattern)
|
||||||
|
switch M {
|
||||||
|
case 0:
|
||||||
|
return Result{0, 0, 0}, posArray(withPos, M)
|
||||||
|
case 1:
|
||||||
|
return ExactMatchNaive(caseSensitive, forward, input, pattern[0:1], withPos, slab)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since O(nm) algorithm can be prohibitively expensive for large input,
|
||||||
|
// we fall back to the greedy algorithm.
|
||||||
|
if slab != nil && N*M > cap(slab.I16) {
|
||||||
|
return FuzzyMatchV1(caseSensitive, forward, input, pattern, withPos, slab)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reuse pre-allocated integer slice to avoid unnecessary sweeping of garbages
|
||||||
|
offset16 := 0
|
||||||
|
offset32 := 0
|
||||||
|
// Bonus point for each position
|
||||||
|
offset16, B := alloc16(offset16, slab, N, false)
|
||||||
|
// The first occurrence of each character in the pattern
|
||||||
|
offset32, F := alloc32(offset32, slab, M, false)
|
||||||
|
// Rune array
|
||||||
|
offset32, T := alloc32(offset32, slab, N, false)
|
||||||
|
|
||||||
|
// Phase 1. Check if there's a match and calculate bonus for each point
|
||||||
|
pidx, lastIdx, prevClass := 0, 0, charNonWord
|
||||||
|
for idx := 0; idx < N; idx++ {
|
||||||
|
char := input.Get(idx)
|
||||||
|
var class charClass
|
||||||
|
if char <= unicode.MaxASCII {
|
||||||
|
class = charClassOfAscii(char)
|
||||||
|
} else {
|
||||||
|
class = charClassOfNonAscii(char)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !caseSensitive && class == charUpper {
|
||||||
|
if char <= unicode.MaxASCII {
|
||||||
|
char += 32
|
||||||
|
} else {
|
||||||
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
T[idx] = char
|
||||||
|
B[idx] = bonusFor(prevClass, class)
|
||||||
|
prevClass = class
|
||||||
|
|
||||||
|
if pidx < M {
|
||||||
|
if char == pattern[pidx] {
|
||||||
|
lastIdx = idx
|
||||||
|
F[pidx] = int32(idx)
|
||||||
|
pidx++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if char == pattern[M-1] {
|
||||||
|
lastIdx = idx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pidx != M {
|
||||||
|
return Result{-1, -1, 0}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 2. Fill in score matrix (H)
|
||||||
|
// Unlike the original algorithm, we do not allow omission.
|
||||||
|
width := lastIdx - int(F[0]) + 1
|
||||||
|
offset16, H := alloc16(offset16, slab, width*M, false)
|
||||||
|
|
||||||
|
// Possible length of consecutive chunk at each position.
|
||||||
|
offset16, C := alloc16(offset16, slab, width*M, false)
|
||||||
|
|
||||||
|
maxScore, maxScorePos := int16(0), 0
|
||||||
|
for i := 0; i < M; i++ {
|
||||||
|
I := i * width
|
||||||
|
inGap := false
|
||||||
|
for j := int(F[i]); j <= lastIdx; j++ {
|
||||||
|
j0 := j - int(F[0])
|
||||||
|
var s1, s2, consecutive int16
|
||||||
|
|
||||||
|
if j > int(F[i]) {
|
||||||
|
if inGap {
|
||||||
|
s2 = H[I+j0-1] + scoreGapExtention
|
||||||
|
} else {
|
||||||
|
s2 = H[I+j0-1] + scoreGapStart
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if pattern[i] == T[j] {
|
||||||
|
var diag int16
|
||||||
|
if i > 0 && j0 > 0 {
|
||||||
|
diag = H[I-width+j0-1]
|
||||||
|
}
|
||||||
|
s1 = diag + scoreMatch
|
||||||
|
b := B[j]
|
||||||
|
if i > 0 {
|
||||||
|
// j > 0 if i > 0
|
||||||
|
consecutive = C[I-width+j0-1] + 1
|
||||||
|
// Break consecutive chunk
|
||||||
|
if b == bonusBoundary {
|
||||||
|
consecutive = 1
|
||||||
|
} else if consecutive > 1 {
|
||||||
|
b = util.Max16(b, util.Max16(bonusConsecutive, B[j-int(consecutive)+1]))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
consecutive = 1
|
||||||
|
b *= bonusFirstCharMultiplier
|
||||||
|
}
|
||||||
|
if s1+b < s2 {
|
||||||
|
s1 += B[j]
|
||||||
|
consecutive = 0
|
||||||
|
} else {
|
||||||
|
s1 += b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
C[I+j0] = consecutive
|
||||||
|
|
||||||
|
inGap = s1 < s2
|
||||||
|
score := util.Max16(util.Max16(s1, s2), 0)
|
||||||
|
if i == M-1 && (forward && score > maxScore || !forward && score >= maxScore) {
|
||||||
|
maxScore, maxScorePos = score, j
|
||||||
|
}
|
||||||
|
H[I+j0] = score
|
||||||
|
}
|
||||||
|
|
||||||
|
if DEBUG {
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Print(" ")
|
||||||
|
for j := int(F[i]); j <= lastIdx; j++ {
|
||||||
|
fmt.Printf(" " + string(input.Get(j)) + " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
fmt.Print(string(pattern[i]) + " ")
|
||||||
|
for idx := int(F[0]); idx < int(F[i]); idx++ {
|
||||||
|
fmt.Print(" 0 ")
|
||||||
|
}
|
||||||
|
for idx := int(F[i]); idx <= lastIdx; idx++ {
|
||||||
|
fmt.Printf("%2d ", H[i*width+idx-int(F[0])])
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Print(" ")
|
||||||
|
for idx, p := range C[I : I+width] {
|
||||||
|
if idx+int(F[0]) < int(F[i]) {
|
||||||
|
p = 0
|
||||||
|
}
|
||||||
|
fmt.Printf("%2d ", p)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 3. (Optional) Backtrace to find character positions
|
||||||
|
pos := posArray(withPos, M)
|
||||||
|
j := int(F[0])
|
||||||
|
if withPos {
|
||||||
|
i := M - 1
|
||||||
|
j = maxScorePos
|
||||||
|
preferMatch := true
|
||||||
|
for {
|
||||||
|
I := i * width
|
||||||
|
j0 := j - int(F[0])
|
||||||
|
s := H[I+j0]
|
||||||
|
|
||||||
|
var s1, s2 int16
|
||||||
|
if i > 0 && j >= int(F[i]) {
|
||||||
|
s1 = H[I-width+j0-1]
|
||||||
|
}
|
||||||
|
if j > int(F[i]) {
|
||||||
|
s2 = H[I+j0-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if s > s1 && (s > s2 || s == s2 && preferMatch) {
|
||||||
|
*pos = append(*pos, j)
|
||||||
|
if i == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
preferMatch = C[I+j0] > 1 || I+width+j0+1 < len(C) && C[I+width+j0+1] > 0
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Start offset we return here is only relevant when begin tiebreak is used.
|
||||||
|
// However finding the accurate offset requires backtracking, and we don't
|
||||||
|
// want to pay extra cost for the option that has lost its importance.
|
||||||
|
return Result{j, maxScorePos + 1, int(maxScore)}, pos
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implement the same sorting criteria as V2
|
||||||
|
func calculateScore(caseSensitive bool, text util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) {
|
||||||
|
pidx, score, inGap, consecutive, firstBonus := 0, 0, false, 0, int16(0)
|
||||||
|
pos := posArray(withPos, len(pattern))
|
||||||
|
prevClass := charNonWord
|
||||||
|
if sidx > 0 {
|
||||||
|
prevClass = charClassOf(text.Get(sidx - 1))
|
||||||
|
}
|
||||||
|
for idx := sidx; idx < eidx; idx++ {
|
||||||
|
char := text.Get(idx)
|
||||||
|
class := charClassOf(char)
|
||||||
|
if !caseSensitive {
|
||||||
|
if char >= 'A' && char <= 'Z' {
|
||||||
|
char += 32
|
||||||
|
} else if char > unicode.MaxASCII {
|
||||||
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if char == pattern[pidx] {
|
||||||
|
if withPos {
|
||||||
|
*pos = append(*pos, idx)
|
||||||
|
}
|
||||||
|
score += scoreMatch
|
||||||
|
bonus := bonusFor(prevClass, class)
|
||||||
|
if consecutive == 0 {
|
||||||
|
firstBonus = bonus
|
||||||
|
} else {
|
||||||
|
// Break consecutive chunk
|
||||||
|
if bonus == bonusBoundary {
|
||||||
|
firstBonus = bonus
|
||||||
|
}
|
||||||
|
bonus = util.Max16(util.Max16(bonus, firstBonus), bonusConsecutive)
|
||||||
|
}
|
||||||
|
if pidx == 0 {
|
||||||
|
score += int(bonus * bonusFirstCharMultiplier)
|
||||||
|
} else {
|
||||||
|
score += int(bonus)
|
||||||
|
}
|
||||||
|
inGap = false
|
||||||
|
consecutive++
|
||||||
|
pidx++
|
||||||
|
} else {
|
||||||
|
if inGap {
|
||||||
|
score += scoreGapExtention
|
||||||
|
} else {
|
||||||
|
score += scoreGapStart
|
||||||
|
}
|
||||||
|
inGap = true
|
||||||
|
consecutive = 0
|
||||||
|
firstBonus = 0
|
||||||
|
}
|
||||||
|
prevClass = class
|
||||||
|
}
|
||||||
|
return score, pos
|
||||||
|
}
|
||||||
|
|
||||||
|
// FuzzyMatchV1 performs fuzzy-match
|
||||||
|
func FuzzyMatchV1(caseSensitive bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
|
if len(pattern) == 0 {
|
||||||
|
return Result{0, 0, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
pidx := 0
|
||||||
sidx := -1
|
sidx := -1
|
||||||
eidx := -1
|
eidx := -1
|
||||||
|
|
||||||
lenRunes := len(runes)
|
lenRunes := text.Length()
|
||||||
lenPattern := len(pattern)
|
lenPattern := len(pattern)
|
||||||
|
|
||||||
for index := range runes {
|
for index := 0; index < lenRunes; index++ {
|
||||||
char := runeAt(runes, index, lenRunes, forward)
|
char := text.Get(indexAt(index, lenRunes, forward))
|
||||||
// This is considerably faster than blindly applying strings.ToLower to the
|
// This is considerably faster than blindly applying strings.ToLower to the
|
||||||
// whole string
|
// whole string
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
@@ -58,7 +514,7 @@ func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune)
|
|||||||
char = unicode.To(unicode.LowerCase, char)
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
pchar := pattern[indexAt(pidx, lenPattern, forward)]
|
||||||
if char == pchar {
|
if char == pchar {
|
||||||
if sidx < 0 {
|
if sidx < 0 {
|
||||||
sidx = index
|
sidx = index
|
||||||
@@ -73,7 +529,8 @@ func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune)
|
|||||||
if sidx >= 0 && eidx >= 0 {
|
if sidx >= 0 && eidx >= 0 {
|
||||||
pidx--
|
pidx--
|
||||||
for index := eidx - 1; index >= sidx; index-- {
|
for index := eidx - 1; index >= sidx; index-- {
|
||||||
char := runeAt(runes, index, lenRunes, forward)
|
tidx := indexAt(index, lenRunes, forward)
|
||||||
|
char := text.Get(tidx)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
if char >= 'A' && char <= 'Z' {
|
if char >= 'A' && char <= 'Z' {
|
||||||
char += 32
|
char += 32
|
||||||
@@ -82,7 +539,8 @@ func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
pidx_ := indexAt(pidx, lenPattern, forward)
|
||||||
|
pchar := pattern[pidx_]
|
||||||
if char == pchar {
|
if char == pchar {
|
||||||
if pidx--; pidx < 0 {
|
if pidx--; pidx < 0 {
|
||||||
sidx = index
|
sidx = index
|
||||||
@@ -90,12 +548,15 @@ func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if forward {
|
|
||||||
return sidx, eidx
|
if !forward {
|
||||||
|
sidx, eidx = lenRunes-eidx, lenRunes-sidx
|
||||||
}
|
}
|
||||||
return lenRunes - eidx, lenRunes - sidx
|
|
||||||
|
score, pos := calculateScore(caseSensitive, text, pattern, sidx, eidx, withPos)
|
||||||
|
return Result{sidx, eidx, score}, pos
|
||||||
}
|
}
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExactMatchNaive is a basic string searching algorithm that handles case
|
// ExactMatchNaive is a basic string searching algorithm that handles case
|
||||||
@@ -103,23 +564,28 @@ func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune)
|
|||||||
// of strings.ToLower + strings.Index for typical fzf use cases where input
|
// of strings.ToLower + strings.Index for typical fzf use cases where input
|
||||||
// strings and patterns are not very long.
|
// strings and patterns are not very long.
|
||||||
//
|
//
|
||||||
// We might try to implement better algorithms in the future:
|
// Since 0.15.0, this function searches for the match with the highest
|
||||||
// http://en.wikipedia.org/wiki/String_searching_algorithm
|
// bonus point, instead of stopping immediately after finding the first match.
|
||||||
func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
// The solution is much cheaper since there is only one possible alignment of
|
||||||
|
// the pattern.
|
||||||
|
func ExactMatchNaive(caseSensitive bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
if len(pattern) == 0 {
|
if len(pattern) == 0 {
|
||||||
return 0, 0
|
return Result{0, 0, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lenRunes := len(runes)
|
lenRunes := text.Length()
|
||||||
lenPattern := len(pattern)
|
lenPattern := len(pattern)
|
||||||
|
|
||||||
if lenRunes < lenPattern {
|
if lenRunes < lenPattern {
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For simplicity, only look at the bonus at the first character position
|
||||||
pidx := 0
|
pidx := 0
|
||||||
|
bestPos, bonus, bestBonus := -1, int16(0), int16(-1)
|
||||||
for index := 0; index < lenRunes; index++ {
|
for index := 0; index < lenRunes; index++ {
|
||||||
char := runeAt(runes, index, lenRunes, forward)
|
index_ := indexAt(index, lenRunes, forward)
|
||||||
|
char := text.Get(index_)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
if char >= 'A' && char <= 'Z' {
|
if char >= 'A' && char <= 'Z' {
|
||||||
char += 32
|
char += 32
|
||||||
@@ -127,73 +593,108 @@ func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []r
|
|||||||
char = unicode.To(unicode.LowerCase, char)
|
char = unicode.To(unicode.LowerCase, char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pchar := runeAt(pattern, pidx, lenPattern, forward)
|
pidx_ := indexAt(pidx, lenPattern, forward)
|
||||||
|
pchar := pattern[pidx_]
|
||||||
if pchar == char {
|
if pchar == char {
|
||||||
|
if pidx_ == 0 {
|
||||||
|
bonus = bonusAt(text, index_)
|
||||||
|
}
|
||||||
pidx++
|
pidx++
|
||||||
if pidx == lenPattern {
|
if pidx == lenPattern {
|
||||||
if forward {
|
if bonus > bestBonus {
|
||||||
return index - lenPattern + 1, index + 1
|
bestPos, bestBonus = index, bonus
|
||||||
}
|
}
|
||||||
return lenRunes - (index + 1), lenRunes - (index - lenPattern + 1)
|
if bonus == bonusBoundary {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
index -= pidx - 1
|
||||||
|
pidx, bonus = 0, 0
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
index -= pidx
|
index -= pidx
|
||||||
pidx = 0
|
pidx, bonus = 0, 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1, -1
|
if bestPos >= 0 {
|
||||||
|
var sidx, eidx int
|
||||||
|
if forward {
|
||||||
|
sidx = bestPos - lenPattern + 1
|
||||||
|
eidx = bestPos + 1
|
||||||
|
} else {
|
||||||
|
sidx = lenRunes - (bestPos + 1)
|
||||||
|
eidx = lenRunes - (bestPos - lenPattern + 1)
|
||||||
|
}
|
||||||
|
score, _ := calculateScore(caseSensitive, text, pattern, sidx, eidx, false)
|
||||||
|
return Result{sidx, eidx, score}, nil
|
||||||
|
}
|
||||||
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrefixMatch performs prefix-match
|
// PrefixMatch performs prefix-match
|
||||||
func PrefixMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
func PrefixMatch(caseSensitive bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
if len(runes) < len(pattern) {
|
if len(pattern) == 0 {
|
||||||
return -1, -1
|
return Result{0, 0, 0}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if text.Length() < len(pattern) {
|
||||||
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, r := range pattern {
|
for index, r := range pattern {
|
||||||
char := runes[index]
|
char := text.Get(index)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
char = unicode.ToLower(char)
|
char = unicode.ToLower(char)
|
||||||
}
|
}
|
||||||
if char != r {
|
if char != r {
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0, len(pattern)
|
lenPattern := len(pattern)
|
||||||
|
score, _ := calculateScore(caseSensitive, text, pattern, 0, lenPattern, false)
|
||||||
|
return Result{0, lenPattern, score}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SuffixMatch performs suffix-match
|
// SuffixMatch performs suffix-match
|
||||||
func SuffixMatch(caseSensitive bool, forward bool, input []rune, pattern []rune) (int, int) {
|
func SuffixMatch(caseSensitive bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
runes := util.TrimRight(input)
|
lenRunes := text.Length()
|
||||||
trimmedLen := len(runes)
|
trimmedLen := lenRunes - text.TrailingWhitespaces()
|
||||||
|
if len(pattern) == 0 {
|
||||||
|
return Result{trimmedLen, trimmedLen, 0}, nil
|
||||||
|
}
|
||||||
diff := trimmedLen - len(pattern)
|
diff := trimmedLen - len(pattern)
|
||||||
if diff < 0 {
|
if diff < 0 {
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, r := range pattern {
|
for index, r := range pattern {
|
||||||
char := runes[index+diff]
|
char := text.Get(index + diff)
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
char = unicode.ToLower(char)
|
char = unicode.ToLower(char)
|
||||||
}
|
}
|
||||||
if char != r {
|
if char != r {
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return trimmedLen - len(pattern), trimmedLen
|
lenPattern := len(pattern)
|
||||||
|
sidx := trimmedLen - lenPattern
|
||||||
|
eidx := trimmedLen
|
||||||
|
score, _ := calculateScore(caseSensitive, text, pattern, sidx, eidx, false)
|
||||||
|
return Result{sidx, eidx, score}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EqualMatch performs equal-match
|
// EqualMatch performs equal-match
|
||||||
func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {
|
func EqualMatch(caseSensitive bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {
|
||||||
if len(runes) != len(pattern) {
|
lenPattern := len(pattern)
|
||||||
return -1, -1
|
if text.Length() != lenPattern {
|
||||||
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
runesStr := string(runes)
|
runesStr := text.ToString()
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
runesStr = strings.ToLower(runesStr)
|
runesStr = strings.ToLower(runesStr)
|
||||||
}
|
}
|
||||||
if runesStr == string(pattern) {
|
if runesStr == string(pattern) {
|
||||||
return 0, len(pattern)
|
return Result{0, lenPattern, (scoreMatch+bonusBoundary)*lenPattern +
|
||||||
|
(bonusFirstCharMultiplier-1)*bonusBoundary}, nil
|
||||||
}
|
}
|
||||||
return -1, -1
|
return Result{-1, -1, 0}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,69 +1,166 @@
|
|||||||
package algo
|
package algo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assertMatch(t *testing.T, fun func(bool, bool, []rune, []rune) (int, int), caseSensitive bool, forward bool, input string, pattern string, sidx int, eidx int) {
|
func assertMatch(t *testing.T, fun Algo, caseSensitive, forward bool, input, pattern string, sidx int, eidx int, score int) {
|
||||||
if !caseSensitive {
|
if !caseSensitive {
|
||||||
pattern = strings.ToLower(pattern)
|
pattern = strings.ToLower(pattern)
|
||||||
}
|
}
|
||||||
s, e := fun(caseSensitive, forward, []rune(input), []rune(pattern))
|
res, pos := fun(caseSensitive, forward, util.RunesToChars([]rune(input)), []rune(pattern), true, nil)
|
||||||
if s != sidx {
|
var start, end int
|
||||||
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
|
if pos == nil || len(*pos) == 0 {
|
||||||
|
start = res.Start
|
||||||
|
end = res.End
|
||||||
|
} else {
|
||||||
|
sort.Ints(*pos)
|
||||||
|
start = (*pos)[0]
|
||||||
|
end = (*pos)[len(*pos)-1] + 1
|
||||||
}
|
}
|
||||||
if e != eidx {
|
if start != sidx {
|
||||||
t.Errorf("Invalid end index: %d (expected: %d, %s / %s)", e, eidx, input, pattern)
|
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", start, sidx, input, pattern)
|
||||||
|
}
|
||||||
|
if end != eidx {
|
||||||
|
t.Errorf("Invalid end index: %d (expected: %d, %s / %s)", end, eidx, input, pattern)
|
||||||
|
}
|
||||||
|
if res.Score != score {
|
||||||
|
t.Errorf("Invalid score: %d (expected: %d, %s / %s)", res.Score, score, input, pattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFuzzyMatch(t *testing.T) {
|
func TestFuzzyMatch(t *testing.T) {
|
||||||
assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9)
|
for _, fn := range []Algo{FuzzyMatchV1, FuzzyMatchV2} {
|
||||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1)
|
for _, forward := range []bool{true, false} {
|
||||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9)
|
assertMatch(t, fn, false, forward, "fooBarbaz1", "oBZ", 2, 9,
|
||||||
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "fooBarbazz", -1, -1)
|
scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtention*3)
|
||||||
|
assertMatch(t, fn, false, forward, "foo bar baz", "fbb", 0, 9,
|
||||||
|
scoreMatch*3+bonusBoundary*bonusFirstCharMultiplier+
|
||||||
|
bonusBoundary*2+2*scoreGapStart+4*scoreGapExtention)
|
||||||
|
assertMatch(t, fn, false, forward, "/AutomatorDocument.icns", "rdoc", 9, 13,
|
||||||
|
scoreMatch*4+bonusCamel123+bonusConsecutive*2)
|
||||||
|
assertMatch(t, fn, false, forward, "/man1/zshcompctl.1", "zshc", 6, 10,
|
||||||
|
scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*3)
|
||||||
|
assertMatch(t, fn, false, forward, "/.oh-my-zsh/cache", "zshc", 8, 13,
|
||||||
|
scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*3+scoreGapStart)
|
||||||
|
assertMatch(t, fn, false, forward, "ab0123 456", "12356", 3, 10,
|
||||||
|
scoreMatch*5+bonusConsecutive*3+scoreGapStart+scoreGapExtention)
|
||||||
|
assertMatch(t, fn, false, forward, "abc123 456", "12356", 3, 10,
|
||||||
|
scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+bonusConsecutive+scoreGapStart+scoreGapExtention)
|
||||||
|
assertMatch(t, fn, false, forward, "foo/bar/baz", "fbb", 0, 9,
|
||||||
|
scoreMatch*3+bonusBoundary*bonusFirstCharMultiplier+
|
||||||
|
bonusBoundary*2+2*scoreGapStart+4*scoreGapExtention)
|
||||||
|
assertMatch(t, fn, false, forward, "fooBarBaz", "fbb", 0, 7,
|
||||||
|
scoreMatch*3+bonusBoundary*bonusFirstCharMultiplier+
|
||||||
|
bonusCamel123*2+2*scoreGapStart+2*scoreGapExtention)
|
||||||
|
assertMatch(t, fn, false, forward, "foo barbaz", "fbb", 0, 8,
|
||||||
|
scoreMatch*3+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary+
|
||||||
|
scoreGapStart*2+scoreGapExtention*3)
|
||||||
|
assertMatch(t, fn, false, forward, "fooBar Baz", "foob", 0, 4,
|
||||||
|
scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*3)
|
||||||
|
assertMatch(t, fn, false, forward, "xFoo-Bar Baz", "foo-b", 1, 6,
|
||||||
|
scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+
|
||||||
|
bonusNonWord+bonusBoundary)
|
||||||
|
|
||||||
|
assertMatch(t, fn, true, forward, "fooBarbaz", "oBz", 2, 9,
|
||||||
|
scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtention*3)
|
||||||
|
assertMatch(t, fn, true, forward, "Foo/Bar/Baz", "FBB", 0, 9,
|
||||||
|
scoreMatch*3+bonusBoundary*(bonusFirstCharMultiplier+2)+
|
||||||
|
scoreGapStart*2+scoreGapExtention*4)
|
||||||
|
assertMatch(t, fn, true, forward, "FooBarBaz", "FBB", 0, 7,
|
||||||
|
scoreMatch*3+bonusBoundary*bonusFirstCharMultiplier+bonusCamel123*2+
|
||||||
|
scoreGapStart*2+scoreGapExtention*2)
|
||||||
|
assertMatch(t, fn, true, forward, "FooBar Baz", "FooB", 0, 4,
|
||||||
|
scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*2+
|
||||||
|
util.Max(bonusCamel123, bonusBoundary))
|
||||||
|
|
||||||
|
// Consecutive bonus updated
|
||||||
|
assertMatch(t, fn, true, forward, "foo-bar", "o-ba", 2, 6,
|
||||||
|
scoreMatch*4+bonusBoundary*3)
|
||||||
|
|
||||||
|
// Non-match
|
||||||
|
assertMatch(t, fn, true, forward, "fooBarbaz", "oBZ", -1, -1, 0)
|
||||||
|
assertMatch(t, fn, true, forward, "Foo Bar Baz", "fbb", -1, -1, 0)
|
||||||
|
assertMatch(t, fn, true, forward, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFuzzyMatchBackward(t *testing.T) {
|
func TestFuzzyMatchBackward(t *testing.T) {
|
||||||
assertMatch(t, FuzzyMatch, false, true, "foobar fb", "fb", 0, 4)
|
assertMatch(t, FuzzyMatchV1, false, true, "foobar fb", "fb", 0, 4,
|
||||||
assertMatch(t, FuzzyMatch, false, false, "foobar fb", "fb", 7, 9)
|
scoreMatch*2+bonusBoundary*bonusFirstCharMultiplier+
|
||||||
|
scoreGapStart+scoreGapExtention)
|
||||||
|
assertMatch(t, FuzzyMatchV1, false, false, "foobar fb", "fb", 7, 9,
|
||||||
|
scoreMatch*2+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactMatchNaive(t *testing.T) {
|
func TestExactMatchNaive(t *testing.T) {
|
||||||
for _, dir := range []bool{true, false} {
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5)
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1, 0)
|
||||||
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1)
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
||||||
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1)
|
|
||||||
|
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5,
|
||||||
|
scoreMatch*3+bonusCamel123+bonusConsecutive)
|
||||||
|
assertMatch(t, ExactMatchNaive, false, dir, "/AutomatorDocument.icns", "rdoc", 9, 13,
|
||||||
|
scoreMatch*4+bonusCamel123+bonusConsecutive*2)
|
||||||
|
assertMatch(t, ExactMatchNaive, false, dir, "/man1/zshcompctl.1", "zshc", 6, 10,
|
||||||
|
scoreMatch*4+bonusBoundary*(bonusFirstCharMultiplier+3))
|
||||||
|
assertMatch(t, ExactMatchNaive, false, dir, "/.oh-my-zsh/cache", "zsh/c", 8, 13,
|
||||||
|
scoreMatch*5+bonusBoundary*(bonusFirstCharMultiplier+4))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactMatchNaiveBackward(t *testing.T) {
|
func TestExactMatchNaiveBackward(t *testing.T) {
|
||||||
assertMatch(t, FuzzyMatch, false, true, "foobar foob", "oo", 1, 3)
|
assertMatch(t, ExactMatchNaive, false, true, "foobar foob", "oo", 1, 3,
|
||||||
assertMatch(t, FuzzyMatch, false, false, "foobar foob", "oo", 8, 10)
|
scoreMatch*2+bonusConsecutive)
|
||||||
|
assertMatch(t, ExactMatchNaive, false, false, "foobar foob", "oo", 8, 10,
|
||||||
|
scoreMatch*2+bonusConsecutive)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrefixMatch(t *testing.T) {
|
func TestPrefixMatch(t *testing.T) {
|
||||||
|
score := (scoreMatch+bonusBoundary)*3 + bonusBoundary*(bonusFirstCharMultiplier-1)
|
||||||
|
|
||||||
for _, dir := range []bool{true, false} {
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3)
|
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
||||||
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1)
|
assertMatch(t, PrefixMatch, false, dir, "fooBarBaz", "baz", -1, -1, 0)
|
||||||
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1)
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, score)
|
||||||
|
assertMatch(t, PrefixMatch, false, dir, "foOBarBaZ", "foo", 0, 3, score)
|
||||||
|
assertMatch(t, PrefixMatch, false, dir, "f-oBarbaz", "f-o", 0, 3, score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSuffixMatch(t *testing.T) {
|
func TestSuffixMatch(t *testing.T) {
|
||||||
for _, dir := range []bool{true, false} {
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1)
|
assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1, 0)
|
||||||
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9)
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
||||||
assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1)
|
|
||||||
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9,
|
||||||
|
scoreMatch*3+bonusConsecutive*2)
|
||||||
|
assertMatch(t, SuffixMatch, false, dir, "fooBarBaZ", "baz", 6, 9,
|
||||||
|
(scoreMatch+bonusCamel123)*3+bonusCamel123*(bonusFirstCharMultiplier-1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyPattern(t *testing.T) {
|
func TestEmptyPattern(t *testing.T) {
|
||||||
for _, dir := range []bool{true, false} {
|
for _, dir := range []bool{true, false} {
|
||||||
assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0)
|
assertMatch(t, FuzzyMatchV1, true, dir, "foobar", "", 0, 0, 0)
|
||||||
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0)
|
assertMatch(t, FuzzyMatchV2, true, dir, "foobar", "", 0, 0, 0)
|
||||||
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0)
|
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0, 0)
|
||||||
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6)
|
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0, 0)
|
||||||
|
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLongString(t *testing.T) {
|
||||||
|
bytes := make([]byte, math.MaxUint16*2)
|
||||||
|
for i := range bytes {
|
||||||
|
bytes[i] = 'x'
|
||||||
|
}
|
||||||
|
bytes[math.MaxUint16] = 'z'
|
||||||
|
assertMatch(t, FuzzyMatchV2, true, true, string(bytes), "zx", math.MaxUint16, math.MaxUint16+2, scoreMatch*2+bonusConsecutive)
|
||||||
|
}
|
||||||
|
|||||||
67
src/ansi.go
67
src/ansi.go
@@ -6,6 +6,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/tui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ansiOffset struct {
|
type ansiOffset struct {
|
||||||
@@ -14,29 +16,38 @@ type ansiOffset struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ansiState struct {
|
type ansiState struct {
|
||||||
fg int
|
fg tui.Color
|
||||||
bg int
|
bg tui.Color
|
||||||
bold bool
|
attr tui.Attr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ansiState) colored() bool {
|
func (s *ansiState) colored() bool {
|
||||||
return s.fg != -1 || s.bg != -1 || s.bold
|
return s.fg != -1 || s.bg != -1 || s.attr > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ansiState) equals(t *ansiState) bool {
|
func (s *ansiState) equals(t *ansiState) bool {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return !s.colored()
|
return !s.colored()
|
||||||
}
|
}
|
||||||
return s.fg == t.fg && s.bg == t.bg && s.bold == t.bold
|
return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr
|
||||||
}
|
}
|
||||||
|
|
||||||
var ansiRegex *regexp.Regexp
|
var ansiRegex *regexp.Regexp
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")
|
/*
|
||||||
|
References:
|
||||||
|
- https://github.com/gnachman/iTerm2
|
||||||
|
- http://ascii-table.com/ansi-escape-sequences.php
|
||||||
|
- http://ascii-table.com/ansi-escape-sequences-vt-100.php
|
||||||
|
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
|
||||||
|
*/
|
||||||
|
// The following regular expression will include not all but most of the
|
||||||
|
// frequently used ANSI sequences
|
||||||
|
ansiRegex = regexp.MustCompile("\x1b[\\[()][0-9;]*[a-zA-Z@]|\x1b.|[\x08\x0e\x0f]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiState) {
|
func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) {
|
||||||
var offsets []ansiOffset
|
var offsets []ansiOffset
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
|
|
||||||
@@ -46,7 +57,11 @@ func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiStat
|
|||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {
|
for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {
|
||||||
output.WriteString(str[idx:offset[0]])
|
prev := str[idx:offset[0]]
|
||||||
|
output.WriteString(prev)
|
||||||
|
if proc != nil && !proc(prev, state) {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
newState := interpretCode(str[offset[0]:offset[1]], state)
|
newState := interpretCode(str[offset[0]:offset[1]], state)
|
||||||
|
|
||||||
if !newState.equals(state) {
|
if !newState.equals(state) {
|
||||||
@@ -77,18 +92,24 @@ func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiStat
|
|||||||
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output.String(), offsets, state
|
if proc != nil {
|
||||||
|
proc(rest, state)
|
||||||
|
}
|
||||||
|
if len(offsets) == 0 {
|
||||||
|
return output.String(), nil, state
|
||||||
|
}
|
||||||
|
return output.String(), &offsets, state
|
||||||
}
|
}
|
||||||
|
|
||||||
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
||||||
// State
|
// State
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
if prevState == nil {
|
if prevState == nil {
|
||||||
state = &ansiState{-1, -1, false}
|
state = &ansiState{-1, -1, 0}
|
||||||
} else {
|
} else {
|
||||||
state = &ansiState{prevState.fg, prevState.bg, prevState.bold}
|
state = &ansiState{prevState.fg, prevState.bg, prevState.attr}
|
||||||
}
|
}
|
||||||
if ansiCode[len(ansiCode)-1] == 'K' {
|
if ansiCode[0] != '\x1b' || ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +119,7 @@ func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
|||||||
init := func() {
|
init := func() {
|
||||||
state.fg = -1
|
state.fg = -1
|
||||||
state.bg = -1
|
state.bg = -1
|
||||||
state.bold = false
|
state.attr = 0
|
||||||
state256 = 0
|
state256 = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,14 +143,26 @@ func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
|||||||
case 49:
|
case 49:
|
||||||
state.bg = -1
|
state.bg = -1
|
||||||
case 1:
|
case 1:
|
||||||
state.bold = true
|
state.attr = tui.Bold
|
||||||
|
case 2:
|
||||||
|
state.attr = tui.Dim
|
||||||
|
case 4:
|
||||||
|
state.attr = tui.Underline
|
||||||
|
case 5:
|
||||||
|
state.attr = tui.Blink
|
||||||
|
case 7:
|
||||||
|
state.attr = tui.Reverse
|
||||||
case 0:
|
case 0:
|
||||||
init()
|
init()
|
||||||
default:
|
default:
|
||||||
if num >= 30 && num <= 37 {
|
if num >= 30 && num <= 37 {
|
||||||
state.fg = num - 30
|
state.fg = tui.Color(num - 30)
|
||||||
} else if num >= 40 && num <= 47 {
|
} else if num >= 40 && num <= 47 {
|
||||||
state.bg = num - 40
|
state.bg = tui.Color(num - 40)
|
||||||
|
} else if num >= 90 && num <= 97 {
|
||||||
|
state.fg = tui.Color(num - 90 + 8)
|
||||||
|
} else if num >= 100 && num <= 107 {
|
||||||
|
state.bg = tui.Color(num - 100 + 8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
@@ -140,7 +173,7 @@ func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
|||||||
state256 = 0
|
state256 = 0
|
||||||
}
|
}
|
||||||
case 2:
|
case 2:
|
||||||
*ptr = num
|
*ptr = tui.Color(num)
|
||||||
state256 = 0
|
state256 = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
108
src/ansi_test.go
108
src/ansi_test.go
@@ -3,150 +3,156 @@ package fzf
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/tui"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExtractColor(t *testing.T) {
|
func TestExtractColor(t *testing.T) {
|
||||||
assert := func(offset ansiOffset, b int32, e int32, fg int, bg int, bold bool) {
|
assert := func(offset ansiOffset, b int32, e int32, fg tui.Color, bg tui.Color, bold bool) {
|
||||||
|
var attr tui.Attr
|
||||||
|
if bold {
|
||||||
|
attr = tui.Bold
|
||||||
|
}
|
||||||
if offset.offset[0] != b || offset.offset[1] != e ||
|
if offset.offset[0] != b || offset.offset[1] != e ||
|
||||||
offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold {
|
offset.color.fg != fg || offset.color.bg != bg || offset.color.attr != attr {
|
||||||
t.Error(offset, b, e, fg, bg, bold)
|
t.Error(offset, b, e, fg, bg, attr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
src := "hello world"
|
src := "hello world"
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
clean := "\x1b[0m"
|
clean := "\x1b[0m"
|
||||||
check := func(assertion func(ansiOffsets []ansiOffset, state *ansiState)) {
|
check := func(assertion func(ansiOffsets *[]ansiOffset, state *ansiState)) {
|
||||||
output, ansiOffsets, newState := extractColor(src, state)
|
output, ansiOffsets, newState := extractColor(src, state, nil)
|
||||||
state = newState
|
state = newState
|
||||||
if output != "hello world" {
|
if output != "hello world" {
|
||||||
t.Errorf("Invalid output: {}", output)
|
t.Errorf("Invalid output: %s %s", output, []rune(output))
|
||||||
}
|
}
|
||||||
fmt.Println(src, ansiOffsets, clean)
|
fmt.Println(src, ansiOffsets, clean)
|
||||||
assertion(ansiOffsets, state)
|
assertion(ansiOffsets, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) > 0 {
|
if offsets != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "\x1b[0mhello world"
|
src = "\x1b[0mhello world"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) > 0 {
|
if offsets != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "\x1b[1mhello world"
|
src = "\x1b[1mhello world"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 0, 11, -1, -1, true)
|
assert((*offsets)[0], 0, 11, -1, -1, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "\x1b[1mhello \x1b[mworld"
|
src = "\x1b[1mhello \x1b[mw\x1b7o\x1b8r\x1b(Bl\x1b[2@d"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 0, 6, -1, -1, true)
|
assert((*offsets)[0], 0, 6, -1, -1, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "\x1b[1mhello \x1b[Kworld"
|
src = "\x1b[1mhello \x1b[Kworld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 0, 11, -1, -1, true)
|
assert((*offsets)[0], 0, 11, -1, -1, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "hello \x1b[34;45;1mworld"
|
src = "hello \x1b[34;45;1mworld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 11, 4, 5, true)
|
assert((*offsets)[0], 6, 11, 4, 5, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
|
src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 11, 4, 5, true)
|
assert((*offsets)[0], 6, 11, 4, 5, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "hello \x1b[34;45;1mwor\x1b[0mld"
|
src = "hello \x1b[34;45;1mwor\x1b[0mld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 9, 4, 5, true)
|
assert((*offsets)[0], 6, 9, 4, 5, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
state = nil
|
state = nil
|
||||||
src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
|
src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 3 {
|
if len(*offsets) != 3 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 8, 4, 233, true)
|
assert((*offsets)[0], 6, 8, 4, 233, true)
|
||||||
assert(offsets[1], 8, 9, 161, 233, true)
|
assert((*offsets)[1], 8, 9, 161, 233, true)
|
||||||
assert(offsets[2], 10, 11, 161, -1, false)
|
assert((*offsets)[2], 10, 11, 161, -1, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
// {38,48};5;{38,48}
|
// {38,48};5;{38,48}
|
||||||
state = nil
|
state = nil
|
||||||
src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
|
src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 2 {
|
if len(*offsets) != 2 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 9, 38, 48, true)
|
assert((*offsets)[0], 6, 9, 38, 48, true)
|
||||||
assert(offsets[1], 9, 10, 48, 38, true)
|
assert((*offsets)[1], 9, 10, 48, 38, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[32;1mworld"
|
src = "hello \x1b[32;1mworld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if state.fg != 2 || state.bg != -1 || !state.bold {
|
if state.fg != 2 || state.bg != -1 || state.attr == 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 6, 11, 2, -1, true)
|
assert((*offsets)[0], 6, 11, 2, -1, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
src = "hello world"
|
src = "hello world"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 1 {
|
if len(*offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if state.fg != 2 || state.bg != -1 || !state.bold {
|
if state.fg != 2 || state.bg != -1 || state.attr == 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 0, 11, 2, -1, true)
|
assert((*offsets)[0], 0, 11, 2, -1, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[0;38;5;200;48;5;100mworld"
|
src = "hello \x1b[0;38;5;200;48;5;100mworld"
|
||||||
check(func(offsets []ansiOffset, state *ansiState) {
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
||||||
if len(offsets) != 2 {
|
if len(*offsets) != 2 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if state.fg != 200 || state.bg != 100 || state.bold {
|
if state.fg != 200 || state.bg != 100 || state.attr > 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
assert(offsets[0], 0, 6, 2, -1, true)
|
assert((*offsets)[0], 0, 6, 2, -1, true)
|
||||||
assert(offsets[1], 6, 11, 200, 100, false)
|
assert((*offsets)[1], 6, 11, 200, 100, false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package fzf
|
|||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
// queryCache associates strings to lists of items
|
// queryCache associates strings to lists of items
|
||||||
type queryCache map[string][]*Item
|
type queryCache map[string][]*Result
|
||||||
|
|
||||||
// ChunkCache associates Chunk and query string to lists of items
|
// ChunkCache associates Chunk and query string to lists of items
|
||||||
type ChunkCache struct {
|
type ChunkCache struct {
|
||||||
@@ -17,7 +17,7 @@ func NewChunkCache() ChunkCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add adds the list to the cache
|
// Add adds the list to the cache
|
||||||
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) {
|
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Result) {
|
||||||
if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {
|
if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find is called to lookup ChunkCache
|
// Find is called to lookup ChunkCache
|
||||||
func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Item, bool) {
|
func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) {
|
||||||
if len(key) == 0 || !chunk.IsFull() {
|
if len(key) == 0 || !chunk.IsFull() {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ func TestChunkCache(t *testing.T) {
|
|||||||
chunk2 := make(Chunk, chunkSize)
|
chunk2 := make(Chunk, chunkSize)
|
||||||
chunk1p := &Chunk{}
|
chunk1p := &Chunk{}
|
||||||
chunk2p := &chunk2
|
chunk2p := &chunk2
|
||||||
items1 := []*Item{&Item{}}
|
items1 := []*Result{&Result{}}
|
||||||
items2 := []*Item{&Item{}, &Item{}}
|
items2 := []*Result{&Result{}, &Result{}}
|
||||||
cache.Add(chunk1p, "foo", items1)
|
cache.Add(chunk1p, "foo", items1)
|
||||||
cache.Add(chunk2p, "foo", items1)
|
cache.Add(chunk2p, "foo", items1)
|
||||||
cache.Add(chunk2p, "bar", items2)
|
cache.Add(chunk2p, "bar", items2)
|
||||||
|
|||||||
@@ -3,11 +3,16 @@ package fzf
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestChunkList(t *testing.T) {
|
func TestChunkList(t *testing.T) {
|
||||||
|
// FIXME global
|
||||||
|
sortCriteria = []criterion{byScore, byLength}
|
||||||
|
|
||||||
cl := NewChunkList(func(s []byte, i int) *Item {
|
cl := NewChunkList(func(s []byte, i int) *Item {
|
||||||
return &Item{text: []rune(string(s)), rank: Rank{0, 0, uint32(i * 2)}}
|
return &Item{text: util.ToChars(s), index: int32(i * 2)}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Snapshot
|
// Snapshot
|
||||||
@@ -36,8 +41,8 @@ func TestChunkList(t *testing.T) {
|
|||||||
if len(*chunk1) != 2 {
|
if len(*chunk1) != 2 {
|
||||||
t.Error("Snapshot should contain only two items")
|
t.Error("Snapshot should contain only two items")
|
||||||
}
|
}
|
||||||
if string((*chunk1)[0].text) != "hello" || (*chunk1)[0].rank.index != 0 ||
|
if (*chunk1)[0].text.ToString() != "hello" || (*chunk1)[0].index != 0 ||
|
||||||
string((*chunk1)[1].text) != "world" || (*chunk1)[1].rank.index != 2 {
|
(*chunk1)[1].text.ToString() != "world" || (*chunk1)[1].index != 2 {
|
||||||
t.Error("Invalid data")
|
t.Error("Invalid data")
|
||||||
}
|
}
|
||||||
if chunk1.IsFull() {
|
if chunk1.IsFull() {
|
||||||
|
|||||||
@@ -8,25 +8,33 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// Current version
|
// Current version
|
||||||
version = "0.11.0"
|
version = "0.15.8"
|
||||||
|
|
||||||
// Core
|
// Core
|
||||||
coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
coordinatorDelayMax time.Duration = 100 * time.Millisecond
|
||||||
coordinatorDelayStep time.Duration = 10 * time.Millisecond
|
coordinatorDelayStep time.Duration = 10 * time.Millisecond
|
||||||
|
|
||||||
// Reader
|
// Reader
|
||||||
defaultCommand = `find . -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | sed s/^..//`
|
readerBufferSize = 64 * 1024
|
||||||
|
|
||||||
// Terminal
|
// Terminal
|
||||||
initialDelay = 100 * time.Millisecond
|
initialDelay = 20 * time.Millisecond
|
||||||
spinnerDuration = 200 * time.Millisecond
|
initialDelayTac = 100 * time.Millisecond
|
||||||
|
spinnerDuration = 200 * time.Millisecond
|
||||||
|
maxPatternLength = 100
|
||||||
|
|
||||||
// Matcher
|
// Matcher
|
||||||
progressMinDuration = 200 * time.Millisecond
|
numPartitionsMultiplier = 8
|
||||||
|
maxPartitions = 32
|
||||||
|
progressMinDuration = 200 * time.Millisecond
|
||||||
|
|
||||||
// Capacity of each chunk
|
// Capacity of each chunk
|
||||||
chunkSize int = 100
|
chunkSize int = 100
|
||||||
|
|
||||||
|
// Pre-allocated memory slices to minimize GC
|
||||||
|
slab16Size int = 100 * 1024 // 200KB * 32 = 12.8MB
|
||||||
|
slab32Size int = 2048 // 8KB * 32 = 256KB
|
||||||
|
|
||||||
// Do not cache results of low selectivity queries
|
// Do not cache results of low selectivity queries
|
||||||
queryCacheMax int = chunkSize / 5
|
queryCacheMax int = chunkSize / 5
|
||||||
|
|
||||||
@@ -35,6 +43,9 @@ const (
|
|||||||
|
|
||||||
// History
|
// History
|
||||||
defaultHistoryMax int = 1000
|
defaultHistoryMax int = 1000
|
||||||
|
|
||||||
|
// Jump labels
|
||||||
|
defaultJumpLabels string = "asdfghjklqwertyuiopzxcvbnm1234567890ASDFGHJKLQWERTYUIOPZXCVBNM`~;:,<.>/?'\"!@#$%^&*()[{]}-_=+"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fzf events
|
// fzf events
|
||||||
|
|||||||
8
src/constants_unix.go
Normal file
8
src/constants_unix.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Reader
|
||||||
|
defaultCommand = `find . -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | sed s/^..//`
|
||||||
|
)
|
||||||
8
src/constants_windows.go
Normal file
8
src/constants_windows.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Reader
|
||||||
|
defaultCommand = `dir /s/b`
|
||||||
|
)
|
||||||
89
src/core.go
89
src/core.go
@@ -3,7 +3,7 @@ Package fzf implements fzf, a command-line fuzzy finder.
|
|||||||
|
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2015 Junegunn Choi
|
Copyright (c) 2016 Junegunn Choi
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -28,16 +28,11 @@ package fzf
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/util"
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initProcs() {
|
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Reader -> EvtReadFin
|
Reader -> EvtReadFin
|
||||||
Reader -> EvtReadNew -> Matcher (restart)
|
Reader -> EvtReadNew -> Matcher (restart)
|
||||||
@@ -49,10 +44,8 @@ Matcher -> EvtHeader -> Terminal (update header)
|
|||||||
|
|
||||||
// Run starts fzf
|
// Run starts fzf
|
||||||
func Run(opts *Options) {
|
func Run(opts *Options) {
|
||||||
initProcs()
|
|
||||||
|
|
||||||
sort := opts.Sort > 0
|
sort := opts.Sort > 0
|
||||||
rankTiebreak = opts.Tiebreak
|
sortCriteria = opts.Criteria
|
||||||
|
|
||||||
if opts.Version {
|
if opts.Version {
|
||||||
fmt.Println(version)
|
fmt.Println(version)
|
||||||
@@ -63,29 +56,29 @@ func Run(opts *Options) {
|
|||||||
eventBox := util.NewEventBox()
|
eventBox := util.NewEventBox()
|
||||||
|
|
||||||
// ANSI code processor
|
// ANSI code processor
|
||||||
ansiProcessor := func(data []byte) ([]rune, []ansiOffset) {
|
ansiProcessor := func(data []byte) (util.Chars, *[]ansiOffset) {
|
||||||
return util.BytesToRunes(data), nil
|
return util.ToChars(data), nil
|
||||||
}
|
}
|
||||||
ansiProcessorRunes := func(data []rune) ([]rune, []ansiOffset) {
|
ansiProcessorRunes := func(data []rune) (util.Chars, *[]ansiOffset) {
|
||||||
return data, nil
|
return util.RunesToChars(data), nil
|
||||||
}
|
}
|
||||||
if opts.Ansi {
|
if opts.Ansi {
|
||||||
if opts.Theme != nil {
|
if opts.Theme != nil {
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {
|
ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {
|
||||||
trimmed, offsets, newState := extractColor(string(data), state)
|
trimmed, offsets, newState := extractColor(string(data), state, nil)
|
||||||
state = newState
|
state = newState
|
||||||
return []rune(trimmed), offsets
|
return util.RunesToChars([]rune(trimmed)), offsets
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// When color is disabled but ansi option is given,
|
// When color is disabled but ansi option is given,
|
||||||
// we simply strip out ANSI codes from the input
|
// we simply strip out ANSI codes from the input
|
||||||
ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {
|
ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {
|
||||||
trimmed, _, _ := extractColor(string(data), nil)
|
trimmed, _, _ := extractColor(string(data), nil, nil)
|
||||||
return []rune(trimmed), nil
|
return util.RunesToChars([]rune(trimmed)), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ansiProcessorRunes = func(data []rune) ([]rune, []ansiOffset) {
|
ansiProcessorRunes = func(data []rune) (util.Chars, *[]ansiOffset) {
|
||||||
return ansiProcessor([]byte(string(data)))
|
return ansiProcessor([]byte(string(data)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,31 +93,28 @@ func Run(opts *Options) {
|
|||||||
eventBox.Set(EvtHeader, header)
|
eventBox.Set(EvtHeader, header)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
runes, colors := ansiProcessor(data)
|
chars, colors := ansiProcessor(data)
|
||||||
return &Item{
|
return &Item{
|
||||||
text: runes,
|
index: int32(index),
|
||||||
index: uint32(index),
|
text: chars,
|
||||||
colors: colors,
|
colors: colors}
|
||||||
rank: Rank{0, 0, uint32(index)}}
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
chunkList = NewChunkList(func(data []byte, index int) *Item {
|
chunkList = NewChunkList(func(data []byte, index int) *Item {
|
||||||
runes := util.BytesToRunes(data)
|
tokens := Tokenize(util.ToChars(data), opts.Delimiter)
|
||||||
tokens := Tokenize(runes, opts.Delimiter)
|
|
||||||
trans := Transform(tokens, opts.WithNth)
|
trans := Transform(tokens, opts.WithNth)
|
||||||
if len(header) < opts.HeaderLines {
|
if len(header) < opts.HeaderLines {
|
||||||
header = append(header, string(joinTokens(trans)))
|
header = append(header, string(joinTokens(trans)))
|
||||||
eventBox.Set(EvtHeader, header)
|
eventBox.Set(EvtHeader, header)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
textRunes := joinTokens(trans)
|
||||||
item := Item{
|
item := Item{
|
||||||
text: joinTokens(trans),
|
index: int32(index),
|
||||||
origText: &runes,
|
origText: &data,
|
||||||
index: uint32(index),
|
colors: nil}
|
||||||
colors: nil,
|
|
||||||
rank: Rank{0, 0, uint32(index)}}
|
|
||||||
|
|
||||||
trimmed, colors := ansiProcessorRunes(item.text)
|
trimmed, colors := ansiProcessorRunes(textRunes)
|
||||||
item.text = trimmed
|
item.text = trimmed
|
||||||
item.colors = colors
|
item.colors = colors
|
||||||
return &item
|
return &item
|
||||||
@@ -141,29 +131,42 @@ func Run(opts *Options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Matcher
|
// Matcher
|
||||||
|
forward := true
|
||||||
|
for _, cri := range opts.Criteria[1:] {
|
||||||
|
if cri == byEnd {
|
||||||
|
forward = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if cri == byBegin {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
patternBuilder := func(runes []rune) *Pattern {
|
patternBuilder := func(runes []rune) *Pattern {
|
||||||
return BuildPattern(
|
return BuildPattern(
|
||||||
opts.Fuzzy, opts.Extended, opts.Case, opts.Tiebreak != byEnd,
|
opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, forward,
|
||||||
opts.Nth, opts.Delimiter, runes)
|
opts.Filter == nil, opts.Nth, opts.Delimiter, runes)
|
||||||
}
|
}
|
||||||
matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)
|
matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)
|
||||||
|
|
||||||
// Filtering mode
|
// Filtering mode
|
||||||
if opts.Filter != nil {
|
if opts.Filter != nil {
|
||||||
if opts.PrintQuery {
|
if opts.PrintQuery {
|
||||||
fmt.Println(*opts.Filter)
|
opts.Printer(*opts.Filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern := patternBuilder([]rune(*opts.Filter))
|
pattern := patternBuilder([]rune(*opts.Filter))
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
if streamingFilter {
|
if streamingFilter {
|
||||||
|
slab := util.MakeSlab(slab16Size, slab32Size)
|
||||||
reader := Reader{
|
reader := Reader{
|
||||||
func(runes []byte) bool {
|
func(runes []byte) bool {
|
||||||
item := chunkList.trans(runes, 0)
|
item := chunkList.trans(runes, 0)
|
||||||
if item != nil && pattern.MatchItem(item) {
|
if item != nil {
|
||||||
fmt.Println(string(item.text))
|
if result, _, _ := pattern.MatchItem(item, false, slab); result != nil {
|
||||||
found = true
|
opts.Printer(item.text.ToString())
|
||||||
|
found = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}, eventBox, opts.ReadZero}
|
}, eventBox, opts.ReadZero}
|
||||||
@@ -177,7 +180,7 @@ func Run(opts *Options) {
|
|||||||
chunks: snapshot,
|
chunks: snapshot,
|
||||||
pattern: pattern})
|
pattern: pattern})
|
||||||
for i := 0; i < merger.Length(); i++ {
|
for i := 0; i < merger.Length(); i++ {
|
||||||
fmt.Println(merger.Get(i).AsString(opts.Ansi))
|
opts.Printer(merger.Get(i).item.AsString(opts.Ansi))
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,13 +254,13 @@ func Run(opts *Options) {
|
|||||||
} else if val.final {
|
} else if val.final {
|
||||||
if opts.Exit0 && count == 0 || opts.Select1 && count == 1 {
|
if opts.Exit0 && count == 0 || opts.Select1 && count == 1 {
|
||||||
if opts.PrintQuery {
|
if opts.PrintQuery {
|
||||||
fmt.Println(opts.Query)
|
opts.Printer(opts.Query)
|
||||||
}
|
}
|
||||||
if len(opts.Expect) > 0 {
|
if len(opts.Expect) > 0 {
|
||||||
fmt.Println()
|
opts.Printer("")
|
||||||
}
|
}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
fmt.Println(val.Get(i).AsString(opts.Ansi))
|
opts.Printer(val.Get(i).item.AsString(opts.Ansi))
|
||||||
}
|
}
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
os.Exit(exitOk)
|
os.Exit(exitOk)
|
||||||
|
|||||||
@@ -1,555 +0,0 @@
|
|||||||
package curses
|
|
||||||
|
|
||||||
/*
|
|
||||||
#include <ncurses.h>
|
|
||||||
#include <locale.h>
|
|
||||||
#cgo !static LDFLAGS: -lncurses
|
|
||||||
#cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"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
|
|
||||||
DoubleClick
|
|
||||||
|
|
||||||
BTab
|
|
||||||
BSpace
|
|
||||||
|
|
||||||
Del
|
|
||||||
PgUp
|
|
||||||
PgDn
|
|
||||||
|
|
||||||
Up
|
|
||||||
Down
|
|
||||||
Left
|
|
||||||
Right
|
|
||||||
Home
|
|
||||||
End
|
|
||||||
|
|
||||||
SLeft
|
|
||||||
SRight
|
|
||||||
|
|
||||||
F1
|
|
||||||
F2
|
|
||||||
F3
|
|
||||||
F4
|
|
||||||
|
|
||||||
AltBS
|
|
||||||
AltA
|
|
||||||
AltB
|
|
||||||
AltC
|
|
||||||
AltD
|
|
||||||
AltE
|
|
||||||
AltF
|
|
||||||
|
|
||||||
AltZ = AltA + 'z' - 'a'
|
|
||||||
)
|
|
||||||
|
|
||||||
// Pallete
|
|
||||||
const (
|
|
||||||
ColNormal = iota
|
|
||||||
ColPrompt
|
|
||||||
ColMatch
|
|
||||||
ColCurrent
|
|
||||||
ColCurrentMatch
|
|
||||||
ColSpinner
|
|
||||||
ColInfo
|
|
||||||
ColCursor
|
|
||||||
ColSelected
|
|
||||||
ColHeader
|
|
||||||
ColUser
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
doubleClickDuration = 500 * time.Millisecond
|
|
||||||
)
|
|
||||||
|
|
||||||
type ColorTheme struct {
|
|
||||||
UseDefault bool
|
|
||||||
Fg int16
|
|
||||||
Bg int16
|
|
||||||
DarkBg int16
|
|
||||||
Prompt int16
|
|
||||||
Match int16
|
|
||||||
Current int16
|
|
||||||
CurrentMatch int16
|
|
||||||
Spinner int16
|
|
||||||
Info int16
|
|
||||||
Cursor int16
|
|
||||||
Selected int16
|
|
||||||
Header int16
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
_colorMap map[int]int
|
|
||||||
_prevDownTime time.Time
|
|
||||||
_clickY []int
|
|
||||||
_screen *C.SCREEN
|
|
||||||
Default16 *ColorTheme
|
|
||||||
Dark256 *ColorTheme
|
|
||||||
Light256 *ColorTheme
|
|
||||||
FG int
|
|
||||||
CurrentFG int
|
|
||||||
BG int
|
|
||||||
DarkBG int
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
_prevDownTime = time.Unix(0, 0)
|
|
||||||
_clickY = []int{}
|
|
||||||
_colorMap = make(map[int]int)
|
|
||||||
Default16 = &ColorTheme{
|
|
||||||
UseDefault: true,
|
|
||||||
Fg: 15,
|
|
||||||
Bg: 0,
|
|
||||||
DarkBg: C.COLOR_BLACK,
|
|
||||||
Prompt: C.COLOR_BLUE,
|
|
||||||
Match: C.COLOR_GREEN,
|
|
||||||
Current: C.COLOR_YELLOW,
|
|
||||||
CurrentMatch: C.COLOR_GREEN,
|
|
||||||
Spinner: C.COLOR_GREEN,
|
|
||||||
Info: C.COLOR_WHITE,
|
|
||||||
Cursor: C.COLOR_RED,
|
|
||||||
Selected: C.COLOR_MAGENTA,
|
|
||||||
Header: C.COLOR_CYAN}
|
|
||||||
Dark256 = &ColorTheme{
|
|
||||||
UseDefault: true,
|
|
||||||
Fg: 15,
|
|
||||||
Bg: 0,
|
|
||||||
DarkBg: 236,
|
|
||||||
Prompt: 110,
|
|
||||||
Match: 108,
|
|
||||||
Current: 254,
|
|
||||||
CurrentMatch: 151,
|
|
||||||
Spinner: 148,
|
|
||||||
Info: 144,
|
|
||||||
Cursor: 161,
|
|
||||||
Selected: 168,
|
|
||||||
Header: 109}
|
|
||||||
Light256 = &ColorTheme{
|
|
||||||
UseDefault: true,
|
|
||||||
Fg: 15,
|
|
||||||
Bg: 0,
|
|
||||||
DarkBg: 251,
|
|
||||||
Prompt: 25,
|
|
||||||
Match: 66,
|
|
||||||
Current: 237,
|
|
||||||
CurrentMatch: 23,
|
|
||||||
Spinner: 65,
|
|
||||||
Info: 101,
|
|
||||||
Cursor: 161,
|
|
||||||
Selected: 168,
|
|
||||||
Header: 31}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(theme *ColorTheme, 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.setlocale(C.LC_ALL, C.CString(""))
|
|
||||||
_screen = C.newterm(nil, C.stderr, C.stdin)
|
|
||||||
if _screen == nil {
|
|
||||||
fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
C.set_term(_screen)
|
|
||||||
if mouse {
|
|
||||||
C.mousemask(C.ALL_MOUSE_EVENTS, nil)
|
|
||||||
}
|
|
||||||
C.noecho()
|
|
||||||
C.raw() // stty dsusp undef
|
|
||||||
|
|
||||||
if theme != nil {
|
|
||||||
C.start_color()
|
|
||||||
initPairs(theme, black)
|
|
||||||
_color = attrColored
|
|
||||||
} else {
|
|
||||||
_color = attrMono
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func initPairs(theme *ColorTheme, black bool) {
|
|
||||||
fg := C.short(theme.Fg)
|
|
||||||
bg := C.short(theme.Bg)
|
|
||||||
if black {
|
|
||||||
bg = C.COLOR_BLACK
|
|
||||||
} else if theme.UseDefault {
|
|
||||||
fg = -1
|
|
||||||
bg = -1
|
|
||||||
C.use_default_colors()
|
|
||||||
}
|
|
||||||
if theme.UseDefault {
|
|
||||||
FG = -1
|
|
||||||
BG = -1
|
|
||||||
} else {
|
|
||||||
FG = int(fg)
|
|
||||||
BG = int(bg)
|
|
||||||
C.assume_default_colors(C.int(theme.Fg), C.int(bg))
|
|
||||||
}
|
|
||||||
|
|
||||||
CurrentFG = int(theme.Current)
|
|
||||||
DarkBG = int(theme.DarkBg)
|
|
||||||
darkBG := C.short(DarkBG)
|
|
||||||
C.init_pair(ColPrompt, C.short(theme.Prompt), bg)
|
|
||||||
C.init_pair(ColMatch, C.short(theme.Match), bg)
|
|
||||||
C.init_pair(ColCurrent, C.short(theme.Current), darkBG)
|
|
||||||
C.init_pair(ColCurrentMatch, C.short(theme.CurrentMatch), darkBG)
|
|
||||||
C.init_pair(ColSpinner, C.short(theme.Spinner), bg)
|
|
||||||
C.init_pair(ColInfo, C.short(theme.Info), bg)
|
|
||||||
C.init_pair(ColCursor, C.short(theme.Cursor), darkBG)
|
|
||||||
C.init_pair(ColSelected, C.short(theme.Selected), darkBG)
|
|
||||||
C.init_pair(ColHeader, C.short(theme.Header), bg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Close() {
|
|
||||||
C.endwin()
|
|
||||||
C.delscreen(_screen)
|
|
||||||
}
|
|
||||||
|
|
||||||
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{Left, 0, nil}
|
|
||||||
case 67:
|
|
||||||
return Event{Right, 0, nil}
|
|
||||||
case 66:
|
|
||||||
return Event{Down, 0, nil}
|
|
||||||
case 65:
|
|
||||||
return Event{Up, 0, nil}
|
|
||||||
case 90:
|
|
||||||
return Event{BTab, 0, nil}
|
|
||||||
case 72:
|
|
||||||
return Event{Home, 0, nil}
|
|
||||||
case 70:
|
|
||||||
return Event{End, 0, nil}
|
|
||||||
case 77:
|
|
||||||
return mouseSequence(sz)
|
|
||||||
case 80:
|
|
||||||
return Event{F1, 0, nil}
|
|
||||||
case 81:
|
|
||||||
return Event{F2, 0, nil}
|
|
||||||
case 82:
|
|
||||||
return Event{F3, 0, nil}
|
|
||||||
case 83:
|
|
||||||
return Event{F4, 0, nil}
|
|
||||||
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{End, 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{Home, 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{Home, 0, nil}
|
|
||||||
case 67:
|
|
||||||
return Event{End, 0, nil}
|
|
||||||
}
|
|
||||||
case 53:
|
|
||||||
switch _buf[5] {
|
|
||||||
case 68:
|
|
||||||
return Event{SLeft, 0, nil}
|
|
||||||
case 67:
|
|
||||||
return Event{SRight, 0, nil}
|
|
||||||
}
|
|
||||||
} // _buf[4]
|
|
||||||
} // _buf[3]
|
|
||||||
} // _buf[2]
|
|
||||||
} // _buf[2]
|
|
||||||
} // _buf[1]
|
|
||||||
if _buf[1] >= 'a' && _buf[1] <= 'z' {
|
|
||||||
return Event{AltA + int(_buf[1]) - 'a', 0, nil}
|
|
||||||
}
|
|
||||||
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:
|
|
||||||
return Event{CtrlC, 0, nil}
|
|
||||||
case CtrlG:
|
|
||||||
return Event{CtrlG, 0, nil}
|
|
||||||
case CtrlQ:
|
|
||||||
return Event{CtrlQ, 0, nil}
|
|
||||||
case 127:
|
|
||||||
return Event{BSpace, 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)
|
|
||||||
if r == utf8.RuneError {
|
|
||||||
return Event{ESC, 0, nil}
|
|
||||||
}
|
|
||||||
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(strings.Map(func(r rune) rune {
|
|
||||||
if r < 32 {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}, 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()
|
|
||||||
}
|
|
||||||
|
|
||||||
func PairFor(fg int, bg int) int {
|
|
||||||
key := (fg << 8) + bg
|
|
||||||
if found, prs := _colorMap[key]; prs {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
|
|
||||||
id := len(_colorMap) + ColUser
|
|
||||||
C.init_pair(C.short(id), C.short(fg), C.short(bg))
|
|
||||||
_colorMap[key] = id
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,23 +13,34 @@ func TestHistory(t *testing.T) {
|
|||||||
|
|
||||||
// Invalid arguments
|
// Invalid arguments
|
||||||
user, _ := user.Current()
|
user, _ := user.Current()
|
||||||
paths := []string{"/etc", "/proc"}
|
var paths []string
|
||||||
if user.Name != "root" {
|
if runtime.GOOS == "windows" {
|
||||||
paths = append(paths, "/etc/sudoers")
|
// GOPATH should exist, so we shouldn't be able to override it
|
||||||
|
paths = []string{os.Getenv("GOPATH")}
|
||||||
|
} else {
|
||||||
|
paths = []string{"/etc", "/proc"}
|
||||||
|
if user.Name != "root" {
|
||||||
|
paths = append(paths, "/etc/sudoers")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
if _, e := NewHistory(path, maxHistory); e == nil {
|
if _, e := NewHistory(path, maxHistory); e == nil {
|
||||||
t.Error("Error expected for: " + path)
|
t.Error("Error expected for: " + path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, _ := ioutil.TempFile("", "fzf-history")
|
||||||
|
f.Close()
|
||||||
|
|
||||||
{ // Append lines
|
{ // Append lines
|
||||||
h, _ := NewHistory("/tmp/fzf-history", maxHistory)
|
h, _ := NewHistory(f.Name(), maxHistory)
|
||||||
for i := 0; i < maxHistory+10; i++ {
|
for i := 0; i < maxHistory+10; i++ {
|
||||||
h.append("foobar")
|
h.append("foobar")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{ // Read lines
|
{ // Read lines
|
||||||
h, _ := NewHistory("/tmp/fzf-history", maxHistory)
|
h, _ := NewHistory(f.Name(), maxHistory)
|
||||||
if len(h.lines) != maxHistory+1 {
|
if len(h.lines) != maxHistory+1 {
|
||||||
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
||||||
}
|
}
|
||||||
@@ -37,13 +51,13 @@ func TestHistory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
{ // Append lines
|
{ // Append lines
|
||||||
h, _ := NewHistory("/tmp/fzf-history", maxHistory)
|
h, _ := NewHistory(f.Name(), maxHistory)
|
||||||
h.append("barfoo")
|
h.append("barfoo")
|
||||||
h.append("")
|
h.append("")
|
||||||
h.append("foobarbaz")
|
h.append("foobarbaz")
|
||||||
}
|
}
|
||||||
{ // Read lines again
|
{ // Read lines again
|
||||||
h, _ := NewHistory("/tmp/fzf-history", maxHistory)
|
h, _ := NewHistory(f.Name(), maxHistory)
|
||||||
if len(h.lines) != maxHistory+1 {
|
if len(h.lines) != maxHistory+1 {
|
||||||
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
||||||
}
|
}
|
||||||
|
|||||||
266
src/item.go
266
src/item.go
@@ -1,271 +1,39 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/curses"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Offset holds three 32-bit integers denoting the offsets of a matched substring
|
|
||||||
type Offset [3]int32
|
|
||||||
|
|
||||||
type colorOffset struct {
|
|
||||||
offset [2]int32
|
|
||||||
color int
|
|
||||||
bold bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Item represents each input line
|
// Item represents each input line
|
||||||
type Item struct {
|
type Item struct {
|
||||||
text []rune
|
index int32
|
||||||
origText *[]rune
|
text util.Chars
|
||||||
|
origText *[]byte
|
||||||
|
colors *[]ansiOffset
|
||||||
transformed []Token
|
transformed []Token
|
||||||
index uint32
|
|
||||||
offsets []Offset
|
|
||||||
colors []ansiOffset
|
|
||||||
rank Rank
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rank is used to sort the search result
|
// Index returns ordinal index of the Item
|
||||||
type Rank struct {
|
func (item *Item) Index() int32 {
|
||||||
matchlen uint16
|
return item.index
|
||||||
tiebreak uint16
|
|
||||||
index uint32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tiebreak criterion to use. Never changes once fzf is started.
|
// Colors returns ansiOffsets of the Item
|
||||||
var rankTiebreak tiebreak
|
func (item *Item) Colors() []ansiOffset {
|
||||||
|
if item.colors == nil {
|
||||||
// Rank calculates rank of the Item
|
return []ansiOffset{}
|
||||||
func (item *Item) Rank(cache bool) Rank {
|
|
||||||
if cache && (item.rank.matchlen > 0 || item.rank.tiebreak > 0) {
|
|
||||||
return item.rank
|
|
||||||
}
|
}
|
||||||
matchlen := 0
|
return *item.colors
|
||||||
prevEnd := 0
|
|
||||||
lenSum := 0
|
|
||||||
minBegin := math.MaxUint16
|
|
||||||
for _, offset := range item.offsets {
|
|
||||||
begin := int(offset[0])
|
|
||||||
end := int(offset[1])
|
|
||||||
trimLen := int(offset[2])
|
|
||||||
lenSum += trimLen
|
|
||||||
if prevEnd > begin {
|
|
||||||
begin = prevEnd
|
|
||||||
}
|
|
||||||
if end > prevEnd {
|
|
||||||
prevEnd = end
|
|
||||||
}
|
|
||||||
if end > begin {
|
|
||||||
if begin < minBegin {
|
|
||||||
minBegin = begin
|
|
||||||
}
|
|
||||||
matchlen += end - begin
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if matchlen == 0 {
|
|
||||||
matchlen = math.MaxUint16
|
|
||||||
}
|
|
||||||
var tiebreak uint16
|
|
||||||
switch rankTiebreak {
|
|
||||||
case byLength:
|
|
||||||
// It is guaranteed that .transformed in not null in normal execution
|
|
||||||
if item.transformed != nil {
|
|
||||||
// If offsets is empty, lenSum will be 0, but we don't care
|
|
||||||
tiebreak = uint16(lenSum)
|
|
||||||
} else {
|
|
||||||
tiebreak = uint16(len(item.text))
|
|
||||||
}
|
|
||||||
case byBegin:
|
|
||||||
// We can't just look at item.offsets[0][0] because it can be an inverse term
|
|
||||||
tiebreak = uint16(minBegin)
|
|
||||||
case byEnd:
|
|
||||||
if prevEnd > 0 {
|
|
||||||
tiebreak = uint16(1 + len(item.text) - prevEnd)
|
|
||||||
} else {
|
|
||||||
// Empty offsets due to inverse terms.
|
|
||||||
tiebreak = 1
|
|
||||||
}
|
|
||||||
case byIndex:
|
|
||||||
tiebreak = 1
|
|
||||||
}
|
|
||||||
rank := Rank{uint16(matchlen), tiebreak, item.index}
|
|
||||||
if cache {
|
|
||||||
item.rank = rank
|
|
||||||
}
|
|
||||||
return rank
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsString returns the original string
|
// AsString returns the original string
|
||||||
func (item *Item) AsString(stripAnsi bool) string {
|
func (item *Item) AsString(stripAnsi bool) string {
|
||||||
return *item.StringPtr(stripAnsi)
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringPtr returns the pointer to the original string
|
|
||||||
func (item *Item) StringPtr(stripAnsi bool) *string {
|
|
||||||
if item.origText != nil {
|
if item.origText != nil {
|
||||||
if stripAnsi {
|
if stripAnsi {
|
||||||
trimmed, _, _ := extractColor(string(*item.origText), nil)
|
trimmed, _, _ := extractColor(string(*item.origText), nil, nil)
|
||||||
return &trimmed
|
return trimmed
|
||||||
}
|
}
|
||||||
orig := string(*item.origText)
|
return string(*item.origText)
|
||||||
return &orig
|
|
||||||
}
|
}
|
||||||
str := string(item.text)
|
return item.text.ToString()
|
||||||
return &str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {
|
|
||||||
if len(item.colors) == 0 {
|
|
||||||
var offsets []colorOffset
|
|
||||||
for _, off := range item.offsets {
|
|
||||||
|
|
||||||
offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, bold: bold})
|
|
||||||
}
|
|
||||||
return offsets
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find max column
|
|
||||||
var maxCol int32
|
|
||||||
for _, off := range item.offsets {
|
|
||||||
if off[1] > maxCol {
|
|
||||||
maxCol = off[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, ansi := range item.colors {
|
|
||||||
if ansi.offset[1] > maxCol {
|
|
||||||
maxCol = ansi.offset[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cols := make([]int, maxCol)
|
|
||||||
|
|
||||||
for colorIndex, ansi := range item.colors {
|
|
||||||
for i := ansi.offset[0]; i < ansi.offset[1]; i++ {
|
|
||||||
cols[i] = colorIndex + 1 // XXX
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, off := range item.offsets {
|
|
||||||
for i := off[0]; i < off[1]; i++ {
|
|
||||||
cols[i] = -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort.Sort(ByOrder(offsets))
|
|
||||||
|
|
||||||
// Merge offsets
|
|
||||||
// ------------ ---- -- ----
|
|
||||||
// ++++++++ ++++++++++
|
|
||||||
// --++++++++-- --++++++++++---
|
|
||||||
curr := 0
|
|
||||||
start := 0
|
|
||||||
var offsets []colorOffset
|
|
||||||
add := func(idx int) {
|
|
||||||
if curr != 0 && idx > start {
|
|
||||||
if curr == -1 {
|
|
||||||
offsets = append(offsets, colorOffset{
|
|
||||||
offset: [2]int32{int32(start), int32(idx)}, color: color, bold: bold})
|
|
||||||
} else {
|
|
||||||
ansi := item.colors[curr-1]
|
|
||||||
fg := ansi.color.fg
|
|
||||||
if fg == -1 {
|
|
||||||
if current {
|
|
||||||
fg = curses.CurrentFG
|
|
||||||
} else {
|
|
||||||
fg = curses.FG
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bg := ansi.color.bg
|
|
||||||
if bg == -1 {
|
|
||||||
if current {
|
|
||||||
bg = curses.DarkBG
|
|
||||||
} else {
|
|
||||||
bg = curses.BG
|
|
||||||
}
|
|
||||||
}
|
|
||||||
offsets = append(offsets, colorOffset{
|
|
||||||
offset: [2]int32{int32(start), int32(idx)},
|
|
||||||
color: curses.PairFor(fg, bg),
|
|
||||||
bold: ansi.color.bold || bold})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for idx, col := range cols {
|
|
||||||
if col != curr {
|
|
||||||
add(idx)
|
|
||||||
start = idx
|
|
||||||
curr = col
|
|
||||||
}
|
|
||||||
}
|
|
||||||
add(int(maxCol))
|
|
||||||
return offsets
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.tiebreak < jrank.tiebreak {
|
|
||||||
return true
|
|
||||||
} else if irank.tiebreak > jrank.tiebreak {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return (irank.index <= jrank.index) != tac
|
|
||||||
}
|
}
|
||||||
|
|||||||
110
src/item_test.go
110
src/item_test.go
@@ -1,105 +1,23 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/curses"
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOffsetSort(t *testing.T) {
|
func TestStringPtr(t *testing.T) {
|
||||||
offsets := []Offset{
|
orig := []byte("\x1b[34mfoo")
|
||||||
Offset{3, 5}, Offset{2, 7},
|
text := []byte("\x1b[34mbar")
|
||||||
Offset{1, 3}, Offset{2, 9}}
|
item := Item{origText: &orig, text: util.ToChars(text)}
|
||||||
sort.Sort(ByOrder(offsets))
|
if item.AsString(true) != "foo" || item.AsString(false) != string(orig) {
|
||||||
|
t.Fail()
|
||||||
if offsets[0][0] != 1 || offsets[0][1] != 3 ||
|
}
|
||||||
offsets[1][0] != 2 || offsets[1][1] != 7 ||
|
if item.AsString(true) != "foo" {
|
||||||
offsets[2][0] != 2 || offsets[2][1] != 9 ||
|
t.Fail()
|
||||||
offsets[3][0] != 3 || offsets[3][1] != 5 {
|
}
|
||||||
t.Error("Invalid order:", offsets)
|
item.origText = nil
|
||||||
|
if item.AsString(true) != string(text) || item.AsString(false) != string(text) {
|
||||||
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
|
|
||||||
item1 := Item{text: strs[0], index: 1, offsets: []Offset{}}
|
|
||||||
rank1 := item1.Rank(true)
|
|
||||||
if rank1.matchlen != math.MaxUint16 || rank1.tiebreak != 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] != &item6 || items[1] != &item4 ||
|
|
||||||
items[2] != &item5 || items[3] != &item3 ||
|
|
||||||
items[4] != &item2 || items[5] != &item1 {
|
|
||||||
t.Error(items)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestColorOffset(t *testing.T) {
|
|
||||||
// ------------ 20 ---- -- ----
|
|
||||||
// ++++++++ ++++++++++
|
|
||||||
// --++++++++-- --++++++++++---
|
|
||||||
item := Item{
|
|
||||||
offsets: []Offset{Offset{5, 15}, Offset{25, 35}},
|
|
||||||
colors: []ansiOffset{
|
|
||||||
ansiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},
|
|
||||||
ansiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},
|
|
||||||
ansiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},
|
|
||||||
ansiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}
|
|
||||||
// [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]
|
|
||||||
|
|
||||||
offsets := item.colorOffsets(99, false, true)
|
|
||||||
assert := func(idx int, b int32, e int32, c int, bold bool) {
|
|
||||||
o := offsets[idx]
|
|
||||||
if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold {
|
|
||||||
t.Error(o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert(0, 0, 5, curses.ColUser, false)
|
|
||||||
assert(1, 5, 15, 99, false)
|
|
||||||
assert(2, 15, 20, curses.ColUser, false)
|
|
||||||
assert(3, 22, 25, curses.ColUser+1, true)
|
|
||||||
assert(4, 25, 35, 99, false)
|
|
||||||
assert(5, 35, 40, curses.ColUser+2, true)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ type Matcher struct {
|
|||||||
eventBox *util.EventBox
|
eventBox *util.EventBox
|
||||||
reqBox *util.EventBox
|
reqBox *util.EventBox
|
||||||
partitions int
|
partitions int
|
||||||
|
slab []*util.Slab
|
||||||
mergerCache map[string]*Merger
|
mergerCache map[string]*Merger
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,13 +38,15 @@ const (
|
|||||||
// NewMatcher returns a new Matcher
|
// NewMatcher returns a new Matcher
|
||||||
func NewMatcher(patternBuilder func([]rune) *Pattern,
|
func NewMatcher(patternBuilder func([]rune) *Pattern,
|
||||||
sort bool, tac bool, eventBox *util.EventBox) *Matcher {
|
sort bool, tac bool, eventBox *util.EventBox) *Matcher {
|
||||||
|
partitions := util.Min(numPartitionsMultiplier*runtime.NumCPU(), maxPartitions)
|
||||||
return &Matcher{
|
return &Matcher{
|
||||||
patternBuilder: patternBuilder,
|
patternBuilder: patternBuilder,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
tac: tac,
|
tac: tac,
|
||||||
eventBox: eventBox,
|
eventBox: eventBox,
|
||||||
reqBox: util.NewEventBox(),
|
reqBox: util.NewEventBox(),
|
||||||
partitions: runtime.NumCPU(),
|
partitions: partitions,
|
||||||
|
slab: make([]*util.Slab, partitions),
|
||||||
mergerCache: make(map[string]*Merger)}
|
mergerCache: make(map[string]*Merger)}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,18 +109,19 @@ func (m *Matcher) Loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {
|
func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {
|
||||||
perSlice := len(chunks) / m.partitions
|
partitions := m.partitions
|
||||||
|
perSlice := len(chunks) / partitions
|
||||||
|
|
||||||
// No need to parallelize
|
|
||||||
if perSlice == 0 {
|
if perSlice == 0 {
|
||||||
return [][]*Chunk{chunks}
|
partitions = len(chunks)
|
||||||
|
perSlice = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
slices := make([][]*Chunk, m.partitions)
|
slices := make([][]*Chunk, partitions)
|
||||||
for i := 0; i < m.partitions; i++ {
|
for i := 0; i < partitions; i++ {
|
||||||
start := i * perSlice
|
start := i * perSlice
|
||||||
end := start + perSlice
|
end := start + perSlice
|
||||||
if i == m.partitions-1 {
|
if i == partitions-1 {
|
||||||
end = len(chunks)
|
end = len(chunks)
|
||||||
}
|
}
|
||||||
slices[i] = chunks[start:end]
|
slices[i] = chunks[start:end]
|
||||||
@@ -127,7 +131,7 @@ func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {
|
|||||||
|
|
||||||
type partialResult struct {
|
type partialResult struct {
|
||||||
index int
|
index int
|
||||||
matches []*Item
|
matches []*Result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
||||||
@@ -152,17 +156,26 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
|
|
||||||
for idx, chunks := range slices {
|
for idx, chunks := range slices {
|
||||||
waitGroup.Add(1)
|
waitGroup.Add(1)
|
||||||
go func(idx int, chunks []*Chunk) {
|
if m.slab[idx] == nil {
|
||||||
|
m.slab[idx] = util.MakeSlab(slab16Size, slab32Size)
|
||||||
|
}
|
||||||
|
go func(idx int, slab *util.Slab, chunks []*Chunk) {
|
||||||
defer func() { waitGroup.Done() }()
|
defer func() { waitGroup.Done() }()
|
||||||
sliceMatches := []*Item{}
|
count := 0
|
||||||
for _, chunk := range chunks {
|
allMatches := make([][]*Result, len(chunks))
|
||||||
matches := request.pattern.Match(chunk)
|
for idx, chunk := range chunks {
|
||||||
sliceMatches = append(sliceMatches, matches...)
|
matches := request.pattern.Match(chunk, slab)
|
||||||
|
allMatches[idx] = matches
|
||||||
|
count += len(matches)
|
||||||
if cancelled.Get() {
|
if cancelled.Get() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
countChan <- len(matches)
|
countChan <- len(matches)
|
||||||
}
|
}
|
||||||
|
sliceMatches := make([]*Result, 0, count)
|
||||||
|
for _, matches := range allMatches {
|
||||||
|
sliceMatches = append(sliceMatches, matches...)
|
||||||
|
}
|
||||||
if m.sort {
|
if m.sort {
|
||||||
if m.tac {
|
if m.tac {
|
||||||
sort.Sort(ByRelevanceTac(sliceMatches))
|
sort.Sort(ByRelevanceTac(sliceMatches))
|
||||||
@@ -171,7 +184,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
resultChan <- partialResult{idx, sliceMatches}
|
resultChan <- partialResult{idx, sliceMatches}
|
||||||
}(idx, chunks)
|
}(idx, m.slab[idx], chunks)
|
||||||
}
|
}
|
||||||
|
|
||||||
wait := func() bool {
|
wait := func() bool {
|
||||||
@@ -199,12 +212,12 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
partialResults := make([][]*Item, numSlices)
|
partialResults := make([][]*Result, numSlices)
|
||||||
for range slices {
|
for _ = range slices {
|
||||||
partialResult := <-resultChan
|
partialResult := <-resultChan
|
||||||
partialResults[partialResult.index] = partialResult.matches
|
partialResults[partialResult.index] = partialResult.matches
|
||||||
}
|
}
|
||||||
return NewMerger(partialResults, m.sort, m.tac), false
|
return NewMerger(pattern, partialResults, m.sort, m.tac), false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset is called to interrupt/signal the ongoing search
|
// Reset is called to interrupt/signal the ongoing search
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package fzf
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// Merger with no data
|
// EmptyMerger is a Merger with no data
|
||||||
var EmptyMerger = NewMerger([][]*Item{}, false, false)
|
var EmptyMerger = NewMerger(nil, [][]*Result{}, false, false)
|
||||||
|
|
||||||
// Merger holds a set of locally sorted lists of items and provides the view of
|
// Merger holds a set of locally sorted lists of items and provides the view of
|
||||||
// a single, globally-sorted list
|
// a single, globally-sorted list
|
||||||
type Merger struct {
|
type Merger struct {
|
||||||
lists [][]*Item
|
pattern *Pattern
|
||||||
merged []*Item
|
lists [][]*Result
|
||||||
|
merged []*Result
|
||||||
chunks *[]*Chunk
|
chunks *[]*Chunk
|
||||||
cursors []int
|
cursors []int
|
||||||
sorted bool
|
sorted bool
|
||||||
@@ -22,9 +23,10 @@ type Merger struct {
|
|||||||
// original order
|
// original order
|
||||||
func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
||||||
mg := Merger{
|
mg := Merger{
|
||||||
chunks: chunks,
|
pattern: nil,
|
||||||
tac: tac,
|
chunks: chunks,
|
||||||
count: 0}
|
tac: tac,
|
||||||
|
count: 0}
|
||||||
|
|
||||||
for _, chunk := range *mg.chunks {
|
for _, chunk := range *mg.chunks {
|
||||||
mg.count += len(*chunk)
|
mg.count += len(*chunk)
|
||||||
@@ -33,10 +35,11 @@ func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewMerger returns a new Merger
|
// NewMerger returns a new Merger
|
||||||
func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger {
|
func NewMerger(pattern *Pattern, lists [][]*Result, sorted bool, tac bool) *Merger {
|
||||||
mg := Merger{
|
mg := Merger{
|
||||||
|
pattern: pattern,
|
||||||
lists: lists,
|
lists: lists,
|
||||||
merged: []*Item{},
|
merged: []*Result{},
|
||||||
chunks: nil,
|
chunks: nil,
|
||||||
cursors: make([]int, len(lists)),
|
cursors: make([]int, len(lists)),
|
||||||
sorted: sorted,
|
sorted: sorted,
|
||||||
@@ -55,14 +58,14 @@ func (mg *Merger) Length() int {
|
|||||||
return mg.count
|
return mg.count
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the pointer to the Item object indexed by the given integer
|
// Get returns the pointer to the Result object indexed by the given integer
|
||||||
func (mg *Merger) Get(idx int) *Item {
|
func (mg *Merger) Get(idx int) *Result {
|
||||||
if mg.chunks != nil {
|
if mg.chunks != nil {
|
||||||
if mg.tac {
|
if mg.tac {
|
||||||
idx = mg.count - idx - 1
|
idx = mg.count - idx - 1
|
||||||
}
|
}
|
||||||
chunk := (*mg.chunks)[idx/chunkSize]
|
chunk := (*mg.chunks)[idx/chunkSize]
|
||||||
return (*chunk)[idx%chunkSize]
|
return &Result{item: (*chunk)[idx%chunkSize]}
|
||||||
}
|
}
|
||||||
|
|
||||||
if mg.sorted {
|
if mg.sorted {
|
||||||
@@ -86,9 +89,9 @@ func (mg *Merger) cacheable() bool {
|
|||||||
return mg.count < mergerCacheMax
|
return mg.count < mergerCacheMax
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Merger) mergedGet(idx int) *Item {
|
func (mg *Merger) mergedGet(idx int) *Result {
|
||||||
for i := len(mg.merged); i <= idx; i++ {
|
for i := len(mg.merged); i <= idx; i++ {
|
||||||
minRank := Rank{0, 0, 0}
|
minRank := minRank()
|
||||||
minIdx := -1
|
minIdx := -1
|
||||||
for listIdx, list := range mg.lists {
|
for listIdx, list := range mg.lists {
|
||||||
cursor := mg.cursors[listIdx]
|
cursor := mg.cursors[listIdx]
|
||||||
@@ -97,7 +100,7 @@ func (mg *Merger) mergedGet(idx int) *Item {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if cursor >= 0 {
|
if cursor >= 0 {
|
||||||
rank := list[cursor].Rank(false)
|
rank := list[cursor].rank
|
||||||
if minIdx < 0 || compareRanks(rank, minRank, mg.tac) {
|
if minIdx < 0 || compareRanks(rank, minRank, mg.tac) {
|
||||||
minRank = rank
|
minRank = rank
|
||||||
minIdx = listIdx
|
minIdx = listIdx
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assert(t *testing.T, cond bool, msg ...string) {
|
func assert(t *testing.T, cond bool, msg ...string) {
|
||||||
@@ -13,18 +15,11 @@ func assert(t *testing.T, cond bool, msg ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func randItem() *Item {
|
func randResult() *Result {
|
||||||
str := fmt.Sprintf("%d", rand.Uint32())
|
str := fmt.Sprintf("%d", rand.Uint32())
|
||||||
offsets := make([]Offset, rand.Int()%3)
|
return &Result{
|
||||||
for idx := range offsets {
|
item: &Item{text: util.RunesToChars([]rune(str))},
|
||||||
sidx := int32(rand.Uint32() % 20)
|
rank: rank{index: rand.Int31()}}
|
||||||
eidx := sidx + int32(rand.Uint32()%20)
|
|
||||||
offsets[idx] = Offset{sidx, eidx}
|
|
||||||
}
|
|
||||||
return &Item{
|
|
||||||
text: []rune(str),
|
|
||||||
index: rand.Uint32(),
|
|
||||||
offsets: offsets}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyMerger(t *testing.T) {
|
func TestEmptyMerger(t *testing.T) {
|
||||||
@@ -34,23 +29,23 @@ func TestEmptyMerger(t *testing.T) {
|
|||||||
assert(t, len(EmptyMerger.merged) == 0, "Invalid merged list")
|
assert(t, len(EmptyMerger.merged) == 0, "Invalid merged list")
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildLists(partiallySorted bool) ([][]*Item, []*Item) {
|
func buildLists(partiallySorted bool) ([][]*Result, []*Result) {
|
||||||
numLists := 4
|
numLists := 4
|
||||||
lists := make([][]*Item, numLists)
|
lists := make([][]*Result, numLists)
|
||||||
cnt := 0
|
cnt := 0
|
||||||
for i := 0; i < numLists; i++ {
|
for i := 0; i < numLists; i++ {
|
||||||
numItems := rand.Int() % 20
|
numResults := rand.Int() % 20
|
||||||
cnt += numItems
|
cnt += numResults
|
||||||
lists[i] = make([]*Item, numItems)
|
lists[i] = make([]*Result, numResults)
|
||||||
for j := 0; j < numItems; j++ {
|
for j := 0; j < numResults; j++ {
|
||||||
item := randItem()
|
item := randResult()
|
||||||
lists[i][j] = item
|
lists[i][j] = item
|
||||||
}
|
}
|
||||||
if partiallySorted {
|
if partiallySorted {
|
||||||
sort.Sort(ByRelevance(lists[i]))
|
sort.Sort(ByRelevance(lists[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
items := []*Item{}
|
items := []*Result{}
|
||||||
for _, list := range lists {
|
for _, list := range lists {
|
||||||
items = append(items, list...)
|
items = append(items, list...)
|
||||||
}
|
}
|
||||||
@@ -62,7 +57,7 @@ func TestMergerUnsorted(t *testing.T) {
|
|||||||
cnt := len(items)
|
cnt := len(items)
|
||||||
|
|
||||||
// Not sorted: same order
|
// Not sorted: same order
|
||||||
mg := NewMerger(lists, false, false)
|
mg := NewMerger(nil, lists, false, false)
|
||||||
assert(t, cnt == mg.Length(), "Invalid Length")
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
||||||
for i := 0; i < cnt; i++ {
|
for i := 0; i < cnt; i++ {
|
||||||
assert(t, items[i] == mg.Get(i), "Invalid Get")
|
assert(t, items[i] == mg.Get(i), "Invalid Get")
|
||||||
@@ -74,7 +69,7 @@ func TestMergerSorted(t *testing.T) {
|
|||||||
cnt := len(items)
|
cnt := len(items)
|
||||||
|
|
||||||
// Sorted sorted order
|
// Sorted sorted order
|
||||||
mg := NewMerger(lists, true, false)
|
mg := NewMerger(nil, lists, true, false)
|
||||||
assert(t, cnt == mg.Length(), "Invalid Length")
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
||||||
sort.Sort(ByRelevance(items))
|
sort.Sort(ByRelevance(items))
|
||||||
for i := 0; i < cnt; i++ {
|
for i := 0; i < cnt; i++ {
|
||||||
@@ -84,7 +79,7 @@ func TestMergerSorted(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Inverse order
|
// Inverse order
|
||||||
mg2 := NewMerger(lists, true, false)
|
mg2 := NewMerger(nil, lists, true, false)
|
||||||
for i := cnt - 1; i >= 0; i-- {
|
for i := cnt - 1; i >= 0; i-- {
|
||||||
if items[i] != mg2.Get(i) {
|
if items[i] != mg2.Get(i) {
|
||||||
t.Error("Not sorted", items[i], mg2.Get(i))
|
t.Error("Not sorted", items[i], mg2.Get(i))
|
||||||
|
|||||||
486
src/options.go
486
src/options.go
@@ -1,13 +1,15 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/curses"
|
"github.com/junegunn/fzf/src/algo"
|
||||||
|
"github.com/junegunn/fzf/src/tui"
|
||||||
|
|
||||||
"github.com/junegunn/go-shellwords"
|
"github.com/junegunn/go-shellwords"
|
||||||
)
|
)
|
||||||
@@ -18,36 +20,54 @@ const usage = `usage: fzf [options]
|
|||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
(enabled by default; +x or --no-extended to disable)
|
(enabled by default; +x or --no-extended to disable)
|
||||||
-e, --exact Enable Exact-match
|
-e, --exact Enable Exact-match
|
||||||
|
--algo=TYPE Fuzzy matching algorithm: [v1|v2] (default: v2)
|
||||||
-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 index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END]).
|
||||||
--with-nth=N[,..] Transform item using index expressions within finder
|
--with-nth=N[,..] Transform the presentation of each line using
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
field index expressions
|
||||||
|
-d, --delimiter=STR Field delimiter regex (default: AWK-style)
|
||||||
+s, --no-sort Do not sort the result
|
+s, --no-sort Do not sort the result
|
||||||
--tac Reverse the order of the input
|
--tac Reverse the order of the input
|
||||||
--tiebreak=CRITERION Sort criterion when the scores are tied;
|
--tiebreak=CRI[,..] Comma-separated list of sort criteria to apply
|
||||||
[length|begin|end|index] (default: length)
|
when the scores are tied [length|begin|end|index]
|
||||||
|
(default: length)
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--ansi Enable processing of ANSI color codes
|
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
--color=COLSPEC Base scheme (dark|light|16|bw) and/or custom colors
|
--bind=KEYBINDS Custom key bindings. Refer to the man page.
|
||||||
--black Use black background
|
|
||||||
--reverse Reverse orientation
|
|
||||||
--margin=MARGIN Screen margin (TRBL / TB,RL / T,RL,B / T,R,B,L)
|
|
||||||
--cycle Enable cyclic scroll
|
--cycle Enable cyclic scroll
|
||||||
--no-hscroll Disable horizontal scroll
|
--no-hscroll Disable horizontal scroll
|
||||||
|
--hscroll-off=COL Number of screen columns to keep to the right of the
|
||||||
|
highlighted substring (default: 10)
|
||||||
|
--jump-labels=CHARS Label characters for jump and jump-accept
|
||||||
|
|
||||||
|
Layout
|
||||||
|
--reverse Reverse orientation
|
||||||
|
--margin=MARGIN Screen margin (TRBL / TB,RL / T,RL,B / T,R,B,L)
|
||||||
--inline-info Display finder info inline with the query
|
--inline-info Display finder info inline with the query
|
||||||
--prompt=STR Input prompt (default: '> ')
|
--prompt=STR Input prompt (default: '> ')
|
||||||
--bind=KEYBINDS Custom key bindings. Refer to the man page.
|
|
||||||
--history=FILE History file
|
|
||||||
--history-size=N Maximum number of history entries (default: 1000)
|
|
||||||
--header=STR String to print as header
|
--header=STR String to print as header
|
||||||
--header-lines=N The first N lines of the input are treated as header
|
--header-lines=N The first N lines of the input are treated as header
|
||||||
|
|
||||||
|
Display
|
||||||
|
--ansi Enable processing of ANSI color codes
|
||||||
|
--tabstop=SPACES Number of spaces for a tab character (default: 8)
|
||||||
|
--color=COLSPEC Base scheme (dark|light|16|bw) and/or custom colors
|
||||||
|
--no-bold Do not use bold text
|
||||||
|
|
||||||
|
History
|
||||||
|
--history=FILE History file
|
||||||
|
--history-size=N Maximum number of history entries (default: 1000)
|
||||||
|
|
||||||
|
Preview
|
||||||
|
--preview=COMMAND Command to preview highlighted line ({})
|
||||||
|
--preview-window=OPT Preview window layout (default: right:50%)
|
||||||
|
[up|down|left|right][:SIZE[%]][:hidden]
|
||||||
|
|
||||||
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
|
||||||
@@ -59,7 +79,7 @@ const usage = `usage: fzf [options]
|
|||||||
|
|
||||||
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. '--reverse --inline-info')
|
FZF_DEFAULT_OPTS Default options (e.g. '--reverse --inline-info')
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -74,22 +94,44 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Sort criteria
|
// Sort criteria
|
||||||
type tiebreak int
|
type criterion int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
byLength tiebreak = iota
|
byScore criterion = iota
|
||||||
|
byLength
|
||||||
byBegin
|
byBegin
|
||||||
byEnd
|
byEnd
|
||||||
byIndex
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func defaultMargin() [4]string {
|
type sizeSpec struct {
|
||||||
return [4]string{"0", "0", "0", "0"}
|
size float64
|
||||||
|
percent bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultMargin() [4]sizeSpec {
|
||||||
|
return [4]sizeSpec{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type windowPosition int
|
||||||
|
|
||||||
|
const (
|
||||||
|
posUp windowPosition = iota
|
||||||
|
posDown
|
||||||
|
posLeft
|
||||||
|
posRight
|
||||||
|
)
|
||||||
|
|
||||||
|
type previewOpts struct {
|
||||||
|
command string
|
||||||
|
position windowPosition
|
||||||
|
size sizeSpec
|
||||||
|
hidden bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options stores the values of command-line options
|
// Options stores the values of command-line options
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Fuzzy bool
|
Fuzzy bool
|
||||||
|
FuzzyAlgo algo.Algo
|
||||||
Extended bool
|
Extended bool
|
||||||
Case Case
|
Case Case
|
||||||
Nth []Range
|
Nth []Range
|
||||||
@@ -97,16 +139,19 @@ type Options struct {
|
|||||||
Delimiter Delimiter
|
Delimiter Delimiter
|
||||||
Sort int
|
Sort int
|
||||||
Tac bool
|
Tac bool
|
||||||
Tiebreak tiebreak
|
Criteria []criterion
|
||||||
Multi bool
|
Multi bool
|
||||||
Ansi bool
|
Ansi bool
|
||||||
Mouse bool
|
Mouse bool
|
||||||
Theme *curses.ColorTheme
|
Theme *tui.ColorTheme
|
||||||
Black bool
|
Black bool
|
||||||
|
Bold bool
|
||||||
Reverse bool
|
Reverse bool
|
||||||
Cycle bool
|
Cycle bool
|
||||||
Hscroll bool
|
Hscroll bool
|
||||||
|
HscrollOff int
|
||||||
InlineInfo bool
|
InlineInfo bool
|
||||||
|
JumpLabels string
|
||||||
Prompt string
|
Prompt string
|
||||||
Query string
|
Query string
|
||||||
Select1 bool
|
Select1 bool
|
||||||
@@ -116,26 +161,23 @@ type Options struct {
|
|||||||
Expect map[int]string
|
Expect map[int]string
|
||||||
Keymap map[int]actionType
|
Keymap map[int]actionType
|
||||||
Execmap map[int]string
|
Execmap map[int]string
|
||||||
|
Preview previewOpts
|
||||||
PrintQuery bool
|
PrintQuery bool
|
||||||
ReadZero bool
|
ReadZero bool
|
||||||
|
Printer func(string)
|
||||||
Sync bool
|
Sync bool
|
||||||
History *History
|
History *History
|
||||||
Header []string
|
Header []string
|
||||||
HeaderLines int
|
HeaderLines int
|
||||||
Margin [4]string
|
Margin [4]sizeSpec
|
||||||
|
Tabstop int
|
||||||
Version bool
|
Version bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultTheme() *curses.ColorTheme {
|
|
||||||
if strings.Contains(os.Getenv("TERM"), "256") {
|
|
||||||
return curses.Dark256
|
|
||||||
}
|
|
||||||
return curses.Default16
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultOptions() *Options {
|
func defaultOptions() *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
Fuzzy: true,
|
Fuzzy: true,
|
||||||
|
FuzzyAlgo: algo.FuzzyMatchV2,
|
||||||
Extended: true,
|
Extended: true,
|
||||||
Case: CaseSmart,
|
Case: CaseSmart,
|
||||||
Nth: make([]Range, 0),
|
Nth: make([]Range, 0),
|
||||||
@@ -143,16 +185,19 @@ func defaultOptions() *Options {
|
|||||||
Delimiter: Delimiter{},
|
Delimiter: Delimiter{},
|
||||||
Sort: 1000,
|
Sort: 1000,
|
||||||
Tac: false,
|
Tac: false,
|
||||||
Tiebreak: byLength,
|
Criteria: []criterion{byScore, byLength},
|
||||||
Multi: false,
|
Multi: false,
|
||||||
Ansi: false,
|
Ansi: false,
|
||||||
Mouse: true,
|
Mouse: true,
|
||||||
Theme: defaultTheme(),
|
Theme: tui.EmptyTheme(),
|
||||||
Black: false,
|
Black: false,
|
||||||
|
Bold: true,
|
||||||
Reverse: false,
|
Reverse: false,
|
||||||
Cycle: false,
|
Cycle: false,
|
||||||
Hscroll: true,
|
Hscroll: true,
|
||||||
|
HscrollOff: 10,
|
||||||
InlineInfo: false,
|
InlineInfo: false,
|
||||||
|
JumpLabels: defaultJumpLabels,
|
||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
Query: "",
|
Query: "",
|
||||||
Select1: false,
|
Select1: false,
|
||||||
@@ -160,15 +205,18 @@ func defaultOptions() *Options {
|
|||||||
Filter: nil,
|
Filter: nil,
|
||||||
ToggleSort: false,
|
ToggleSort: false,
|
||||||
Expect: make(map[int]string),
|
Expect: make(map[int]string),
|
||||||
Keymap: defaultKeymap(),
|
Keymap: make(map[int]actionType),
|
||||||
Execmap: make(map[int]string),
|
Execmap: make(map[int]string),
|
||||||
|
Preview: previewOpts{"", posRight, sizeSpec{50, true}, false},
|
||||||
PrintQuery: false,
|
PrintQuery: false,
|
||||||
ReadZero: false,
|
ReadZero: false,
|
||||||
|
Printer: func(str string) { fmt.Println(str) },
|
||||||
Sync: false,
|
Sync: false,
|
||||||
History: nil,
|
History: nil,
|
||||||
Header: make([]string, 0),
|
Header: make([]string, 0),
|
||||||
HeaderLines: 0,
|
HeaderLines: 0,
|
||||||
Margin: defaultMargin(),
|
Margin: defaultMargin(),
|
||||||
|
Tabstop: 8,
|
||||||
Version: false}
|
Version: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +249,7 @@ func nextString(args []string, i *int, message string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func optionalNextString(args []string, i *int) string {
|
func optionalNextString(args []string, i *int) string {
|
||||||
if len(args) > *i+1 {
|
if len(args) > *i+1 && !strings.HasPrefix(args[*i+1], "-") {
|
||||||
*i++
|
*i++
|
||||||
return args[*i]
|
return args[*i]
|
||||||
}
|
}
|
||||||
@@ -282,6 +330,22 @@ func isAlphabet(char uint8) bool {
|
|||||||
return char >= 'a' && char <= 'z'
|
return char >= 'a' && char <= 'z'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isNumeric(char uint8) bool {
|
||||||
|
return char >= '0' && char <= '9'
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAlgo(str string) algo.Algo {
|
||||||
|
switch str {
|
||||||
|
case "v1":
|
||||||
|
return algo.FuzzyMatchV1
|
||||||
|
case "v2":
|
||||||
|
return algo.FuzzyMatchV2
|
||||||
|
default:
|
||||||
|
errorExit("invalid algorithm (expected: v1 or v2)")
|
||||||
|
}
|
||||||
|
return algo.FuzzyMatchV2
|
||||||
|
}
|
||||||
|
|
||||||
func parseKeyChords(str string, message string) map[int]string {
|
func parseKeyChords(str string, message string) map[int]string {
|
||||||
if len(str) == 0 {
|
if len(str) == 0 {
|
||||||
errorExit(message)
|
errorExit(message)
|
||||||
@@ -301,52 +365,66 @@ func parseKeyChords(str string, message string) map[int]string {
|
|||||||
chord := 0
|
chord := 0
|
||||||
switch lkey {
|
switch lkey {
|
||||||
case "up":
|
case "up":
|
||||||
chord = curses.Up
|
chord = tui.Up
|
||||||
case "down":
|
case "down":
|
||||||
chord = curses.Down
|
chord = tui.Down
|
||||||
case "left":
|
case "left":
|
||||||
chord = curses.Left
|
chord = tui.Left
|
||||||
case "right":
|
case "right":
|
||||||
chord = curses.Right
|
chord = tui.Right
|
||||||
case "enter", "return":
|
case "enter", "return":
|
||||||
chord = curses.CtrlM
|
chord = tui.CtrlM
|
||||||
case "space":
|
case "space":
|
||||||
chord = curses.AltZ + int(' ')
|
chord = tui.AltZ + int(' ')
|
||||||
case "bspace", "bs":
|
case "bspace", "bs":
|
||||||
chord = curses.BSpace
|
chord = tui.BSpace
|
||||||
|
case "alt-enter", "alt-return":
|
||||||
|
chord = tui.AltEnter
|
||||||
|
case "alt-space":
|
||||||
|
chord = tui.AltSpace
|
||||||
|
case "alt-/":
|
||||||
|
chord = tui.AltSlash
|
||||||
case "alt-bs", "alt-bspace":
|
case "alt-bs", "alt-bspace":
|
||||||
chord = curses.AltBS
|
chord = tui.AltBS
|
||||||
case "tab":
|
case "tab":
|
||||||
chord = curses.Tab
|
chord = tui.Tab
|
||||||
case "btab", "shift-tab":
|
case "btab", "shift-tab":
|
||||||
chord = curses.BTab
|
chord = tui.BTab
|
||||||
case "esc":
|
case "esc":
|
||||||
chord = curses.ESC
|
chord = tui.ESC
|
||||||
case "del":
|
case "del":
|
||||||
chord = curses.Del
|
chord = tui.Del
|
||||||
case "home":
|
case "home":
|
||||||
chord = curses.Home
|
chord = tui.Home
|
||||||
case "end":
|
case "end":
|
||||||
chord = curses.End
|
chord = tui.End
|
||||||
case "pgup", "page-up":
|
case "pgup", "page-up":
|
||||||
chord = curses.PgUp
|
chord = tui.PgUp
|
||||||
case "pgdn", "page-down":
|
case "pgdn", "page-down":
|
||||||
chord = curses.PgDn
|
chord = tui.PgDn
|
||||||
case "shift-left":
|
case "shift-left":
|
||||||
chord = curses.SLeft
|
chord = tui.SLeft
|
||||||
case "shift-right":
|
case "shift-right":
|
||||||
chord = curses.SRight
|
chord = tui.SRight
|
||||||
case "double-click":
|
case "double-click":
|
||||||
chord = curses.DoubleClick
|
chord = tui.DoubleClick
|
||||||
|
case "f10":
|
||||||
|
chord = tui.F10
|
||||||
|
case "f11":
|
||||||
|
chord = tui.F11
|
||||||
|
case "f12":
|
||||||
|
chord = tui.F12
|
||||||
default:
|
default:
|
||||||
if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {
|
if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {
|
||||||
chord = curses.CtrlA + int(lkey[5]) - 'a'
|
chord = tui.CtrlA + int(lkey[5]) - 'a'
|
||||||
} else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {
|
} else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {
|
||||||
chord = curses.AltA + int(lkey[4]) - 'a'
|
chord = tui.AltA + int(lkey[4]) - 'a'
|
||||||
} else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' {
|
} else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isNumeric(lkey[4]) {
|
||||||
chord = curses.F1 + int(key[1]) - '1'
|
chord = tui.Alt0 + int(lkey[4]) - '0'
|
||||||
|
} else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '9' {
|
||||||
|
chord = tui.F1 + int(key[1]) - '1'
|
||||||
} else if utf8.RuneCountInString(key) == 1 {
|
} else if utf8.RuneCountInString(key) == 1 {
|
||||||
chord = curses.AltZ + int([]rune(key)[0])
|
chord = tui.AltZ + int([]rune(key)[0])
|
||||||
} else {
|
} else {
|
||||||
errorExit("unsupported key: " + key)
|
errorExit("unsupported key: " + key)
|
||||||
}
|
}
|
||||||
@@ -358,23 +436,42 @@ func parseKeyChords(str string, message string) map[int]string {
|
|||||||
return chords
|
return chords
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTiebreak(str string) tiebreak {
|
func parseTiebreak(str string) []criterion {
|
||||||
switch strings.ToLower(str) {
|
criteria := []criterion{byScore}
|
||||||
case "length":
|
hasIndex := false
|
||||||
return byLength
|
hasLength := false
|
||||||
case "index":
|
hasBegin := false
|
||||||
return byIndex
|
hasEnd := false
|
||||||
case "begin":
|
check := func(notExpected *bool, name string) {
|
||||||
return byBegin
|
if *notExpected {
|
||||||
case "end":
|
errorExit("duplicate sort criteria: " + name)
|
||||||
return byEnd
|
}
|
||||||
default:
|
if hasIndex {
|
||||||
errorExit("invalid sort criterion: " + str)
|
errorExit("index should be the last criterion")
|
||||||
|
}
|
||||||
|
*notExpected = true
|
||||||
}
|
}
|
||||||
return byLength
|
for _, str := range strings.Split(strings.ToLower(str), ",") {
|
||||||
|
switch str {
|
||||||
|
case "index":
|
||||||
|
check(&hasIndex, "index")
|
||||||
|
case "length":
|
||||||
|
check(&hasLength, "length")
|
||||||
|
criteria = append(criteria, byLength)
|
||||||
|
case "begin":
|
||||||
|
check(&hasBegin, "begin")
|
||||||
|
criteria = append(criteria, byBegin)
|
||||||
|
case "end":
|
||||||
|
check(&hasEnd, "end")
|
||||||
|
criteria = append(criteria, byEnd)
|
||||||
|
default:
|
||||||
|
errorExit("invalid sort criterion: " + str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return criteria
|
||||||
}
|
}
|
||||||
|
|
||||||
func dupeTheme(theme *curses.ColorTheme) *curses.ColorTheme {
|
func dupeTheme(theme *tui.ColorTheme) *tui.ColorTheme {
|
||||||
if theme != nil {
|
if theme != nil {
|
||||||
dupe := *theme
|
dupe := *theme
|
||||||
return &dupe
|
return &dupe
|
||||||
@@ -382,16 +479,16 @@ func dupeTheme(theme *curses.ColorTheme) *curses.ColorTheme {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTheme(defaultTheme *curses.ColorTheme, str string) *curses.ColorTheme {
|
func parseTheme(defaultTheme *tui.ColorTheme, str string) *tui.ColorTheme {
|
||||||
theme := dupeTheme(defaultTheme)
|
theme := dupeTheme(defaultTheme)
|
||||||
for _, str := range strings.Split(strings.ToLower(str), ",") {
|
for _, str := range strings.Split(strings.ToLower(str), ",") {
|
||||||
switch str {
|
switch str {
|
||||||
case "dark":
|
case "dark":
|
||||||
theme = dupeTheme(curses.Dark256)
|
theme = dupeTheme(tui.Dark256)
|
||||||
case "light":
|
case "light":
|
||||||
theme = dupeTheme(curses.Light256)
|
theme = dupeTheme(tui.Light256)
|
||||||
case "16":
|
case "16":
|
||||||
theme = dupeTheme(curses.Default16)
|
theme = dupeTheme(tui.Default16)
|
||||||
case "bw", "no":
|
case "bw", "no":
|
||||||
theme = nil
|
theme = nil
|
||||||
default:
|
default:
|
||||||
@@ -411,14 +508,12 @@ func parseTheme(defaultTheme *curses.ColorTheme, str string) *curses.ColorTheme
|
|||||||
if err != nil || ansi32 < -1 || ansi32 > 255 {
|
if err != nil || ansi32 < -1 || ansi32 > 255 {
|
||||||
fail()
|
fail()
|
||||||
}
|
}
|
||||||
ansi := int16(ansi32)
|
ansi := tui.Color(ansi32)
|
||||||
switch pair[0] {
|
switch pair[0] {
|
||||||
case "fg":
|
case "fg":
|
||||||
theme.Fg = ansi
|
theme.Fg = ansi
|
||||||
theme.UseDefault = theme.UseDefault && ansi < 0
|
|
||||||
case "bg":
|
case "bg":
|
||||||
theme.Bg = ansi
|
theme.Bg = ansi
|
||||||
theme.UseDefault = theme.UseDefault && ansi < 0
|
|
||||||
case "fg+":
|
case "fg+":
|
||||||
theme.Current = ansi
|
theme.Current = ansi
|
||||||
case "bg+":
|
case "bg+":
|
||||||
@@ -427,6 +522,8 @@ func parseTheme(defaultTheme *curses.ColorTheme, str string) *curses.ColorTheme
|
|||||||
theme.Match = ansi
|
theme.Match = ansi
|
||||||
case "hl+":
|
case "hl+":
|
||||||
theme.CurrentMatch = ansi
|
theme.CurrentMatch = ansi
|
||||||
|
case "border":
|
||||||
|
theme.Border = ansi
|
||||||
case "prompt":
|
case "prompt":
|
||||||
theme.Prompt = ansi
|
theme.Prompt = ansi
|
||||||
case "spinner":
|
case "spinner":
|
||||||
@@ -461,7 +558,7 @@ const (
|
|||||||
escapedComma = 1
|
escapedComma = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort bool, str string) (map[int]actionType, map[int]string, bool) {
|
func parseKeymap(keymap map[int]actionType, execmap map[int]string, str string) {
|
||||||
if executeRegexp == nil {
|
if executeRegexp == nil {
|
||||||
// Backreferences are not supported.
|
// Backreferences are not supported.
|
||||||
// "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|')
|
// "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|')
|
||||||
@@ -488,9 +585,9 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
}
|
}
|
||||||
var key int
|
var key int
|
||||||
if len(pair[0]) == 1 && pair[0][0] == escapedColon {
|
if len(pair[0]) == 1 && pair[0][0] == escapedColon {
|
||||||
key = ':' + curses.AltZ
|
key = ':' + tui.AltZ
|
||||||
} else if len(pair[0]) == 1 && pair[0][0] == escapedComma {
|
} else if len(pair[0]) == 1 && pair[0][0] == escapedComma {
|
||||||
key = ',' + curses.AltZ
|
key = ',' + tui.AltZ
|
||||||
} else {
|
} else {
|
||||||
keys := parseKeyChords(pair[0], "key name required")
|
keys := parseKeyChords(pair[0], "key name required")
|
||||||
key = firstKey(keys)
|
key = firstKey(keys)
|
||||||
@@ -507,6 +604,8 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
keymap[key] = actAbort
|
keymap[key] = actAbort
|
||||||
case "accept":
|
case "accept":
|
||||||
keymap[key] = actAccept
|
keymap[key] = actAccept
|
||||||
|
case "print-query":
|
||||||
|
keymap[key] = actPrintQuery
|
||||||
case "backward-char":
|
case "backward-char":
|
||||||
keymap[key] = actBackwardChar
|
keymap[key] = actBackwardChar
|
||||||
case "backward-delete-char":
|
case "backward-delete-char":
|
||||||
@@ -527,6 +626,10 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
keymap[key] = actForwardChar
|
keymap[key] = actForwardChar
|
||||||
case "forward-word":
|
case "forward-word":
|
||||||
keymap[key] = actForwardWord
|
keymap[key] = actForwardWord
|
||||||
|
case "jump":
|
||||||
|
keymap[key] = actJump
|
||||||
|
case "jump-accept":
|
||||||
|
keymap[key] = actJumpAccept
|
||||||
case "kill-line":
|
case "kill-line":
|
||||||
keymap[key] = actKillLine
|
keymap[key] = actKillLine
|
||||||
case "kill-word":
|
case "kill-word":
|
||||||
@@ -543,6 +646,10 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
keymap[key] = actToggleDown
|
keymap[key] = actToggleDown
|
||||||
case "toggle-up":
|
case "toggle-up":
|
||||||
keymap[key] = actToggleUp
|
keymap[key] = actToggleUp
|
||||||
|
case "toggle-in":
|
||||||
|
keymap[key] = actToggleIn
|
||||||
|
case "toggle-out":
|
||||||
|
keymap[key] = actToggleOut
|
||||||
case "toggle-all":
|
case "toggle-all":
|
||||||
keymap[key] = actToggleAll
|
keymap[key] = actToggleAll
|
||||||
case "select-all":
|
case "select-all":
|
||||||
@@ -563,9 +670,18 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
keymap[key] = actPreviousHistory
|
keymap[key] = actPreviousHistory
|
||||||
case "next-history":
|
case "next-history":
|
||||||
keymap[key] = actNextHistory
|
keymap[key] = actNextHistory
|
||||||
|
case "toggle-preview":
|
||||||
|
keymap[key] = actTogglePreview
|
||||||
case "toggle-sort":
|
case "toggle-sort":
|
||||||
keymap[key] = actToggleSort
|
keymap[key] = actToggleSort
|
||||||
toggleSort = true
|
case "preview-up":
|
||||||
|
keymap[key] = actPreviewUp
|
||||||
|
case "preview-down":
|
||||||
|
keymap[key] = actPreviewDown
|
||||||
|
case "preview-page-up":
|
||||||
|
keymap[key] = actPreviewPageUp
|
||||||
|
case "preview-page-down":
|
||||||
|
keymap[key] = actPreviewPageDown
|
||||||
default:
|
default:
|
||||||
if isExecuteAction(actLower) {
|
if isExecuteAction(actLower) {
|
||||||
var offset int
|
var offset int
|
||||||
@@ -586,7 +702,6 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return keymap, execmap, toggleSort
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isExecuteAction(str string) bool {
|
func isExecuteAction(str string) bool {
|
||||||
@@ -608,53 +723,99 @@ func isExecuteAction(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {
|
func parseToggleSort(keymap map[int]actionType, str string) {
|
||||||
keys := parseKeyChords(str, "key name required")
|
keys := parseKeyChords(str, "key name required")
|
||||||
if len(keys) != 1 {
|
if len(keys) != 1 {
|
||||||
errorExit("multiple keys specified")
|
errorExit("multiple keys specified")
|
||||||
}
|
}
|
||||||
keymap[firstKey(keys)] = actToggleSort
|
keymap[firstKey(keys)] = actToggleSort
|
||||||
return keymap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func strLines(str string) []string {
|
func strLines(str string) []string {
|
||||||
return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
|
return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMargin(margin string) [4]string {
|
func parseSize(str string, maxPercent float64, label string) sizeSpec {
|
||||||
margins := strings.Split(margin, ",")
|
var val float64
|
||||||
checked := func(str string) string {
|
percent := strings.HasSuffix(str, "%")
|
||||||
if strings.HasSuffix(str, "%") {
|
if percent {
|
||||||
val := atof(str[:len(str)-1])
|
val = atof(str[:len(str)-1])
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
errorExit("margin must be non-negative")
|
errorExit(label + " must be non-negative")
|
||||||
}
|
|
||||||
if val > 100 {
|
|
||||||
errorExit("margin too large")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
val := atoi(str)
|
|
||||||
if val < 0 {
|
|
||||||
errorExit("margin must be non-negative")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return str
|
if val > maxPercent {
|
||||||
|
errorExit(fmt.Sprintf("%s too large (max: %d%%)", label, int(maxPercent)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if strings.Contains(str, ".") {
|
||||||
|
errorExit(label + " (without %) must be a non-negative integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
val = float64(atoi(str))
|
||||||
|
if val < 0 {
|
||||||
|
errorExit(label + " must be non-negative")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sizeSpec{val, percent}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePreviewWindow(opts *previewOpts, input string) {
|
||||||
|
layout := input
|
||||||
|
opts.hidden = false
|
||||||
|
if strings.HasSuffix(layout, ":hidden") {
|
||||||
|
opts.hidden = true
|
||||||
|
layout = strings.TrimSuffix(layout, ":hidden")
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens := strings.Split(layout, ":")
|
||||||
|
if len(tokens) == 0 || len(tokens) > 2 {
|
||||||
|
errorExit("invalid window layout: " + input)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tokens) > 1 {
|
||||||
|
opts.size = parseSize(tokens[1], 99, "window size")
|
||||||
|
} else {
|
||||||
|
opts.size = sizeSpec{50, true}
|
||||||
|
}
|
||||||
|
if !opts.size.percent && opts.size.size > 0 {
|
||||||
|
// Adjust size for border
|
||||||
|
opts.size.size += 2
|
||||||
|
}
|
||||||
|
|
||||||
|
switch tokens[0] {
|
||||||
|
case "up":
|
||||||
|
opts.position = posUp
|
||||||
|
case "down":
|
||||||
|
opts.position = posDown
|
||||||
|
case "left":
|
||||||
|
opts.position = posLeft
|
||||||
|
case "right":
|
||||||
|
opts.position = posRight
|
||||||
|
default:
|
||||||
|
errorExit("invalid window position: " + input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseMargin(margin string) [4]sizeSpec {
|
||||||
|
margins := strings.Split(margin, ",")
|
||||||
|
checked := func(str string) sizeSpec {
|
||||||
|
return parseSize(str, 49, "margin")
|
||||||
}
|
}
|
||||||
switch len(margins) {
|
switch len(margins) {
|
||||||
case 1:
|
case 1:
|
||||||
m := checked(margins[0])
|
m := checked(margins[0])
|
||||||
return [4]string{m, m, m, m}
|
return [4]sizeSpec{m, m, m, m}
|
||||||
case 2:
|
case 2:
|
||||||
tb := checked(margins[0])
|
tb := checked(margins[0])
|
||||||
rl := checked(margins[1])
|
rl := checked(margins[1])
|
||||||
return [4]string{tb, rl, tb, rl}
|
return [4]sizeSpec{tb, rl, tb, rl}
|
||||||
case 3:
|
case 3:
|
||||||
t := checked(margins[0])
|
t := checked(margins[0])
|
||||||
rl := checked(margins[1])
|
rl := checked(margins[1])
|
||||||
b := checked(margins[2])
|
b := checked(margins[2])
|
||||||
return [4]string{t, rl, b, rl}
|
return [4]sizeSpec{t, rl, b, rl}
|
||||||
case 4:
|
case 4:
|
||||||
return [4]string{
|
return [4]sizeSpec{
|
||||||
checked(margins[0]), checked(margins[1]),
|
checked(margins[0]), checked(margins[1]),
|
||||||
checked(margins[2]), checked(margins[3])}
|
checked(margins[2]), checked(margins[3])}
|
||||||
default:
|
default:
|
||||||
@@ -664,7 +825,6 @@ func parseMargin(margin string) [4]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseOptions(opts *Options, allArgs []string) {
|
func parseOptions(opts *Options, allArgs []string) {
|
||||||
keymap := make(map[int]actionType)
|
|
||||||
var historyMax int
|
var historyMax int
|
||||||
if opts.History == nil {
|
if opts.History == nil {
|
||||||
historyMax = defaultHistoryMax
|
historyMax = defaultHistoryMax
|
||||||
@@ -687,6 +847,7 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
opts.History.maxSize = historyMax
|
opts.History.maxSize = historyMax
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
validateJumpLabels := false
|
||||||
for i := 0; i < len(allArgs); i++ {
|
for i := 0; i < len(allArgs); i++ {
|
||||||
arg := allArgs[i]
|
arg := allArgs[i]
|
||||||
switch arg {
|
switch arg {
|
||||||
@@ -709,23 +870,23 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
case "-f", "--filter":
|
case "-f", "--filter":
|
||||||
filter := nextString(allArgs, &i, "query string required")
|
filter := nextString(allArgs, &i, "query string required")
|
||||||
opts.Filter = &filter
|
opts.Filter = &filter
|
||||||
|
case "--algo":
|
||||||
|
opts.FuzzyAlgo = parseAlgo(nextString(allArgs, &i, "algorithm required (v1|v2)"))
|
||||||
case "--expect":
|
case "--expect":
|
||||||
opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")
|
opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")
|
||||||
case "--tiebreak":
|
case "--tiebreak":
|
||||||
opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))
|
opts.Criteria = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))
|
||||||
case "--bind":
|
case "--bind":
|
||||||
keymap, opts.Execmap, opts.ToggleSort =
|
parseKeymap(opts.Keymap, opts.Execmap, nextString(allArgs, &i, "bind expression required"))
|
||||||
parseKeymap(keymap, opts.Execmap, opts.ToggleSort, nextString(allArgs, &i, "bind expression required"))
|
|
||||||
case "--color":
|
case "--color":
|
||||||
spec := optionalNextString(allArgs, &i)
|
spec := optionalNextString(allArgs, &i)
|
||||||
if len(spec) == 0 {
|
if len(spec) == 0 {
|
||||||
opts.Theme = defaultTheme()
|
opts.Theme = tui.EmptyTheme()
|
||||||
} else {
|
} else {
|
||||||
opts.Theme = parseTheme(opts.Theme, spec)
|
opts.Theme = parseTheme(opts.Theme, spec)
|
||||||
}
|
}
|
||||||
case "--toggle-sort":
|
case "--toggle-sort":
|
||||||
keymap = checkToggleSort(keymap, nextString(allArgs, &i, "key name required"))
|
parseToggleSort(opts.Keymap, nextString(allArgs, &i, "key name required"))
|
||||||
opts.ToggleSort = true
|
|
||||||
case "-d", "--delimiter":
|
case "-d", "--delimiter":
|
||||||
opts.Delimiter = delimiterRegexp(nextString(allArgs, &i, "delimiter required"))
|
opts.Delimiter = delimiterRegexp(nextString(allArgs, &i, "delimiter required"))
|
||||||
case "-n", "--nth":
|
case "-n", "--nth":
|
||||||
@@ -757,11 +918,15 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
case "+c", "--no-color":
|
case "+c", "--no-color":
|
||||||
opts.Theme = nil
|
opts.Theme = nil
|
||||||
case "+2", "--no-256":
|
case "+2", "--no-256":
|
||||||
opts.Theme = curses.Default16
|
opts.Theme = tui.Default16
|
||||||
case "--black":
|
case "--black":
|
||||||
opts.Black = true
|
opts.Black = true
|
||||||
case "--no-black":
|
case "--no-black":
|
||||||
opts.Black = false
|
opts.Black = false
|
||||||
|
case "--bold":
|
||||||
|
opts.Bold = true
|
||||||
|
case "--no-bold":
|
||||||
|
opts.Bold = false
|
||||||
case "--reverse":
|
case "--reverse":
|
||||||
opts.Reverse = true
|
opts.Reverse = true
|
||||||
case "--no-reverse":
|
case "--no-reverse":
|
||||||
@@ -774,10 +939,15 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
opts.Hscroll = true
|
opts.Hscroll = true
|
||||||
case "--no-hscroll":
|
case "--no-hscroll":
|
||||||
opts.Hscroll = false
|
opts.Hscroll = false
|
||||||
|
case "--hscroll-off":
|
||||||
|
opts.HscrollOff = nextInt(allArgs, &i, "hscroll offset required")
|
||||||
case "--inline-info":
|
case "--inline-info":
|
||||||
opts.InlineInfo = true
|
opts.InlineInfo = true
|
||||||
case "--no-inline-info":
|
case "--no-inline-info":
|
||||||
opts.InlineInfo = false
|
opts.InlineInfo = false
|
||||||
|
case "--jump-labels":
|
||||||
|
opts.JumpLabels = nextString(allArgs, &i, "label characters required")
|
||||||
|
validateJumpLabels = true
|
||||||
case "-1", "--select-1":
|
case "-1", "--select-1":
|
||||||
opts.Select1 = true
|
opts.Select1 = true
|
||||||
case "+1", "--no-select-1":
|
case "+1", "--no-select-1":
|
||||||
@@ -790,6 +960,10 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
opts.ReadZero = true
|
opts.ReadZero = true
|
||||||
case "--no-read0":
|
case "--no-read0":
|
||||||
opts.ReadZero = false
|
opts.ReadZero = false
|
||||||
|
case "--print0":
|
||||||
|
opts.Printer = func(str string) { fmt.Print(str, "\x00") }
|
||||||
|
case "--no-print0":
|
||||||
|
opts.Printer = func(str string) { fmt.Println(str) }
|
||||||
case "--print-query":
|
case "--print-query":
|
||||||
opts.PrintQuery = true
|
opts.PrintQuery = true
|
||||||
case "--no-print-query":
|
case "--no-print-query":
|
||||||
@@ -817,15 +991,26 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
case "--header-lines":
|
case "--header-lines":
|
||||||
opts.HeaderLines = atoi(
|
opts.HeaderLines = atoi(
|
||||||
nextString(allArgs, &i, "number of header lines required"))
|
nextString(allArgs, &i, "number of header lines required"))
|
||||||
|
case "--preview":
|
||||||
|
opts.Preview.command = nextString(allArgs, &i, "preview command required")
|
||||||
|
case "--no-preview":
|
||||||
|
opts.Preview.command = ""
|
||||||
|
case "--preview-window":
|
||||||
|
parsePreviewWindow(&opts.Preview,
|
||||||
|
nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]]"))
|
||||||
case "--no-margin":
|
case "--no-margin":
|
||||||
opts.Margin = defaultMargin()
|
opts.Margin = defaultMargin()
|
||||||
case "--margin":
|
case "--margin":
|
||||||
opts.Margin = parseMargin(
|
opts.Margin = parseMargin(
|
||||||
nextString(allArgs, &i, "margin required (TRBL / TB,RL / T,RL,B / T,R,B,L)"))
|
nextString(allArgs, &i, "margin required (TRBL / TB,RL / T,RL,B / T,R,B,L)"))
|
||||||
|
case "--tabstop":
|
||||||
|
opts.Tabstop = nextInt(allArgs, &i, "tab stop required")
|
||||||
case "--version":
|
case "--version":
|
||||||
opts.Version = true
|
opts.Version = true
|
||||||
default:
|
default:
|
||||||
if match, value := optString(arg, "-q", "--query="); match {
|
if match, value := optString(arg, "--algo="); match {
|
||||||
|
opts.FuzzyAlgo = parseAlgo(value)
|
||||||
|
} else if match, value := optString(arg, "-q", "--query="); match {
|
||||||
opts.Query = value
|
opts.Query = value
|
||||||
} else if match, value := optString(arg, "-f", "--filter="); match {
|
} else if match, value := optString(arg, "-f", "--filter="); match {
|
||||||
opts.Filter = &value
|
opts.Filter = &value
|
||||||
@@ -840,17 +1025,15 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
} else if match, _ := optString(arg, "-s", "--sort="); match {
|
} else if match, _ := optString(arg, "-s", "--sort="); match {
|
||||||
opts.Sort = 1 // Don't care
|
opts.Sort = 1 // Don't care
|
||||||
} else if match, value := optString(arg, "--toggle-sort="); match {
|
} else if match, value := optString(arg, "--toggle-sort="); match {
|
||||||
keymap = checkToggleSort(keymap, value)
|
parseToggleSort(opts.Keymap, value)
|
||||||
opts.ToggleSort = true
|
|
||||||
} else if match, value := optString(arg, "--expect="); match {
|
} else if match, value := optString(arg, "--expect="); match {
|
||||||
opts.Expect = parseKeyChords(value, "key names required")
|
opts.Expect = parseKeyChords(value, "key names required")
|
||||||
} else if match, value := optString(arg, "--tiebreak="); match {
|
} else if match, value := optString(arg, "--tiebreak="); match {
|
||||||
opts.Tiebreak = parseTiebreak(value)
|
opts.Criteria = parseTiebreak(value)
|
||||||
} else if match, value := optString(arg, "--color="); match {
|
} else if match, value := optString(arg, "--color="); match {
|
||||||
opts.Theme = parseTheme(opts.Theme, value)
|
opts.Theme = parseTheme(opts.Theme, value)
|
||||||
} else if match, value := optString(arg, "--bind="); match {
|
} else if match, value := optString(arg, "--bind="); match {
|
||||||
keymap, opts.Execmap, opts.ToggleSort =
|
parseKeymap(opts.Keymap, opts.Execmap, value)
|
||||||
parseKeymap(keymap, opts.Execmap, opts.ToggleSort, value)
|
|
||||||
} else if match, value := optString(arg, "--history="); match {
|
} else if match, value := optString(arg, "--history="); match {
|
||||||
setHistory(value)
|
setHistory(value)
|
||||||
} else if match, value := optString(arg, "--history-size="); match {
|
} else if match, value := optString(arg, "--history-size="); match {
|
||||||
@@ -859,8 +1042,18 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
opts.Header = strLines(value)
|
opts.Header = strLines(value)
|
||||||
} else if match, value := optString(arg, "--header-lines="); match {
|
} else if match, value := optString(arg, "--header-lines="); match {
|
||||||
opts.HeaderLines = atoi(value)
|
opts.HeaderLines = atoi(value)
|
||||||
|
} else if match, value := optString(arg, "--preview="); match {
|
||||||
|
opts.Preview.command = value
|
||||||
|
} else if match, value := optString(arg, "--preview-window="); match {
|
||||||
|
parsePreviewWindow(&opts.Preview, value)
|
||||||
} else if match, value := optString(arg, "--margin="); match {
|
} else if match, value := optString(arg, "--margin="); match {
|
||||||
opts.Margin = parseMargin(value)
|
opts.Margin = parseMargin(value)
|
||||||
|
} else if match, value := optString(arg, "--tabstop="); match {
|
||||||
|
opts.Tabstop = atoi(value)
|
||||||
|
} else if match, value := optString(arg, "--hscroll-off="); match {
|
||||||
|
opts.HscrollOff = atoi(value)
|
||||||
|
} else if match, value := optString(arg, "--jump-labels="); match {
|
||||||
|
opts.JumpLabels = value
|
||||||
} else {
|
} else {
|
||||||
errorExit("unknown option: " + arg)
|
errorExit("unknown option: " + arg)
|
||||||
}
|
}
|
||||||
@@ -871,20 +1064,47 @@ func parseOptions(opts *Options, allArgs []string) {
|
|||||||
errorExit("header lines must be a non-negative integer")
|
errorExit("header lines must be a non-negative integer")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change default actions for CTRL-N / CTRL-P when --history is used
|
if opts.HscrollOff < 0 {
|
||||||
if opts.History != nil {
|
errorExit("hscroll offset must be a non-negative integer")
|
||||||
if _, prs := keymap[curses.CtrlP]; !prs {
|
}
|
||||||
keymap[curses.CtrlP] = actPreviousHistory
|
|
||||||
|
if opts.Tabstop < 1 {
|
||||||
|
errorExit("tab stop must be a positive integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(opts.JumpLabels) == 0 {
|
||||||
|
errorExit("empty jump labels")
|
||||||
|
}
|
||||||
|
|
||||||
|
if validateJumpLabels {
|
||||||
|
for _, r := range opts.JumpLabels {
|
||||||
|
if r < 32 || r > 126 {
|
||||||
|
errorExit("non-ascii jump labels are not allowed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if _, prs := keymap[curses.CtrlN]; !prs {
|
}
|
||||||
keymap[curses.CtrlN] = actNextHistory
|
}
|
||||||
|
|
||||||
|
func postProcessOptions(opts *Options) {
|
||||||
|
// Default actions for CTRL-N / CTRL-P when --history is set
|
||||||
|
if opts.History != nil {
|
||||||
|
if _, prs := opts.Keymap[tui.CtrlP]; !prs {
|
||||||
|
opts.Keymap[tui.CtrlP] = actPreviousHistory
|
||||||
|
}
|
||||||
|
if _, prs := opts.Keymap[tui.CtrlN]; !prs {
|
||||||
|
opts.Keymap[tui.CtrlN] = actNextHistory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override default key bindings
|
// Extend the default key map
|
||||||
for key, act := range keymap {
|
keymap := defaultKeymap()
|
||||||
opts.Keymap[key] = act
|
for key, act := range opts.Keymap {
|
||||||
|
if act == actToggleSort {
|
||||||
|
opts.ToggleSort = true
|
||||||
|
}
|
||||||
|
keymap[key] = act
|
||||||
}
|
}
|
||||||
|
opts.Keymap = keymap
|
||||||
|
|
||||||
// If we're not using extended search mode, --nth option becomes irrelevant
|
// If we're not using extended search mode, --nth option becomes irrelevant
|
||||||
// if it contains the whole range
|
// if it contains the whole range
|
||||||
@@ -904,9 +1124,13 @@ func ParseOptions() *Options {
|
|||||||
|
|
||||||
// Options from Env var
|
// Options from Env var
|
||||||
words, _ := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
|
words, _ := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
|
||||||
parseOptions(opts, words)
|
if len(words) > 0 {
|
||||||
|
parseOptions(opts, words)
|
||||||
|
}
|
||||||
|
|
||||||
// Options from command-line arguments
|
// Options from command-line arguments
|
||||||
parseOptions(opts, os.Args[1:])
|
parseOptions(opts, os.Args[1:])
|
||||||
|
|
||||||
|
postProcessOptions(opts)
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package fzf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/curses"
|
"github.com/junegunn/fzf/src/tui"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDelimiterRegex(t *testing.T) {
|
func TestDelimiterRegex(t *testing.T) {
|
||||||
@@ -42,24 +44,24 @@ func TestDelimiterRegex(t *testing.T) {
|
|||||||
|
|
||||||
func TestDelimiterRegexString(t *testing.T) {
|
func TestDelimiterRegexString(t *testing.T) {
|
||||||
delim := delimiterRegexp("*")
|
delim := delimiterRegexp("*")
|
||||||
tokens := Tokenize([]rune("-*--*---**---"), delim)
|
tokens := Tokenize(util.RunesToChars([]rune("-*--*---**---")), delim)
|
||||||
if delim.regex != nil ||
|
if delim.regex != nil ||
|
||||||
string(tokens[0].text) != "-*" ||
|
tokens[0].text.ToString() != "-*" ||
|
||||||
string(tokens[1].text) != "--*" ||
|
tokens[1].text.ToString() != "--*" ||
|
||||||
string(tokens[2].text) != "---*" ||
|
tokens[2].text.ToString() != "---*" ||
|
||||||
string(tokens[3].text) != "*" ||
|
tokens[3].text.ToString() != "*" ||
|
||||||
string(tokens[4].text) != "---" {
|
tokens[4].text.ToString() != "---" {
|
||||||
t.Errorf("%s %s %d", delim, tokens, len(tokens))
|
t.Errorf("%s %s %d", delim, tokens, len(tokens))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDelimiterRegexRegex(t *testing.T) {
|
func TestDelimiterRegexRegex(t *testing.T) {
|
||||||
delim := delimiterRegexp("--\\*")
|
delim := delimiterRegexp("--\\*")
|
||||||
tokens := Tokenize([]rune("-*--*---**---"), delim)
|
tokens := Tokenize(util.RunesToChars([]rune("-*--*---**---")), delim)
|
||||||
if delim.str != nil ||
|
if delim.str != nil ||
|
||||||
string(tokens[0].text) != "-*--*" ||
|
tokens[0].text.ToString() != "-*--*" ||
|
||||||
string(tokens[1].text) != "---*" ||
|
tokens[1].text.ToString() != "---*" ||
|
||||||
string(tokens[2].text) != "*---" {
|
tokens[2].text.ToString() != "*---" {
|
||||||
t.Errorf("%s %d", tokens, len(tokens))
|
t.Errorf("%s %d", tokens, len(tokens))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,6 +98,7 @@ func TestIrrelevantNth(t *testing.T) {
|
|||||||
opts := defaultOptions()
|
opts := defaultOptions()
|
||||||
words := []string{"--nth", "..", "-x"}
|
words := []string{"--nth", "..", "-x"}
|
||||||
parseOptions(opts, words)
|
parseOptions(opts, words)
|
||||||
|
postProcessOptions(opts)
|
||||||
if len(opts.Nth) != 0 {
|
if len(opts.Nth) != 0 {
|
||||||
t.Errorf("nth should be empty: %s", opts.Nth)
|
t.Errorf("nth should be empty: %s", opts.Nth)
|
||||||
}
|
}
|
||||||
@@ -104,6 +107,7 @@ func TestIrrelevantNth(t *testing.T) {
|
|||||||
{
|
{
|
||||||
opts := defaultOptions()
|
opts := defaultOptions()
|
||||||
parseOptions(opts, words)
|
parseOptions(opts, words)
|
||||||
|
postProcessOptions(opts)
|
||||||
if len(opts.Nth) != 0 {
|
if len(opts.Nth) != 0 {
|
||||||
t.Errorf("nth should be empty: %s", opts.Nth)
|
t.Errorf("nth should be empty: %s", opts.Nth)
|
||||||
}
|
}
|
||||||
@@ -112,6 +116,7 @@ func TestIrrelevantNth(t *testing.T) {
|
|||||||
opts := defaultOptions()
|
opts := defaultOptions()
|
||||||
words = append(words, "-x")
|
words = append(words, "-x")
|
||||||
parseOptions(opts, words)
|
parseOptions(opts, words)
|
||||||
|
postProcessOptions(opts)
|
||||||
if len(opts.Nth) != 2 {
|
if len(opts.Nth) != 2 {
|
||||||
t.Errorf("nth should not be empty: %s", opts.Nth)
|
t.Errorf("nth should not be empty: %s", opts.Nth)
|
||||||
}
|
}
|
||||||
@@ -120,55 +125,57 @@ func TestIrrelevantNth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseKeys(t *testing.T) {
|
func TestParseKeys(t *testing.T) {
|
||||||
pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "")
|
pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g,ALT-enter,alt-SPACE", "")
|
||||||
check := func(i int, s string) {
|
check := func(i int, s string) {
|
||||||
if pairs[i] != s {
|
if pairs[i] != s {
|
||||||
t.Errorf("%s != %s", pairs[i], s)
|
t.Errorf("%s != %s", pairs[i], s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(pairs) != 9 {
|
if len(pairs) != 11 {
|
||||||
t.Error(9)
|
t.Error(11)
|
||||||
}
|
}
|
||||||
check(curses.CtrlZ, "ctrl-z")
|
check(tui.CtrlZ, "ctrl-z")
|
||||||
check(curses.AltZ, "alt-z")
|
check(tui.AltZ, "alt-z")
|
||||||
check(curses.F2, "f2")
|
check(tui.F2, "f2")
|
||||||
check(curses.AltZ+'@', "@")
|
check(tui.AltZ+'@', "@")
|
||||||
check(curses.AltA, "Alt-a")
|
check(tui.AltA, "Alt-a")
|
||||||
check(curses.AltZ+'!', "!")
|
check(tui.AltZ+'!', "!")
|
||||||
check(curses.CtrlA+'g'-'a', "ctrl-G")
|
check(tui.CtrlA+'g'-'a', "ctrl-G")
|
||||||
check(curses.AltZ+'J', "J")
|
check(tui.AltZ+'J', "J")
|
||||||
check(curses.AltZ+'g', "g")
|
check(tui.AltZ+'g', "g")
|
||||||
|
check(tui.AltEnter, "ALT-enter")
|
||||||
|
check(tui.AltSpace, "alt-SPACE")
|
||||||
|
|
||||||
// Synonyms
|
// Synonyms
|
||||||
pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")
|
pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")
|
||||||
if len(pairs) != 9 {
|
if len(pairs) != 9 {
|
||||||
t.Error(9)
|
t.Error(9)
|
||||||
}
|
}
|
||||||
check(curses.CtrlM, "Return")
|
check(tui.CtrlM, "Return")
|
||||||
check(curses.AltZ+' ', "space")
|
check(tui.AltZ+' ', "space")
|
||||||
check(curses.Tab, "tab")
|
check(tui.Tab, "tab")
|
||||||
check(curses.BTab, "btab")
|
check(tui.BTab, "btab")
|
||||||
check(curses.ESC, "esc")
|
check(tui.ESC, "esc")
|
||||||
check(curses.Up, "up")
|
check(tui.Up, "up")
|
||||||
check(curses.Down, "down")
|
check(tui.Down, "down")
|
||||||
check(curses.Left, "left")
|
check(tui.Left, "left")
|
||||||
check(curses.Right, "right")
|
check(tui.Right, "right")
|
||||||
|
|
||||||
pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")
|
pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")
|
||||||
if len(pairs) != 11 {
|
if len(pairs) != 11 {
|
||||||
t.Error(11)
|
t.Error(11)
|
||||||
}
|
}
|
||||||
check(curses.Tab, "Ctrl-I")
|
check(tui.Tab, "Ctrl-I")
|
||||||
check(curses.PgUp, "page-up")
|
check(tui.PgUp, "page-up")
|
||||||
check(curses.PgDn, "Page-Down")
|
check(tui.PgDn, "Page-Down")
|
||||||
check(curses.Home, "Home")
|
check(tui.Home, "Home")
|
||||||
check(curses.End, "End")
|
check(tui.End, "End")
|
||||||
check(curses.AltBS, "Alt-BSpace")
|
check(tui.AltBS, "Alt-BSpace")
|
||||||
check(curses.SLeft, "shift-left")
|
check(tui.SLeft, "shift-left")
|
||||||
check(curses.SRight, "shift-right")
|
check(tui.SRight, "shift-right")
|
||||||
check(curses.BTab, "shift-tab")
|
check(tui.BTab, "shift-tab")
|
||||||
check(curses.CtrlM, "Enter")
|
check(tui.CtrlM, "Enter")
|
||||||
check(curses.BSpace, "bspace")
|
check(tui.BSpace, "bspace")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseKeysWithComma(t *testing.T) {
|
func TestParseKeysWithComma(t *testing.T) {
|
||||||
@@ -185,36 +192,36 @@ func TestParseKeysWithComma(t *testing.T) {
|
|||||||
|
|
||||||
pairs := parseKeyChords(",", "")
|
pairs := parseKeyChords(",", "")
|
||||||
checkN(len(pairs), 1)
|
checkN(len(pairs), 1)
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
|
|
||||||
pairs = parseKeyChords(",,a,b", "")
|
pairs = parseKeyChords(",,a,b", "")
|
||||||
checkN(len(pairs), 3)
|
checkN(len(pairs), 3)
|
||||||
check(pairs, curses.AltZ+'a', "a")
|
check(pairs, tui.AltZ+'a', "a")
|
||||||
check(pairs, curses.AltZ+'b', "b")
|
check(pairs, tui.AltZ+'b', "b")
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
|
|
||||||
pairs = parseKeyChords("a,b,,", "")
|
pairs = parseKeyChords("a,b,,", "")
|
||||||
checkN(len(pairs), 3)
|
checkN(len(pairs), 3)
|
||||||
check(pairs, curses.AltZ+'a', "a")
|
check(pairs, tui.AltZ+'a', "a")
|
||||||
check(pairs, curses.AltZ+'b', "b")
|
check(pairs, tui.AltZ+'b', "b")
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
|
|
||||||
pairs = parseKeyChords("a,,,b", "")
|
pairs = parseKeyChords("a,,,b", "")
|
||||||
checkN(len(pairs), 3)
|
checkN(len(pairs), 3)
|
||||||
check(pairs, curses.AltZ+'a', "a")
|
check(pairs, tui.AltZ+'a', "a")
|
||||||
check(pairs, curses.AltZ+'b', "b")
|
check(pairs, tui.AltZ+'b', "b")
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
|
|
||||||
pairs = parseKeyChords("a,,,b,c", "")
|
pairs = parseKeyChords("a,,,b,c", "")
|
||||||
checkN(len(pairs), 4)
|
checkN(len(pairs), 4)
|
||||||
check(pairs, curses.AltZ+'a', "a")
|
check(pairs, tui.AltZ+'a', "a")
|
||||||
check(pairs, curses.AltZ+'b', "b")
|
check(pairs, tui.AltZ+'b', "b")
|
||||||
check(pairs, curses.AltZ+'c', "c")
|
check(pairs, tui.AltZ+'c', "c")
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
|
|
||||||
pairs = parseKeyChords(",,,", "")
|
pairs = parseKeyChords(",,,", "")
|
||||||
checkN(len(pairs), 1)
|
checkN(len(pairs), 1)
|
||||||
check(pairs, curses.AltZ+',', ",")
|
check(pairs, tui.AltZ+',', ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBind(t *testing.T) {
|
func TestBind(t *testing.T) {
|
||||||
@@ -230,49 +237,41 @@ func TestBind(t *testing.T) {
|
|||||||
}
|
}
|
||||||
keymap := defaultKeymap()
|
keymap := defaultKeymap()
|
||||||
execmap := make(map[int]string)
|
execmap := make(map[int]string)
|
||||||
check(actBeginningOfLine, keymap[curses.CtrlA])
|
check(actBeginningOfLine, keymap[tui.CtrlA])
|
||||||
keymap, execmap, toggleSort :=
|
parseKeymap(keymap, execmap,
|
||||||
parseKeymap(keymap, execmap, false,
|
"ctrl-a:kill-line,ctrl-b:toggle-sort,c:page-up,alt-z:page-down,"+
|
||||||
"ctrl-a:kill-line,ctrl-b:toggle-sort,c:page-up,alt-z:page-down,"+
|
"f1:execute(ls {}),f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+
|
||||||
"f1:execute(ls {}),f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+
|
"alt-a:execute@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};"+
|
||||||
"alt-a:execute@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};"+
|
",,:abort,::accept,X:execute:\nfoobar,Y:execute(baz)")
|
||||||
",,:abort,::accept,X:execute:\nfoobar,Y:execute(baz)")
|
check(actKillLine, keymap[tui.CtrlA])
|
||||||
if !toggleSort {
|
check(actToggleSort, keymap[tui.CtrlB])
|
||||||
t.Errorf("toggleSort not set")
|
check(actPageUp, keymap[tui.AltZ+'c'])
|
||||||
}
|
check(actAbort, keymap[tui.AltZ+','])
|
||||||
check(actKillLine, keymap[curses.CtrlA])
|
check(actAccept, keymap[tui.AltZ+':'])
|
||||||
check(actToggleSort, keymap[curses.CtrlB])
|
check(actPageDown, keymap[tui.AltZ])
|
||||||
check(actPageUp, keymap[curses.AltZ+'c'])
|
check(actExecute, keymap[tui.F1])
|
||||||
check(actAbort, keymap[curses.AltZ+','])
|
check(actExecute, keymap[tui.F2])
|
||||||
check(actAccept, keymap[curses.AltZ+':'])
|
check(actExecute, keymap[tui.F3])
|
||||||
check(actPageDown, keymap[curses.AltZ])
|
check(actExecute, keymap[tui.F4])
|
||||||
check(actExecute, keymap[curses.F1])
|
checkString("ls {}", execmap[tui.F1])
|
||||||
check(actExecute, keymap[curses.F2])
|
checkString("echo {}, {}, {}", execmap[tui.F2])
|
||||||
check(actExecute, keymap[curses.F3])
|
checkString("echo '({})'", execmap[tui.F3])
|
||||||
check(actExecute, keymap[curses.F4])
|
checkString("less {}", execmap[tui.F4])
|
||||||
checkString("ls {}", execmap[curses.F1])
|
checkString("echo (,),[,],/,:,;,%,{}", execmap[tui.AltA])
|
||||||
checkString("echo {}, {}, {}", execmap[curses.F2])
|
checkString("echo (,),[,],/,:,@,%,{}", execmap[tui.AltB])
|
||||||
checkString("echo '({})'", execmap[curses.F3])
|
checkString("\nfoobar,Y:execute(baz)", execmap[tui.AltZ+'X'])
|
||||||
checkString("less {}", execmap[curses.F4])
|
|
||||||
checkString("echo (,),[,],/,:,;,%,{}", execmap[curses.AltA])
|
|
||||||
checkString("echo (,),[,],/,:,@,%,{}", execmap[curses.AltB])
|
|
||||||
checkString("\nfoobar,Y:execute(baz)", execmap[curses.AltZ+'X'])
|
|
||||||
|
|
||||||
for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {
|
for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {
|
||||||
keymap, execmap, toggleSort =
|
parseKeymap(keymap, execmap, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))
|
||||||
parseKeymap(keymap, execmap, false, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))
|
checkString("foobar", execmap[tui.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0])])
|
||||||
checkString("foobar", execmap[curses.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0])])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
keymap, execmap, toggleSort = parseKeymap(keymap, execmap, false, "f1:abort")
|
parseKeymap(keymap, execmap, "f1:abort")
|
||||||
if toggleSort {
|
check(actAbort, keymap[tui.F1])
|
||||||
t.Errorf("toggleSort set")
|
|
||||||
}
|
|
||||||
check(actAbort, keymap[curses.F1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestColorSpec(t *testing.T) {
|
func TestColorSpec(t *testing.T) {
|
||||||
theme := curses.Dark256
|
theme := tui.Dark256
|
||||||
dark := parseTheme(theme, "dark")
|
dark := parseTheme(theme, "dark")
|
||||||
if *dark != *theme {
|
if *dark != *theme {
|
||||||
t.Errorf("colors should be equivalent")
|
t.Errorf("colors should be equivalent")
|
||||||
@@ -285,7 +284,7 @@ func TestColorSpec(t *testing.T) {
|
|||||||
if *light == *theme {
|
if *light == *theme {
|
||||||
t.Errorf("should not be equivalent")
|
t.Errorf("should not be equivalent")
|
||||||
}
|
}
|
||||||
if *light != *curses.Light256 {
|
if *light != *tui.Light256 {
|
||||||
t.Errorf("colors should be equivalent")
|
t.Errorf("colors should be equivalent")
|
||||||
}
|
}
|
||||||
if light == theme {
|
if light == theme {
|
||||||
@@ -296,29 +295,23 @@ func TestColorSpec(t *testing.T) {
|
|||||||
if customized.Fg != 231 || customized.Bg != 232 {
|
if customized.Fg != 231 || customized.Bg != 232 {
|
||||||
t.Errorf("color not customized")
|
t.Errorf("color not customized")
|
||||||
}
|
}
|
||||||
if *curses.Dark256 == *customized {
|
if *tui.Dark256 == *customized {
|
||||||
t.Errorf("colors should not be equivalent")
|
t.Errorf("colors should not be equivalent")
|
||||||
}
|
}
|
||||||
customized.Fg = curses.Dark256.Fg
|
customized.Fg = tui.Dark256.Fg
|
||||||
customized.Bg = curses.Dark256.Bg
|
customized.Bg = tui.Dark256.Bg
|
||||||
if *curses.Dark256 == *customized {
|
if *tui.Dark256 != *customized {
|
||||||
t.Errorf("colors should now be equivalent")
|
t.Errorf("colors should now be equivalent: %v, %v", tui.Dark256, customized)
|
||||||
}
|
}
|
||||||
|
|
||||||
customized = parseTheme(theme, "fg:231,dark,bg:232")
|
customized = parseTheme(theme, "fg:231,dark,bg:232")
|
||||||
if customized.Fg != curses.Dark256.Fg || customized.Bg == curses.Dark256.Bg {
|
if customized.Fg != tui.Dark256.Fg || customized.Bg == tui.Dark256.Bg {
|
||||||
t.Errorf("color not customized")
|
t.Errorf("color not customized")
|
||||||
}
|
}
|
||||||
if customized.UseDefault {
|
|
||||||
t.Errorf("not using default colors")
|
|
||||||
}
|
|
||||||
if !curses.Dark256.UseDefault {
|
|
||||||
t.Errorf("using default colors")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseNilTheme(t *testing.T) {
|
func TestParseNilTheme(t *testing.T) {
|
||||||
var theme *curses.ColorTheme
|
var theme *tui.ColorTheme
|
||||||
newTheme := parseTheme(theme, "prompt:12")
|
newTheme := parseTheme(theme, "prompt:12")
|
||||||
if newTheme != nil {
|
if newTheme != nil {
|
||||||
t.Errorf("color is disabled. keep it that way.")
|
t.Errorf("color is disabled. keep it that way.")
|
||||||
@@ -328,3 +321,83 @@ func TestParseNilTheme(t *testing.T) {
|
|||||||
t.Errorf("color should now be enabled and customized")
|
t.Errorf("color should now be enabled and customized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultCtrlNP(t *testing.T) {
|
||||||
|
check := func(words []string, key int, expected actionType) {
|
||||||
|
opts := defaultOptions()
|
||||||
|
parseOptions(opts, words)
|
||||||
|
postProcessOptions(opts)
|
||||||
|
if opts.Keymap[key] != expected {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check([]string{}, tui.CtrlN, actDown)
|
||||||
|
check([]string{}, tui.CtrlP, actUp)
|
||||||
|
|
||||||
|
check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)
|
||||||
|
check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)
|
||||||
|
|
||||||
|
f, _ := ioutil.TempFile("", "fzf-history")
|
||||||
|
f.Close()
|
||||||
|
hist := "--history=" + f.Name()
|
||||||
|
check([]string{hist}, tui.CtrlN, actNextHistory)
|
||||||
|
check([]string{hist}, tui.CtrlP, actPreviousHistory)
|
||||||
|
|
||||||
|
check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)
|
||||||
|
check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlP, actPreviousHistory)
|
||||||
|
|
||||||
|
check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlN, actNextHistory)
|
||||||
|
check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)
|
||||||
|
}
|
||||||
|
|
||||||
|
func optsFor(words ...string) *Options {
|
||||||
|
opts := defaultOptions()
|
||||||
|
parseOptions(opts, words)
|
||||||
|
postProcessOptions(opts)
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggle(t *testing.T) {
|
||||||
|
opts := optsFor()
|
||||||
|
if opts.ToggleSort {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = optsFor("--bind=a:toggle-sort")
|
||||||
|
if !opts.ToggleSort {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = optsFor("--bind=a:toggle-sort", "--bind=a:up")
|
||||||
|
if opts.ToggleSort {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreviewOpts(t *testing.T) {
|
||||||
|
opts := optsFor()
|
||||||
|
if !(opts.Preview.command == "" &&
|
||||||
|
opts.Preview.hidden == false &&
|
||||||
|
opts.Preview.position == posRight &&
|
||||||
|
opts.Preview.size.percent == true &&
|
||||||
|
opts.Preview.size.size == 50) {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
opts = optsFor("--preview", "cat {}", "--preview-window=left:15:hidden")
|
||||||
|
if !(opts.Preview.command == "cat {}" &&
|
||||||
|
opts.Preview.hidden == true &&
|
||||||
|
opts.Preview.position == posLeft &&
|
||||||
|
opts.Preview.size.percent == false &&
|
||||||
|
opts.Preview.size.size == 15+2) {
|
||||||
|
t.Error(opts.Preview)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = optsFor("--preview-window=left:15:hidden", "--preview-window=down")
|
||||||
|
if !(opts.Preview.command == "" &&
|
||||||
|
opts.Preview.hidden == false &&
|
||||||
|
opts.Preview.position == posDown &&
|
||||||
|
opts.Preview.size.percent == true &&
|
||||||
|
opts.Preview.size.size == 50) {
|
||||||
|
t.Error(opts.Preview)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
150
src/pattern.go
150
src/pattern.go
@@ -2,7 +2,6 @@ package fzf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/algo"
|
"github.com/junegunn/fzf/src/algo"
|
||||||
@@ -41,6 +40,7 @@ type termSet []term
|
|||||||
// Pattern represents search pattern
|
// Pattern represents search pattern
|
||||||
type Pattern struct {
|
type Pattern struct {
|
||||||
fuzzy bool
|
fuzzy bool
|
||||||
|
fuzzyAlgo algo.Algo
|
||||||
extended bool
|
extended bool
|
||||||
caseSensitive bool
|
caseSensitive bool
|
||||||
forward bool
|
forward bool
|
||||||
@@ -49,7 +49,7 @@ type Pattern struct {
|
|||||||
cacheable bool
|
cacheable bool
|
||||||
delimiter Delimiter
|
delimiter Delimiter
|
||||||
nth []Range
|
nth []Range
|
||||||
procFun map[termType]func(bool, bool, []rune, []rune) (int, int)
|
procFun map[termType]algo.Algo
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -75,8 +75,8 @@ func clearChunkCache() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildPattern builds Pattern object from the given arguments
|
// BuildPattern builds Pattern object from the given arguments
|
||||||
func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,
|
func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, forward bool,
|
||||||
nth []Range, delimiter Delimiter, runes []rune) *Pattern {
|
cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
|
||||||
|
|
||||||
var asString string
|
var asString string
|
||||||
if extended {
|
if extended {
|
||||||
@@ -90,7 +90,7 @@ func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,
|
|||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
caseSensitive, cacheable := true, true
|
caseSensitive := true
|
||||||
termSets := []termSet{}
|
termSets := []termSet{}
|
||||||
|
|
||||||
if extended {
|
if extended {
|
||||||
@@ -100,7 +100,7 @@ func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,
|
|||||||
for idx, term := range termSet {
|
for idx, term := range termSet {
|
||||||
// If the query contains inverse search terms or OR operators,
|
// If the query contains inverse search terms or OR operators,
|
||||||
// we cannot cache the search scope
|
// we cannot cache the search scope
|
||||||
if idx > 0 || term.inv {
|
if !cacheable || idx > 0 || term.inv {
|
||||||
cacheable = false
|
cacheable = false
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
@@ -117,6 +117,7 @@ func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,
|
|||||||
|
|
||||||
ptr := &Pattern{
|
ptr := &Pattern{
|
||||||
fuzzy: fuzzy,
|
fuzzy: fuzzy,
|
||||||
|
fuzzyAlgo: fuzzyAlgo,
|
||||||
extended: extended,
|
extended: extended,
|
||||||
caseSensitive: caseSensitive,
|
caseSensitive: caseSensitive,
|
||||||
forward: forward,
|
forward: forward,
|
||||||
@@ -125,9 +126,9 @@ func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,
|
|||||||
cacheable: cacheable,
|
cacheable: cacheable,
|
||||||
nth: nth,
|
nth: nth,
|
||||||
delimiter: delimiter,
|
delimiter: delimiter,
|
||||||
procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}
|
procFun: make(map[termType]algo.Algo)}
|
||||||
|
|
||||||
ptr.procFun[termFuzzy] = algo.FuzzyMatch
|
ptr.procFun[termFuzzy] = fuzzyAlgo
|
||||||
ptr.procFun[termEqual] = algo.EqualMatch
|
ptr.procFun[termEqual] = algo.EqualMatch
|
||||||
ptr.procFun[termExact] = algo.ExactMatchNaive
|
ptr.procFun[termExact] = algo.ExactMatchNaive
|
||||||
ptr.procFun[termPrefix] = algo.PrefixMatch
|
ptr.procFun[termPrefix] = algo.PrefixMatch
|
||||||
@@ -162,12 +163,13 @@ func parseTerms(fuzzy bool, caseMode Case, str string) []termSet {
|
|||||||
|
|
||||||
if strings.HasPrefix(text, "!") {
|
if strings.HasPrefix(text, "!") {
|
||||||
inv = true
|
inv = true
|
||||||
|
typ = termExact
|
||||||
text = text[1:]
|
text = text[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(text, "'") {
|
if strings.HasPrefix(text, "'") {
|
||||||
// Flip exactness
|
// Flip exactness
|
||||||
if fuzzy {
|
if fuzzy && !inv {
|
||||||
typ = termExact
|
typ = termExact
|
||||||
text = text[1:]
|
text = text[1:]
|
||||||
} else {
|
} else {
|
||||||
@@ -227,7 +229,7 @@ func (p *Pattern) CacheKey() string {
|
|||||||
}
|
}
|
||||||
cacheableTerms := []string{}
|
cacheableTerms := []string{}
|
||||||
for _, termSet := range p.termSets {
|
for _, termSet := range p.termSets {
|
||||||
if len(termSet) == 1 && !termSet[0].inv {
|
if len(termSet) == 1 && !termSet[0].inv && (p.fuzzy || termSet[0].typ == termExact) {
|
||||||
cacheableTerms = append(cacheableTerms, string(termSet[0].origText))
|
cacheableTerms = append(cacheableTerms, string(termSet[0].origText))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -235,9 +237,7 @@ func (p *Pattern) CacheKey() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Match returns the list of matches Items in the given Chunk
|
// Match returns the list of matches Items in the given Chunk
|
||||||
func (p *Pattern) Match(chunk *Chunk) []*Item {
|
func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []*Result {
|
||||||
space := chunk
|
|
||||||
|
|
||||||
// ChunkCache: Exact match
|
// ChunkCache: Exact match
|
||||||
cacheKey := p.CacheKey()
|
cacheKey := p.CacheKey()
|
||||||
if p.cacheable {
|
if p.cacheable {
|
||||||
@@ -246,7 +246,8 @@ func (p *Pattern) Match(chunk *Chunk) []*Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChunkCache: Prefix/suffix match
|
// Prefix/suffix cache
|
||||||
|
var space []*Result
|
||||||
Loop:
|
Loop:
|
||||||
for idx := 1; idx < len(cacheKey); idx++ {
|
for idx := 1; idx < len(cacheKey); idx++ {
|
||||||
// [---------| ] | [ |---------]
|
// [---------| ] | [ |---------]
|
||||||
@@ -256,14 +257,13 @@ Loop:
|
|||||||
suffix := cacheKey[idx:]
|
suffix := cacheKey[idx:]
|
||||||
for _, substr := range [2]*string{&prefix, &suffix} {
|
for _, substr := range [2]*string{&prefix, &suffix} {
|
||||||
if cached, found := _cache.Find(chunk, *substr); found {
|
if cached, found := _cache.Find(chunk, *substr); found {
|
||||||
cachedChunk := Chunk(cached)
|
space = cached
|
||||||
space = &cachedChunk
|
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
matches := p.matchChunk(space)
|
matches := p.matchChunk(chunk, space, slab)
|
||||||
|
|
||||||
if p.cacheable {
|
if p.cacheable {
|
||||||
_cache.Add(chunk, cacheKey, matches)
|
_cache.Add(chunk, cacheKey, matches)
|
||||||
@@ -271,19 +271,19 @@ Loop:
|
|||||||
return matches
|
return matches
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
func (p *Pattern) matchChunk(chunk *Chunk, space []*Result, slab *util.Slab) []*Result {
|
||||||
matches := []*Item{}
|
matches := []*Result{}
|
||||||
if !p.extended {
|
|
||||||
|
if space == nil {
|
||||||
for _, item := range *chunk {
|
for _, item := range *chunk {
|
||||||
if sidx, eidx, tlen := p.basicMatch(item); sidx >= 0 {
|
if match, _, _ := p.MatchItem(item, false, slab); match != nil {
|
||||||
matches = append(matches,
|
matches = append(matches, match)
|
||||||
dupItem(item, []Offset{Offset{int32(sidx), int32(eidx), int32(tlen)}}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, item := range *chunk {
|
for _, result := range space {
|
||||||
if offsets := p.extendedMatch(item); len(offsets) == len(p.termSets) {
|
if match, _, _ := p.MatchItem(result.item, false, slab); match != nil {
|
||||||
matches = append(matches, dupItem(item, offsets))
|
matches = append(matches, match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -291,58 +291,75 @@ func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MatchItem returns true if the Item is a match
|
// MatchItem returns true if the Item is a match
|
||||||
func (p *Pattern) MatchItem(item *Item) bool {
|
func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) {
|
||||||
if !p.extended {
|
if p.extended {
|
||||||
sidx, _, _ := p.basicMatch(item)
|
if offsets, bonus, trimLen, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) {
|
||||||
return sidx >= 0
|
return buildResult(item, offsets, bonus, trimLen), offsets, pos
|
||||||
|
}
|
||||||
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
offsets := p.extendedMatch(item)
|
offset, bonus, trimLen, pos := p.basicMatch(item, withPos, slab)
|
||||||
return len(offsets) == len(p.termSets)
|
if sidx := offset[0]; sidx >= 0 {
|
||||||
|
offsets := []Offset{offset}
|
||||||
|
return buildResult(item, offsets, bonus, trimLen), offsets, pos
|
||||||
|
}
|
||||||
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dupItem(item *Item, offsets []Offset) *Item {
|
func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, int, *[]int) {
|
||||||
sort.Sort(ByOrder(offsets))
|
|
||||||
return &Item{
|
|
||||||
text: item.text,
|
|
||||||
origText: item.origText,
|
|
||||||
transformed: item.transformed,
|
|
||||||
index: item.index,
|
|
||||||
offsets: offsets,
|
|
||||||
colors: item.colors,
|
|
||||||
rank: Rank{0, 0, item.index}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pattern) basicMatch(item *Item) (int, int, int) {
|
|
||||||
input := p.prepareInput(item)
|
input := p.prepareInput(item)
|
||||||
if p.fuzzy {
|
if p.fuzzy {
|
||||||
return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.forward, p.text)
|
return p.iter(p.fuzzyAlgo, input, p.caseSensitive, p.forward, p.text, withPos, slab)
|
||||||
}
|
}
|
||||||
return p.iter(algo.ExactMatchNaive, input, p.caseSensitive, p.forward, p.text)
|
return p.iter(algo.ExactMatchNaive, input, p.caseSensitive, p.forward, p.text, withPos, slab)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
func (p *Pattern) extendedMatch(item *Item, withPos bool, slab *util.Slab) ([]Offset, int, int, *[]int) {
|
||||||
input := p.prepareInput(item)
|
input := p.prepareInput(item)
|
||||||
offsets := []Offset{}
|
offsets := []Offset{}
|
||||||
|
var totalScore int
|
||||||
|
var totalTrimLen int
|
||||||
|
var allPos *[]int
|
||||||
|
if withPos {
|
||||||
|
allPos = &[]int{}
|
||||||
|
}
|
||||||
for _, termSet := range p.termSets {
|
for _, termSet := range p.termSets {
|
||||||
var offset *Offset
|
var offset Offset
|
||||||
|
var currentScore int
|
||||||
|
var trimLen int
|
||||||
|
matched := false
|
||||||
for _, term := range termSet {
|
for _, term := range termSet {
|
||||||
pfun := p.procFun[term.typ]
|
pfun := p.procFun[term.typ]
|
||||||
if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {
|
off, score, tLen, pos := p.iter(pfun, input, term.caseSensitive, p.forward, term.text, withPos, slab)
|
||||||
|
if sidx := off[0]; sidx >= 0 {
|
||||||
if term.inv {
|
if term.inv {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
offset = &Offset{int32(sidx), int32(eidx), int32(tlen)}
|
offset, currentScore, trimLen = off, score, tLen
|
||||||
|
matched = true
|
||||||
|
if withPos {
|
||||||
|
if pos != nil {
|
||||||
|
*allPos = append(*allPos, *pos...)
|
||||||
|
} else {
|
||||||
|
for idx := off[0]; idx < off[1]; idx++ {
|
||||||
|
*allPos = append(*allPos, int(idx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
break
|
break
|
||||||
} else if term.inv {
|
} else if term.inv {
|
||||||
offset = &Offset{0, 0, 0}
|
offset, currentScore, trimLen = Offset{0, 0}, 0, 0
|
||||||
|
matched = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if offset != nil {
|
if matched {
|
||||||
offsets = append(offsets, *offset)
|
offsets = append(offsets, offset)
|
||||||
|
totalScore += currentScore
|
||||||
|
totalTrimLen += trimLen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return offsets
|
return offsets, totalScore, totalTrimLen, allPos
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) prepareInput(item *Item) []Token {
|
func (p *Pattern) prepareInput(item *Item) []Token {
|
||||||
@@ -351,23 +368,28 @@ func (p *Pattern) prepareInput(item *Item) []Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ret []Token
|
var ret []Token
|
||||||
if len(p.nth) > 0 {
|
if len(p.nth) == 0 {
|
||||||
|
ret = []Token{Token{text: &item.text, prefixLength: 0, trimLength: int32(item.text.TrimLength())}}
|
||||||
|
} else {
|
||||||
tokens := Tokenize(item.text, p.delimiter)
|
tokens := Tokenize(item.text, p.delimiter)
|
||||||
ret = Transform(tokens, p.nth)
|
ret = Transform(tokens, p.nth)
|
||||||
} else {
|
|
||||||
ret = []Token{Token{text: item.text, prefixLength: 0, trimLength: util.TrimLen(item.text)}}
|
|
||||||
}
|
}
|
||||||
item.transformed = ret
|
item.transformed = ret
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),
|
func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, int, *[]int) {
|
||||||
tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int, int) {
|
|
||||||
for _, part := range tokens {
|
for _, part := range tokens {
|
||||||
prefixLength := part.prefixLength
|
if res, pos := pfun(caseSensitive, forward, *part.text, pattern, withPos, slab); res.Start >= 0 {
|
||||||
if sidx, eidx := pfun(caseSensitive, forward, part.text, pattern); sidx >= 0 {
|
sidx := int32(res.Start) + part.prefixLength
|
||||||
return sidx + prefixLength, eidx + prefixLength, part.trimLength
|
eidx := int32(res.End) + part.prefixLength
|
||||||
|
if pos != nil {
|
||||||
|
for idx := range *pos {
|
||||||
|
(*pos)[idx] += int(part.prefixLength)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Offset{sidx, eidx}, res.Score, int(part.trimLength), pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1, -1, -1 // math.MaxUint16
|
return Offset{-1, -1}, 0, -1, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/algo"
|
"github.com/junegunn/fzf/src/algo"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var slab *util.Slab
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
slab = util.MakeSlab(slab16Size, slab32Size)
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseTermsExtended(t *testing.T) {
|
func TestParseTermsExtended(t *testing.T) {
|
||||||
terms := parseTerms(true, CaseSmart,
|
terms := parseTerms(true, CaseSmart,
|
||||||
"| aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | | zzz$ | !ZZZ |")
|
"| aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | | zzz$ | !ZZZ |")
|
||||||
@@ -15,15 +22,15 @@ func TestParseTermsExtended(t *testing.T) {
|
|||||||
terms[1][0].typ != termExact || terms[1][0].inv ||
|
terms[1][0].typ != termExact || terms[1][0].inv ||
|
||||||
terms[2][0].typ != termPrefix || terms[2][0].inv ||
|
terms[2][0].typ != termPrefix || terms[2][0].inv ||
|
||||||
terms[3][0].typ != termSuffix || terms[3][0].inv ||
|
terms[3][0].typ != termSuffix || terms[3][0].inv ||
|
||||||
terms[4][0].typ != termFuzzy || !terms[4][0].inv ||
|
terms[4][0].typ != termExact || !terms[4][0].inv ||
|
||||||
terms[5][0].typ != termExact || !terms[5][0].inv ||
|
terms[5][0].typ != termFuzzy || !terms[5][0].inv ||
|
||||||
terms[6][0].typ != termPrefix || !terms[6][0].inv ||
|
terms[6][0].typ != termPrefix || !terms[6][0].inv ||
|
||||||
terms[7][0].typ != termSuffix || !terms[7][0].inv ||
|
terms[7][0].typ != termSuffix || !terms[7][0].inv ||
|
||||||
terms[7][1].typ != termEqual || terms[7][1].inv ||
|
terms[7][1].typ != termEqual || terms[7][1].inv ||
|
||||||
terms[8][0].typ != termPrefix || terms[8][0].inv ||
|
terms[8][0].typ != termPrefix || terms[8][0].inv ||
|
||||||
terms[8][1].typ != termExact || terms[8][1].inv ||
|
terms[8][1].typ != termExact || terms[8][1].inv ||
|
||||||
terms[8][2].typ != termSuffix || terms[8][2].inv ||
|
terms[8][2].typ != termSuffix || terms[8][2].inv ||
|
||||||
terms[8][3].typ != termFuzzy || !terms[8][3].inv {
|
terms[8][3].typ != termExact || !terms[8][3].inv {
|
||||||
t.Errorf("%s", terms)
|
t.Errorf("%s", terms)
|
||||||
}
|
}
|
||||||
for idx, termSet := range terms[:8] {
|
for idx, termSet := range terms[:8] {
|
||||||
@@ -68,25 +75,31 @@ func TestParseTermsEmpty(t *testing.T) {
|
|||||||
func TestExact(t *testing.T) {
|
func TestExact(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pattern := BuildPattern(true, true, CaseSmart, true,
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true,
|
||||||
[]Range{}, Delimiter{}, []rune("'abc"))
|
[]Range{}, Delimiter{}, []rune("'abc"))
|
||||||
sidx, eidx := algo.ExactMatchNaive(
|
res, pos := algo.ExactMatchNaive(
|
||||||
pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.termSets[0][0].text)
|
pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune("aabbcc abc")), pattern.termSets[0][0].text, true, nil)
|
||||||
if sidx != 7 || eidx != 10 {
|
if res.Start != 7 || res.End != 10 {
|
||||||
t.Errorf("%s / %d / %d", pattern.termSets, sidx, eidx)
|
t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)
|
||||||
|
}
|
||||||
|
if pos != nil {
|
||||||
|
t.Errorf("pos is expected to be nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEqual(t *testing.T) {
|
func TestEqual(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pattern := BuildPattern(true, true, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
||||||
|
|
||||||
match := func(str string, sidxExpected int, eidxExpected int) {
|
match := func(str string, sidxExpected int, eidxExpected int) {
|
||||||
sidx, eidx := algo.EqualMatch(
|
res, pos := algo.EqualMatch(
|
||||||
pattern.caseSensitive, pattern.forward, []rune(str), pattern.termSets[0][0].text)
|
pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune(str)), pattern.termSets[0][0].text, true, nil)
|
||||||
if sidx != sidxExpected || eidx != eidxExpected {
|
if res.Start != sidxExpected || res.End != eidxExpected {
|
||||||
t.Errorf("%s / %d / %d", pattern.termSets, sidx, eidx)
|
t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)
|
||||||
|
}
|
||||||
|
if pos != nil {
|
||||||
|
t.Errorf("pos is expected to be nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match("ABC", -1, -1)
|
match("ABC", -1, -1)
|
||||||
@@ -96,17 +109,17 @@ func TestEqual(t *testing.T) {
|
|||||||
func TestCaseSensitivity(t *testing.T) {
|
func TestCaseSensitivity(t *testing.T) {
|
||||||
defer clearPatternCache()
|
defer clearPatternCache()
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat1 := BuildPattern(true, false, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))
|
pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat2 := BuildPattern(true, false, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))
|
pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat3 := BuildPattern(true, false, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))
|
pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat4 := BuildPattern(true, false, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))
|
pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat5 := BuildPattern(true, false, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))
|
pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
||||||
clearPatternCache()
|
clearPatternCache()
|
||||||
pat6 := BuildPattern(true, false, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))
|
pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
||||||
|
|
||||||
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
||||||
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
||||||
@@ -119,31 +132,42 @@ func TestCaseSensitivity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOrigTextAndTransformed(t *testing.T) {
|
func TestOrigTextAndTransformed(t *testing.T) {
|
||||||
pattern := BuildPattern(true, true, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("jg"))
|
||||||
tokens := Tokenize([]rune("junegunn"), Delimiter{})
|
tokens := Tokenize(util.RunesToChars([]rune("junegunn")), Delimiter{})
|
||||||
trans := Transform(tokens, []Range{Range{1, 1}})
|
trans := Transform(tokens, []Range{Range{1, 1}})
|
||||||
|
|
||||||
origRunes := []rune("junegunn.choi")
|
origBytes := []byte("junegunn.choi")
|
||||||
for _, extended := range []bool{false, true} {
|
for _, extended := range []bool{false, true} {
|
||||||
chunk := Chunk{
|
chunk := Chunk{
|
||||||
&Item{
|
&Item{
|
||||||
text: []rune("junegunn"),
|
text: util.RunesToChars([]rune("junegunn")),
|
||||||
origText: &origRunes,
|
origText: &origBytes,
|
||||||
transformed: trans},
|
transformed: trans},
|
||||||
}
|
}
|
||||||
pattern.extended = extended
|
pattern.extended = extended
|
||||||
matches := pattern.matchChunk(&chunk)
|
matches := pattern.matchChunk(&chunk, nil, slab) // No cache
|
||||||
if string(matches[0].text) != "junegunn" || string(*matches[0].origText) != "junegunn.choi" ||
|
if !(matches[0].item.text.ToString() == "junegunn" &&
|
||||||
matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||
|
string(*matches[0].item.origText) == "junegunn.choi" &&
|
||||||
!reflect.DeepEqual(matches[0].transformed, trans) {
|
reflect.DeepEqual(matches[0].item.transformed, trans)) {
|
||||||
t.Error("Invalid match result", matches)
|
t.Error("Invalid match result", matches)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match, offsets, pos := pattern.MatchItem(chunk[0], true, slab)
|
||||||
|
if !(match.item.text.ToString() == "junegunn" &&
|
||||||
|
string(*match.item.origText) == "junegunn.choi" &&
|
||||||
|
offsets[0][0] == 0 && offsets[0][1] == 5 &&
|
||||||
|
reflect.DeepEqual(match.item.transformed, trans)) {
|
||||||
|
t.Error("Invalid match result", match, offsets, extended)
|
||||||
|
}
|
||||||
|
if !((*pos)[0] == 4 && (*pos)[1] == 0) {
|
||||||
|
t.Error("Invalid pos array", *pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheKey(t *testing.T) {
|
func TestCacheKey(t *testing.T) {
|
||||||
test := func(extended bool, patStr string, expected string, cacheable bool) {
|
test := func(extended bool, patStr string, expected string, cacheable bool) {
|
||||||
pat := BuildPattern(true, extended, CaseSmart, true, []Range{}, Delimiter{}, []rune(patStr))
|
pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, true, true, []Range{}, Delimiter{}, []rune(patStr))
|
||||||
if pat.CacheKey() != expected {
|
if pat.CacheKey() != expected {
|
||||||
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
|
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
|
|
||||||
"github.com/junegunn/fzf/src/util"
|
"github.com/junegunn/fzf/src/util"
|
||||||
)
|
)
|
||||||
@@ -35,14 +34,20 @@ func (r *Reader) feed(src io.Reader) {
|
|||||||
if r.delimNil {
|
if r.delimNil {
|
||||||
delim = '\000'
|
delim = '\000'
|
||||||
}
|
}
|
||||||
reader := bufio.NewReader(src)
|
reader := bufio.NewReaderSize(src, readerBufferSize)
|
||||||
for {
|
for {
|
||||||
// ReadBytes returns err != nil if and only if the returned data does not
|
// ReadBytes returns err != nil if and only if the returned data does not
|
||||||
// end in delim.
|
// end in delim.
|
||||||
bytea, err := reader.ReadBytes(delim)
|
bytea, err := reader.ReadBytes(delim)
|
||||||
|
byteaLen := len(bytea)
|
||||||
if len(bytea) > 0 {
|
if len(bytea) > 0 {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
bytea = bytea[:len(bytea)-1]
|
// get rid of carriage return if under Windows:
|
||||||
|
if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {
|
||||||
|
bytea = bytea[:byteaLen-2]
|
||||||
|
} else {
|
||||||
|
bytea = bytea[:byteaLen-1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if r.pusher(bytea) {
|
if r.pusher(bytea) {
|
||||||
r.eventBox.Set(EvtReadNew, nil)
|
r.eventBox.Set(EvtReadNew, nil)
|
||||||
@@ -59,7 +64,7 @@ func (r *Reader) readFromStdin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readFromCommand(cmd string) {
|
func (r *Reader) readFromCommand(cmd string) {
|
||||||
listCommand := exec.Command("sh", "-c", cmd)
|
listCommand := util.ExecCommand(cmd)
|
||||||
out, err := listCommand.StdoutPipe()
|
out, err := listCommand.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
243
src/result.go
Normal file
243
src/result.go
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/tui"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
||||||
|
type Offset [2]int32
|
||||||
|
|
||||||
|
type colorOffset struct {
|
||||||
|
offset [2]int32
|
||||||
|
color tui.ColorPair
|
||||||
|
attr tui.Attr
|
||||||
|
index int32
|
||||||
|
}
|
||||||
|
|
||||||
|
type rank struct {
|
||||||
|
points [4]uint16
|
||||||
|
index int32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
item *Item
|
||||||
|
rank rank
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildResult(item *Item, offsets []Offset, score int, trimLen int) *Result {
|
||||||
|
if len(offsets) > 1 {
|
||||||
|
sort.Sort(ByOrder(offsets))
|
||||||
|
}
|
||||||
|
|
||||||
|
result := Result{item: item, rank: rank{index: item.index}}
|
||||||
|
numChars := item.text.Length()
|
||||||
|
minBegin := math.MaxUint16
|
||||||
|
maxEnd := 0
|
||||||
|
validOffsetFound := false
|
||||||
|
for _, offset := range offsets {
|
||||||
|
b, e := int(offset[0]), int(offset[1])
|
||||||
|
if b < e {
|
||||||
|
minBegin = util.Min(b, minBegin)
|
||||||
|
maxEnd = util.Max(e, maxEnd)
|
||||||
|
validOffsetFound = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx, criterion := range sortCriteria {
|
||||||
|
val := uint16(math.MaxUint16)
|
||||||
|
switch criterion {
|
||||||
|
case byScore:
|
||||||
|
// Higher is better
|
||||||
|
val = math.MaxUint16 - util.AsUint16(score)
|
||||||
|
case byLength:
|
||||||
|
// If offsets is empty, trimLen will be 0, but we don't care
|
||||||
|
val = util.AsUint16(trimLen)
|
||||||
|
case byBegin, byEnd:
|
||||||
|
if validOffsetFound {
|
||||||
|
whitePrefixLen := 0
|
||||||
|
for idx := 0; idx < numChars; idx++ {
|
||||||
|
r := item.text.Get(idx)
|
||||||
|
whitePrefixLen = idx
|
||||||
|
if idx == minBegin || !unicode.IsSpace(r) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if criterion == byBegin {
|
||||||
|
val = util.AsUint16(minBegin - whitePrefixLen)
|
||||||
|
} else {
|
||||||
|
val = util.AsUint16(math.MaxUint16 - math.MaxUint16*(maxEnd-whitePrefixLen)/trimLen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.rank.points[idx] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort criteria to use. Never changes once fzf is started.
|
||||||
|
var sortCriteria []criterion
|
||||||
|
|
||||||
|
// Index returns ordinal index of the Item
|
||||||
|
func (result *Result) Index() int32 {
|
||||||
|
return result.item.index
|
||||||
|
}
|
||||||
|
|
||||||
|
func minRank() rank {
|
||||||
|
return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, color tui.ColorPair, attr tui.Attr, current bool) []colorOffset {
|
||||||
|
itemColors := result.item.Colors()
|
||||||
|
|
||||||
|
// No ANSI code, or --color=no
|
||||||
|
if len(itemColors) == 0 {
|
||||||
|
var offsets []colorOffset
|
||||||
|
for _, off := range matchOffsets {
|
||||||
|
offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, attr: attr})
|
||||||
|
}
|
||||||
|
return offsets
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find max column
|
||||||
|
var maxCol int32
|
||||||
|
for _, off := range matchOffsets {
|
||||||
|
if off[1] > maxCol {
|
||||||
|
maxCol = off[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, ansi := range itemColors {
|
||||||
|
if ansi.offset[1] > maxCol {
|
||||||
|
maxCol = ansi.offset[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cols := make([]int, maxCol)
|
||||||
|
|
||||||
|
for colorIndex, ansi := range itemColors {
|
||||||
|
for i := ansi.offset[0]; i < ansi.offset[1]; i++ {
|
||||||
|
cols[i] = colorIndex + 1 // XXX
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, off := range matchOffsets {
|
||||||
|
for i := off[0]; i < off[1]; i++ {
|
||||||
|
cols[i] = -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort.Sort(ByOrder(offsets))
|
||||||
|
|
||||||
|
// Merge offsets
|
||||||
|
// ------------ ---- -- ----
|
||||||
|
// ++++++++ ++++++++++
|
||||||
|
// --++++++++-- --++++++++++---
|
||||||
|
curr := 0
|
||||||
|
start := 0
|
||||||
|
var colors []colorOffset
|
||||||
|
add := func(idx int) {
|
||||||
|
if curr != 0 && idx > start {
|
||||||
|
if curr == -1 {
|
||||||
|
colors = append(colors, colorOffset{
|
||||||
|
offset: [2]int32{int32(start), int32(idx)}, color: color, attr: attr})
|
||||||
|
} else {
|
||||||
|
ansi := itemColors[curr-1]
|
||||||
|
fg := ansi.color.fg
|
||||||
|
bg := ansi.color.bg
|
||||||
|
if theme != nil {
|
||||||
|
if fg == -1 {
|
||||||
|
if current {
|
||||||
|
fg = theme.Current
|
||||||
|
} else {
|
||||||
|
fg = theme.Fg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bg == -1 {
|
||||||
|
if current {
|
||||||
|
bg = theme.DarkBg
|
||||||
|
} else {
|
||||||
|
bg = theme.Bg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
colors = append(colors, colorOffset{
|
||||||
|
offset: [2]int32{int32(start), int32(idx)},
|
||||||
|
color: tui.PairFor(fg, bg),
|
||||||
|
attr: ansi.color.attr.Merge(attr)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for idx, col := range cols {
|
||||||
|
if col != curr {
|
||||||
|
add(idx)
|
||||||
|
start = idx
|
||||||
|
curr = col
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(int(maxCol))
|
||||||
|
return colors
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 []*Result
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return compareRanks((*a[i]).rank, (*a[j]).rank, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByRelevanceTac is for sorting Items
|
||||||
|
type ByRelevanceTac []*Result
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return compareRanks((*a[i]).rank, (*a[j]).rank, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareRanks(irank rank, jrank rank, tac bool) bool {
|
||||||
|
for idx := 0; idx < 4; idx++ {
|
||||||
|
left := irank.points[idx]
|
||||||
|
right := jrank.points[idx]
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
} else if left > right {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (irank.index <= jrank.index) != tac
|
||||||
|
}
|
||||||
125
src/result_test.go
Normal file
125
src/result_test.go
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
// +build !tcell
|
||||||
|
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/tui"
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
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) {
|
||||||
|
rank := func(vals ...uint16) rank {
|
||||||
|
return rank{
|
||||||
|
points: [4]uint16{vals[0], vals[1], vals[2], vals[3]},
|
||||||
|
index: int32(vals[4])}
|
||||||
|
}
|
||||||
|
if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), false) ||
|
||||||
|
!compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||
|
||||||
|
!compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), false) ||
|
||||||
|
!compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {
|
||||||
|
t.Error("Invalid order")
|
||||||
|
}
|
||||||
|
|
||||||
|
if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), true) ||
|
||||||
|
!compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||
|
||||||
|
!compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), true) ||
|
||||||
|
!compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {
|
||||||
|
t.Error("Invalid order (tac)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match length, string length, index
|
||||||
|
func TestResultRank(t *testing.T) {
|
||||||
|
// FIXME global
|
||||||
|
sortCriteria = []criterion{byScore, byLength}
|
||||||
|
|
||||||
|
strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
|
||||||
|
item1 := buildResult(&Item{text: util.RunesToChars(strs[0]), index: 1}, []Offset{}, 2, 3)
|
||||||
|
if item1.rank.points[0] != math.MaxUint16-2 || // Bonus
|
||||||
|
item1.rank.points[1] != 3 || // Length
|
||||||
|
item1.rank.points[2] != 0 || // Unused
|
||||||
|
item1.rank.points[3] != 0 || // Unused
|
||||||
|
item1.item.index != 1 {
|
||||||
|
t.Error(item1.rank)
|
||||||
|
}
|
||||||
|
// Only differ in index
|
||||||
|
item2 := buildResult(&Item{text: util.RunesToChars(strs[0])}, []Offset{}, 2, 3)
|
||||||
|
|
||||||
|
items := []*Result{item1, item2}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if items[0] != item2 || items[1] != item1 {
|
||||||
|
t.Error(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
items = []*Result{item2, item1, item1, item2}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if items[0] != item2 || items[1] != item2 ||
|
||||||
|
items[2] != item1 || items[3] != item1 {
|
||||||
|
t.Error(items, item1, item1.item.index, item2, item2.item.index)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by relevance
|
||||||
|
item3 := buildResult(&Item{index: 2}, []Offset{Offset{1, 3}, Offset{5, 7}}, 3, 0)
|
||||||
|
item4 := buildResult(&Item{index: 2}, []Offset{Offset{1, 2}, Offset{6, 7}}, 4, 0)
|
||||||
|
item5 := buildResult(&Item{index: 2}, []Offset{Offset{1, 3}, Offset{5, 7}}, 5, 0)
|
||||||
|
item6 := buildResult(&Item{index: 2}, []Offset{Offset{1, 2}, Offset{6, 7}}, 6, 0)
|
||||||
|
items = []*Result{item1, item2, item3, item4, item5, item6}
|
||||||
|
sort.Sort(ByRelevance(items))
|
||||||
|
if !(items[0] == item6 && items[1] == item5 &&
|
||||||
|
items[2] == item4 && items[3] == item3 &&
|
||||||
|
items[4] == item2 && items[5] == item1) {
|
||||||
|
t.Error(items, item1, item2, item3, item4, item5, item6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestColorOffset(t *testing.T) {
|
||||||
|
// ------------ 20 ---- -- ----
|
||||||
|
// ++++++++ ++++++++++
|
||||||
|
// --++++++++-- --++++++++++---
|
||||||
|
|
||||||
|
offsets := []Offset{Offset{5, 15}, Offset{25, 35}}
|
||||||
|
item := Result{
|
||||||
|
item: &Item{
|
||||||
|
colors: &[]ansiOffset{
|
||||||
|
ansiOffset{[2]int32{0, 20}, ansiState{1, 5, 0}},
|
||||||
|
ansiOffset{[2]int32{22, 27}, ansiState{2, 6, tui.Bold}},
|
||||||
|
ansiOffset{[2]int32{30, 32}, ansiState{3, 7, 0}},
|
||||||
|
ansiOffset{[2]int32{33, 40}, ansiState{4, 8, tui.Bold}}}}}
|
||||||
|
// [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]
|
||||||
|
|
||||||
|
colors := item.colorOffsets(offsets, tui.Dark256, 99, 0, true)
|
||||||
|
assert := func(idx int, b int32, e int32, c tui.ColorPair, bold bool) {
|
||||||
|
var attr tui.Attr
|
||||||
|
if bold {
|
||||||
|
attr = tui.Bold
|
||||||
|
}
|
||||||
|
o := colors[idx]
|
||||||
|
if o.offset[0] != b || o.offset[1] != e || o.color != c || o.attr != attr {
|
||||||
|
t.Error(o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(0, 0, 5, tui.ColUser, false)
|
||||||
|
assert(1, 5, 15, 99, false)
|
||||||
|
assert(2, 15, 20, tui.ColUser, false)
|
||||||
|
assert(3, 22, 25, tui.ColUser+1, true)
|
||||||
|
assert(4, 25, 35, 99, false)
|
||||||
|
assert(5, 35, 40, tui.ColUser+2, true)
|
||||||
|
}
|
||||||
792
src/terminal.go
792
src/terminal.go
File diff suppressed because it is too large
Load Diff
73
src/terminal_test.go
Normal file
73
src/terminal_test.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newItem(str string) *Item {
|
||||||
|
bytes := []byte(str)
|
||||||
|
trimmed, _, _ := extractColor(str, nil, nil)
|
||||||
|
return &Item{origText: &bytes, text: util.RunesToChars([]rune(trimmed))}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplacePlaceholder(t *testing.T) {
|
||||||
|
items1 := []*Item{newItem(" foo'bar \x1b[31mbaz\x1b[m")}
|
||||||
|
items2 := []*Item{
|
||||||
|
newItem("foo'bar \x1b[31mbaz\x1b[m"),
|
||||||
|
newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}
|
||||||
|
|
||||||
|
var result string
|
||||||
|
check := func(expected string) {
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("expected: %s, actual: %s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// {}, preserve ansi
|
||||||
|
result = replacePlaceholder("echo {}", false, Delimiter{}, "query", items1)
|
||||||
|
check("echo ' foo'\\''bar \x1b[31mbaz\x1b[m'")
|
||||||
|
|
||||||
|
// {}, strip ansi
|
||||||
|
result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items1)
|
||||||
|
check("echo ' foo'\\''bar baz'")
|
||||||
|
|
||||||
|
// {}, with multiple items
|
||||||
|
result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items2)
|
||||||
|
check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ'")
|
||||||
|
|
||||||
|
// {..}, strip leading whitespaces, preserve ansi
|
||||||
|
result = replacePlaceholder("echo {..}", false, Delimiter{}, "query", items1)
|
||||||
|
check("echo 'foo'\\''bar \x1b[31mbaz\x1b[m'")
|
||||||
|
|
||||||
|
// {..}, strip leading whitespaces, strip ansi
|
||||||
|
result = replacePlaceholder("echo {..}", true, Delimiter{}, "query", items1)
|
||||||
|
check("echo 'foo'\\''bar baz'")
|
||||||
|
|
||||||
|
// {q}
|
||||||
|
result = replacePlaceholder("echo {} {q}", true, Delimiter{}, "query", items1)
|
||||||
|
check("echo ' foo'\\''bar baz' 'query'")
|
||||||
|
|
||||||
|
// {q}, multiple items
|
||||||
|
result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, "query 'string'", items2)
|
||||||
|
check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ''query '\\''string'\\''''foo'\\''bar baz' 'FOO'\\''BAR BAZ'")
|
||||||
|
|
||||||
|
result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items1)
|
||||||
|
check("echo 'foo'\\''bar'/'baz'/'bazfoo'\\''bar'/'baz'/'foo'\\''bar'/' foo'\\''bar baz'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")
|
||||||
|
|
||||||
|
result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items2)
|
||||||
|
check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")
|
||||||
|
|
||||||
|
// String delimiter
|
||||||
|
delim := "'"
|
||||||
|
result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, "query", items1)
|
||||||
|
check("echo ' foo'\\''bar baz'/'foo'/'bar baz'")
|
||||||
|
|
||||||
|
// Regex delimiter
|
||||||
|
regex := regexp.MustCompile("[oa]+")
|
||||||
|
// foo'bar baz
|
||||||
|
result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, "query", items1)
|
||||||
|
check("echo ' foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")
|
||||||
|
}
|
||||||
13
src/terminal_unix.go
Normal file
13
src/terminal_unix.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func notifyOnResize(resizeChan chan<- os.Signal) {
|
||||||
|
signal.Notify(resizeChan, syscall.SIGWINCH)
|
||||||
|
}
|
||||||
11
src/terminal_windows.go
Normal file
11
src/terminal_windows.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package fzf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func notifyOnResize(resizeChan chan<- os.Signal) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
102
src/tokenizer.go
102
src/tokenizer.go
@@ -18,9 +18,9 @@ type Range struct {
|
|||||||
|
|
||||||
// Token contains the tokenized part of the strings and its prefix length
|
// Token contains the tokenized part of the strings and its prefix length
|
||||||
type Token struct {
|
type Token struct {
|
||||||
text []rune
|
text *util.Chars
|
||||||
prefixLength int
|
prefixLength int32
|
||||||
trimLength int
|
trimLength int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delimiter for tokenizing the input
|
// Delimiter for tokenizing the input
|
||||||
@@ -75,15 +75,14 @@ func ParseRange(str *string) (Range, bool) {
|
|||||||
return newRange(n, n), true
|
return newRange(n, n), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func withPrefixLengths(tokens [][]rune, begin int) []Token {
|
func withPrefixLengths(tokens []util.Chars, begin int) []Token {
|
||||||
ret := make([]Token, len(tokens))
|
ret := make([]Token, len(tokens))
|
||||||
|
|
||||||
prefixLength := begin
|
prefixLength := begin
|
||||||
for idx, token := range tokens {
|
for idx, token := range tokens {
|
||||||
// Need to define a new local variable instead of the reused token to take
|
// NOTE: &tokens[idx] instead of &tokens
|
||||||
// the pointer to it
|
ret[idx] = Token{&tokens[idx], int32(prefixLength), int32(token.TrimLength())}
|
||||||
ret[idx] = Token{token, prefixLength, util.TrimLen(token)}
|
prefixLength += token.Length()
|
||||||
prefixLength += len(token)
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@@ -94,59 +93,60 @@ const (
|
|||||||
awkWhite
|
awkWhite
|
||||||
)
|
)
|
||||||
|
|
||||||
func awkTokenizer(input []rune) ([][]rune, int) {
|
func awkTokenizer(input util.Chars) ([]util.Chars, int) {
|
||||||
// 9, 32
|
// 9, 32
|
||||||
ret := [][]rune{}
|
ret := []util.Chars{}
|
||||||
str := []rune{}
|
|
||||||
prefixLength := 0
|
prefixLength := 0
|
||||||
state := awkNil
|
state := awkNil
|
||||||
for _, r := range input {
|
numChars := input.Length()
|
||||||
|
begin := 0
|
||||||
|
end := 0
|
||||||
|
for idx := 0; idx < numChars; idx++ {
|
||||||
|
r := input.Get(idx)
|
||||||
white := r == 9 || r == 32
|
white := r == 9 || r == 32
|
||||||
switch state {
|
switch state {
|
||||||
case awkNil:
|
case awkNil:
|
||||||
if white {
|
if white {
|
||||||
prefixLength++
|
prefixLength++
|
||||||
} else {
|
} else {
|
||||||
state = awkBlack
|
state, begin, end = awkBlack, idx, idx+1
|
||||||
str = append(str, r)
|
|
||||||
}
|
}
|
||||||
case awkBlack:
|
case awkBlack:
|
||||||
str = append(str, r)
|
end = idx + 1
|
||||||
if white {
|
if white {
|
||||||
state = awkWhite
|
state = awkWhite
|
||||||
}
|
}
|
||||||
case awkWhite:
|
case awkWhite:
|
||||||
if white {
|
if white {
|
||||||
str = append(str, r)
|
end = idx + 1
|
||||||
} else {
|
} else {
|
||||||
ret = append(ret, str)
|
ret = append(ret, input.Slice(begin, end))
|
||||||
state = awkBlack
|
state, begin, end = awkBlack, idx, idx+1
|
||||||
str = []rune{r}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(str) > 0 {
|
if begin < end {
|
||||||
ret = append(ret, str)
|
ret = append(ret, input.Slice(begin, end))
|
||||||
}
|
}
|
||||||
return ret, prefixLength
|
return ret, prefixLength
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tokenize tokenizes the given string with the delimiter
|
// Tokenize tokenizes the given string with the delimiter
|
||||||
func Tokenize(runes []rune, delimiter Delimiter) []Token {
|
func Tokenize(text util.Chars, delimiter Delimiter) []Token {
|
||||||
if delimiter.str == nil && delimiter.regex == nil {
|
if delimiter.str == nil && delimiter.regex == nil {
|
||||||
// AWK-style (\S+\s*)
|
// AWK-style (\S+\s*)
|
||||||
tokens, prefixLength := awkTokenizer(runes)
|
tokens, prefixLength := awkTokenizer(text)
|
||||||
return withPrefixLengths(tokens, prefixLength)
|
return withPrefixLengths(tokens, prefixLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokens []string
|
|
||||||
if delimiter.str != nil {
|
if delimiter.str != nil {
|
||||||
tokens = strings.Split(string(runes), *delimiter.str)
|
return withPrefixLengths(text.Split(*delimiter.str), 0)
|
||||||
for i := 0; i < len(tokens)-1; i++ {
|
}
|
||||||
tokens[i] = tokens[i] + *delimiter.str
|
|
||||||
}
|
// FIXME performance
|
||||||
} else if delimiter.regex != nil {
|
var tokens []string
|
||||||
str := string(runes)
|
if delimiter.regex != nil {
|
||||||
|
str := text.ToString()
|
||||||
for len(str) > 0 {
|
for len(str) > 0 {
|
||||||
loc := delimiter.regex.FindStringIndex(str)
|
loc := delimiter.regex.FindStringIndex(str)
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
@@ -157,9 +157,9 @@ func Tokenize(runes []rune, delimiter Delimiter) []Token {
|
|||||||
str = str[last:]
|
str = str[last:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
asRunes := make([][]rune, len(tokens))
|
asRunes := make([]util.Chars, len(tokens))
|
||||||
for i, token := range tokens {
|
for i, token := range tokens {
|
||||||
asRunes[i] = []rune(token)
|
asRunes[i] = util.RunesToChars([]rune(token))
|
||||||
}
|
}
|
||||||
return withPrefixLengths(asRunes, 0)
|
return withPrefixLengths(asRunes, 0)
|
||||||
}
|
}
|
||||||
@@ -167,15 +167,7 @@ func Tokenize(runes []rune, delimiter Delimiter) []Token {
|
|||||||
func joinTokens(tokens []Token) []rune {
|
func joinTokens(tokens []Token) []rune {
|
||||||
ret := []rune{}
|
ret := []rune{}
|
||||||
for _, token := range tokens {
|
for _, token := range tokens {
|
||||||
ret = append(ret, token.text...)
|
ret = append(ret, token.text.ToRunes()...)
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func joinTokensAsRunes(tokens []Token) []rune {
|
|
||||||
ret := []rune{}
|
|
||||||
for _, token := range tokens {
|
|
||||||
ret = append(ret, token.text...)
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@@ -185,19 +177,20 @@ func Transform(tokens []Token, withNth []Range) []Token {
|
|||||||
transTokens := make([]Token, len(withNth))
|
transTokens := make([]Token, len(withNth))
|
||||||
numTokens := len(tokens)
|
numTokens := len(tokens)
|
||||||
for idx, r := range withNth {
|
for idx, r := range withNth {
|
||||||
part := []rune{}
|
parts := []*util.Chars{}
|
||||||
minIdx := 0
|
minIdx := 0
|
||||||
if r.begin == r.end {
|
if r.begin == r.end {
|
||||||
idx := r.begin
|
idx := r.begin
|
||||||
if idx == rangeEllipsis {
|
if idx == rangeEllipsis {
|
||||||
part = append(part, joinTokensAsRunes(tokens)...)
|
chars := util.RunesToChars(joinTokens(tokens))
|
||||||
|
parts = append(parts, &chars)
|
||||||
} else {
|
} else {
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
idx += numTokens + 1
|
idx += numTokens + 1
|
||||||
}
|
}
|
||||||
if idx >= 1 && idx <= numTokens {
|
if idx >= 1 && idx <= numTokens {
|
||||||
minIdx = idx - 1
|
minIdx = idx - 1
|
||||||
part = append(part, tokens[idx-1].text...)
|
parts = append(parts, tokens[idx-1].text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -224,17 +217,32 @@ func Transform(tokens []Token, withNth []Range) []Token {
|
|||||||
minIdx = util.Max(0, begin-1)
|
minIdx = util.Max(0, begin-1)
|
||||||
for idx := begin; idx <= end; idx++ {
|
for idx := begin; idx <= end; idx++ {
|
||||||
if idx >= 1 && idx <= numTokens {
|
if idx >= 1 && idx <= numTokens {
|
||||||
part = append(part, tokens[idx-1].text...)
|
parts = append(parts, tokens[idx-1].text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var prefixLength int
|
// Merge multiple parts
|
||||||
|
var merged util.Chars
|
||||||
|
switch len(parts) {
|
||||||
|
case 0:
|
||||||
|
merged = util.RunesToChars([]rune{})
|
||||||
|
case 1:
|
||||||
|
merged = *parts[0]
|
||||||
|
default:
|
||||||
|
runes := []rune{}
|
||||||
|
for _, part := range parts {
|
||||||
|
runes = append(runes, part.ToRunes()...)
|
||||||
|
}
|
||||||
|
merged = util.RunesToChars(runes)
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefixLength int32
|
||||||
if minIdx < numTokens {
|
if minIdx < numTokens {
|
||||||
prefixLength = tokens[minIdx].prefixLength
|
prefixLength = tokens[minIdx].prefixLength
|
||||||
} else {
|
} else {
|
||||||
prefixLength = 0
|
prefixLength = 0
|
||||||
}
|
}
|
||||||
transTokens[idx] = Token{part, prefixLength, util.TrimLen(part)}
|
transTokens[idx] = Token{&merged, prefixLength, int32(merged.TrimLength())}
|
||||||
}
|
}
|
||||||
return transTokens
|
return transTokens
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/junegunn/fzf/src/util"
|
||||||
|
)
|
||||||
|
|
||||||
func TestParseRange(t *testing.T) {
|
func TestParseRange(t *testing.T) {
|
||||||
{
|
{
|
||||||
@@ -43,23 +47,23 @@ func TestParseRange(t *testing.T) {
|
|||||||
func TestTokenize(t *testing.T) {
|
func TestTokenize(t *testing.T) {
|
||||||
// AWK-style
|
// AWK-style
|
||||||
input := " abc: def: ghi "
|
input := " abc: def: ghi "
|
||||||
tokens := Tokenize([]rune(input), Delimiter{})
|
tokens := Tokenize(util.RunesToChars([]rune(input)), Delimiter{})
|
||||||
if string(tokens[0].text) != "abc: " || tokens[0].prefixLength != 2 || tokens[0].trimLength != 4 {
|
if tokens[0].text.ToString() != "abc: " || tokens[0].prefixLength != 2 || tokens[0].trimLength != 4 {
|
||||||
t.Errorf("%s", tokens)
|
t.Errorf("%s", tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With delimiter
|
// With delimiter
|
||||||
tokens = Tokenize([]rune(input), delimiterRegexp(":"))
|
tokens = Tokenize(util.RunesToChars([]rune(input)), delimiterRegexp(":"))
|
||||||
if string(tokens[0].text) != " abc:" || tokens[0].prefixLength != 0 || tokens[0].trimLength != 4 {
|
if tokens[0].text.ToString() != " abc:" || tokens[0].prefixLength != 0 || tokens[0].trimLength != 4 {
|
||||||
t.Errorf("%s", tokens)
|
t.Errorf("%s", tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With delimiter regex
|
// With delimiter regex
|
||||||
tokens = Tokenize([]rune(input), delimiterRegexp("\\s+"))
|
tokens = Tokenize(util.RunesToChars([]rune(input)), delimiterRegexp("\\s+"))
|
||||||
if string(tokens[0].text) != " " || tokens[0].prefixLength != 0 || tokens[0].trimLength != 0 ||
|
if tokens[0].text.ToString() != " " || tokens[0].prefixLength != 0 || tokens[0].trimLength != 0 ||
|
||||||
string(tokens[1].text) != "abc: " || tokens[1].prefixLength != 2 || tokens[1].trimLength != 4 ||
|
tokens[1].text.ToString() != "abc: " || tokens[1].prefixLength != 2 || tokens[1].trimLength != 4 ||
|
||||||
string(tokens[2].text) != "def: " || tokens[2].prefixLength != 8 || tokens[2].trimLength != 4 ||
|
tokens[2].text.ToString() != "def: " || tokens[2].prefixLength != 8 || tokens[2].trimLength != 4 ||
|
||||||
string(tokens[3].text) != "ghi " || tokens[3].prefixLength != 14 || tokens[3].trimLength != 3 {
|
tokens[3].text.ToString() != "ghi " || tokens[3].prefixLength != 14 || tokens[3].trimLength != 3 {
|
||||||
t.Errorf("%s", tokens)
|
t.Errorf("%s", tokens)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +71,7 @@ func TestTokenize(t *testing.T) {
|
|||||||
func TestTransform(t *testing.T) {
|
func TestTransform(t *testing.T) {
|
||||||
input := " abc: def: ghi: jkl"
|
input := " abc: def: ghi: jkl"
|
||||||
{
|
{
|
||||||
tokens := Tokenize([]rune(input), Delimiter{})
|
tokens := Tokenize(util.RunesToChars([]rune(input)), Delimiter{})
|
||||||
{
|
{
|
||||||
ranges := splitNth("1,2,3")
|
ranges := splitNth("1,2,3")
|
||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
@@ -80,25 +84,25 @@ func TestTransform(t *testing.T) {
|
|||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
if string(joinTokens(tx)) != "abc: def: ghi: def: ghi: jklabc: " ||
|
if string(joinTokens(tx)) != "abc: def: ghi: def: ghi: jklabc: " ||
|
||||||
len(tx) != 4 ||
|
len(tx) != 4 ||
|
||||||
string(tx[0].text) != "abc: def: " || tx[0].prefixLength != 2 ||
|
tx[0].text.ToString() != "abc: def: " || tx[0].prefixLength != 2 ||
|
||||||
string(tx[1].text) != "ghi: " || tx[1].prefixLength != 14 ||
|
tx[1].text.ToString() != "ghi: " || tx[1].prefixLength != 14 ||
|
||||||
string(tx[2].text) != "def: ghi: jkl" || tx[2].prefixLength != 8 ||
|
tx[2].text.ToString() != "def: ghi: jkl" || tx[2].prefixLength != 8 ||
|
||||||
string(tx[3].text) != "abc: " || tx[3].prefixLength != 2 {
|
tx[3].text.ToString() != "abc: " || tx[3].prefixLength != 2 {
|
||||||
t.Errorf("%s", tx)
|
t.Errorf("%s", tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
tokens := Tokenize([]rune(input), delimiterRegexp(":"))
|
tokens := Tokenize(util.RunesToChars([]rune(input)), delimiterRegexp(":"))
|
||||||
{
|
{
|
||||||
ranges := splitNth("1..2,3,2..,1")
|
ranges := splitNth("1..2,3,2..,1")
|
||||||
tx := Transform(tokens, ranges)
|
tx := Transform(tokens, ranges)
|
||||||
if string(joinTokens(tx)) != " abc: def: ghi: def: ghi: jkl abc:" ||
|
if string(joinTokens(tx)) != " abc: def: ghi: def: ghi: jkl abc:" ||
|
||||||
len(tx) != 4 ||
|
len(tx) != 4 ||
|
||||||
string(tx[0].text) != " abc: def:" || tx[0].prefixLength != 0 ||
|
tx[0].text.ToString() != " abc: def:" || tx[0].prefixLength != 0 ||
|
||||||
string(tx[1].text) != " ghi:" || tx[1].prefixLength != 12 ||
|
tx[1].text.ToString() != " ghi:" || tx[1].prefixLength != 12 ||
|
||||||
string(tx[2].text) != " def: ghi: jkl" || tx[2].prefixLength != 6 ||
|
tx[2].text.ToString() != " def: ghi: jkl" || tx[2].prefixLength != 6 ||
|
||||||
string(tx[3].text) != " abc:" || tx[3].prefixLength != 0 {
|
tx[3].text.ToString() != " abc:" || tx[3].prefixLength != 0 {
|
||||||
t.Errorf("%s", tx)
|
t.Errorf("%s", tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
449
src/tui/ncurses.go
Normal file
449
src/tui/ncurses.go
Normal file
@@ -0,0 +1,449 @@
|
|||||||
|
// +build !windows
|
||||||
|
// +build !tcell
|
||||||
|
|
||||||
|
package tui
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#cgo !static LDFLAGS: -lncurses
|
||||||
|
#cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl
|
||||||
|
#cgo android static LDFLAGS: -l:libncurses.a -fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch
|
||||||
|
|
||||||
|
FILE* c_tty() {
|
||||||
|
return fopen("/dev/tty", "r");
|
||||||
|
}
|
||||||
|
|
||||||
|
SCREEN* c_newterm(FILE* tty) {
|
||||||
|
return newterm(NULL, stderr, tty);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ColorPair int16
|
||||||
|
type Attr C.int
|
||||||
|
type WindowImpl C.WINDOW
|
||||||
|
|
||||||
|
const (
|
||||||
|
Bold Attr = C.A_BOLD
|
||||||
|
Dim = C.A_DIM
|
||||||
|
Blink = C.A_BLINK
|
||||||
|
Reverse = C.A_REVERSE
|
||||||
|
Underline = C.A_UNDERLINE
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AttrRegular Attr = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pallete
|
||||||
|
const (
|
||||||
|
ColDefault ColorPair = iota
|
||||||
|
ColNormal
|
||||||
|
ColPrompt
|
||||||
|
ColMatch
|
||||||
|
ColCurrent
|
||||||
|
ColCurrentMatch
|
||||||
|
ColSpinner
|
||||||
|
ColInfo
|
||||||
|
ColCursor
|
||||||
|
ColSelected
|
||||||
|
ColHeader
|
||||||
|
ColBorder
|
||||||
|
ColUser // Should be the last entry
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_screen *C.SCREEN
|
||||||
|
_colorMap map[int]ColorPair
|
||||||
|
_colorFn func(ColorPair, Attr) C.int
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_colorMap = make(map[int]ColorPair)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Attr) Merge(b Attr) Attr {
|
||||||
|
return a | b
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultTheme() *ColorTheme {
|
||||||
|
if C.tigetnum(C.CString("colors")) >= 256 {
|
||||||
|
return Dark256
|
||||||
|
}
|
||||||
|
return Default16
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init(theme *ColorTheme, black bool, mouse bool) {
|
||||||
|
C.setlocale(C.LC_ALL, C.CString(""))
|
||||||
|
tty := C.c_tty()
|
||||||
|
if tty == nil {
|
||||||
|
fmt.Println("Failed to open /dev/tty")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
_screen = C.c_newterm(tty)
|
||||||
|
if _screen == nil {
|
||||||
|
fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
C.set_term(_screen)
|
||||||
|
if mouse {
|
||||||
|
C.mousemask(C.ALL_MOUSE_EVENTS, nil)
|
||||||
|
C.mouseinterval(0)
|
||||||
|
}
|
||||||
|
C.noecho()
|
||||||
|
C.raw() // stty dsusp undef
|
||||||
|
C.nonl()
|
||||||
|
C.keypad(C.stdscr, true)
|
||||||
|
C.set_escdelay(100)
|
||||||
|
C.timeout(100) // ESCDELAY 100ms + timeout 100ms
|
||||||
|
|
||||||
|
_color = theme != nil
|
||||||
|
if _color {
|
||||||
|
C.start_color()
|
||||||
|
InitTheme(theme, black)
|
||||||
|
initPairs(theme)
|
||||||
|
C.bkgd(C.chtype(C.COLOR_PAIR(C.int(ColNormal))))
|
||||||
|
_colorFn = attrColored
|
||||||
|
} else {
|
||||||
|
_colorFn = attrMono
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initPairs(theme *ColorTheme) {
|
||||||
|
C.assume_default_colors(C.int(theme.Fg), C.int(theme.Bg))
|
||||||
|
initPair := func(group ColorPair, fg Color, bg Color) {
|
||||||
|
C.init_pair(C.short(group), C.short(fg), C.short(bg))
|
||||||
|
}
|
||||||
|
initPair(ColNormal, theme.Fg, theme.Bg)
|
||||||
|
initPair(ColPrompt, theme.Prompt, theme.Bg)
|
||||||
|
initPair(ColMatch, theme.Match, theme.Bg)
|
||||||
|
initPair(ColCurrent, theme.Current, theme.DarkBg)
|
||||||
|
initPair(ColCurrentMatch, theme.CurrentMatch, theme.DarkBg)
|
||||||
|
initPair(ColSpinner, theme.Spinner, theme.Bg)
|
||||||
|
initPair(ColInfo, theme.Info, theme.Bg)
|
||||||
|
initPair(ColCursor, theme.Cursor, theme.DarkBg)
|
||||||
|
initPair(ColSelected, theme.Selected, theme.DarkBg)
|
||||||
|
initPair(ColHeader, theme.Header, theme.Bg)
|
||||||
|
initPair(ColBorder, theme.Border, theme.Bg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pause() {
|
||||||
|
C.endwin()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Resume() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close() {
|
||||||
|
C.endwin()
|
||||||
|
C.delscreen(_screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWindow(top int, left int, width int, height int, border bool) *Window {
|
||||||
|
win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))
|
||||||
|
if _color {
|
||||||
|
C.wbkgd(win, C.chtype(C.COLOR_PAIR(C.int(ColNormal))))
|
||||||
|
}
|
||||||
|
if border {
|
||||||
|
attr := _colorFn(ColBorder, 0)
|
||||||
|
C.wattron(win, attr)
|
||||||
|
C.box(win, 0, 0)
|
||||||
|
C.wattroff(win, attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Window{
|
||||||
|
impl: (*WindowImpl)(win),
|
||||||
|
Top: top,
|
||||||
|
Left: left,
|
||||||
|
Width: width,
|
||||||
|
Height: height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func attrColored(pair ColorPair, a Attr) C.int {
|
||||||
|
var attr C.int
|
||||||
|
if pair > 0 {
|
||||||
|
attr = C.COLOR_PAIR(C.int(pair))
|
||||||
|
}
|
||||||
|
return attr | C.int(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func attrMono(pair ColorPair, a Attr) C.int {
|
||||||
|
var attr C.int
|
||||||
|
switch pair {
|
||||||
|
case ColCurrent:
|
||||||
|
attr = C.A_REVERSE
|
||||||
|
case ColMatch:
|
||||||
|
attr = C.A_UNDERLINE
|
||||||
|
case ColCurrentMatch:
|
||||||
|
attr = C.A_UNDERLINE | C.A_REVERSE
|
||||||
|
}
|
||||||
|
if C.int(a)&C.A_BOLD == C.A_BOLD {
|
||||||
|
attr = attr | C.A_BOLD
|
||||||
|
}
|
||||||
|
return attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxX() int {
|
||||||
|
return int(C.COLS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxY() int {
|
||||||
|
return int(C.LINES)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) win() *C.WINDOW {
|
||||||
|
return (*C.WINDOW)(w.impl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Close() {
|
||||||
|
C.delwin(w.win())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Enclose(y int, x int) bool {
|
||||||
|
return bool(C.wenclose(w.win(), C.int(y), C.int(x)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Move(y int, x int) {
|
||||||
|
C.wmove(w.win(), C.int(y), C.int(x))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) MoveAndClear(y int, x int) {
|
||||||
|
w.Move(y, x)
|
||||||
|
C.wclrtoeol(w.win())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Print(text string) {
|
||||||
|
C.waddstr(w.win(), C.CString(strings.Map(func(r rune) rune {
|
||||||
|
if r < 32 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}, text)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) CPrint(pair ColorPair, a Attr, text string) {
|
||||||
|
attr := _colorFn(pair, a)
|
||||||
|
C.wattron(w.win(), attr)
|
||||||
|
w.Print(text)
|
||||||
|
C.wattroff(w.win(), attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() {
|
||||||
|
C.clear()
|
||||||
|
C.endwin()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Refresh() {
|
||||||
|
C.refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Erase() {
|
||||||
|
C.werase(w.win())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Fill(str string) bool {
|
||||||
|
return C.waddstr(w.win(), C.CString(str)) == C.OK
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) CFill(str string, fg Color, bg Color, a Attr) bool {
|
||||||
|
attr := _colorFn(PairFor(fg, bg), a)
|
||||||
|
C.wattron(w.win(), attr)
|
||||||
|
ret := w.Fill(str)
|
||||||
|
C.wattroff(w.win(), attr)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func RefreshWindows(windows []*Window) {
|
||||||
|
for _, w := range windows {
|
||||||
|
C.wnoutrefresh(w.win())
|
||||||
|
}
|
||||||
|
C.doupdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func PairFor(fg Color, bg Color) ColorPair {
|
||||||
|
key := (int(fg) << 8) + int(bg)
|
||||||
|
if found, prs := _colorMap[key]; prs {
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
id := ColorPair(len(_colorMap) + int(ColUser))
|
||||||
|
C.init_pair(C.short(id), C.short(fg), C.short(bg))
|
||||||
|
_colorMap[key] = id
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
func consume(expects ...rune) bool {
|
||||||
|
for _, r := range expects {
|
||||||
|
if int(C.getch()) != int(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func escSequence() Event {
|
||||||
|
// nodelay is not thread-safe (e.g. <ESC><CTRL-P>)
|
||||||
|
// C.nodelay(C.stdscr, true)
|
||||||
|
c := C.getch()
|
||||||
|
switch c {
|
||||||
|
case C.ERR:
|
||||||
|
return Event{ESC, 0, nil}
|
||||||
|
case CtrlM:
|
||||||
|
return Event{AltEnter, 0, nil}
|
||||||
|
case '/':
|
||||||
|
return Event{AltSlash, 0, nil}
|
||||||
|
case ' ':
|
||||||
|
return Event{AltSpace, 0, nil}
|
||||||
|
case 127, C.KEY_BACKSPACE:
|
||||||
|
return Event{AltBS, 0, nil}
|
||||||
|
case '[':
|
||||||
|
// Bracketed paste mode (printf "\e[?2004h")
|
||||||
|
// \e[200~ TEXT \e[201~
|
||||||
|
if consume('2', '0', '0', '~') {
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c >= 'a' && c <= 'z' {
|
||||||
|
return Event{AltA + int(c) - 'a', 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c >= '0' && c <= '9' {
|
||||||
|
return Event{Alt0 + int(c) - '0', 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't care. Ignore the rest.
|
||||||
|
for ; c != C.ERR; c = C.getch() {
|
||||||
|
}
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetChar() Event {
|
||||||
|
c := C.getch()
|
||||||
|
switch c {
|
||||||
|
case C.ERR:
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
case C.KEY_UP:
|
||||||
|
return Event{Up, 0, nil}
|
||||||
|
case C.KEY_DOWN:
|
||||||
|
return Event{Down, 0, nil}
|
||||||
|
case C.KEY_LEFT:
|
||||||
|
return Event{Left, 0, nil}
|
||||||
|
case C.KEY_RIGHT:
|
||||||
|
return Event{Right, 0, nil}
|
||||||
|
case C.KEY_HOME:
|
||||||
|
return Event{Home, 0, nil}
|
||||||
|
case C.KEY_END:
|
||||||
|
return Event{End, 0, nil}
|
||||||
|
case C.KEY_BACKSPACE:
|
||||||
|
return Event{BSpace, 0, nil}
|
||||||
|
case C.KEY_F0 + 1:
|
||||||
|
return Event{F1, 0, nil}
|
||||||
|
case C.KEY_F0 + 2:
|
||||||
|
return Event{F2, 0, nil}
|
||||||
|
case C.KEY_F0 + 3:
|
||||||
|
return Event{F3, 0, nil}
|
||||||
|
case C.KEY_F0 + 4:
|
||||||
|
return Event{F4, 0, nil}
|
||||||
|
case C.KEY_F0 + 5:
|
||||||
|
return Event{F5, 0, nil}
|
||||||
|
case C.KEY_F0 + 6:
|
||||||
|
return Event{F6, 0, nil}
|
||||||
|
case C.KEY_F0 + 7:
|
||||||
|
return Event{F7, 0, nil}
|
||||||
|
case C.KEY_F0 + 8:
|
||||||
|
return Event{F8, 0, nil}
|
||||||
|
case C.KEY_F0 + 9:
|
||||||
|
return Event{F9, 0, nil}
|
||||||
|
case C.KEY_F0 + 10:
|
||||||
|
return Event{F10, 0, nil}
|
||||||
|
case C.KEY_F0 + 11:
|
||||||
|
return Event{F11, 0, nil}
|
||||||
|
case C.KEY_F0 + 12:
|
||||||
|
return Event{F12, 0, nil}
|
||||||
|
case C.KEY_DC:
|
||||||
|
return Event{Del, 0, nil}
|
||||||
|
case C.KEY_PPAGE:
|
||||||
|
return Event{PgUp, 0, nil}
|
||||||
|
case C.KEY_NPAGE:
|
||||||
|
return Event{PgDn, 0, nil}
|
||||||
|
case C.KEY_BTAB:
|
||||||
|
return Event{BTab, 0, nil}
|
||||||
|
case C.KEY_ENTER:
|
||||||
|
return Event{CtrlM, 0, nil}
|
||||||
|
case C.KEY_SLEFT:
|
||||||
|
return Event{SLeft, 0, nil}
|
||||||
|
case C.KEY_SRIGHT:
|
||||||
|
return Event{SRight, 0, nil}
|
||||||
|
case C.KEY_MOUSE:
|
||||||
|
var me C.MEVENT
|
||||||
|
if C.getmouse(&me) != C.ERR {
|
||||||
|
mod := ((me.bstate & C.BUTTON_SHIFT) | (me.bstate & C.BUTTON_CTRL) | (me.bstate & C.BUTTON_ALT)) > 0
|
||||||
|
x := int(me.x)
|
||||||
|
y := int(me.y)
|
||||||
|
/* Cannot use BUTTON1_DOUBLE_CLICKED due to mouseinterval(0) */
|
||||||
|
if (me.bstate & C.BUTTON1_PRESSED) > 0 {
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(_prevDownTime) < doubleClickDuration {
|
||||||
|
_clickY = append(_clickY, y)
|
||||||
|
} else {
|
||||||
|
_clickY = []int{y}
|
||||||
|
_prevDownTime = now
|
||||||
|
}
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, 0, true, false, mod}}
|
||||||
|
} else if (me.bstate & C.BUTTON1_RELEASED) > 0 {
|
||||||
|
double := false
|
||||||
|
if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&
|
||||||
|
time.Now().Sub(_prevDownTime) < doubleClickDuration {
|
||||||
|
double = true
|
||||||
|
}
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, 0, false, double, mod}}
|
||||||
|
} else if (me.bstate&0x8000000) > 0 || (me.bstate&0x80) > 0 {
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, mod}}
|
||||||
|
} else if (me.bstate & C.BUTTON4_PRESSED) > 0 {
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, 1, false, false, mod}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
case C.KEY_RESIZE:
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
case ESC:
|
||||||
|
return escSequence()
|
||||||
|
case 127:
|
||||||
|
return Event{BSpace, 0, nil}
|
||||||
|
}
|
||||||
|
// CTRL-A ~ CTRL-Z
|
||||||
|
if c >= CtrlA && c <= CtrlZ {
|
||||||
|
return Event{int(c), 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multi-byte character
|
||||||
|
buffer := []byte{byte(c)}
|
||||||
|
for {
|
||||||
|
r, _ := utf8.DecodeRune(buffer)
|
||||||
|
if r != utf8.RuneError {
|
||||||
|
return Event{Rune, r, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
c := C.getch()
|
||||||
|
if c == C.ERR {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if c >= C.KEY_CODE_YES {
|
||||||
|
C.ungetch(c)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buffer = append(buffer, byte(c))
|
||||||
|
}
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
578
src/tui/tcell.go
Normal file
578
src/tui/tcell.go
Normal file
@@ -0,0 +1,578 @@
|
|||||||
|
// +build tcell windows
|
||||||
|
|
||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
"github.com/gdamore/tcell/encoding"
|
||||||
|
|
||||||
|
"github.com/junegunn/go-runewidth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ColorPair [2]Color
|
||||||
|
|
||||||
|
func (p ColorPair) fg() Color {
|
||||||
|
return p[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ColorPair) bg() Color {
|
||||||
|
return p[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ColorPair) style() tcell.Style {
|
||||||
|
style := tcell.StyleDefault
|
||||||
|
return style.Foreground(tcell.Color(p.fg())).Background(tcell.Color(p.bg()))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Attr tcell.Style
|
||||||
|
|
||||||
|
type WindowTcell struct {
|
||||||
|
LastX int
|
||||||
|
LastY int
|
||||||
|
MoveCursor bool
|
||||||
|
Border bool
|
||||||
|
}
|
||||||
|
type WindowImpl WindowTcell
|
||||||
|
|
||||||
|
const (
|
||||||
|
Bold Attr = Attr(tcell.AttrBold)
|
||||||
|
Dim = Attr(tcell.AttrDim)
|
||||||
|
Blink = Attr(tcell.AttrBlink)
|
||||||
|
Reverse = Attr(tcell.AttrReverse)
|
||||||
|
Underline = Attr(tcell.AttrUnderline)
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AttrRegular Attr = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ColDefault = ColorPair{colDefault, colDefault}
|
||||||
|
ColNormal ColorPair
|
||||||
|
ColPrompt ColorPair
|
||||||
|
ColMatch ColorPair
|
||||||
|
ColCurrent ColorPair
|
||||||
|
ColCurrentMatch ColorPair
|
||||||
|
ColSpinner ColorPair
|
||||||
|
ColInfo ColorPair
|
||||||
|
ColCursor ColorPair
|
||||||
|
ColSelected ColorPair
|
||||||
|
ColHeader ColorPair
|
||||||
|
ColBorder ColorPair
|
||||||
|
ColUser ColorPair
|
||||||
|
)
|
||||||
|
|
||||||
|
func DefaultTheme() *ColorTheme {
|
||||||
|
if _screen.Colors() >= 256 {
|
||||||
|
return Dark256
|
||||||
|
}
|
||||||
|
return Default16
|
||||||
|
}
|
||||||
|
|
||||||
|
func PairFor(fg Color, bg Color) ColorPair {
|
||||||
|
return [2]Color{fg, bg}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_colorToAttribute = []tcell.Color{
|
||||||
|
tcell.ColorBlack,
|
||||||
|
tcell.ColorRed,
|
||||||
|
tcell.ColorGreen,
|
||||||
|
tcell.ColorYellow,
|
||||||
|
tcell.ColorBlue,
|
||||||
|
tcell.ColorDarkMagenta,
|
||||||
|
tcell.ColorLightCyan,
|
||||||
|
tcell.ColorWhite,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c Color) Style() tcell.Color {
|
||||||
|
if c <= colDefault {
|
||||||
|
return tcell.ColorDefault
|
||||||
|
} else if c >= colBlack && c <= colWhite {
|
||||||
|
return _colorToAttribute[int(c)]
|
||||||
|
} else {
|
||||||
|
return tcell.Color(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Attr) Merge(b Attr) Attr {
|
||||||
|
return a | b
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_screen tcell.Screen
|
||||||
|
_mouse bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func initScreen() {
|
||||||
|
s, e := tcell.NewScreen()
|
||||||
|
if e != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
if e = s.Init(); e != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
if _mouse {
|
||||||
|
s.EnableMouse()
|
||||||
|
} else {
|
||||||
|
s.DisableMouse()
|
||||||
|
}
|
||||||
|
_screen = s
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init(theme *ColorTheme, black bool, mouse bool) {
|
||||||
|
encoding.Register()
|
||||||
|
|
||||||
|
_mouse = mouse
|
||||||
|
initScreen()
|
||||||
|
|
||||||
|
_color = theme != nil
|
||||||
|
if _color {
|
||||||
|
InitTheme(theme, black)
|
||||||
|
} else {
|
||||||
|
theme = DefaultTheme()
|
||||||
|
}
|
||||||
|
ColNormal = ColorPair{theme.Fg, theme.Bg}
|
||||||
|
ColPrompt = ColorPair{theme.Prompt, theme.Bg}
|
||||||
|
ColMatch = ColorPair{theme.Match, theme.Bg}
|
||||||
|
ColCurrent = ColorPair{theme.Current, theme.DarkBg}
|
||||||
|
ColCurrentMatch = ColorPair{theme.CurrentMatch, theme.DarkBg}
|
||||||
|
ColSpinner = ColorPair{theme.Spinner, theme.Bg}
|
||||||
|
ColInfo = ColorPair{theme.Info, theme.Bg}
|
||||||
|
ColCursor = ColorPair{theme.Cursor, theme.DarkBg}
|
||||||
|
ColSelected = ColorPair{theme.Selected, theme.DarkBg}
|
||||||
|
ColHeader = ColorPair{theme.Header, theme.Bg}
|
||||||
|
ColBorder = ColorPair{theme.Border, theme.Bg}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxX() int {
|
||||||
|
ncols, _ := _screen.Size()
|
||||||
|
return int(ncols)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxY() int {
|
||||||
|
_, nlines := _screen.Size()
|
||||||
|
return int(nlines)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) win() *WindowTcell {
|
||||||
|
return (*WindowTcell)(w.impl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() {
|
||||||
|
_screen.Sync()
|
||||||
|
_screen.Clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Refresh() {
|
||||||
|
// noop
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetChar() Event {
|
||||||
|
ev := _screen.PollEvent()
|
||||||
|
switch ev := ev.(type) {
|
||||||
|
case *tcell.EventResize:
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
|
||||||
|
// process mouse events:
|
||||||
|
case *tcell.EventMouse:
|
||||||
|
x, y := ev.Position()
|
||||||
|
button := ev.Buttons()
|
||||||
|
mod := ev.Modifiers() != 0
|
||||||
|
if button&tcell.WheelDown != 0 {
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, mod}}
|
||||||
|
} else if button&tcell.WheelUp != 0 {
|
||||||
|
return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, mod}}
|
||||||
|
} else if runtime.GOOS != "windows" {
|
||||||
|
// double and single taps on Windows don't quite work due to
|
||||||
|
// the console acting on the events and not allowing us
|
||||||
|
// to consume them.
|
||||||
|
|
||||||
|
down := button&tcell.Button1 != 0 // left
|
||||||
|
double := false
|
||||||
|
if down {
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(_prevDownTime) < doubleClickDuration {
|
||||||
|
_clickY = append(_clickY, x)
|
||||||
|
} else {
|
||||||
|
_clickY = []int{x}
|
||||||
|
_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}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// process keyboard:
|
||||||
|
case *tcell.EventKey:
|
||||||
|
alt := (ev.Modifiers() & tcell.ModAlt) > 0
|
||||||
|
switch ev.Key() {
|
||||||
|
case tcell.KeyCtrlA:
|
||||||
|
return Event{CtrlA, 0, nil}
|
||||||
|
case tcell.KeyCtrlB:
|
||||||
|
return Event{CtrlB, 0, nil}
|
||||||
|
case tcell.KeyCtrlC:
|
||||||
|
return Event{CtrlC, 0, nil}
|
||||||
|
case tcell.KeyCtrlD:
|
||||||
|
return Event{CtrlD, 0, nil}
|
||||||
|
case tcell.KeyCtrlE:
|
||||||
|
return Event{CtrlE, 0, nil}
|
||||||
|
case tcell.KeyCtrlF:
|
||||||
|
return Event{CtrlF, 0, nil}
|
||||||
|
case tcell.KeyCtrlG:
|
||||||
|
return Event{CtrlG, 0, nil}
|
||||||
|
case tcell.KeyCtrlJ:
|
||||||
|
return Event{CtrlJ, 0, nil}
|
||||||
|
case tcell.KeyCtrlK:
|
||||||
|
return Event{CtrlK, 0, nil}
|
||||||
|
case tcell.KeyCtrlL:
|
||||||
|
return Event{CtrlL, 0, nil}
|
||||||
|
case tcell.KeyCtrlM:
|
||||||
|
if alt {
|
||||||
|
return Event{AltEnter, 0, nil}
|
||||||
|
}
|
||||||
|
return Event{CtrlM, 0, nil}
|
||||||
|
case tcell.KeyCtrlN:
|
||||||
|
return Event{CtrlN, 0, nil}
|
||||||
|
case tcell.KeyCtrlO:
|
||||||
|
return Event{CtrlO, 0, nil}
|
||||||
|
case tcell.KeyCtrlP:
|
||||||
|
return Event{CtrlP, 0, nil}
|
||||||
|
case tcell.KeyCtrlQ:
|
||||||
|
return Event{CtrlQ, 0, nil}
|
||||||
|
case tcell.KeyCtrlR:
|
||||||
|
return Event{CtrlR, 0, nil}
|
||||||
|
case tcell.KeyCtrlS:
|
||||||
|
return Event{CtrlS, 0, nil}
|
||||||
|
case tcell.KeyCtrlT:
|
||||||
|
return Event{CtrlT, 0, nil}
|
||||||
|
case tcell.KeyCtrlU:
|
||||||
|
return Event{CtrlU, 0, nil}
|
||||||
|
case tcell.KeyCtrlV:
|
||||||
|
return Event{CtrlV, 0, nil}
|
||||||
|
case tcell.KeyCtrlW:
|
||||||
|
return Event{CtrlW, 0, nil}
|
||||||
|
case tcell.KeyCtrlX:
|
||||||
|
return Event{CtrlX, 0, nil}
|
||||||
|
case tcell.KeyCtrlY:
|
||||||
|
return Event{CtrlY, 0, nil}
|
||||||
|
case tcell.KeyCtrlZ:
|
||||||
|
return Event{CtrlZ, 0, nil}
|
||||||
|
case tcell.KeyBackspace, tcell.KeyBackspace2:
|
||||||
|
if alt {
|
||||||
|
return Event{AltBS, 0, nil}
|
||||||
|
}
|
||||||
|
return Event{BSpace, 0, nil}
|
||||||
|
|
||||||
|
case tcell.KeyUp:
|
||||||
|
return Event{Up, 0, nil}
|
||||||
|
case tcell.KeyDown:
|
||||||
|
return Event{Down, 0, nil}
|
||||||
|
case tcell.KeyLeft:
|
||||||
|
return Event{Left, 0, nil}
|
||||||
|
case tcell.KeyRight:
|
||||||
|
return Event{Right, 0, nil}
|
||||||
|
|
||||||
|
case tcell.KeyHome:
|
||||||
|
return Event{Home, 0, nil}
|
||||||
|
case tcell.KeyDelete:
|
||||||
|
return Event{Del, 0, nil}
|
||||||
|
case tcell.KeyEnd:
|
||||||
|
return Event{End, 0, nil}
|
||||||
|
case tcell.KeyPgUp:
|
||||||
|
return Event{PgUp, 0, nil}
|
||||||
|
case tcell.KeyPgDn:
|
||||||
|
return Event{PgDn, 0, nil}
|
||||||
|
|
||||||
|
case tcell.KeyTab:
|
||||||
|
return Event{Tab, 0, nil}
|
||||||
|
case tcell.KeyBacktab:
|
||||||
|
return Event{BTab, 0, nil}
|
||||||
|
|
||||||
|
case tcell.KeyF1:
|
||||||
|
return Event{F1, 0, nil}
|
||||||
|
case tcell.KeyF2:
|
||||||
|
return Event{F2, 0, nil}
|
||||||
|
case tcell.KeyF3:
|
||||||
|
return Event{F3, 0, nil}
|
||||||
|
case tcell.KeyF4:
|
||||||
|
return Event{F4, 0, nil}
|
||||||
|
case tcell.KeyF5:
|
||||||
|
return Event{F5, 0, nil}
|
||||||
|
case tcell.KeyF6:
|
||||||
|
return Event{F6, 0, nil}
|
||||||
|
case tcell.KeyF7:
|
||||||
|
return Event{F7, 0, nil}
|
||||||
|
case tcell.KeyF8:
|
||||||
|
return Event{F8, 0, nil}
|
||||||
|
case tcell.KeyF9:
|
||||||
|
return Event{F9, 0, nil}
|
||||||
|
case tcell.KeyF10:
|
||||||
|
return Event{F10, 0, nil}
|
||||||
|
case tcell.KeyF11:
|
||||||
|
return Event{F11, 0, nil}
|
||||||
|
case tcell.KeyF12:
|
||||||
|
return Event{F12, 0, nil}
|
||||||
|
|
||||||
|
// ev.Ch doesn't work for some reason for space:
|
||||||
|
case tcell.KeyRune:
|
||||||
|
r := ev.Rune()
|
||||||
|
if alt {
|
||||||
|
switch r {
|
||||||
|
case ' ':
|
||||||
|
return Event{AltSpace, 0, nil}
|
||||||
|
case '/':
|
||||||
|
return Event{AltSlash, 0, nil}
|
||||||
|
}
|
||||||
|
if r >= 'a' && r <= 'z' {
|
||||||
|
return Event{AltA + int(r) - 'a', 0, nil}
|
||||||
|
}
|
||||||
|
if r >= '0' && r <= '9' {
|
||||||
|
return Event{Alt0 + int(r) - '0', 0, nil}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Event{Rune, r, nil}
|
||||||
|
|
||||||
|
case tcell.KeyEsc:
|
||||||
|
return Event{ESC, 0, nil}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Event{Invalid, 0, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pause() {
|
||||||
|
_screen.Fini()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Resume() bool {
|
||||||
|
initScreen()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close() {
|
||||||
|
_screen.Fini()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RefreshWindows(windows []*Window) {
|
||||||
|
// TODO
|
||||||
|
for _, w := range windows {
|
||||||
|
if w.win().MoveCursor {
|
||||||
|
_screen.ShowCursor(w.Left+w.win().LastX, w.Top+w.win().LastY)
|
||||||
|
w.win().MoveCursor = false
|
||||||
|
}
|
||||||
|
w.win().LastX = 0
|
||||||
|
w.win().LastY = 0
|
||||||
|
if w.win().Border {
|
||||||
|
w.DrawBorder()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_screen.Show()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWindow(top int, left int, width int, height int, border bool) *Window {
|
||||||
|
// TODO
|
||||||
|
win := new(WindowTcell)
|
||||||
|
win.Border = border
|
||||||
|
return &Window{
|
||||||
|
impl: (*WindowImpl)(win),
|
||||||
|
Top: top,
|
||||||
|
Left: left,
|
||||||
|
Width: width,
|
||||||
|
Height: height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Close() {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
func fill(x, y, w, h int, r rune) {
|
||||||
|
for ly := 0; ly <= h; ly++ {
|
||||||
|
for lx := 0; lx <= w; lx++ {
|
||||||
|
_screen.SetContent(x+lx, y+ly, r, nil, ColDefault.style())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Erase() {
|
||||||
|
// TODO
|
||||||
|
fill(w.Left, w.Top, w.Width, w.Height, ' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Enclose(y int, x int) bool {
|
||||||
|
return x >= w.Left && x <= (w.Left+w.Width) &&
|
||||||
|
y >= w.Top && y <= (w.Top+w.Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Move(y int, x int) {
|
||||||
|
w.win().LastX = x
|
||||||
|
w.win().LastY = y
|
||||||
|
w.win().MoveCursor = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) MoveAndClear(y int, x int) {
|
||||||
|
w.Move(y, x)
|
||||||
|
for i := w.win().LastX; i < w.Width; i++ {
|
||||||
|
_screen.SetContent(i+w.Left, w.win().LastY+w.Top, rune(' '), nil, ColDefault.style())
|
||||||
|
}
|
||||||
|
w.win().LastX = x
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Print(text string) {
|
||||||
|
w.PrintString(text, ColDefault, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) PrintString(text string, pair ColorPair, a Attr) {
|
||||||
|
t := text
|
||||||
|
lx := 0
|
||||||
|
|
||||||
|
var style tcell.Style
|
||||||
|
if _color {
|
||||||
|
style = pair.style().
|
||||||
|
Reverse(a&Attr(tcell.AttrReverse) != 0).
|
||||||
|
Underline(a&Attr(tcell.AttrUnderline) != 0)
|
||||||
|
} else {
|
||||||
|
style = ColDefault.style().
|
||||||
|
Reverse(a&Attr(tcell.AttrReverse) != 0 || pair == ColCurrent || pair == ColCurrentMatch).
|
||||||
|
Underline(a&Attr(tcell.AttrUnderline) != 0 || pair == ColMatch || pair == ColCurrentMatch)
|
||||||
|
}
|
||||||
|
style = style.
|
||||||
|
Blink(a&Attr(tcell.AttrBlink) != 0).
|
||||||
|
Bold(a&Attr(tcell.AttrBold) != 0).
|
||||||
|
Dim(a&Attr(tcell.AttrDim) != 0)
|
||||||
|
|
||||||
|
for {
|
||||||
|
if len(t) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r, size := utf8.DecodeRuneInString(t)
|
||||||
|
t = t[size:]
|
||||||
|
|
||||||
|
if r < rune(' ') { // ignore control characters
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if r == '\n' {
|
||||||
|
w.win().LastY++
|
||||||
|
lx = 0
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if r == '\u000D' { // skip carriage return
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var xPos = w.Left + w.win().LastX + lx
|
||||||
|
var yPos = w.Top + w.win().LastY
|
||||||
|
if xPos < (w.Left+w.Width) && yPos < (w.Top+w.Height) {
|
||||||
|
_screen.SetContent(xPos, yPos, r, nil, style)
|
||||||
|
}
|
||||||
|
lx += runewidth.RuneWidth(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.win().LastX += lx
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) CPrint(pair ColorPair, a Attr, text string) {
|
||||||
|
w.PrintString(text, pair, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) FillString(text string, pair ColorPair, a Attr) bool {
|
||||||
|
lx := 0
|
||||||
|
|
||||||
|
var style tcell.Style
|
||||||
|
if _color {
|
||||||
|
style = pair.style()
|
||||||
|
} else {
|
||||||
|
style = ColDefault.style()
|
||||||
|
}
|
||||||
|
style = style.
|
||||||
|
Blink(a&Attr(tcell.AttrBlink) != 0).
|
||||||
|
Bold(a&Attr(tcell.AttrBold) != 0).
|
||||||
|
Dim(a&Attr(tcell.AttrDim) != 0).
|
||||||
|
Reverse(a&Attr(tcell.AttrReverse) != 0).
|
||||||
|
Underline(a&Attr(tcell.AttrUnderline) != 0)
|
||||||
|
|
||||||
|
for _, r := range text {
|
||||||
|
if r == '\n' {
|
||||||
|
w.win().LastY++
|
||||||
|
w.win().LastX = 0
|
||||||
|
lx = 0
|
||||||
|
} else {
|
||||||
|
var xPos = w.Left + w.win().LastX + lx
|
||||||
|
|
||||||
|
// word wrap:
|
||||||
|
if xPos > (w.Left + w.Width) {
|
||||||
|
w.win().LastY++
|
||||||
|
w.win().LastX = 0
|
||||||
|
lx = 0
|
||||||
|
xPos = w.Left
|
||||||
|
}
|
||||||
|
var yPos = w.Top + w.win().LastY
|
||||||
|
|
||||||
|
if yPos >= (w.Top + w.Height) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_screen.SetContent(xPos, yPos, r, nil, style)
|
||||||
|
lx += runewidth.RuneWidth(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.win().LastX += lx
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) Fill(str string) bool {
|
||||||
|
return w.FillString(str, ColDefault, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) CFill(str string, fg Color, bg Color, a Attr) bool {
|
||||||
|
return w.FillString(str, ColorPair{fg, bg}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Window) DrawBorder() {
|
||||||
|
left := w.Left
|
||||||
|
right := left + w.Width
|
||||||
|
top := w.Top
|
||||||
|
bot := top + w.Height
|
||||||
|
|
||||||
|
var style tcell.Style
|
||||||
|
if _color {
|
||||||
|
style = ColBorder.style()
|
||||||
|
} else {
|
||||||
|
style = ColDefault.style()
|
||||||
|
}
|
||||||
|
|
||||||
|
for x := left; x < right; x++ {
|
||||||
|
_screen.SetContent(x, top, tcell.RuneHLine, nil, style)
|
||||||
|
_screen.SetContent(x, bot-1, tcell.RuneHLine, nil, style)
|
||||||
|
}
|
||||||
|
|
||||||
|
for y := top; y < bot; y++ {
|
||||||
|
_screen.SetContent(left, y, tcell.RuneVLine, nil, style)
|
||||||
|
_screen.SetContent(right-1, y, tcell.RuneVLine, nil, style)
|
||||||
|
}
|
||||||
|
|
||||||
|
_screen.SetContent(left, top, tcell.RuneULCorner, nil, style)
|
||||||
|
_screen.SetContent(right-1, top, tcell.RuneURCorner, nil, style)
|
||||||
|
_screen.SetContent(left, bot-1, tcell.RuneLLCorner, nil, style)
|
||||||
|
_screen.SetContent(right-1, bot-1, tcell.RuneLRCorner, nil, style)
|
||||||
|
}
|
||||||
255
src/tui/tui.go
Normal file
255
src/tui/tui.go
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
DoubleClick
|
||||||
|
|
||||||
|
BTab
|
||||||
|
BSpace
|
||||||
|
|
||||||
|
Del
|
||||||
|
PgUp
|
||||||
|
PgDn
|
||||||
|
|
||||||
|
Up
|
||||||
|
Down
|
||||||
|
Left
|
||||||
|
Right
|
||||||
|
Home
|
||||||
|
End
|
||||||
|
|
||||||
|
SLeft
|
||||||
|
SRight
|
||||||
|
|
||||||
|
F1
|
||||||
|
F2
|
||||||
|
F3
|
||||||
|
F4
|
||||||
|
F5
|
||||||
|
F6
|
||||||
|
F7
|
||||||
|
F8
|
||||||
|
F9
|
||||||
|
F10
|
||||||
|
F11
|
||||||
|
F12
|
||||||
|
|
||||||
|
AltEnter
|
||||||
|
AltSpace
|
||||||
|
AltSlash
|
||||||
|
AltBS
|
||||||
|
|
||||||
|
Alt0
|
||||||
|
)
|
||||||
|
|
||||||
|
const ( // Reset iota
|
||||||
|
AltA = Alt0 + 'a' - '0' + iota
|
||||||
|
AltB
|
||||||
|
AltC
|
||||||
|
AltD
|
||||||
|
AltE
|
||||||
|
AltF
|
||||||
|
AltZ = AltA + 'z' - 'a'
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
doubleClickDuration = 500 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
type Color int16
|
||||||
|
|
||||||
|
const (
|
||||||
|
colUndefined Color = -2
|
||||||
|
colDefault = -1
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
colBlack Color = iota
|
||||||
|
colRed
|
||||||
|
colGreen
|
||||||
|
colYellow
|
||||||
|
colBlue
|
||||||
|
colMagenta
|
||||||
|
colCyan
|
||||||
|
colWhite
|
||||||
|
)
|
||||||
|
|
||||||
|
type ColorTheme struct {
|
||||||
|
Fg Color
|
||||||
|
Bg Color
|
||||||
|
DarkBg Color
|
||||||
|
Prompt Color
|
||||||
|
Match Color
|
||||||
|
Current Color
|
||||||
|
CurrentMatch Color
|
||||||
|
Spinner Color
|
||||||
|
Info Color
|
||||||
|
Cursor Color
|
||||||
|
Selected Color
|
||||||
|
Header Color
|
||||||
|
Border Color
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
|
_color bool
|
||||||
|
_prevDownTime time.Time
|
||||||
|
_clickY []int
|
||||||
|
Default16 *ColorTheme
|
||||||
|
Dark256 *ColorTheme
|
||||||
|
Light256 *ColorTheme
|
||||||
|
)
|
||||||
|
|
||||||
|
type Window struct {
|
||||||
|
impl *WindowImpl
|
||||||
|
Top int
|
||||||
|
Left int
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
}
|
||||||
|
|
||||||
|
func EmptyTheme() *ColorTheme {
|
||||||
|
return &ColorTheme{
|
||||||
|
Fg: colUndefined,
|
||||||
|
Bg: colUndefined,
|
||||||
|
DarkBg: colUndefined,
|
||||||
|
Prompt: colUndefined,
|
||||||
|
Match: colUndefined,
|
||||||
|
Current: colUndefined,
|
||||||
|
CurrentMatch: colUndefined,
|
||||||
|
Spinner: colUndefined,
|
||||||
|
Info: colUndefined,
|
||||||
|
Cursor: colUndefined,
|
||||||
|
Selected: colUndefined,
|
||||||
|
Header: colUndefined,
|
||||||
|
Border: colUndefined}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_prevDownTime = time.Unix(0, 0)
|
||||||
|
_clickY = []int{}
|
||||||
|
Default16 = &ColorTheme{
|
||||||
|
Fg: colDefault,
|
||||||
|
Bg: colDefault,
|
||||||
|
DarkBg: colBlack,
|
||||||
|
Prompt: colBlue,
|
||||||
|
Match: colGreen,
|
||||||
|
Current: colYellow,
|
||||||
|
CurrentMatch: colGreen,
|
||||||
|
Spinner: colGreen,
|
||||||
|
Info: colWhite,
|
||||||
|
Cursor: colRed,
|
||||||
|
Selected: colMagenta,
|
||||||
|
Header: colCyan,
|
||||||
|
Border: colBlack}
|
||||||
|
Dark256 = &ColorTheme{
|
||||||
|
Fg: colDefault,
|
||||||
|
Bg: colDefault,
|
||||||
|
DarkBg: 236,
|
||||||
|
Prompt: 110,
|
||||||
|
Match: 108,
|
||||||
|
Current: 254,
|
||||||
|
CurrentMatch: 151,
|
||||||
|
Spinner: 148,
|
||||||
|
Info: 144,
|
||||||
|
Cursor: 161,
|
||||||
|
Selected: 168,
|
||||||
|
Header: 109,
|
||||||
|
Border: 59}
|
||||||
|
Light256 = &ColorTheme{
|
||||||
|
Fg: colDefault,
|
||||||
|
Bg: colDefault,
|
||||||
|
DarkBg: 251,
|
||||||
|
Prompt: 25,
|
||||||
|
Match: 66,
|
||||||
|
Current: 237,
|
||||||
|
CurrentMatch: 23,
|
||||||
|
Spinner: 65,
|
||||||
|
Info: 101,
|
||||||
|
Cursor: 161,
|
||||||
|
Selected: 168,
|
||||||
|
Header: 31,
|
||||||
|
Border: 145}
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitTheme(theme *ColorTheme, black bool) {
|
||||||
|
_color = theme != nil
|
||||||
|
if !_color {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
baseTheme := DefaultTheme()
|
||||||
|
if black {
|
||||||
|
theme.Bg = colBlack
|
||||||
|
}
|
||||||
|
|
||||||
|
o := func(a Color, b Color) Color {
|
||||||
|
if b == colUndefined {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
theme.Fg = o(baseTheme.Fg, theme.Fg)
|
||||||
|
theme.Bg = o(baseTheme.Bg, theme.Bg)
|
||||||
|
theme.DarkBg = o(baseTheme.DarkBg, theme.DarkBg)
|
||||||
|
theme.Prompt = o(baseTheme.Prompt, theme.Prompt)
|
||||||
|
theme.Match = o(baseTheme.Match, theme.Match)
|
||||||
|
theme.Current = o(baseTheme.Current, theme.Current)
|
||||||
|
theme.CurrentMatch = o(baseTheme.CurrentMatch, theme.CurrentMatch)
|
||||||
|
theme.Spinner = o(baseTheme.Spinner, theme.Spinner)
|
||||||
|
theme.Info = o(baseTheme.Info, theme.Info)
|
||||||
|
theme.Cursor = o(baseTheme.Cursor, theme.Cursor)
|
||||||
|
theme.Selected = o(baseTheme.Selected, theme.Selected)
|
||||||
|
theme.Header = o(baseTheme.Header, theme.Header)
|
||||||
|
theme.Border = o(baseTheme.Border, theme.Border)
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package curses
|
package tui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
# http://www.rubydoc.info/github/rest-client/rest-client/RestClient
|
# http://www.rubydoc.info/github/rest-client/rest-client/RestClient
|
||||||
require 'rest_client'
|
require 'rest_client'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
if ARGV.length < 3
|
if ARGV.length < 3
|
||||||
puts "usage: #$0 <token> <version> <files...>"
|
puts "usage: #$0 <token> <version> <files...>"
|
||||||
|
|||||||
157
src/util/chars.go
Normal file
157
src/util/chars.go
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unicode"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Chars struct {
|
||||||
|
runes []rune
|
||||||
|
bytes []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToChars converts byte array into rune array
|
||||||
|
func ToChars(bytea []byte) Chars {
|
||||||
|
var runes []rune
|
||||||
|
ascii := true
|
||||||
|
numBytes := len(bytea)
|
||||||
|
for i := 0; i < numBytes; {
|
||||||
|
if bytea[i] < utf8.RuneSelf {
|
||||||
|
if !ascii {
|
||||||
|
runes = append(runes, rune(bytea[i]))
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
if ascii {
|
||||||
|
ascii = false
|
||||||
|
runes = make([]rune, i, numBytes)
|
||||||
|
for j := 0; j < i; j++ {
|
||||||
|
runes[j] = rune(bytea[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r, sz := utf8.DecodeRune(bytea[i:])
|
||||||
|
i += sz
|
||||||
|
runes = append(runes, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ascii {
|
||||||
|
return Chars{bytes: bytea}
|
||||||
|
}
|
||||||
|
return Chars{runes: runes}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunesToChars(runes []rune) Chars {
|
||||||
|
return Chars{runes: runes}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) Get(i int) rune {
|
||||||
|
if chars.runes != nil {
|
||||||
|
return chars.runes[i]
|
||||||
|
}
|
||||||
|
return rune(chars.bytes[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) Length() int {
|
||||||
|
if chars.runes != nil {
|
||||||
|
return len(chars.runes)
|
||||||
|
}
|
||||||
|
return len(chars.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrimLength returns the length after trimming leading and trailing whitespaces
|
||||||
|
func (chars *Chars) TrimLength() int {
|
||||||
|
var i int
|
||||||
|
len := chars.Length()
|
||||||
|
for i = len - 1; i >= 0; i-- {
|
||||||
|
char := chars.Get(i)
|
||||||
|
if !unicode.IsSpace(char) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Completely empty
|
||||||
|
if i < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var j int
|
||||||
|
for j = 0; j < len; j++ {
|
||||||
|
char := chars.Get(j)
|
||||||
|
if !unicode.IsSpace(char) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i - j + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) TrailingWhitespaces() int {
|
||||||
|
whitespaces := 0
|
||||||
|
for i := chars.Length() - 1; i >= 0; i-- {
|
||||||
|
char := chars.Get(i)
|
||||||
|
if !unicode.IsSpace(char) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
whitespaces++
|
||||||
|
}
|
||||||
|
return whitespaces
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) ToString() string {
|
||||||
|
if chars.runes != nil {
|
||||||
|
return string(chars.runes)
|
||||||
|
}
|
||||||
|
return string(chars.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) ToRunes() []rune {
|
||||||
|
if chars.runes != nil {
|
||||||
|
return chars.runes
|
||||||
|
}
|
||||||
|
runes := make([]rune, len(chars.bytes))
|
||||||
|
for idx, b := range chars.bytes {
|
||||||
|
runes[idx] = rune(b)
|
||||||
|
}
|
||||||
|
return runes
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) Slice(b int, e int) Chars {
|
||||||
|
if chars.runes != nil {
|
||||||
|
return Chars{runes: chars.runes[b:e]}
|
||||||
|
}
|
||||||
|
return Chars{bytes: chars.bytes[b:e]}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (chars *Chars) Split(delimiter string) []Chars {
|
||||||
|
delim := []rune(delimiter)
|
||||||
|
numChars := chars.Length()
|
||||||
|
numDelim := len(delim)
|
||||||
|
begin := 0
|
||||||
|
ret := make([]Chars, 0, 1)
|
||||||
|
|
||||||
|
for index := 0; index < numChars; {
|
||||||
|
if index+numDelim <= numChars {
|
||||||
|
match := true
|
||||||
|
for off, d := range delim {
|
||||||
|
if chars.Get(index+off) != d {
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Found the delimiter
|
||||||
|
if match {
|
||||||
|
incr := Max(numDelim, 1)
|
||||||
|
ret = append(ret, chars.Slice(begin, index+incr))
|
||||||
|
index += incr
|
||||||
|
begin = index
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Impossible to find the delimiter in the remaining substring
|
||||||
|
break
|
||||||
|
}
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
if begin < numChars || len(ret) == 0 {
|
||||||
|
ret = append(ret, chars.Slice(begin, numChars))
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
82
src/util/chars_test.go
Normal file
82
src/util/chars_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestToCharsNil(t *testing.T) {
|
||||||
|
bs := Chars{bytes: []byte{}}
|
||||||
|
if bs.bytes == nil || bs.runes != nil {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
rs := RunesToChars([]rune{})
|
||||||
|
if rs.bytes != nil || rs.runes == nil {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToCharsAscii(t *testing.T) {
|
||||||
|
chars := ToChars([]byte("foobar"))
|
||||||
|
if chars.ToString() != "foobar" || chars.runes != nil {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCharsLength(t *testing.T) {
|
||||||
|
chars := ToChars([]byte("\tabc한글 "))
|
||||||
|
if chars.Length() != 8 || chars.TrimLength() != 5 {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCharsToString(t *testing.T) {
|
||||||
|
text := "\tabc한글 "
|
||||||
|
chars := ToChars([]byte(text))
|
||||||
|
if chars.ToString() != text {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrimLength(t *testing.T) {
|
||||||
|
check := func(str string, exp int) {
|
||||||
|
chars := ToChars([]byte(str))
|
||||||
|
trimmed := chars.TrimLength()
|
||||||
|
if trimmed != exp {
|
||||||
|
t.Errorf("Invalid TrimLength result for '%s': %d (expected %d)",
|
||||||
|
str, trimmed, exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check("hello", 5)
|
||||||
|
check("hello ", 5)
|
||||||
|
check("hello ", 5)
|
||||||
|
check(" hello", 5)
|
||||||
|
check(" hello", 5)
|
||||||
|
check(" hello ", 5)
|
||||||
|
check(" hello ", 5)
|
||||||
|
check("h o", 5)
|
||||||
|
check(" h o ", 5)
|
||||||
|
check(" ", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplit(t *testing.T) {
|
||||||
|
check := func(str string, delim string, tokens ...string) {
|
||||||
|
input := ToChars([]byte(str))
|
||||||
|
result := input.Split(delim)
|
||||||
|
if len(result) != len(tokens) {
|
||||||
|
t.Errorf("Invalid Split result for '%s': %d tokens found (expected %d): %s",
|
||||||
|
str, len(result), len(tokens), result)
|
||||||
|
}
|
||||||
|
for idx, token := range tokens {
|
||||||
|
if result[idx].ToString() != token {
|
||||||
|
t.Errorf("Invalid Split result for '%s': %s (expected %s)",
|
||||||
|
str, result[idx].ToString(), token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check("abc:def::", ":", "abc:", "def:", ":")
|
||||||
|
check("abc:def::", "-", "abc:def::")
|
||||||
|
check("abc", "", "a", "b", "c")
|
||||||
|
check("abc", "a", "a", "bc")
|
||||||
|
check("abc", "ab", "ab", "c")
|
||||||
|
check("abc", "abc", "abc")
|
||||||
|
check("abc", "abcd", "abc")
|
||||||
|
check("", "abcd", "")
|
||||||
|
}
|
||||||
12
src/util/slab.go
Normal file
12
src/util/slab.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
type Slab struct {
|
||||||
|
I16 []int16
|
||||||
|
I32 []int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeSlab(size16 int, size32 int) *Slab {
|
||||||
|
return &Slab{
|
||||||
|
I16: make([]int16, size16),
|
||||||
|
I32: make([]int32, size32)}
|
||||||
|
}
|
||||||
103
src/util/util.go
103
src/util/util.go
@@ -1,28 +1,24 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
// #include <unistd.h>
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
|
||||||
|
"github.com/junegunn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Max returns the largest integer
|
// Max returns the largest integer
|
||||||
func Max(first int, items ...int) int {
|
func Max(first int, second int) int {
|
||||||
max := first
|
if first >= second {
|
||||||
for _, item := range items {
|
return first
|
||||||
if item > max {
|
|
||||||
max = item
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return max
|
return second
|
||||||
}
|
}
|
||||||
|
|
||||||
// Min32 returns the smallest 32-bit integer
|
// Max16 returns the largest integer
|
||||||
func Min32(first int32, second int32) int32 {
|
func Max16(first int16, second int16) int16 {
|
||||||
if first <= second {
|
if first >= second {
|
||||||
return first
|
return first
|
||||||
}
|
}
|
||||||
return second
|
return second
|
||||||
@@ -36,6 +32,22 @@ func Max32(first int32, second int32) int32 {
|
|||||||
return second
|
return second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Min returns the smallest integer
|
||||||
|
func Min(first int, second int) int {
|
||||||
|
if first <= second {
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
return second
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min32 returns the smallest 32-bit integer
|
||||||
|
func Min32(first int32, second int32) int32 {
|
||||||
|
if first <= second {
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
return second
|
||||||
|
}
|
||||||
|
|
||||||
// Constrain32 limits the given 32-bit integer with the upper and lower bounds
|
// Constrain32 limits the given 32-bit integer with the upper and lower bounds
|
||||||
func Constrain32(val int32, min int32, max int32) int32 {
|
func Constrain32(val int32, min int32, max int32) int32 {
|
||||||
if val < min {
|
if val < min {
|
||||||
@@ -58,6 +70,15 @@ func Constrain(val int, min int, max int) int {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AsUint16(val int) uint16 {
|
||||||
|
if val > math.MaxUint16 {
|
||||||
|
return math.MaxUint16
|
||||||
|
} else if val < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return uint16(val)
|
||||||
|
}
|
||||||
|
|
||||||
// DurWithin limits the given time.Duration with the upper and lower bounds
|
// DurWithin limits the given time.Duration with the upper and lower bounds
|
||||||
func DurWithin(
|
func DurWithin(
|
||||||
val time.Duration, min time.Duration, max time.Duration) time.Duration {
|
val time.Duration, min time.Duration, max time.Duration) time.Duration {
|
||||||
@@ -72,57 +93,5 @@ func DurWithin(
|
|||||||
|
|
||||||
// IsTty returns true is stdin is a terminal
|
// IsTty returns true is stdin is a terminal
|
||||||
func IsTty() bool {
|
func IsTty() bool {
|
||||||
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
return isatty.IsTerminal(os.Stdin.Fd())
|
||||||
}
|
|
||||||
|
|
||||||
// TrimRight returns rune array with trailing white spaces cut off
|
|
||||||
func TrimRight(runes []rune) []rune {
|
|
||||||
var i int
|
|
||||||
for i = len(runes) - 1; i >= 0; i-- {
|
|
||||||
char := runes[i]
|
|
||||||
if char != ' ' && char != '\t' {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return runes[0 : i+1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// BytesToRunes converts byte array into rune array
|
|
||||||
func BytesToRunes(bytea []byte) []rune {
|
|
||||||
runes := make([]rune, 0, len(bytea))
|
|
||||||
for i := 0; i < len(bytea); {
|
|
||||||
if bytea[i] < utf8.RuneSelf {
|
|
||||||
runes = append(runes, rune(bytea[i]))
|
|
||||||
i++
|
|
||||||
} else {
|
|
||||||
r, sz := utf8.DecodeRune(bytea[i:])
|
|
||||||
i += sz
|
|
||||||
runes = append(runes, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return runes
|
|
||||||
}
|
|
||||||
|
|
||||||
// TrimLen returns the length of trimmed rune array
|
|
||||||
func TrimLen(runes []rune) int {
|
|
||||||
var i int
|
|
||||||
for i = len(runes) - 1; i >= 0; i-- {
|
|
||||||
char := runes[i]
|
|
||||||
if char != ' ' && char != '\t' {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Completely empty
|
|
||||||
if i < 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
var j int
|
|
||||||
for j = 0; j < len(runes); j++ {
|
|
||||||
char := runes[j]
|
|
||||||
if char != ' ' && char != '\t' {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return i - j + 1
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package util
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestMax(t *testing.T) {
|
func TestMax(t *testing.T) {
|
||||||
if Max(-2, 5, 1, 4, 3) != 5 {
|
if Max(-2, 5) != 5 {
|
||||||
t.Error("Invalid result")
|
t.Error("Invalid result")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,23 +20,3 @@ func TestContrain(t *testing.T) {
|
|||||||
t.Error("Expected", 3)
|
t.Error("Expected", 3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrimLen(t *testing.T) {
|
|
||||||
check := func(str string, exp int) {
|
|
||||||
trimmed := TrimLen([]rune(str))
|
|
||||||
if trimmed != exp {
|
|
||||||
t.Errorf("Invalid TrimLen result for '%s': %d (expected %d)",
|
|
||||||
str, trimmed, exp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check("hello", 5)
|
|
||||||
check("hello ", 5)
|
|
||||||
check("hello ", 5)
|
|
||||||
check(" hello", 5)
|
|
||||||
check(" hello", 5)
|
|
||||||
check(" hello ", 5)
|
|
||||||
check(" hello ", 5)
|
|
||||||
check("h o", 5)
|
|
||||||
check(" h o ", 5)
|
|
||||||
check(" ", 0)
|
|
||||||
}
|
|
||||||
|
|||||||
22
src/util/util_unix.go
Normal file
22
src/util/util_unix.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExecCommand executes the given command with $SHELL
|
||||||
|
func ExecCommand(command string) *exec.Cmd {
|
||||||
|
shell := os.Getenv("SHELL")
|
||||||
|
if len(shell) == 0 {
|
||||||
|
shell = "sh"
|
||||||
|
}
|
||||||
|
return exec.Command(shell, "-c", command)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWindows returns true on Windows
|
||||||
|
func IsWindows() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
28
src/util/util_windows.go
Normal file
28
src/util/util_windows.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/junegunn/go-shellwords"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExecCommand executes the given command with $SHELL
|
||||||
|
func ExecCommand(command string) *exec.Cmd {
|
||||||
|
shell := os.Getenv("SHELL")
|
||||||
|
if len(shell) == 0 {
|
||||||
|
shell = "cmd"
|
||||||
|
}
|
||||||
|
args, _ := shellwords.Parse(command)
|
||||||
|
allArgs := make([]string, len(args)+1)
|
||||||
|
allArgs[0] = "/c"
|
||||||
|
copy(allArgs[1:], args)
|
||||||
|
return exec.Command(shell, allArgs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWindows returns true on Windows
|
||||||
|
func IsWindows() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
112
test/fzf.vader
112
test/fzf.vader
@@ -1,6 +1,8 @@
|
|||||||
Execute (Setup):
|
Execute (Setup):
|
||||||
let g:dir = fnamemodify(g:vader_file, ':p:h')
|
let g:dir = fnamemodify(g:vader_file, ':p:h')
|
||||||
|
unlet! g:fzf_layout g:fzf_action g:fzf_history_dir
|
||||||
Log 'Test directory: ' . g:dir
|
Log 'Test directory: ' . g:dir
|
||||||
|
Save &acd
|
||||||
|
|
||||||
Execute (fzf#run with dir option):
|
Execute (fzf#run with dir option):
|
||||||
let cwd = getcwd()
|
let cwd = getcwd()
|
||||||
@@ -35,6 +37,116 @@ 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' }))
|
||||||
AssertEqual ['hi'], result
|
AssertEqual ['hi'], result
|
||||||
|
|
||||||
|
Execute (fzf#run with dir option and noautochdir):
|
||||||
|
set noacd
|
||||||
|
let cwd = getcwd()
|
||||||
|
call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})
|
||||||
|
" No change in working directory
|
||||||
|
AssertEqual cwd, getcwd()
|
||||||
|
|
||||||
|
call fzf#run({'source': ['/foobar'], 'sink': 'tabe', 'dir': '/tmp', 'options': '-1'})
|
||||||
|
AssertEqual cwd, getcwd()
|
||||||
|
tabclose
|
||||||
|
AssertEqual cwd, getcwd()
|
||||||
|
|
||||||
|
Execute (Incomplete fzf#run with dir option and autochdir):
|
||||||
|
set acd
|
||||||
|
let cwd = getcwd()
|
||||||
|
call fzf#run({'source': [], 'sink': 'e', 'dir': '/tmp', 'options': '-0'})
|
||||||
|
" No change in working directory even if &acd is set
|
||||||
|
AssertEqual cwd, getcwd()
|
||||||
|
|
||||||
|
Execute (fzf#run with dir option and autochdir):
|
||||||
|
set acd
|
||||||
|
let cwd = getcwd()
|
||||||
|
call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})
|
||||||
|
" Working directory changed due to &acd
|
||||||
|
AssertEqual '/', getcwd()
|
||||||
|
|
||||||
|
Execute (fzf#run with dir option and autochdir when final cwd is same as dir):
|
||||||
|
set acd
|
||||||
|
cd /tmp
|
||||||
|
call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})
|
||||||
|
" Working directory changed due to &acd
|
||||||
|
AssertEqual '/', getcwd()
|
||||||
|
|
||||||
|
Execute (fzf#wrap):
|
||||||
|
AssertThrows fzf#wrap({'foo': 'bar'})
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar')
|
||||||
|
Log opts
|
||||||
|
AssertEqual '~40%', opts.down
|
||||||
|
Assert opts.options =~ '--expect='
|
||||||
|
Assert !has_key(opts, 'sink')
|
||||||
|
Assert has_key(opts, 'sink*')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {}, 0)
|
||||||
|
Log opts
|
||||||
|
AssertEqual '~40%', opts.down
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {}, 1)
|
||||||
|
Log opts
|
||||||
|
Assert !has_key(opts, 'down')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'down': '50%'})
|
||||||
|
Log opts
|
||||||
|
AssertEqual '50%', opts.down
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'down': '50%'}, 1)
|
||||||
|
Log opts
|
||||||
|
Assert !has_key(opts, 'down')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'sink': 'e'})
|
||||||
|
Log opts
|
||||||
|
AssertEqual 'e', opts.sink
|
||||||
|
Assert !has_key(opts, 'sink*')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'options': '--reverse'})
|
||||||
|
Log opts
|
||||||
|
Assert opts.options =~ '--expect='
|
||||||
|
Assert opts.options =~ '--reverse'
|
||||||
|
|
||||||
|
let g:fzf_layout = {'window': 'enew'}
|
||||||
|
let opts = fzf#wrap('foobar')
|
||||||
|
Log opts
|
||||||
|
AssertEqual 'enew', opts.window
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {}, 1)
|
||||||
|
Log opts
|
||||||
|
Assert !has_key(opts, 'window')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'right': '50%'})
|
||||||
|
Log opts
|
||||||
|
Assert !has_key(opts, 'window')
|
||||||
|
AssertEqual '50%', opts.right
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'right': '50%'}, 1)
|
||||||
|
Log opts
|
||||||
|
Assert !has_key(opts, 'window')
|
||||||
|
Assert !has_key(opts, 'right')
|
||||||
|
|
||||||
|
let g:fzf_action = {'a': 'tabe'}
|
||||||
|
let opts = fzf#wrap('foobar')
|
||||||
|
Log opts
|
||||||
|
Assert opts.options =~ '--expect=a'
|
||||||
|
Assert !has_key(opts, 'sink')
|
||||||
|
Assert has_key(opts, 'sink*')
|
||||||
|
|
||||||
|
let opts = fzf#wrap('foobar', {'sink': 'e'})
|
||||||
|
Log opts
|
||||||
|
AssertEqual 'e', opts.sink
|
||||||
|
Assert !has_key(opts, 'sink*')
|
||||||
|
|
||||||
|
let g:fzf_history_dir = '/tmp'
|
||||||
|
let opts = fzf#wrap('foobar', {'options': '--color light'})
|
||||||
|
Log opts
|
||||||
|
Assert opts.options =~ '--history /tmp/foobar'
|
||||||
|
Assert opts.options =~ '--color light'
|
||||||
|
|
||||||
|
let g:fzf_colors = { 'fg': ['fg', 'Error'] }
|
||||||
|
let opts = fzf#wrap({})
|
||||||
|
Assert opts.options =~ '^--color=fg:'
|
||||||
|
|
||||||
Execute (Cleanup):
|
Execute (Cleanup):
|
||||||
unlet g:dir
|
unlet g:dir
|
||||||
Restore
|
Restore
|
||||||
|
|||||||
542
test/test_go.rb
542
test/test_go.rb
@@ -6,6 +6,7 @@ require 'fileutils'
|
|||||||
|
|
||||||
DEFAULT_TIMEOUT = 20
|
DEFAULT_TIMEOUT = 20
|
||||||
|
|
||||||
|
FILE = File.expand_path(__FILE__)
|
||||||
base = File.expand_path('../../', __FILE__)
|
base = File.expand_path('../../', __FILE__)
|
||||||
Dir.chdir base
|
Dir.chdir base
|
||||||
FZF = "FZF_DEFAULT_OPTS= FZF_DEFAULT_COMMAND= #{base}/bin/fzf"
|
FZF = "FZF_DEFAULT_OPTS= FZF_DEFAULT_COMMAND= #{base}/bin/fzf"
|
||||||
@@ -30,11 +31,15 @@ def wait
|
|||||||
return if yield
|
return if yield
|
||||||
sleep 0.05
|
sleep 0.05
|
||||||
end
|
end
|
||||||
throw 'timeout'
|
raise 'timeout'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Shell
|
class Shell
|
||||||
class << self
|
class << self
|
||||||
|
def unsets
|
||||||
|
'unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS FZF_CTRL_T_COMMAND FZF_CTRL_T_OPTS FZF_ALT_C_COMMAND FZF_ALT_C_OPTS FZF_CTRL_R_OPTS;'
|
||||||
|
end
|
||||||
|
|
||||||
def bash
|
def bash
|
||||||
'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.bash'
|
'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.bash'
|
||||||
end
|
end
|
||||||
@@ -44,6 +49,10 @@ class Shell
|
|||||||
FileUtils.cp File.expand_path('~/.fzf.zsh'), '/tmp/fzf-zsh/.zshrc'
|
FileUtils.cp File.expand_path('~/.fzf.zsh'), '/tmp/fzf-zsh/.zshrc'
|
||||||
'PS1= PROMPT_COMMAND= HISTSIZE=100 ZDOTDIR=/tmp/fzf-zsh zsh'
|
'PS1= PROMPT_COMMAND= HISTSIZE=100 ZDOTDIR=/tmp/fzf-zsh zsh'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fish
|
||||||
|
'fish'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -56,11 +65,11 @@ class Tmux
|
|||||||
@win =
|
@win =
|
||||||
case shell
|
case shell
|
||||||
when :bash
|
when :bash
|
||||||
go("new-window -d -P -F '#I' '#{Shell.bash}'").first
|
go("new-window -d -P -F '#I' '#{Shell.unsets + Shell.bash}'").first
|
||||||
when :zsh
|
when :zsh
|
||||||
go("new-window -d -P -F '#I' '#{Shell.zsh}'").first
|
go("new-window -d -P -F '#I' '#{Shell.unsets + Shell.zsh}'").first
|
||||||
when :fish
|
when :fish
|
||||||
go("new-window -d -P -F '#I' 'fish'").first
|
go("new-window -d -P -F '#I' '#{Shell.unsets + Shell.fish}'").first
|
||||||
else
|
else
|
||||||
raise "Unknown shell: #{shell}"
|
raise "Unknown shell: #{shell}"
|
||||||
end
|
end
|
||||||
@@ -89,6 +98,10 @@ class Tmux
|
|||||||
go("send-keys -t #{target} #{args}")
|
go("send-keys -t #{target} #{args}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def paste str
|
||||||
|
%x[tmux setb '#{str.gsub("'", "'\\''")}' \\; pasteb -t #{win} \\; send-keys -t #{win} Enter]
|
||||||
|
end
|
||||||
|
|
||||||
def capture pane = 0
|
def capture pane = 0
|
||||||
File.unlink TEMPNAME while File.exists? TEMPNAME
|
File.unlink TEMPNAME while File.exists? TEMPNAME
|
||||||
wait do
|
wait do
|
||||||
@@ -123,8 +136,10 @@ class Tmux
|
|||||||
def prepare
|
def prepare
|
||||||
tries = 0
|
tries = 0
|
||||||
begin
|
begin
|
||||||
self.send_keys 'C-u', 'hello', 'Right'
|
self.until do |lines|
|
||||||
self.until { |lines| lines[-1].end_with?('hello') }
|
self.send_keys 'C-u', 'hello'
|
||||||
|
lines[-1].end_with?('hello')
|
||||||
|
end
|
||||||
rescue Exception
|
rescue Exception
|
||||||
(tries += 1) < 5 ? retry : raise
|
(tries += 1) < 5 ? retry : raise
|
||||||
end
|
end
|
||||||
@@ -142,14 +157,10 @@ class TestBase < Minitest::Test
|
|||||||
attr_reader :tmux
|
attr_reader :tmux
|
||||||
|
|
||||||
def tempname
|
def tempname
|
||||||
|
@temp_suffix ||= 0
|
||||||
[TEMPNAME,
|
[TEMPNAME,
|
||||||
caller_locations.map(&:label).find { |l| l =~ /^test_/ }].join '-'
|
caller_locations.map(&:label).find { |l| l =~ /^test_/ },
|
||||||
end
|
@temp_suffix].join '-'
|
||||||
|
|
||||||
def setup
|
|
||||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
|
||||||
ENV.delete 'FZF_CTRL_T_COMMAND'
|
|
||||||
ENV.delete 'FZF_DEFAULT_COMMAND'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def readonce
|
def readonce
|
||||||
@@ -157,6 +168,7 @@ class TestBase < Minitest::Test
|
|||||||
File.read(tempname)
|
File.read(tempname)
|
||||||
ensure
|
ensure
|
||||||
File.unlink tempname while File.exists?(tempname)
|
File.unlink tempname while File.exists?(tempname)
|
||||||
|
@temp_suffix += 1
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -203,13 +215,13 @@ class TestGoFZF < TestBase
|
|||||||
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.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' }
|
tmux.until { |lines| lines[-2] == ' 856/100000' }
|
||||||
lines = tmux.capture
|
lines = tmux.capture
|
||||||
assert_equal '> 1391', lines[-4]
|
assert_equal '> 3910', lines[-4]
|
||||||
assert_equal ' 391', lines[-3]
|
assert_equal ' 391', lines[-3]
|
||||||
assert_equal ' 856/100000', lines[-2]
|
assert_equal ' 856/100000', lines[-2]
|
||||||
assert_equal '> 391', lines[-1]
|
assert_equal '> 391', lines[-1]
|
||||||
|
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
assert_equal '1391', readonce.chomp
|
assert_equal '3910', readonce.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fzf_default_command
|
def test_fzf_default_command
|
||||||
@@ -353,12 +365,12 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys :BTab, :BTab, :BTab
|
tmux.send_keys :BTab, :BTab, :BTab
|
||||||
tmux.until { |lines| lines[-2].include?('(3)') }
|
tmux.until { |lines| lines[-2].include?('(3)') }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
assert_equal ['5', '5', '15', '25'], readonce.split($/)
|
assert_equal ['5', '5', '50', '51'], readonce.split($/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_query_unicode
|
def test_query_unicode
|
||||||
tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter
|
tmux.paste "(echo abc; echo 가나다) | #{fzf :query, '가다'}"
|
||||||
tmux.until { |lines| lines[-2].include? '1/2' }
|
tmux.until { |lines| lines[-2].include? '1/2' }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
assert_equal ['가나다'], readonce.split($/)
|
assert_equal ['가나다'], readonce.split($/)
|
||||||
@@ -374,7 +386,7 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
tmux.until { |lines| lines[-1] == '>' }
|
tmux.until { |lines| lines[-1] == '>' }
|
||||||
tmux.send_keys 'C-K', :Enter
|
tmux.send_keys 'C-K', :Enter
|
||||||
assert_equal ['1919'], readonce.split($/)
|
assert_equal ['9090'], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tac
|
def test_tac
|
||||||
@@ -390,6 +402,7 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys "seq 1 1000 | #{fzf :tac, :multi}", :Enter
|
tmux.send_keys "seq 1 1000 | #{fzf :tac, :multi}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include? '1000/1000' }
|
tmux.until { |lines| lines[-2].include? '1000/1000' }
|
||||||
tmux.send_keys '99'
|
tmux.send_keys '99'
|
||||||
|
tmux.until { |lines| lines[-2].include? '28/1000' }
|
||||||
tmux.send_keys :BTab, :BTab, :BTab
|
tmux.send_keys :BTab, :BTab, :BTab
|
||||||
tmux.until { |lines| lines[-2].include?('(3)') }
|
tmux.until { |lines| lines[-2].include?('(3)') }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
@@ -409,11 +422,12 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_expect
|
def test_expect
|
||||||
test = lambda do |key, feed, expected = key|
|
test = lambda do |key, feed, expected = key|
|
||||||
tmux.send_keys "seq 1 100 | #{fzf :expect, key}", :Enter
|
tmux.send_keys "seq 1 100 | #{fzf :expect, key}; sync", :Enter
|
||||||
tmux.until { |lines| lines[-2].include? '100/100' }
|
tmux.until { |lines| lines[-2].include? '100/100' }
|
||||||
tmux.send_keys '55'
|
tmux.send_keys '55'
|
||||||
tmux.until { |lines| lines[-2].include? '1/100' }
|
tmux.until { |lines| lines[-2].include? '1/100' }
|
||||||
tmux.send_keys *feed
|
tmux.send_keys *feed
|
||||||
|
tmux.prepare
|
||||||
assert_equal [expected, '55'], readonce.split($/)
|
assert_equal [expected, '55'], readonce.split($/)
|
||||||
end
|
end
|
||||||
test.call 'ctrl-t', 'C-T'
|
test.call 'ctrl-t', 'C-T'
|
||||||
@@ -424,6 +438,10 @@ class TestGoFZF < TestBase
|
|||||||
test.call 'f3', 'f3'
|
test.call 'f3', 'f3'
|
||||||
test.call 'f2,f4', 'f2', 'f2'
|
test.call 'f2,f4', 'f2', 'f2'
|
||||||
test.call 'f2,f4', 'f4', 'f4'
|
test.call 'f2,f4', 'f4', 'f4'
|
||||||
|
test.call 'alt-/', [:Escape, :/]
|
||||||
|
%w[f5 f6 f7 f8 f9 f10].each do |key|
|
||||||
|
test.call 'f5,f6,f7,f8,f9,f10', key, key
|
||||||
|
end
|
||||||
test.call '@', '@'
|
test.call '@', '@'
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -436,6 +454,15 @@ class TestGoFZF < TestBase
|
|||||||
assert_equal ['55', 'alt-z', '55'], readonce.split($/)
|
assert_equal ['55', 'alt-z', '55'], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_expect_printable_character_print_query
|
||||||
|
tmux.send_keys "seq 1 100 | #{fzf '--expect=z --print-query'}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '100/100' }
|
||||||
|
tmux.send_keys '55'
|
||||||
|
tmux.until { |lines| lines[-2].include? '1/100' }
|
||||||
|
tmux.send_keys 'z'
|
||||||
|
assert_equal ['55', 'z', '55'], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
def test_expect_print_query_select_1
|
def test_expect_print_query_select_1
|
||||||
tmux.send_keys "seq 1 100 | #{fzf '-q55 -1 --expect=alt-z --print-query'}", :Enter
|
tmux.send_keys "seq 1 100 | #{fzf '-q55 -1 --expect=alt-z --print-query'}", :Enter
|
||||||
assert_equal ['55', '', '55'], readonce.split($/)
|
assert_equal ['55', '', '55'], readonce.split($/)
|
||||||
@@ -458,8 +485,8 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_unicode_case
|
def test_unicode_case
|
||||||
writelines tempname, %w[строКА1 СТРОКА2 строка3 Строка4]
|
writelines tempname, %w[строКА1 СТРОКА2 строка3 Строка4]
|
||||||
assert_equal %w[СТРОКА2 Строка4], `cat #{tempname} | #{FZF} -fС`.split($/)
|
assert_equal %w[СТРОКА2 Строка4], `#{FZF} -fС < #{tempname}`.split($/)
|
||||||
assert_equal %w[строКА1 СТРОКА2 строка3 Строка4], `cat #{tempname} | #{FZF} -fс`.split($/)
|
assert_equal %w[строКА1 СТРОКА2 строка3 Строка4], `#{FZF} -fс < #{tempname}`.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tiebreak
|
def test_tiebreak
|
||||||
@@ -471,7 +498,7 @@ class TestGoFZF < TestBase
|
|||||||
]
|
]
|
||||||
writelines tempname, input
|
writelines tempname, input
|
||||||
|
|
||||||
assert_equal input, `cat #{tempname} | #{FZF} -ffoobar --tiebreak=index`.split($/)
|
assert_equal input, `#{FZF} -ffoobar --tiebreak=index < #{tempname}`.split($/)
|
||||||
|
|
||||||
by_length = %w[
|
by_length = %w[
|
||||||
----foobar--
|
----foobar--
|
||||||
@@ -479,8 +506,8 @@ class TestGoFZF < TestBase
|
|||||||
-------foobar-
|
-------foobar-
|
||||||
--foobar--------
|
--foobar--------
|
||||||
]
|
]
|
||||||
assert_equal by_length, `cat #{tempname} | #{FZF} -ffoobar`.split($/)
|
assert_equal by_length, `#{FZF} -ffoobar < #{tempname}`.split($/)
|
||||||
assert_equal by_length, `cat #{tempname} | #{FZF} -ffoobar --tiebreak=length`.split($/)
|
assert_equal by_length, `#{FZF} -ffoobar --tiebreak=length < #{tempname}`.split($/)
|
||||||
|
|
||||||
by_begin = %w[
|
by_begin = %w[
|
||||||
--foobar--------
|
--foobar--------
|
||||||
@@ -488,17 +515,104 @@ class TestGoFZF < TestBase
|
|||||||
-----foobar---
|
-----foobar---
|
||||||
-------foobar-
|
-------foobar-
|
||||||
]
|
]
|
||||||
assert_equal by_begin, `cat #{tempname} | #{FZF} -ffoobar --tiebreak=begin`.split($/)
|
assert_equal by_begin, `#{FZF} -ffoobar --tiebreak=begin < #{tempname}`.split($/)
|
||||||
assert_equal by_begin, `cat #{tempname} | #{FZF} -f"!z foobar" -x --tiebreak begin`.split($/)
|
assert_equal by_begin, `#{FZF} -f"!z foobar" -x --tiebreak begin < #{tempname}`.split($/)
|
||||||
|
|
||||||
assert_equal %w[
|
assert_equal %w[
|
||||||
-------foobar-
|
-------foobar-
|
||||||
----foobar--
|
----foobar--
|
||||||
-----foobar---
|
-----foobar---
|
||||||
--foobar--------
|
--foobar--------
|
||||||
], `cat #{tempname} | #{FZF} -ffoobar --tiebreak end`.split($/)
|
], `#{FZF} -ffoobar --tiebreak end < #{tempname}`.split($/)
|
||||||
|
|
||||||
assert_equal input, `cat #{tempname} | #{FZF} -f"!z" -x --tiebreak end`.split($/)
|
assert_equal input, `#{FZF} -f"!z" -x --tiebreak end < #{tempname}`.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tiebreak_index_begin
|
||||||
|
writelines tempname, [
|
||||||
|
'xoxxxxxoxx',
|
||||||
|
'xoxxxxxox',
|
||||||
|
'xxoxxxoxx',
|
||||||
|
'xxxoxoxxx',
|
||||||
|
'xxxxoxox',
|
||||||
|
' xxoxoxxx',
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
'xxxxoxox',
|
||||||
|
' xxoxoxxx',
|
||||||
|
'xxxoxoxxx',
|
||||||
|
'xxoxxxoxx',
|
||||||
|
'xoxxxxxox',
|
||||||
|
'xoxxxxxoxx',
|
||||||
|
], `#{FZF} -foo < #{tempname}`.split($/)
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
'xxxoxoxxx',
|
||||||
|
'xxxxoxox',
|
||||||
|
' xxoxoxxx',
|
||||||
|
'xxoxxxoxx',
|
||||||
|
'xoxxxxxoxx',
|
||||||
|
'xoxxxxxox',
|
||||||
|
], `#{FZF} -foo --tiebreak=index < #{tempname}`.split($/)
|
||||||
|
|
||||||
|
# Note that --tiebreak=begin is now based on the first occurrence of the
|
||||||
|
# first character on the pattern
|
||||||
|
assert_equal [
|
||||||
|
' xxoxoxxx',
|
||||||
|
'xxxoxoxxx',
|
||||||
|
'xxxxoxox',
|
||||||
|
'xxoxxxoxx',
|
||||||
|
'xoxxxxxoxx',
|
||||||
|
'xoxxxxxox',
|
||||||
|
], `#{FZF} -foo --tiebreak=begin < #{tempname}`.split($/)
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
' xxoxoxxx',
|
||||||
|
'xxxoxoxxx',
|
||||||
|
'xxxxoxox',
|
||||||
|
'xxoxxxoxx',
|
||||||
|
'xoxxxxxox',
|
||||||
|
'xoxxxxxoxx',
|
||||||
|
], `#{FZF} -foo --tiebreak=begin,length < #{tempname}`.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_tiebreak_end
|
||||||
|
writelines tempname, [
|
||||||
|
'xoxxxxxxxx',
|
||||||
|
'xxoxxxxxxx',
|
||||||
|
'xxxoxxxxxx',
|
||||||
|
'xxxxoxxxx',
|
||||||
|
'xxxxxoxxx',
|
||||||
|
' xxxxoxxx',
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
' xxxxoxxx',
|
||||||
|
'xxxxoxxxx',
|
||||||
|
'xxxxxoxxx',
|
||||||
|
'xoxxxxxxxx',
|
||||||
|
'xxoxxxxxxx',
|
||||||
|
'xxxoxxxxxx',
|
||||||
|
], `#{FZF} -fo < #{tempname}`.split($/)
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
'xxxxxoxxx',
|
||||||
|
' xxxxoxxx',
|
||||||
|
'xxxxoxxxx',
|
||||||
|
'xxxoxxxxxx',
|
||||||
|
'xxoxxxxxxx',
|
||||||
|
'xoxxxxxxxx',
|
||||||
|
], `#{FZF} -fo --tiebreak=end < #{tempname}`.split($/)
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
'xxxxxoxxx',
|
||||||
|
' xxxxoxxx',
|
||||||
|
'xxxxoxxxx',
|
||||||
|
'xxxoxxxxxx',
|
||||||
|
'xxoxxxxxxx',
|
||||||
|
'xoxxxxxxxx',
|
||||||
|
], `#{FZF} -fo --tiebreak=end,length,begin < #{tempname}`.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tiebreak_length_with_nth
|
def test_tiebreak_length_with_nth
|
||||||
@@ -516,7 +630,7 @@ class TestGoFZF < TestBase
|
|||||||
123:hello
|
123:hello
|
||||||
1234567:h
|
1234567:h
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fh`.split($/)
|
assert_equal output, `#{FZF} -fh < #{tempname}`.split($/)
|
||||||
|
|
||||||
output = %w[
|
output = %w[
|
||||||
1234567:h
|
1234567:h
|
||||||
@@ -524,7 +638,7 @@ class TestGoFZF < TestBase
|
|||||||
1:hell
|
1:hell
|
||||||
123:hello
|
123:hello
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/)
|
assert_equal output, `#{FZF} -fh -n2 -d: < #{tempname}`.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tiebreak_length_with_nth_trim_length
|
def test_tiebreak_length_with_nth_trim_length
|
||||||
@@ -543,16 +657,16 @@ class TestGoFZF < TestBase
|
|||||||
"apple juice bottle 1",
|
"apple juice bottle 1",
|
||||||
"apple ui bottle 2",
|
"apple ui bottle 2",
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fa -n1`.split($/)
|
assert_equal output, `#{FZF} -fa -n1 < #{tempname}`.split($/)
|
||||||
|
|
||||||
# len(1 ~ 2)
|
# len(1 ~ 2)
|
||||||
output = [
|
output = [
|
||||||
"apple ui bottle 2",
|
|
||||||
"app ic bottle 4",
|
"app ic bottle 4",
|
||||||
"apple juice bottle 1",
|
|
||||||
"app ice bottle 3",
|
"app ice bottle 3",
|
||||||
|
"apple ui bottle 2",
|
||||||
|
"apple juice bottle 1",
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fai -n1..2`.split($/)
|
assert_equal output, `#{FZF} -fai -n1..2 < #{tempname}`.split($/)
|
||||||
|
|
||||||
# len(1) + len(2)
|
# len(1) + len(2)
|
||||||
output = [
|
output = [
|
||||||
@@ -561,28 +675,17 @@ class TestGoFZF < TestBase
|
|||||||
"apple ui bottle 2",
|
"apple ui bottle 2",
|
||||||
"apple juice bottle 1",
|
"apple juice bottle 1",
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -x -f"a i" -n1,2`.split($/)
|
assert_equal output, `#{FZF} -x -f"a i" -n1,2 < #{tempname}`.split($/)
|
||||||
|
|
||||||
# len(2)
|
# len(2)
|
||||||
output = [
|
output = [
|
||||||
"apple ui bottle 2",
|
|
||||||
"app ic bottle 4",
|
"app ic bottle 4",
|
||||||
"app ice bottle 3",
|
"app ice bottle 3",
|
||||||
|
"apple ui bottle 2",
|
||||||
"apple juice bottle 1",
|
"apple juice bottle 1",
|
||||||
]
|
]
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fi -n2`.split($/)
|
assert_equal output, `#{FZF} -fi -n2 < #{tempname}`.split($/)
|
||||||
assert_equal output, `cat #{tempname} | #{FZF} -fi -n2,1..2`.split($/)
|
assert_equal output, `#{FZF} -fi -n2,1..2 < #{tempname}`.split($/)
|
||||||
end
|
|
||||||
|
|
||||||
def test_tiebreak_end_backward_scan
|
|
||||||
input = %w[
|
|
||||||
foobar-fb
|
|
||||||
fubar
|
|
||||||
]
|
|
||||||
writelines tempname, input
|
|
||||||
|
|
||||||
assert_equal input.reverse, `cat #{tempname} | #{FZF} -f fb`.split($/)
|
|
||||||
assert_equal input, `cat #{tempname} | #{FZF} -f fb --tiebreak=end`.split($/)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_cache
|
def test_invalid_cache
|
||||||
@@ -607,12 +710,19 @@ class TestGoFZF < TestBase
|
|||||||
assert_equal %w[4 5 6 9], readonce.split($/)
|
assert_equal %w[4 5 6 9], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_bind_print_query
|
||||||
|
tmux.send_keys "seq 1 1000 | #{fzf '-m --bind=ctrl-j:print-query'}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].end_with? '/1000' }
|
||||||
|
tmux.send_keys 'print-my-query', 'C-j'
|
||||||
|
assert_equal %w[print-my-query], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
def test_long_line
|
def test_long_line
|
||||||
data = '.' * 256 * 1024
|
data = '.' * 256 * 1024
|
||||||
File.open(tempname, 'w') do |f|
|
File.open(tempname, 'w') do |f|
|
||||||
f << data
|
f << data
|
||||||
end
|
end
|
||||||
assert_equal data, `cat #{tempname} | #{FZF} -f .`.chomp
|
assert_equal data, `#{FZF} -f . < #{tempname}`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_read0
|
def test_read0
|
||||||
@@ -694,20 +804,34 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_execute
|
def test_execute
|
||||||
output = '/tmp/fzf-test-execute'
|
output = '/tmp/fzf-test-execute'
|
||||||
opts = %[--bind \\"alt-a:execute(echo '[{}]' >> #{output}),alt-b:execute[echo '({}), ({})' >> #{output}],C:execute:echo '({}), [{}], @{}@' >> #{output}\\"]
|
opts = %[--bind \\"alt-a:execute(echo [{}] >> #{output}),alt-b:execute[echo /{}{}/ >> #{output}],C:execute:echo /{}{}{}/ >> #{output}\\"]
|
||||||
tmux.send_keys "seq 100 | #{fzf opts}", :Enter
|
wait = lambda { |exp| tmux.until { |lines| lines[-2].include? exp } }
|
||||||
tmux.until { |lines| lines[-2].include? '100/100' }
|
writelines tempname, %w[foo'bar foo"bar foo$bar]
|
||||||
tmux.send_keys :Escape, :a, :Escape, :a
|
tmux.send_keys "cat #{tempname} | #{fzf opts}; sync", :Enter
|
||||||
|
wait['3/3']
|
||||||
|
tmux.send_keys :Escape, :a
|
||||||
|
wait['/3']
|
||||||
|
tmux.send_keys :Escape, :a
|
||||||
|
wait['/3']
|
||||||
tmux.send_keys :Up
|
tmux.send_keys :Up
|
||||||
tmux.send_keys :Escape, :b, :Escape, :b
|
tmux.send_keys :Escape, :b
|
||||||
|
wait['/3']
|
||||||
|
tmux.send_keys :Escape, :b
|
||||||
|
wait['/3']
|
||||||
tmux.send_keys :Up
|
tmux.send_keys :Up
|
||||||
tmux.send_keys :C
|
tmux.send_keys :C
|
||||||
tmux.send_keys 'foobar'
|
wait['3/3']
|
||||||
tmux.until { |lines| lines[-2].include? '0/100' }
|
tmux.send_keys 'barfoo'
|
||||||
tmux.send_keys :Escape, :a, :Escape, :b, :Escape, :c
|
wait['0/3']
|
||||||
|
tmux.send_keys :Escape, :a
|
||||||
|
wait['/3']
|
||||||
|
tmux.send_keys :Escape, :b
|
||||||
|
wait['/3']
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
readonce
|
readonce
|
||||||
assert_equal ['["1"]', '["1"]', '("2"), ("2")', '("2"), ("2")', '("3"), ["3"], @"3"@'],
|
assert_equal %w[[foo'bar] [foo'bar]
|
||||||
|
/foo"barfoo"bar/ /foo"barfoo"bar/
|
||||||
|
/foo$barfoo$barfoo$bar/],
|
||||||
File.readlines(output).map(&:chomp)
|
File.readlines(output).map(&:chomp)
|
||||||
ensure
|
ensure
|
||||||
File.unlink output rescue nil
|
File.unlink output rescue nil
|
||||||
@@ -715,22 +839,47 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_execute_multi
|
def test_execute_multi
|
||||||
output = '/tmp/fzf-test-execute-multi'
|
output = '/tmp/fzf-test-execute-multi'
|
||||||
opts = %[--multi --bind \\"alt-a:execute-multi(echo '[{}], @{}@' >> #{output})\\"]
|
opts = %[--multi --bind \\"alt-a:execute-multi(echo {}/{} >> #{output}; sync)\\"]
|
||||||
tmux.send_keys "seq 100 | #{fzf opts}", :Enter
|
writelines tempname, %w[foo'bar foo"bar foo$bar foobar]
|
||||||
tmux.until { |lines| lines[-2].include? '100/100' }
|
tmux.send_keys "cat #{tempname} | #{fzf opts}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '4/4' }
|
||||||
tmux.send_keys :Escape, :a
|
tmux.send_keys :Escape, :a
|
||||||
|
tmux.until { |lines| lines[-2].include? '/4' }
|
||||||
tmux.send_keys :BTab, :BTab, :BTab
|
tmux.send_keys :BTab, :BTab, :BTab
|
||||||
tmux.send_keys :Escape, :a
|
tmux.send_keys :Escape, :a
|
||||||
|
tmux.until { |lines| lines[-2].include? '/4' }
|
||||||
tmux.send_keys :Tab, :Tab
|
tmux.send_keys :Tab, :Tab
|
||||||
tmux.send_keys :Escape, :a
|
tmux.send_keys :Escape, :a
|
||||||
|
tmux.until { |lines| lines[-2].include? '/4' }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
|
tmux.prepare
|
||||||
readonce
|
readonce
|
||||||
assert_equal ['["1"], @"1"@', '["1" "2" "3"], @"1" "2" "3"@', '["1" "2" "4"], @"1" "2" "4"@'],
|
assert_equal [%[foo'bar/foo'bar],
|
||||||
|
%[foo'bar foo"bar foo$bar/foo'bar foo"bar foo$bar],
|
||||||
|
%[foo'bar foo"bar foobar/foo'bar foo"bar foobar]],
|
||||||
File.readlines(output).map(&:chomp)
|
File.readlines(output).map(&:chomp)
|
||||||
ensure
|
ensure
|
||||||
File.unlink output rescue nil
|
File.unlink output rescue nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_execute_shell
|
||||||
|
# Custom script to use as $SHELL
|
||||||
|
output = tempname + '.out'
|
||||||
|
File.unlink output rescue nil
|
||||||
|
writelines tempname, ['#!/usr/bin/env bash', "echo $1 / $2 > #{output}", "sync"]
|
||||||
|
system "chmod +x #{tempname}"
|
||||||
|
|
||||||
|
tmux.send_keys "echo foo | SHELL=#{tempname} fzf --bind 'enter:execute:{}bar'", :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '1/1' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-2].include? '1/1' }
|
||||||
|
tmux.send_keys 'C-c'
|
||||||
|
tmux.prepare
|
||||||
|
assert_equal ["-c / 'foo'bar"], File.readlines(output).map(&:chomp)
|
||||||
|
ensure
|
||||||
|
File.unlink output rescue nil
|
||||||
|
end
|
||||||
|
|
||||||
def test_cycle
|
def test_cycle
|
||||||
tmux.send_keys "seq 8 | #{fzf :cycle}", :Enter
|
tmux.send_keys "seq 8 | #{fzf :cycle}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include? '8/8' }
|
tmux.until { |lines| lines[-2].include? '8/8' }
|
||||||
@@ -757,12 +906,12 @@ class TestGoFZF < TestBase
|
|||||||
lines[-2].include?('/90') &&
|
lines[-2].include?('/90') &&
|
||||||
lines[-3] == ' 1' &&
|
lines[-3] == ' 1' &&
|
||||||
lines[-4] == ' 2' &&
|
lines[-4] == ' 2' &&
|
||||||
lines[-13] == '> 15'
|
lines[-13] == '> 50'
|
||||||
end
|
end
|
||||||
tmux.send_keys :Down
|
tmux.send_keys :Down
|
||||||
end
|
end
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
assert_equal '15', readonce.chomp
|
assert_equal '50', readonce.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_header_lines_reverse
|
def test_header_lines_reverse
|
||||||
@@ -772,12 +921,12 @@ class TestGoFZF < TestBase
|
|||||||
lines[1].include?('/90') &&
|
lines[1].include?('/90') &&
|
||||||
lines[2] == ' 1' &&
|
lines[2] == ' 1' &&
|
||||||
lines[3] == ' 2' &&
|
lines[3] == ' 2' &&
|
||||||
lines[12] == '> 15'
|
lines[12] == '> 50'
|
||||||
end
|
end
|
||||||
tmux.send_keys :Up
|
tmux.send_keys :Up
|
||||||
end
|
end
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
assert_equal '15', readonce.chomp
|
assert_equal '50', readonce.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_header_lines_overflow
|
def test_header_lines_overflow
|
||||||
@@ -803,8 +952,8 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_header
|
def test_header
|
||||||
tmux.send_keys "seq 100 | #{fzf "--header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--header \\\"\\$(head -5 #{FILE})\\\""}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(FILE).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[-2].include?('100/100') &&
|
lines[-2].include?('100/100') &&
|
||||||
lines[-7..-3].map(&:strip) == header
|
lines[-7..-3].map(&:strip) == header
|
||||||
@@ -812,8 +961,8 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_header_reverse
|
def test_header_reverse
|
||||||
tmux.send_keys "seq 100 | #{fzf "--header=\\\"\\$(head -5 #{__FILE__})\\\" --reverse"}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--header=\\\"\\$(head -5 #{FILE})\\\" --reverse"}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(FILE).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[1].include?('100/100') &&
|
lines[1].include?('100/100') &&
|
||||||
lines[2..6].map(&:strip) == header
|
lines[2..6].map(&:strip) == header
|
||||||
@@ -821,8 +970,8 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_header_and_header_lines
|
def test_header_and_header_lines
|
||||||
tmux.send_keys "seq 100 | #{fzf "--header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--header-lines 10 --header \\\"\\$(head -5 #{FILE})\\\""}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(FILE).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[-2].include?('90/90') &&
|
lines[-2].include?('90/90') &&
|
||||||
lines[-7...-2].map(&:strip) == header &&
|
lines[-7...-2].map(&:strip) == header &&
|
||||||
@@ -831,8 +980,8 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_header_and_header_lines_reverse
|
def test_header_and_header_lines_reverse
|
||||||
tmux.send_keys "seq 100 | #{fzf "--reverse --header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter
|
tmux.send_keys "seq 100 | #{fzf "--reverse --header-lines 10 --header \\\"\\$(head -5 #{FILE})\\\""}", :Enter
|
||||||
header = File.readlines(__FILE__).take(5).map(&:strip)
|
header = File.readlines(FILE).take(5).map(&:strip)
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
lines[1].include?('90/90') &&
|
lines[1].include?('90/90') &&
|
||||||
lines[2...7].map(&:strip) == header &&
|
lines[2...7].map(&:strip) == header &&
|
||||||
@@ -840,7 +989,7 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_canel
|
def test_cancel
|
||||||
tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter
|
tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include?('10/10') }
|
tmux.until { |lines| lines[-2].include?('10/10') }
|
||||||
tmux.send_keys '123'
|
tmux.send_keys '123'
|
||||||
@@ -865,20 +1014,40 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_tabstop
|
||||||
|
writelines tempname, ["f\too\tba\tr\tbaz\tbarfooq\tux"]
|
||||||
|
{
|
||||||
|
1 => '> f oo ba r baz barfooq ux',
|
||||||
|
2 => '> f oo ba r baz barfooq ux',
|
||||||
|
3 => '> f oo ba r baz barfooq ux',
|
||||||
|
4 => '> f oo ba r baz barfooq ux',
|
||||||
|
5 => '> f oo ba r baz barfooq ux',
|
||||||
|
6 => '> f oo ba r baz barfooq ux',
|
||||||
|
7 => '> f oo ba r baz barfooq ux',
|
||||||
|
8 => '> f oo ba r baz barfooq ux',
|
||||||
|
9 => '> f oo ba r baz barfooq ux',
|
||||||
|
}.each do |ts, exp|
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys %[cat #{tempname} | fzf --tabstop=#{ts}], :Enter
|
||||||
|
tmux.until { |lines| exp.start_with? lines[-3].to_s.strip.sub(/\.\.$/, '') }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_with_nth
|
def test_with_nth
|
||||||
writelines tempname, ['hello world ', 'byebye']
|
writelines tempname, ['hello world ', 'byebye']
|
||||||
assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1`.chomp
|
assert_equal 'hello world ', `#{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 < #{tempname}`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_with_nth_ansi
|
def test_with_nth_ansi
|
||||||
writelines tempname, ["\x1b[33mhello \x1b[34;1mworld\x1b[m ", 'byebye']
|
writelines tempname, ["\x1b[33mhello \x1b[34;1mworld\x1b[m ", 'byebye']
|
||||||
assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi`.chomp
|
assert_equal 'hello world ', `#{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi < #{tempname}`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_with_nth_no_ansi
|
def test_with_nth_no_ansi
|
||||||
src = "\x1b[33mhello \x1b[34;1mworld\x1b[m "
|
src = "\x1b[33mhello \x1b[34;1mworld\x1b[m "
|
||||||
writelines tempname, [src, 'byebye']
|
writelines tempname, [src, 'byebye']
|
||||||
assert_equal src, `cat #{tempname} | #{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi`.chomp
|
assert_equal src, `#{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi < #{tempname}`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0_exit_code
|
def test_exit_0_exit_code
|
||||||
@@ -889,7 +1058,7 @@ class TestGoFZF < TestBase
|
|||||||
def test_invalid_term
|
def test_invalid_term
|
||||||
lines = `TERM=xxx #{FZF}`
|
lines = `TERM=xxx #{FZF}`
|
||||||
assert_equal 2, $?.exitstatus
|
assert_equal 2, $?.exitstatus
|
||||||
assert lines.include?('Invalid $TERM: xxx')
|
assert lines.include?('Invalid $TERM: xxx') || lines.include?('terminal entry not found')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_option
|
def test_invalid_option
|
||||||
@@ -911,13 +1080,9 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_exitstatus_empty
|
def test_exitstatus_empty
|
||||||
{ '99' => '0', '999' => '1' }.each do |query, status|
|
{ '99' => '0', '999' => '1' }.each do |query, status|
|
||||||
tmux.send_keys "seq 100 | #{FZF} -q #{query}", :Enter
|
tmux.send_keys "seq 100 | #{FZF} -q #{query}; echo --\\$?--", :Enter
|
||||||
tmux.until { |lines| lines[-2] =~ %r{ [10]/100} }
|
tmux.until { |lines| lines[-2] =~ %r{ [10]/100} }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
|
|
||||||
tmux.send_keys 'echo --\$?--'
|
|
||||||
tmux.until { |lines| lines.last.include? "echo --$?--" }
|
|
||||||
tmux.send_keys :Enter
|
|
||||||
tmux.until { |lines| lines.last.include? "--#{status}--" }
|
tmux.until { |lines| lines.last.include? "--#{status}--" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -939,10 +1104,97 @@ class TestGoFZF < TestBase
|
|||||||
`seq 10 | #{FZF} -f '1 | !1'`.lines.map(&:chomp)
|
`seq 10 | #{FZF} -f '1 | !1'`.lines.map(&:chomp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_hscroll_off
|
||||||
|
writelines tempname, ['=' * 10000 + '0123456789']
|
||||||
|
[0, 3, 6].each do |off|
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys "#{FZF} --hscroll-off=#{off} -q 0 < #{tempname}", :Enter
|
||||||
|
tmux.until { |lines| lines[-3].end_with?((0..off).to_a.join + '..') }
|
||||||
|
tmux.send_keys '9'
|
||||||
|
tmux.until { |lines| lines[-3].end_with? '789' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_partial_caching
|
||||||
|
tmux.send_keys 'seq 1000 | fzf -e', :Enter
|
||||||
|
tmux.until { |lines| lines[-2] == ' 1000/1000' }
|
||||||
|
tmux.send_keys 11
|
||||||
|
tmux.until { |lines| lines[-2] == ' 19/1000' }
|
||||||
|
tmux.send_keys 'C-a', "'"
|
||||||
|
tmux.until { |lines| lines[-2] == ' 28/1000' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_jump
|
||||||
|
tmux.send_keys "seq 1000 | #{fzf "--multi --jump-labels 12345 --bind 'ctrl-j:jump'"}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2] == ' 1000/1000' }
|
||||||
|
tmux.send_keys 'C-j'
|
||||||
|
tmux.until { |lines| lines[-7] == '5 5' }
|
||||||
|
tmux.until { |lines| lines[-8] == ' 6' }
|
||||||
|
tmux.send_keys '5'
|
||||||
|
tmux.until { |lines| lines[-7] == '> 5' }
|
||||||
|
tmux.send_keys :Tab
|
||||||
|
tmux.until { |lines| lines[-7] == ' >5' }
|
||||||
|
tmux.send_keys 'C-j'
|
||||||
|
tmux.until { |lines| lines[-7] == '5>5' }
|
||||||
|
tmux.send_keys '2'
|
||||||
|
tmux.until { |lines| lines[-4] == '> 2' }
|
||||||
|
tmux.send_keys :Tab
|
||||||
|
tmux.until { |lines| lines[-4] == ' >2' }
|
||||||
|
tmux.send_keys 'C-j'
|
||||||
|
tmux.until { |lines| lines[-7] == '5>5' }
|
||||||
|
|
||||||
|
# Press any key other than jump labels to cancel jump
|
||||||
|
tmux.send_keys '6'
|
||||||
|
tmux.until { |lines| lines[-3] == '> 1' }
|
||||||
|
tmux.send_keys :Tab
|
||||||
|
tmux.until { |lines| lines[-3] == '>>1' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
assert_equal %w[5 2 1], readonce.split($/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_jump_accept
|
||||||
|
tmux.send_keys "seq 1000 | #{fzf "--multi --jump-labels 12345 --bind 'ctrl-j:jump-accept'"}", :Enter
|
||||||
|
tmux.until { |lines| lines[-2] == ' 1000/1000' }
|
||||||
|
tmux.send_keys 'C-j'
|
||||||
|
tmux.until { |lines| lines[-7] == '5 5' }
|
||||||
|
tmux.send_keys '3'
|
||||||
|
assert_equal '3', readonce.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_preview
|
||||||
|
tmux.send_keys %[seq 1000 | sed s/^2$// | #{FZF} --preview 'sleep 0.2; echo {{}-{}}' --bind ?:toggle-preview], :Enter
|
||||||
|
tmux.until { |lines| lines[1].include?(' {1-1}') }
|
||||||
|
tmux.send_keys :Up
|
||||||
|
tmux.until { |lines| lines[1].include?(' {-}') }
|
||||||
|
tmux.send_keys '555'
|
||||||
|
tmux.until { |lines| lines[1].include?(' {555-555}') }
|
||||||
|
tmux.send_keys '?'
|
||||||
|
tmux.until { |lines| !lines[1].include?(' {555-555}') }
|
||||||
|
tmux.send_keys '?'
|
||||||
|
tmux.until { |lines| lines[1].include?(' {555-555}') }
|
||||||
|
tmux.send_keys :BSpace
|
||||||
|
tmux.until { |lines| lines[-2].start_with? ' 28/1000' }
|
||||||
|
tmux.send_keys 'foobar'
|
||||||
|
tmux.until { |lines| !lines[1].include?('{') }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_preview_hidden
|
||||||
|
tmux.send_keys %[seq 1000 | #{FZF} --preview 'echo {{}-{}}' --preview-window down:1:hidden --bind ?:toggle-preview], :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == '>' }
|
||||||
|
tmux.send_keys '?'
|
||||||
|
tmux.until { |lines| lines[-2].include?(' {1-1}') }
|
||||||
|
tmux.send_keys '555'
|
||||||
|
tmux.until { |lines| lines[-2].include?(' {555-555}') }
|
||||||
|
tmux.send_keys '?'
|
||||||
|
tmux.until { |lines| lines[-1] == '> 555' }
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def writelines path, lines
|
def writelines path, lines
|
||||||
File.unlink path while File.exists? path
|
File.unlink path while File.exists? path
|
||||||
File.open(path, 'w') { |f| f << lines.join($/) }
|
File.open(path, 'w') { |f| f << lines.join($/) + $/ }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -994,6 +1246,40 @@ module TestShell
|
|||||||
tmux.until(0) { |lines| lines[-1].include? '1 2 3' }
|
tmux.until(0) { |lines| lines[-1].include? '1 2 3' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_ctrl_t_unicode
|
||||||
|
FileUtils.mkdir_p '/tmp/fzf-test'
|
||||||
|
tmux.paste 'cd /tmp/fzf-test; echo -n test1 > "fzf-unicode 테스트1"; echo -n test2 > "fzf-unicode 테스트2"'
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'cat ', 'C-t', pane: 0
|
||||||
|
tmux.until(1) { |lines| lines.item_count >= 1 }
|
||||||
|
tmux.send_keys 'fzf-unicode', pane: 1
|
||||||
|
redraw = ->() { tmux.send_keys 'C-l', pane: 1 }
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *2/) }
|
||||||
|
|
||||||
|
tmux.send_keys '1', pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *1/) }
|
||||||
|
tmux.send_keys :BTab, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2].include? '(1)' }
|
||||||
|
|
||||||
|
tmux.send_keys :BSpace, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *2/) }
|
||||||
|
|
||||||
|
tmux.send_keys '2', pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *1/) }
|
||||||
|
tmux.send_keys :BTab, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2].include? '(2)' }
|
||||||
|
|
||||||
|
tmux.send_keys :Enter, pane: 1
|
||||||
|
tmux.until do |lines|
|
||||||
|
tmux.send_keys 'C-l'
|
||||||
|
[-1, -2].map { |offset| lines[offset] }.any? do |line|
|
||||||
|
line.start_with?('cat') && line.include?('fzf-unicode')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include? 'test1test2' }
|
||||||
|
end
|
||||||
|
|
||||||
def test_alt_c
|
def test_alt_c
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys :Escape, :c, pane: 0
|
tmux.send_keys :Escape, :c, pane: 0
|
||||||
@@ -1050,6 +1336,8 @@ module CompletionTest
|
|||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0
|
tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0
|
||||||
tmux.until(1) { |lines| lines.item_count > 0 }
|
tmux.until(1) { |lines| lines.item_count > 0 }
|
||||||
|
tmux.send_keys ' !d'
|
||||||
|
tmux.until(1) { |lines| lines[-2].include?(' 2/') }
|
||||||
tmux.send_keys :BTab, :BTab
|
tmux.send_keys :BTab, :BTab
|
||||||
tmux.until(1) { |lines| lines[-2].include?('(2)') }
|
tmux.until(1) { |lines| lines[-2].include?('(2)') }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
@@ -1085,17 +1373,34 @@ module CompletionTest
|
|||||||
tmux.send_keys 'C-u'
|
tmux.send_keys 'C-u'
|
||||||
tmux.send_keys 'cat /tmp/fzf\ test/**', :Tab, pane: 0
|
tmux.send_keys 'cat /tmp/fzf\ test/**', :Tab, pane: 0
|
||||||
tmux.until(1) { |lines| lines.item_count > 0 }
|
tmux.until(1) { |lines| lines.item_count > 0 }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys 'C-K', :Enter
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
tmux.send_keys 'C-L'
|
tmux.send_keys 'C-L'
|
||||||
lines[-1].end_with?('/tmp/fzf\ test/foobar')
|
lines[-1].end_with?('/tmp/fzf\ test/foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Should include hidden files
|
||||||
|
(1..100).each { |i| FileUtils.touch "/tmp/fzf-test/.hidden-#{i}" }
|
||||||
|
tmux.send_keys 'C-u'
|
||||||
|
tmux.send_keys 'cat /tmp/fzf-test/hidden**', :Tab, pane: 0
|
||||||
|
tmux.until(1) do |lines|
|
||||||
|
tmux.send_keys 'C-L'
|
||||||
|
lines[-2].include?('100/') &&
|
||||||
|
lines[-3].include?('/tmp/fzf-test/.hidden-')
|
||||||
|
end
|
||||||
|
tmux.send_keys :Enter
|
||||||
ensure
|
ensure
|
||||||
['/tmp/fzf-test', '/tmp/fzf test', '~/.fzf-home', 'no~such~user'].each do |f|
|
['/tmp/fzf-test', '/tmp/fzf test', '~/.fzf-home', 'no~such~user'].each do |f|
|
||||||
FileUtils.rm_rf File.expand_path(f)
|
FileUtils.rm_rf File.expand_path(f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_file_completion_root
|
||||||
|
tmux.send_keys 'ls /**', :Tab, pane: 0
|
||||||
|
tmux.until(1) { |lines| lines.item_count > 0 }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
end
|
||||||
|
|
||||||
def test_dir_completion
|
def test_dir_completion
|
||||||
tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter
|
tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
@@ -1142,6 +1447,69 @@ module CompletionTest
|
|||||||
ensure
|
ensure
|
||||||
Process.kill 'KILL', pid.to_i rescue nil if pid
|
Process.kill 'KILL', pid.to_i rescue nil if pid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_completion
|
||||||
|
tmux.send_keys '_fzf_compgen_path() { echo "\$1"; seq 10; }', :Enter
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'ls /tmp/**', :Tab, pane: 0
|
||||||
|
tmux.until(1) { |lines| lines.item_count == 11 }
|
||||||
|
tmux.send_keys :BTab, :BTab, :BTab
|
||||||
|
tmux.until(1) { |lines| lines[-2].include? '(3)' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until do |lines|
|
||||||
|
tmux.send_keys 'C-L'
|
||||||
|
lines[-1] == "ls /tmp 1 2"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_unset_completion
|
||||||
|
tmux.send_keys 'export FOO=BAR', :Enter
|
||||||
|
tmux.prepare
|
||||||
|
|
||||||
|
# Using tmux
|
||||||
|
tmux.send_keys 'unset FOO**', :Tab, pane: 0
|
||||||
|
tmux.until(1) { |lines| lines[-2].include? ' 1/' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == 'unset FOO' }
|
||||||
|
tmux.send_keys 'C-c'
|
||||||
|
|
||||||
|
# FZF_TMUX=0
|
||||||
|
new_shell
|
||||||
|
tmux.send_keys 'unset FOO**', :Tab
|
||||||
|
tmux.until { |lines| lines[-2].include? ' 1/' }
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1] == 'unset FOO' }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_file_completion_unicode
|
||||||
|
FileUtils.mkdir_p '/tmp/fzf-test'
|
||||||
|
tmux.paste 'cd /tmp/fzf-test; echo -n test3 > "fzf-unicode 테스트1"; echo -n test4 > "fzf-unicode 테스트2"'
|
||||||
|
tmux.prepare
|
||||||
|
tmux.send_keys 'cat fzf-unicode**', :Tab, pane: 0
|
||||||
|
redraw = ->() { tmux.send_keys 'C-l', pane: 1 }
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *2/) }
|
||||||
|
|
||||||
|
tmux.send_keys '1', pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *1/) }
|
||||||
|
tmux.send_keys :BTab, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2].include? '(1)' }
|
||||||
|
|
||||||
|
tmux.send_keys :BSpace, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *2/) }
|
||||||
|
|
||||||
|
tmux.send_keys '2', pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2] =~ %r(^ *1/) }
|
||||||
|
tmux.send_keys :BTab, pane: 1
|
||||||
|
tmux.until(1) { |lines| redraw.(); lines[-2].include? '(2)' }
|
||||||
|
|
||||||
|
tmux.send_keys :Enter, pane: 1
|
||||||
|
tmux.until do |lines|
|
||||||
|
tmux.send_keys 'C-l'
|
||||||
|
lines[-1].include?('cat') || lines[-2].include?('cat')
|
||||||
|
end
|
||||||
|
tmux.send_keys :Enter
|
||||||
|
tmux.until { |lines| lines[-1].include? 'test3test4' }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestBash < TestBase
|
class TestBash < TestBase
|
||||||
|
|||||||
Reference in New Issue
Block a user