Add scrollbar to the preview window
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 ee5cdb9 · Browse files · 4 files·+123 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+123 added−24 removed
- tests:+7 added−7 removed
- other:+116 added−17 removed
Provenance
- Commit
3b22440- Tree
7baafc1- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ee5cdb9
4 files changed+123 added−24 removed
modified:src/tui/tui.go+16 added
305 unmodified lines306 BorderLeft307 BorderRight308 )309 310+func (s BorderShape) HasRight() bool {311+ switch s {312+ case BorderNone, BorderLeft, BorderTop, BorderBottom, BorderHorizontal: // No right313+ return false314+ }315+ return true316+}317+318+func (s BorderShape) HasTop() bool {319+ switch s {320+ case BorderNone, BorderLeft, BorderRight, BorderBottom, BorderVertical: // No top321+ return false322+ }323+ return true324+}325+326 type BorderStyle struct {327 shape BorderShape328 horizontal rune329 vertical rune381 unmodified linesmodified:src/terminal.go+99 added-16 removed
75 unmodified lines76 scrollable bool77 final bool78 following bool79 spinner string80+ bar []bool81 }82 83 type previewed struct {84 version int64516 unmodified lines601 selected: make(map[int32]selectedItem),602 reqBox: util.NewEventBox(),603 initialPreviewOpts: opts.Preview,604 previewOpts: opts.Preview,604- previewer: previewer{0, []string{}, 0, showPreviewWindow, false, true, false, ""},605+ previewer: previewer{0, []string{}, 0, showPreviewWindow, false, true, false, "", []bool{}},606 previewed: previewed{0, 0, 0, false},607 previewBox: previewBox,608 eventBox: eventBox,609 mutex: sync.Mutex{},164 unmodified lines774 func (t *Terminal) noInfoLine() bool {775 return t.infoStyle != infoDefault776 }777 777-func (t *Terminal) getScrollbar() (int, int) {778- total := t.merger.Length()779- if total == 0 {778+func getScrollbar(total int, height int, offset int) (int, int) {779+ if total == 0 || total <= height {780 return 0, 0781 }782-783- maxItems := t.maxItems()784- barLength := util.Max(1, maxItems*maxItems/total)782+ barLength := util.Max(1, height*height/total)785- if total <= maxItems {786- return 0, 0787- }788-783 var barStart int790- if total == maxItems {784+ if total == height {785 barStart = 0786 } else {793- barStart = (maxItems - barLength) * t.offset / (total - maxItems)787+ barStart = (height - barLength) * offset / (total - height)788 }789 return barLength, barStart790 }791 792+func (t *Terminal) getScrollbar() (int, int) {793+ return getScrollbar(t.merger.Length(), t.maxItems(), t.offset)794+}795+796 // Input returns current query string797 func (t *Terminal) Input() (bool, []rune) {798 t.mutex.Lock()799 defer t.mutex.Unlock()303 unmodified lines1103 case tui.BorderVertical:1104 pwidth -= 41105 x += 21106 }1107+ if len(t.scrollbar) > 0 && !previewOpts.border.HasRight() {1108+ // Need a column to show scrollbar1109+ pwidth -= 11110+ }1111 t.pwindow = t.tui.NewWindow(y, x, pwidth, pheight, true, noBorder)1112 }1113 verticalPad := 21114 minPreviewHeight := 313 unmodified lines1128 resizePreviewWindows(*previewOpts.alternative)1129 }1130 return1131 }1132+ // Put scrollbar closer to the right border for consistent look1133+ if t.borderShape.HasRight() {1134+ width++1135+ }1136 if previewOpts.position == posUp {1137 t.window = t.tui.NewWindow(1138 marginInt[0]+pheight, marginInt[3], width, height-pheight, false, noBorder)1139 createPreviewWindow(marginInt[0], marginInt[3], width, pheight)10 unmodified lines1150 }1151 return1152 }1153 if previewOpts.position == posLeft {1154+ // Put scrollbar closer to the right border for consistent look1155+ if t.borderShape.HasRight() {1156+ width++1157+ }1158+ // Add a 1-column margin between the preview window and the main window1159 t.window = t.tui.NewWindow(1149- marginInt[0], marginInt[3]+pwidth, width-pwidth, height, false, noBorder)1160+ marginInt[0], marginInt[3]+pwidth+1, width-pwidth-1, height, false, noBorder)1161 createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)1162 } else {1163 t.window = t.tui.NewWindow(1164 marginInt[0], marginInt[3], width-pwidth, height, false, noBorder)1165+ // NOTE: fzf --preview 'cat {}' --preview-window border-left --border1166+ x := marginInt[3] + width - pwidth1167+ if !previewOpts.border.HasRight() && t.borderShape.HasRight() {1168+ pwidth++1169+ }1154- createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)1170+ createPreviewWindow(marginInt[0], x, pwidth, height)1171 }1172 }1173 }1174 resizePreviewWindows(t.previewOpts)1175 }1176+1177+ // Without preview window1178 if t.window == nil {1179+ if t.borderShape.HasRight() {1180+ // Put scrollbar closer to the right border for consistent look1181+ width++1182+ }1183 t.window = t.tui.NewWindow(1184 marginInt[0],1185 marginInt[3],1186 width,511 unmodified lines1698 1699 if !unchanged {1700 t.pwindow.FinishFill()1701 }1702+1703+ if len(t.scrollbar) == 0 {1704+ return1705+ }1706+1707+ effectiveHeight := height - headerLines1708+ barLength, barStart := getScrollbar(len(body), effectiveHeight, util.Min(len(body)-effectiveHeight, t.previewer.offset-headerLines))1709+ t.renderPreviewScrollbar(headerLines, barLength, barStart)1710 }1711 1712 func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unchanged bool) {1713 maxWidth := t.pwindow.Width()56 unmodified lines1770 lineNo++1771 }1772 }1773 1774+func (t *Terminal) renderPreviewScrollbar(yoff int, barLength int, barStart int) {1775+ height := t.pwindow.Height()1776+ w := t.pborder.Width()1777+ if len(t.previewer.bar) != height {1778+ t.previewer.bar = make([]bool, height)1779+ }1780+ xshift := -21781+ if !t.previewOpts.border.HasRight() {1782+ xshift = -11783+ }1784+ yshift := 11785+ if !t.previewOpts.border.HasTop() {1786+ yshift = 01787+ }1788+ for i := yoff; i < height; i++ {1789+ x := w + xshift1790+ y := i + yshift1791+1792+ // Avoid unnecessary redraws1793+ bar := i >= yoff+barStart && i < yoff+barStart+barLength1794+ if bar == t.previewer.bar[i] {1795+ continue1796+ }1797+1798+ t.previewer.bar[i] = bar1799+ t.pborder.Move(y, x)1800+ if i >= yoff+barStart && i < yoff+barStart+barLength {1801+ t.pborder.CPrint(tui.ColScrollbar, t.scrollbar)1802+ } else {1803+ t.pborder.Print(" ")1804+ }1805+ }1806+}1807+1808 func (t *Terminal) printPreview() {1809 if !t.hasPreviewWindow() {1810 return1811 }849 unmodified lines2661 }2662 }()2663 previewDraggingPos := -12664 barDragging := false2665+ pbarDragging := false2666 wasDown := false2667 for looping {2668 var newCommand *string2669 var reloadSync bool442 unmodified lines3112 clicked := !wasDown && me.Down3113 wasDown = me.Down3114 if !me.Down {3115 barDragging = false3116+ pbarDragging = false3117 previewDraggingPos = -13118 }3119 3120 // Scrolling10 unmodified lines3131 break3132 }3133 3134 // Preview dragging3069- if me.Down && (previewDraggingPos > 0 || clicked && t.hasPreviewWindow() && t.pwindow.Enclose(my, mx)) {3135+ if me.Down && (previewDraggingPos >= 0 || clicked && t.hasPreviewWindow() && t.pwindow.Enclose(my, mx)) {3136 if previewDraggingPos > 0 {3137 scrollPreviewBy(previewDraggingPos - my)3138 }3139 previewDraggingPos = my3140 break3141 }3142 3143+ // Prevew scrollbar dragging3144+ headerLines := t.previewOpts.headerLines3145+ pbarDragging = me.Down && (pbarDragging || clicked && t.hasPreviewWindow() && my >= t.pwindow.Top()+headerLines && my < t.pwindow.Top()+t.pwindow.Height() && mx == t.pwindow.Left()+t.pwindow.Width())3146+ if pbarDragging {3147+ effectiveHeight := t.pwindow.Height() - headerLines3148+ numLines := len(t.previewer.lines) - headerLines3149+ barLength, _ := getScrollbar(numLines, effectiveHeight, util.Min(numLines-effectiveHeight, t.previewer.offset-headerLines))3150+ if barLength > 0 {3151+ y := my - t.pwindow.Top() - headerLines - barLength/23152+ y = util.Constrain(y, 0, effectiveHeight-barLength)3153+ // offset = (total - maxItems) * barStart / (maxItems - barLength)3154+ t.previewer.offset = headerLines + int(math.Ceil(float64(y)*float64(numLines-effectiveHeight)/float64(effectiveHeight-barLength)))3155+ req(reqPreviewRefresh)3156+ }3157+ break3158+ }3159+3160 // Ignored3161 if !t.window.Enclose(my, mx) && !barDragging {3162 break3163 }280 unmodified linesmodified:test/test_go.rb+7 added-7 removed
2378 unmodified lines2379 def test_height_range_fit_preview_left2380 tmux.send_keys "seq 3 | fzf --height ~100% --border=vertical --preview 'seq {}' --preview-window left,5,border-right --padding 1 --exit-0 --header $'hello\\nworld' --header-lines=2", :Enter2381 expected = <<~OUTPUT2382 │2383- │ 1 │> 32384- │ 2 │ 22385- │ 3 │ 12386- │ │ hello2387- │ │ world2388- │ │ 1/1 ─2389- │ │>2383+ │ 1 │ > 32384+ │ 2 │ 22385+ │ 3 │ 12386+ │ │ hello2387+ │ │ world2388+ │ │ 1/1 ─2389+ │ │ >2390 │2391 OUTPUT2392 tmux.until { assert_block(expected, _1) }2393 end589 unmodified linesmodified:CHANGELOG.md+1 added-1 removed
10 unmodified lines11 12 # Send actions to the server13 curl -XPOST localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'14 ```15-- Added scrollbar on the main search window15+- Added draggable scrollbar to the main search window and the preview window16 ```sh17 # Hide scrollbar18 fzf --no-scrollbar19 1485 unmodified lines