Skip to content

Commit 6657639

Browse files
committed
Export both FZF_IDLE_TIME and FZF_IDLE_TIME_MS
Seconds is more ergonomic for shell threshold checks; milliseconds covers sub-second every() bindings. Exporting both lets the same script pick whichever resolution matches its every() interval.
1 parent 4364dc1 commit 6657639

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ CHANGELOG
44
0.73.0
55
------
66
- Timer-driven `every(N)` event for `--bind`, where `N` is seconds (fractional, floored to `0.01`). Ticks that overlap an in-flight action are coalesced, so a slow `reload` cannot accumulate a backlog.
7-
- New `FZF_IDLE_MS` environment variable (milliseconds since the last user activity) exported to child processes. Pair with `every(N)` to build idle-based behavior such as auto-accept or auto-quit (#1211).
7+
- New `FZF_IDLE_TIME` (whole seconds) and `FZF_IDLE_TIME_MS` (milliseconds) environment variables exported to child processes, holding the elapsed time since the last user activity. Pair with `every(N)` to build idle-based behavior such as auto-accept or auto-quit (#1211).
88
```sh
99
# Live process list; --track --id-nth 2 keeps the cursor on the same PID across reloads
1010
fzf --header-lines 1 --track --id-nth 2 --bind 'start,every(2):reload-sync:ps -ef'
1111

1212
# Auto-accept after 10 seconds of inactivity, with a countdown in the footer after 5s
1313
fzf --bind 'every(1):bg-transform:
14-
if [[ $FZF_IDLE_MS -lt 5000 ]]; then echo change-footer:
15-
elif [[ $FZF_IDLE_MS -lt 10000 ]]; then echo "change-footer:auto-accept in $(((10000 - FZF_IDLE_MS) / 1000))s"
14+
if [[ $FZF_IDLE_TIME -lt 5 ]]; then echo change-footer:
15+
elif [[ $FZF_IDLE_TIME -lt 10 ]]; then echo "change-footer:auto-accept in $((10 - FZF_IDLE_TIME))s"
1616
else echo accept
1717
fi'
1818
```

man/man1/fzf.1

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,9 @@ fzf exports the following environment variables to its child processes.
15001500
.br
15011501
.BR FZF_KEY " The name of the last key pressed"
15021502
.br
1503-
.BR FZF_IDLE_MS " Milliseconds since the last user activity"
1503+
.BR FZF_IDLE_TIME " Whole seconds since the last user activity"
1504+
.br
1505+
.BR FZF_IDLE_TIME_MS " Milliseconds since the last user activity"
15041506
.br
15051507
.BR FZF_PORT " Port number when \-\-listen option is used"
15061508
.br
@@ -1947,9 +1949,9 @@ Triggered every \fIN\fR seconds (\fIN\fR can be a fractional number, e.g.
19471949
\fB0.5\fR). The minimum interval is \fB0.01\fR seconds; values are floored
19481950
to that.
19491951

1950-
Combine with the \fBFZF_IDLE_MS\fR environment variable (milliseconds
1951-
since the last user activity) to build idle\-based behavior without a
1952-
separate event.
1952+
Combine with the \fBFZF_IDLE_TIME\fR (whole seconds) and
1953+
\fBFZF_IDLE_TIME_MS\fR (milliseconds) environment variables to build
1954+
idle\-based behavior without a separate event.
19531955

19541956
e.g.
19551957
\fB# Live process list, refreshed every 2 seconds.
@@ -1959,8 +1961,8 @@ e.g.
19591961

19601962
# Auto\-accept after 10 seconds of inactivity, with a countdown in the footer after 5s.
19611963
fzf \-\-bind 'every(1):bg\-transform:
1962-
if [[ $FZF_IDLE_MS \-lt 5000 ]]; then echo change\-footer:
1963-
elif [[ $FZF_IDLE_MS \-lt 10000 ]]; then echo "change\-footer:auto\-accept in $(((10000 \- FZF_IDLE_MS) / 1000))s"
1964+
if [[ $FZF_IDLE_TIME \-lt 5 ]]; then echo change\-footer:
1965+
elif [[ $FZF_IDLE_TIME \-lt 10 ]]; then echo "change\-footer:auto\-accept in $((10 \- FZF_IDLE_TIME))s"
19641966
else echo accept
19651967
fi'\fR
19661968
.RE

src/terminal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,9 @@ func (t *Terminal) environImpl(forPreview bool) []string {
13891389
env = append(env, "FZF_QUERY="+string(t.input))
13901390
env = append(env, "FZF_ACTION="+t.lastAction.Name())
13911391
env = append(env, "FZF_KEY="+t.lastKey)
1392-
env = append(env, fmt.Sprintf("FZF_IDLE_MS=%d", time.Since(t.lastActivity).Milliseconds()))
1392+
idleMs := time.Since(t.lastActivity).Milliseconds()
1393+
env = append(env, fmt.Sprintf("FZF_IDLE_TIME=%d", idleMs/1000))
1394+
env = append(env, fmt.Sprintf("FZF_IDLE_TIME_MS=%d", idleMs))
13931395
env = append(env, "FZF_PROMPT="+string(t.promptString))
13941396
env = append(env, "FZF_GHOST="+string(t.ghost))
13951397
env = append(env, "FZF_POINTER="+string(t.pointer))

test/test_core.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,20 +1431,19 @@ def test_every_event_unbind
14311431
assert_includes tmux.capture[-3], 'STOPPED'
14321432
end
14331433

1434-
def test_fzf_idle_ms_env
1435-
# FZF_IDLE_MS, combined with every(), implements idle-based behavior.
1436-
# Print seconds derived from milliseconds for stable assertions.
1437-
tmux.send_keys %(seq 100 | fzf --bind 'every(0.2):transform-header(echo "idle=$((FZF_IDLE_MS / 1000))")'), :Enter
1434+
def test_fzf_idle_time_env
1435+
# FZF_IDLE_TIME + FZF_IDLE_TIME_MS combined with every() implement idle-based behavior.
1436+
tmux.send_keys %(seq 100 | fzf --bind 'every(0.2):transform-header(echo "s=$FZF_IDLE_TIME ms_ok=$((FZF_IDLE_TIME_MS / 1000 == FZF_IDLE_TIME))")'), :Enter
14381437
tmux.until { |lines| assert_equal 100, lines.match_count }
1439-
# Idle counter advances without any input
1440-
tmux.until { |lines| assert_includes lines[-3], 'idle=1' }
1441-
tmux.until { |lines| assert_includes lines[-3], 'idle=2' }
1438+
# Idle counter advances without any input; ms/1000 stays consistent with seconds.
1439+
tmux.until { |lines| assert_includes lines[-3], 's=1 ms_ok=1' }
1440+
tmux.until { |lines| assert_includes lines[-3], 's=2 ms_ok=1' }
14421441
# Any keystroke resets the counter
14431442
tmux.send_keys 'x'
1444-
tmux.until { |lines| assert_includes lines[-3], 'idle=0' }
1443+
tmux.until { |lines| assert_includes lines[-3], 's=0 ms_ok=1' }
14451444
tmux.send_keys :BSpace
14461445
# And it advances again afterwards
1447-
tmux.until { |lines| assert_includes lines[-3], 'idle=1' }
1446+
tmux.until { |lines| assert_includes lines[-3], 's=1 ms_ok=1' }
14481447
end
14491448

14501449
def test_every_event_rejects_invalid_arg

0 commit comments

Comments
 (0)