m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-15 14:53:47 -05:00

Add $FZF_RAW for conditional actions

This commit is contained in:
Junegunn Choi
2025-09-29 22:36:44 +09:00
parent 0eefcf348e
commit 8f0c91545d
4 changed files with 35 additions and 1 deletions

View File

@@ -50,6 +50,22 @@ fzf --raw --color hidden:red:strikethrough
fzf --raw --color hidden:regular:red:strikethrough fzf --raw --color hidden:regular:red:strikethrough
``` ```
#### Conditional actions for raw mode
You may want to perform different actions depending on whether the current item
is a match or not. For that, fzf now exports `$FZF_RAW` environment variable.
It's:
- Undefined if raw mode is disabled
- `1` if the current item is a match
- `0` otherwise
```sh
# Do not allow selecting non-matching items
fzf --raw --bind 'enter:transform:[[ ${FZF_RAW-1} = 1 ]] && echo accept || echo bell'
```
#### Leveraging raw mode in shell integration #### Leveraging raw mode in shell integration
The `CTRL-R` binding (command history) now lets you toggle raw mode with The `CTRL-R` binding (command history) now lets you toggle raw mode with

View File

@@ -1377,6 +1377,8 @@ fzf exports the following environment variables to its child processes.
.BR FZF_PREVIEW_LINES " Number of lines in the preview window" .BR FZF_PREVIEW_LINES " Number of lines in the preview window"
.br .br
.BR FZF_PREVIEW_COLUMNS " Number of columns in the preview window" .BR FZF_PREVIEW_COLUMNS " Number of columns in the preview window"
.br
.BR FZF_RAW " Only in raw mode. 1 if the current item matches, 0 otherwise"
.SH EXTENDED SEARCH MODE .SH EXTENDED SEARCH MODE

View File

@@ -1305,6 +1305,13 @@ func (t *Terminal) environImpl(forPreview bool) []string {
if len(t.nthCurrent) > 0 { if len(t.nthCurrent) > 0 {
env = append(env, "FZF_NTH="+RangesToString(t.nthCurrent)) env = append(env, "FZF_NTH="+RangesToString(t.nthCurrent))
} }
if t.raw {
val := "0"
if t.isCurrentItemMatch() {
val = "1"
}
env = append(env, "FZF_RAW="+val)
}
inputState := "enabled" inputState := "enabled"
if t.inputless { if t.inputless {
inputState = "hidden" inputState = "hidden"

View File

@@ -5,7 +5,7 @@ require_relative 'lib/common'
# Testing raw mode # Testing raw mode
class TestRaw < TestInteractive class TestRaw < TestInteractive
def test_raw_mode def test_raw_mode
tmux.send_keys %(seq 1000 | #{FZF} --raw --bind ctrl-x:toggle-raw,a:enable-raw,b:disable-raw --gutter '▌' --multi), :Enter tmux.send_keys %(seq 1000 | #{FZF} --raw --bind ctrl-x:toggle-raw,a:enable-raw,b:disable-raw --gutter '▌' --multi --bind 'space:transform-prompt:echo "[[$FZF_RAW]] "'), :Enter
tmux.until { assert_equal 1000, it.match_count } tmux.until { assert_equal 1000, it.match_count }
tmux.send_keys 1 tmux.send_keys 1
@@ -64,11 +64,20 @@ class TestRaw < TestInteractive
assert_includes it, '▖ 10' assert_includes it, '▖ 10'
end end
tmux.send_keys :Down, :Space
tmux.until { assert_includes it, '[[0]] 11' }
tmux.send_keys :Up, :Space
tmux.until { assert_includes it, '[[1]] 11' }
tmux.send_keys 'b' tmux.send_keys 'b'
tmux.until do tmux.until do
assert_equal 1, it.select_count assert_equal 1, it.select_count
assert_includes it, '▌ 110' assert_includes it, '▌ 110'
assert_includes it, '>>11' assert_includes it, '>>11'
end end
tmux.send_keys :Space
tmux.until { assert_includes it, '[[]] 11' }
end end
end end