Skip to content
Myr

Resume preview following if the user scrolls the window to the bottom

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 0f03223 · Browse files · 3 files+111 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+111 added−10 removed

  • tests:+63 added−2 removed
  • other:+48 added−8 removed

3 files+111 added−10 removed

modified:src/terminal.go+43 added-8 removed
66 unmodified lines
67 jumpEnabled68 jumpAcceptEnabled69)7071type resumableState int7273const (74 disabledState resumableState = iota75 pausedState76 enabledState77)7879func (s resumableState) Enabled() bool {80 return s == enabledState81}8283func (s *resumableState) Force(flag bool) {84 if flag {85 *s = enabledState86 } else {87 *s = disabledState88 }89}9091func (s *resumableState) Set(flag bool) {92 if *s == disabledState {93 return94 }9596 if flag {97 *s = enabledState98 } else {99 *s = pausedState100 }101}102103type previewer struct {104 version int64105 lines []string106 offset int107 enabled bool108 scrollable bool109 final bool78 following bool110 following resumableState111 spinner string112 bar []bool113}114
517 unmodified lines
632 selected: make(map[int32]selectedItem),633 reqBox: util.NewEventBox(),634 initialPreviewOpts: opts.Preview,635 previewOpts: opts.Preview,604 previewer: previewer{0, []string{}, 0, len(opts.Preview.command) > 0, false, true, false, "", []bool{}},636 previewer: previewer{0, []string{}, 0, len(opts.Preview.command) > 0, false, true, disabledState, "", []bool{}},637 previewed: previewed{0, 0, 0, false},638 previewBox: previewBox,639 eventBox: eventBox,640 mutex: sync.Mutex{},
2029 unmodified lines
2670 case reqPreviewDisplay:2671 result := value.(previewResult)2672 if t.previewer.version != result.version {2673 t.previewer.version = result.version2642 t.previewer.following = t.previewOpts.follow2674 t.previewer.following.Force(t.previewOpts.follow)2675 if t.previewer.following.Enabled() {2676 t.previewer.offset = 02677 }2678 }2679 t.previewer.lines = result.lines2680 t.previewer.spinner = result.spinner2646 if t.previewer.following {2647 t.previewer.offset = len(t.previewer.lines) - t.pwindow.Height()2681 if t.previewer.following.Enabled() {2682 t.previewer.offset = util.Max(t.previewer.offset, len(t.previewer.lines)-(t.pwindow.Height()-t.previewOpts.headerLines))2683 } else if result.offset >= 0 {2684 t.previewer.offset = util.Constrain(result.offset, t.previewOpts.headerLines, len(t.previewer.lines)-1)2685 }2686 t.printPreview()
88 unmodified lines
2775 scrollPreviewTo := func(newOffset int) {2776 if !t.previewer.scrollable {2777 return2778 }2744 t.previewer.following = false2779 numLines := len(t.previewer.lines)2780 headerLines := t.previewOpts.headerLines2781 if t.previewOpts.cycle {2782 offsetRange := numLines - headerLines
1 unmodified line
2784 }2785 newOffset = util.Constrain(newOffset, headerLines, numLines-1)2786 if t.previewer.offset != newOffset {2787 t.previewer.offset = newOffset2788 t.previewer.following.Set(t.previewer.offset >= numLines-(t.pwindow.Height()-headerLines))2789 req(reqPreviewRefresh)2790 }2791 }2792 scrollPreviewBy := func(amount int) {
418 unmodified lines
3211 y := my - t.pwindow.Top() - headerLines - barLength/23212 y = util.Constrain(y, 0, effectiveHeight-barLength)3213 // offset = (total - maxItems) * barStart / (maxItems - barLength)3214 t.previewer.offset = headerLines + int(math.Ceil(float64(y)*float64(numLines-effectiveHeight)/float64(effectiveHeight-barLength)))3180 t.previewer.following = false3215 t.previewer.following.Set(t.previewer.offset >= numLines-effectiveHeight)3216 req(reqPreviewRefresh)3217 }3218 break3219 }
136 unmodified lines
3356 scrollPreviewTo(t.evaluateScrollOffset())3357 }33583359 // Resume following3325 t.previewer.following = t.previewOpts.follow3360 t.previewer.following.Force(t.previewOpts.follow)3361 case actNextSelected, actPrevSelected:3362 if len(t.selected) > 0 {3363 total := t.merger.Length()3364 for i := 1; i < total; i++ {
142 unmodified lines
modified:test/test_go.rb+63 added-2 removed
1941 unmodified lines
1942 tmux.until { |lines| assert_equal 'b> foo', lines[-1] }1943 end19441945 def test_preview_window_follow1946 tmux.send_keys "#{FZF} --preview 'seq 1000 | nl' --preview-window down:noborder:follow", :Enter1947 tmux.until { |lines| assert_equal '1000 1000', lines[-1].strip }1946 file = Tempfile.new('fzf-follow')1947 file.sync = true19481949 tmux.send_keys %[seq 100 | #{FZF} --preview 'tail -f "#{file.path}"' --preview-window follow --bind 'up:preview-up,down:preview-down,space:change-preview-window:follow|nofollow' --preview-window '~3'], :Enter1950 tmux.until { |lines| lines.item_count == 100 }19511952 # Write to the temporary file, and check if the preview window is showing1953 # the last line of the file1954 3.times { file.puts _1 } # header lines1955 1000.times { file.puts _1 }1956 tmux.until { |lines| assert_includes lines[1], '/1003' }1957 tmux.until { |lines| assert_includes lines[-2], '999' }19581959 # Scroll the preview window and fzf should stop following the file content1960 tmux.send_keys :Up1961 tmux.until { |lines| assert_includes lines[-2], '998' }1962 file.puts 'foo', 'bar'1963 tmux.until do |lines|1964 assert_includes lines[1], '/1005'1965 assert_includes lines[-2], '998'1966 end19671968 # Scroll back to the bottom and fzf should start following the file again1969 %w[999 foo bar].each do |item|1970 wait do1971 tmux.send_keys :Down1972 tmux.until { |lines| assert_includes lines[-2], item }1973 end1974 end1975 file.puts 'baz'1976 tmux.until do |lines|1977 assert_includes lines[1], '/1006'1978 assert_includes lines[-2], 'baz'1979 end19801981 # Scroll upwards to stop following1982 tmux.send_keys :Up1983 wait { |line| assert_includes lines[-2], 'bar' }1984 file.puts 'aaa'1985 tmux.until do |lines|1986 assert_includes lines[1], '/1007'1987 assert_includes lines[-2], 'bar'1988 end19891990 # Manually enable following1991 tmux.send_keys :Space1992 tmux.until { |lines| assert_includes lines[-2], 'aaa' }1993 file.puts 'bbb'1994 tmux.until do |lines|1995 assert_includes lines[1], '/1008'1996 assert_includes lines[-2], 'bbb'1997 end19981999 # Disable following2000 tmux.send_keys :Space2001 file.puts 'ccc', 'ddd'2002 tmux.until do |lines|2003 assert_includes lines[1], '/1010'2004 assert_includes lines[-2], 'bbb'2005 end2006 rescue2007 file.close2008 file.unlink2009 end20102011 def test_toggle_preview_wrap2012 tmux.send_keys "#{FZF} --preview 'for i in $(seq $FZF_PREVIEW_COLUMNS); do echo -n .; done; echo wrapped; echo 2nd line' --bind ctrl-w:toggle-preview-wrap", :Enter
1039 unmodified lines
modified:CHANGELOG.md+5 added
107 unmodified lines
108 - If the color for `separator` is not specified, it will default to the109 color for `border`. Same holds true for `scrollbar`. This is to reduce110 the number of configuration items required to achieve a consistent color111 scheme.112 - If `follow` flag is specified in `--preview-window` option, fzf will113 automatically scroll to the bottom of the streaming preview output. But114 when the user manually scrolls the window, the following stops. With115 this version, fzf will resume following if the user scrolls the window116 to the bottom.117- Added color name `preview-label` for `--preview-label` (defaults to `label`118 for `--border-label`)119- Minor bug fixes and improvements120
1389 unmodified lines