Allow removing header line with change-header and transform-header

If the new header is an empty string.

  fzf --header loading --bind 'start:reload:sleep 3; ls' --bind 'load:change-header:'
  fzf --header loading --bind 'start:reload:sleep 3; ls' --bind 'load:transform-header:'
This commit is contained in:
Junegunn Choi 2024-06-25 17:10:22 +09:00
parent 70bf8bc35d
commit 3b944addd4
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 13 additions and 11 deletions

View File

@ -1202,7 +1202,10 @@ func (t *Terminal) UpdateCount(cnt int, final bool, failedCommand *string) {
} }
func (t *Terminal) changeHeader(header string) bool { func (t *Terminal) changeHeader(header string) bool {
lines := strings.Split(strings.TrimSuffix(header, "\n"), "\n") var lines []string
if len(header) > 0 {
lines = strings.Split(strings.TrimSuffix(header, "\n"), "\n")
}
needFullRedraw := len(t.header0) != len(lines) needFullRedraw := len(t.header0) != len(lines)
t.header0 = lines t.header0 = lines
return needFullRedraw return needFullRedraw
@ -4094,16 +4097,13 @@ func (t *Terminal) Loop() error {
case actChangeQuery: case actChangeQuery:
t.input = []rune(a.a) t.input = []rune(a.a)
t.cx = len(t.input) t.cx = len(t.input)
case actTransformHeader: case actChangeHeader, actTransformHeader:
header := t.executeCommand(a.a, false, true, true, false, "") header := a.a
if t.changeHeader(header) { if a.t == actTransformHeader {
req(reqFullRedraw) header = t.executeCommand(a.a, false, true, true, false, "")
} else {
req(reqHeader)
} }
case actChangeHeader: if t.changeHeader(header) {
if t.changeHeader(a.a) { req(reqHeader, reqList, reqPrompt, reqInfo)
req(reqFullRedraw)
} else { } else {
req(reqHeader) req(reqHeader)
} }

View File

@ -3358,11 +3358,13 @@ class TestGoFZF < TestBase
end end
def test_start_on_reload def test_start_on_reload
tmux.send_keys %(echo foo | #{FZF} --header Loading --header-lines 1 --bind 'start:reload:sleep 2; echo bar' --bind 'load:change-header:Loaded'), :Enter tmux.send_keys %(echo foo | #{FZF} --header Loading --header-lines 1 --bind 'start:reload:sleep 2; echo bar' --bind 'load:change-header:Loaded' --bind space:change-header:), :Enter
tmux.until(timeout: 1) { |lines| assert_includes lines[-3], 'Loading' } tmux.until(timeout: 1) { |lines| assert_includes lines[-3], 'Loading' }
tmux.until(timeout: 1) { |lines| refute_includes lines[-4], 'foo' } tmux.until(timeout: 1) { |lines| refute_includes lines[-4], 'foo' }
tmux.until { |lines| assert_includes lines[-3], 'Loaded' } tmux.until { |lines| assert_includes lines[-3], 'Loaded' }
tmux.until { |lines| assert_includes lines[-4], 'bar' } tmux.until { |lines| assert_includes lines[-4], 'bar' }
tmux.send_keys :Space
tmux.until { |lines| assert_includes lines[-3], 'bar' }
end end
end end