Add scrollbar
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #3096
authored by Junegunn Choi · committed by Junegunn Choi · parent ec20dfe · Browse files · 7 files·+116 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+116 added−9 removed
- tests:+5 added−3 removed
- other:+111 added−6 removed
Provenance
- Commit
5cd6f1d- Tree
912b935- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ec20dfe
7 files changed+116 added−9 removed
modified:man/man1/fzf.1+9 added
355 unmodified lines356 Do not display horizontal separator on the info line. A synonym for357 \fB--separator=''\fB358 359 .TP360+.BI "--scrollbar=" "CHAR"361+Use the given character to render scrollbar. (default: '▏' or ':' depending on362+\fB--no-unicode\fR).363+364+.TP365+.B "--no-scrollbar"366+Do not display scrollbar. A synonym for \fB--scrollbar=''\fB367+368+.TP369 .BI "--prompt=" "STR"370 Input prompt (default: '> ')371 .TP372 .BI "--pointer=" "STR"825 unmodified linesmodified:src/tui/light.go+4 added-1 removed
174 unmodified lines175 }176 177 if r.mouse {178 r.csi("?1000h")179+ r.csi("?1002h")180 r.csi("?1006h")181 }182 r.csi(fmt.Sprintf("%dA", r.MaxY()-1))183 r.csi("G")385 unmodified lines569 // shift := t & 0b100570 // ctrl := t & 0b1000571 mod := t&0b1100 > 0572 573+ drag := t&0b100000 > 0574+575 if scroll != 0 {576 return Event{Mouse, 0, &MouseEvent{y, x, scroll, false, false, false, mod}}577 }578 579 double := false577- if down {580+ if down && !drag {581 now := time.Now()582 if !left { // Right double click is not allowed583 r.clickY = []int{}584 } else if now.Sub(r.prevDownTime) < doubleClickDuration {445 unmodified linesmodified:src/tui/tui.go+9 added
267 unmodified lines268 Cursor ColorAttr269 Selected ColorAttr270 Header ColorAttr271 Separator ColorAttr272+ Scrollbar ColorAttr273 Border ColorAttr274 BorderLabel ColorAttr275 PreviewLabel ColorAttr276 }189 unmodified lines466 ColSpinner ColorPair467 ColInfo ColorPair468 ColHeader ColorPair469 ColSeparator ColorPair470+ ColScrollbar ColorPair471 ColBorder ColorPair472 ColPreview ColorPair473 ColPreviewBorder ColorPair474 ColBorderLabel ColorPair16 unmodified lines491 Cursor: ColorAttr{colUndefined, AttrUndefined},492 Selected: ColorAttr{colUndefined, AttrUndefined},493 Header: ColorAttr{colUndefined, AttrUndefined},494 Separator: ColorAttr{colUndefined, AttrUndefined},495+ Scrollbar: ColorAttr{colUndefined, AttrUndefined},496 Border: ColorAttr{colUndefined, AttrUndefined},497 BorderLabel: ColorAttr{colUndefined, AttrUndefined},498 Disabled: ColorAttr{colUndefined, AttrUndefined},499 PreviewFg: ColorAttr{colUndefined, AttrUndefined},19 unmodified lines519 Cursor: ColorAttr{colDefault, AttrRegular},520 Selected: ColorAttr{colDefault, AttrRegular},521 Header: ColorAttr{colDefault, AttrRegular},522 Separator: ColorAttr{colDefault, AttrRegular},523+ Scrollbar: ColorAttr{colDefault, AttrRegular},524 Border: ColorAttr{colDefault, AttrRegular},525 BorderLabel: ColorAttr{colDefault, AttrRegular},526 Disabled: ColorAttr{colDefault, AttrRegular},527 PreviewFg: ColorAttr{colDefault, AttrRegular},24 unmodified lines552 Cursor: ColorAttr{colRed, AttrUndefined},553 Selected: ColorAttr{colMagenta, AttrUndefined},554 Header: ColorAttr{colCyan, AttrUndefined},555 Separator: ColorAttr{colBlack, AttrUndefined},556+ Scrollbar: ColorAttr{colBlack, AttrUndefined},557 Border: ColorAttr{colBlack, AttrUndefined},558 BorderLabel: ColorAttr{colWhite, AttrUndefined},559 Disabled: ColorAttr{colUndefined, AttrUndefined},560 PreviewFg: ColorAttr{colUndefined, AttrUndefined},16 unmodified lines577 Cursor: ColorAttr{161, AttrUndefined},578 Selected: ColorAttr{168, AttrUndefined},579 Header: ColorAttr{109, AttrUndefined},580 Separator: ColorAttr{59, AttrUndefined},581+ Scrollbar: ColorAttr{59, AttrUndefined},582 Border: ColorAttr{59, AttrUndefined},583 BorderLabel: ColorAttr{145, AttrUndefined},584 Disabled: ColorAttr{colUndefined, AttrUndefined},585 PreviewFg: ColorAttr{colUndefined, AttrUndefined},16 unmodified lines602 Cursor: ColorAttr{161, AttrUndefined},603 Selected: ColorAttr{168, AttrUndefined},604 Header: ColorAttr{31, AttrUndefined},605 Separator: ColorAttr{145, AttrUndefined},606+ Scrollbar: ColorAttr{145, AttrUndefined},607 Border: ColorAttr{145, AttrUndefined},608 BorderLabel: ColorAttr{59, AttrUndefined},609 Disabled: ColorAttr{colUndefined, AttrUndefined},610 PreviewFg: ColorAttr{colUndefined, AttrUndefined},40 unmodified lines651 theme.Gutter = o(theme.DarkBg, theme.Gutter)652 theme.PreviewFg = o(theme.Fg, theme.PreviewFg)653 theme.PreviewBg = o(theme.Bg, theme.PreviewBg)654 theme.PreviewLabel = o(theme.BorderLabel, theme.PreviewLabel)655+ theme.Scrollbar = o(theme.Separator, theme.Scrollbar)656 657 initPalette(theme)658 }659 24 unmodified lines684 ColSpinner = pair(theme.Spinner, theme.Bg)685 ColInfo = pair(theme.Info, theme.Bg)686 ColHeader = pair(theme.Header, theme.Bg)687 ColSeparator = pair(theme.Separator, theme.Bg)688+ ColScrollbar = pair(theme.Scrollbar, theme.Bg)689 ColBorder = pair(theme.Border, theme.Bg)690 ColBorderLabel = pair(theme.BorderLabel, theme.Bg)691 ColPreviewLabel = pair(theme.PreviewLabel, theme.PreviewBg)692 ColPreview = pair(theme.PreviewFg, theme.PreviewBg)2 unmodified linesmodified:src/options.go+23 added
71 unmodified lines72 --padding=PADDING Padding inside border (TRBL | TB,RL | T,RL,B | T,R,B,L)73 --info=STYLE Finder info style [default|inline|hidden]74 --separator=STR String to form horizontal separator on info line75 --no-separator Hide info line separator76+ --scrollbar[=CHAR] Scrollbar character77+ --no-scrollbar Hide scrollbar78 --prompt=STR Input prompt (default: '> ')79 --pointer=STR Pointer to the current line (default: '>')80 --marker=STR Multi-select marker (default: '>')81 --header=STR String to print as header209 unmodified lines291 Header []string292 HeaderLines int293 HeaderFirst bool294 Ellipsis string295+ Scrollbar *string296 Margin [4]sizeSpec297 Padding [4]sizeSpec298 BorderShape tui.BorderShape299 BorderLabel labelOpts61 unmodified lines361 Header: make([]string, 0),362 HeaderLines: 0,363 HeaderFirst: false,364 Ellipsis: "..",365+ Scrollbar: nil,366 Margin: defaultMargin(),367 Padding: defaultMargin(),368 Unicode: true,369 Tabstop: 8,480 unmodified lines850 case "border":851 mergeAttr(&theme.Border)852 case "separator":853 mergeAttr(&theme.Separator)854+ case "scrollbar":855+ mergeAttr(&theme.Scrollbar)856 case "label":857 mergeAttr(&theme.BorderLabel)858 case "preview-label":859 mergeAttr(&theme.PreviewLabel)715 unmodified lines1575 opts.Separator = &separator1576 case "--no-separator":1577 nosep := ""1578 opts.Separator = &nosep1579+ case "--scrollbar":1580+ given, bar := optionalNextString(allArgs, &i)1581+ if given {1582+ opts.Scrollbar = &bar1583+ } else {1584+ opts.Scrollbar = nil1585+ }1586+ case "--no-scrollbar":1587+ noBar := ""1588+ opts.Scrollbar = &noBar1589 case "--jump-labels":1590 opts.JumpLabels = nextString(allArgs, &i, "label characters required")1591 validateJumpLabels = true1592 case "-1", "--select-1":161 unmodified lines1754 } else if match, value := optString(arg, "--info="); match {1755 opts.InfoStyle = parseInfoStyle(value)1756 } else if match, value := optString(arg, "--separator="); match {1757 opts.Separator = &value1758+ } else if match, value := optString(arg, "--scrollbar="); match {1759+ opts.Scrollbar = &value1760 } else if match, value := optString(arg, "--toggle-sort="); match {1761 parseToggleSort(opts.Keymap, value)1762 } else if match, value := optString(arg, "--expect="); match {1763 for k, v := range parseKeyChords(value, "key names required") {98 unmodified lines1862 func postProcessOptions(opts *Options) {1863 if !opts.Version && !tui.IsLightRendererSupported() && opts.Height.size > 0 {1864 errorExit("--height option is currently not supported on this platform")1865 }1866+1867+ if opts.Scrollbar != nil && runewidth.StringWidth(*opts.Scrollbar) > 1 {1868+ errorExit("scrollbar display width should be 1")1869+ }1870+1871 // Default actions for CTRL-N / CTRL-P when --history is set1872 if opts.History != nil {1873 if _, prs := opts.Keymap[tui.CtrlP.AsEvent()]; !prs {1874 opts.Keymap[tui.CtrlP.AsEvent()] = toActions(actPrevHistory)131 unmodified linesmodified:src/terminal.go+58 added-5 removed
95 unmodified lines96 selected bool97 label string98 queryLen int99 width int100+ bar bool101 result Result102 }103 104 type fitpad struct {56 unmodified lines161 headerLines int162 header []string163 header0 []string164 ellipsis string165+ scrollbar string166 ansi bool167 tabstop int168 margin [4]sizeSpec169 padding [4]sizeSpec463 unmodified lines633 bar = "-"634 }635 t.separator, t.separatorLen = t.ansiLabelPrinter(bar, &tui.ColSeparator, true)636 }637+ if opts.Scrollbar == nil {638+ if t.unicode {639+ t.scrollbar = "▏" // Left one eighth block640+ } else {641+ t.scrollbar = "|"642+ }643+ } else {644+ t.scrollbar = *opts.Scrollbar645+ }646 647 _, t.hasLoadActions = t.keymap[tui.Load.AsEvent()]648 649 if err := startHttpServer(t.listenPort, t.serverChan); err != nil {123 unmodified lines773 func (t *Terminal) noInfoLine() bool {774 return t.infoStyle != infoDefault775 }776 777+func (t *Terminal) getScrollbar() (int, int) {778+ total := t.merger.Length()779+ if total == 0 {780+ return 0, 0781+ }782+783+ maxItems := t.maxItems()784+ barLength := util.Max(1, maxItems*maxItems/total)785+ if total <= maxItems {786+ return 0, 0787+ }788+789+ var barStart int790+ if total == maxItems {791+ barStart = 0792+ } else {793+ barStart = (maxItems - barLength) * t.offset / (total - maxItems)794+ }795+ return barLength, barStart796+}797+798 // Input returns current query string799 func (t *Terminal) Input() (bool, []rune) {800 t.mutex.Lock()801 defer t.mutex.Unlock()578 unmodified lines1380 }1381 1382 func (t *Terminal) printList() {1383 t.constrain()1384+ barLength, barStart := t.getScrollbar()1385 1386 maxy := t.maxItems()1387 count := t.merger.Length() - t.offset1388 for j := 0; j < maxy; j++ {5 unmodified lines1394 if t.noInfoLine() {1395 line--1396 }1397 if i < count {1365- t.printItem(t.merger.Get(i+t.offset), line, i, i == t.cy-t.offset)1398+ t.printItem(t.merger.Get(i+t.offset), line, i, i == t.cy-t.offset, i >= barStart && i < barStart+barLength)1399 } else if t.prevLines[i] != emptyLine {1400 t.prevLines[i] = emptyLine1401 t.move(line, 0, true)1402 }1403 }1404 }1405 1373-func (t *Terminal) printItem(result Result, line int, i int, current bool) {1406+func (t *Terminal) printItem(result Result, line int, i int, current bool, bar bool) {1407 item := result.item1408 _, selected := t.selected[item.Index()]1409 label := ""1410 if t.jumping != jumpDisabled {7 unmodified lines1418 }1419 1420 // Avoid unnecessary redraw1421 newLine := itemLine{current: current, selected: selected, label: label,1389- result: result, queryLen: len(t.input), width: 0}1422+ result: result, queryLen: len(t.input), width: 0, bar: bar}1423 prevLine := t.prevLines[i]1424 if prevLine.current == newLine.current &&1425 prevLine.selected == newLine.selected &&1426 prevLine.label == newLine.label &&31 unmodified lines1458 fillSpaces := prevLine.width - newLine.width1459 if fillSpaces > 0 {1460 t.window.Print(strings.Repeat(" ", fillSpaces))1461 }1462+ if len(t.scrollbar) > 0 && bar != prevLine.bar {1463+ t.move(line, t.window.Width()-1, true)1464+ if bar {1465+ t.window.CPrint(tui.ColScrollbar, t.scrollbar)1466+ }1467+ }1468 t.prevLines[i] = newLine1469 }1470 1471 func (t *Terminal) trimRight(runes []rune, width int) ([]rune, bool) {1565 unmodified lines3037 scrollPreviewBy(-me.S)3038 }3039 } else if t.window.Enclose(my, mx) {3040 mx -= t.window.Left()3002- my -= t.window.Top()3041+ bar := mx == t.window.Width()-13042 mx = util.Constrain(mx-t.promptLen, 0, len(t.input))3043+ my -= t.window.Top()3044 min := 2 + len(t.header)3045 if t.noInfoLine() {3046 min--3047 }7 unmodified lines3055 } else {3056 my = h - my - 13057 }3058 }3059+ if bar && my >= min {3060+ barLength, barStart := t.getScrollbar()3061+ if barLength > 0 {3062+ maxItems := t.maxItems()3063+ if newBarStart := util.Constrain(my-min-barLength/2, 0, maxItems-barLength); newBarStart != barStart {3064+ total := t.merger.Length()3065+ prevOffset := t.offset3066+ // barStart = (maxItems - barLength) * t.offset / (total - maxItems)3067+ t.offset = int(math.Ceil(float64(newBarStart) * float64(total-maxItems) / float64(maxItems-barLength)))3068+ t.cy = t.offset + t.cy - prevOffset3069+ req(reqList)3070+ }3071+ }3019- if me.Double {3072+ } else if me.Double {3073 // Double-click3074 if my >= min {3075 if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {3076 return doActions(actionsFor(tui.DoubleClick))233 unmodified linesmodified:test/test_go.rb+5 added-3 removed
21 unmodified lines22 23 FILE = File.expand_path(__FILE__)24 BASE = File.expand_path('..', __dir__)25 Dir.chdir(BASE)26-FZF = "FZF_DEFAULT_OPTS= FZF_DEFAULT_COMMAND= #{BASE}/bin/fzf"26+FZF = "FZF_DEFAULT_OPTS=--no-scrollbar FZF_DEFAULT_COMMAND= #{BASE}/bin/fzf"27 28 def wait29 since = Time.now30 begin33 unmodified lines64 end65 end66 67 def fish68- UNSETS.map { |v| v + '= ' }.join + 'fish'68+ UNSETS.map { |v| v + '= ' }.join + ' FZF_DEFAULT_OPTS=--no-scrollbar fish'69 end70 end71 end72 2834 unmodified lines2907 :fish2908 end2909 2910 def new_shell2911- tmux.send_keys 'env FZF_TMUX=1 fish', :Enter2911+ tmux.send_keys 'env FZF_TMUX=1 FZF_DEFAULT_OPTS=--no-scrollbar fish', :Enter2912 tmux.send_keys 'function fish_prompt; end; clear', :Enter2913 tmux.until { |lines| assert_empty lines }2914 end2915 10 unmodified lines2926 unset <%= UNSETS.join(' ') %>2927 unset $(env | sed -n /^_fzf_orig/s/=.*//p)2928 unset $(declare -F | sed -n "/_fzf/s/.*-f //p")2929 2930+export FZF_DEFAULT_OPTS=--no-scrollbar2931+2932 # Setup fzf2933 # ---------2934 if [[ ! "$PATH" == *<%= BASE %>/bin* ]]; then2935 export PATH="${PATH:+${PATH}:}<%= BASE %>/bin"47 unmodified linesmodified:CHANGELOG.md+8 added
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 window16+ ```sh17+ # Hide scrollbar18+ fzf --no-scrollbar19+20+ # Customize scrollbar21+ fzf --scrollbar ┆ --color scrollbar:blue22+ ```23 - New event24 - Added `load` event that is triggered when the input stream is complete25 and the initial processing of the list is complete.26 ```sh1474 unmodified lines