Skip to content

Commit

Permalink
Export the current nth value as $FZF_NTH
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Jan 16, 2025
1 parent 938c15e commit b712f2b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ Also, fzf now offers "style presets" for quick customization, which can be activ
# Dim the other parts
ls -al | fzf --nth -1 --color nth:regular,fg:dim,current-fg:dim

# With 'change-nth'
# With 'change-nth'. The current nth option is exported as $FZF_NTH.
ps -ef | fzf --reverse --header-lines 1 --header-border bottom --input-border \
--color nth:regular,fg:dim,current-fg:dim \
--nth 8.. --bind 'ctrl-n:change-nth(..|1|2|3|4|5|6|7|)'
--color nth:regular,fg:dim,current-fg:dim \
--nth 8.. --bind 'ctrl-n:change-nth(1|2|3|4|5|6|7|)' \
--bind 'result:transform-prompt:echo "${FZF_NTH}> "'
```
- A single-character delimiter is now treated as a plain string delimiter rather than a regular expression delimiter, even if it's a regular expression meta-character.
- This means you can just write `--delimiter '|'` instead of escaping it as `--delimiter '\|'`
Expand Down
3 changes: 3 additions & 0 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,9 @@ func (t *Terminal) environImpl(forPreview bool) []string {
env = append(env, "FZF_PREVIEW_LABEL="+t.previewLabelOpts.label)
env = append(env, "FZF_BORDER_LABEL="+t.borderLabelOpts.label)
env = append(env, "FZF_LIST_LABEL="+t.listLabelOpts.label)
if len(t.nthCurrent) > 0 {
env = append(env, "FZF_NTH="+RangesToString(t.nthCurrent))
}
env = append(env, fmt.Sprintf("FZF_TOTAL_COUNT=%d", t.count))
env = append(env, fmt.Sprintf("FZF_MATCH_COUNT=%d", t.merger.Length()))
env = append(env, fmt.Sprintf("FZF_SELECT_COUNT=%d", len(t.selected)))
Expand Down
28 changes: 27 additions & 1 deletion src/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ type Range struct {
end int
}

func RangesToString(ranges []Range) string {
strs := []string{}
for _, r := range ranges {
s := ""
if r.begin == rangeEllipsis && r.end == rangeEllipsis {
s = ".."
} else if r.begin == r.end {
s = strconv.Itoa(r.begin)
} else {
if r.begin != rangeEllipsis {
s += strconv.Itoa(r.begin)
}

if r.begin != -1 {
s += ".."
if r.end != rangeEllipsis {
s += strconv.Itoa(r.end)
}
}
}
strs = append(strs, s)
}

return strings.Join(strs, ",")
}

// Token contains the tokenized part of the strings and its prefix length
type Token struct {
text *util.Chars
Expand All @@ -41,7 +67,7 @@ func (d Delimiter) String() string {
}

func newRange(begin int, end int) Range {
if begin == 1 {
if begin == 1 && end != 1 {
begin = rangeEllipsis
}
if end == -1 {
Expand Down
33 changes: 26 additions & 7 deletions test/test_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3729,19 +3729,38 @@ def test_change_nth
*[''] * 1000
]
writelines(input)
tmux.send_keys %(#{FZF} -qfoo -n1 --bind 'space:change-nth:2|3|4|5|' < #{tempname}), :Enter
nths = '1,2..4,-1,-3..,..2'
tmux.send_keys %(#{FZF} -qfoo -n#{nths} --bind 'space:change-nth(2|3|4|5|),result:transform-prompt:echo "[$FZF_NTH] "' < #{tempname}), :Enter

tmux.until { |lines| assert_equal 4, lines.match_count }
tmux.until do |lines|
assert lines.any_include?("[#{nths}] foo")
assert_equal 4, lines.match_count
end
tmux.send_keys :Space
tmux.until { |lines| assert_equal 3, lines.match_count }
tmux.until do |lines|
assert lines.any_include?('[2] foo')
assert_equal 3, lines.match_count
end
tmux.send_keys :Space
tmux.until { |lines| assert_equal 2, lines.match_count }
tmux.until do |lines|
assert lines.any_include?('[3] foo')
assert_equal 2, lines.match_count
end
tmux.send_keys :Space
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.until do |lines|
assert lines.any_include?('[4] foo')
assert_equal 1, lines.match_count
end
tmux.send_keys :Space
tmux.until { |lines| assert_equal 0, lines.match_count }
tmux.until do |lines|
assert lines.any_include?('[5] foo')
assert_equal 0, lines.match_count
end
tmux.send_keys :Space
tmux.until { |lines| assert_equal 4, lines.match_count }
tmux.until do |lines|
assert lines.any_include?("[#{nths}] foo")
assert_equal 4, lines.match_count
end
end
end

Expand Down

0 comments on commit b712f2b

Please sign in to comment.