Implement multi-line display of multi-line items
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 5b204c5 · Browse files · 6 files·+406 added−182 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+406 added−182 removed
- tests:+2 added−2 removed
- other:+404 added−180 removed
Provenance
- Commit
04db440- Tree
ccb739f- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
5b204c5
6 files changed+406 added−182 removed
modified:man/man1/fzf.1+12 added-3 removed
160 unmodified lines161 .TP162 .B "--cycle"163 Enable cyclic scroll164 .TP165+.B "--no-multi-line"166+Disable multi-line display of items when using \fB--read0\fR167+.TP168 .B "--keep-right"169 Keep the right end of the line visible when it's too long. Effective only when170 the query string is empty.171 .TP34 unmodified lines206 207 fzf --height=-1208 209 When prefixed with \fB~\fR, fzf will automatically determine the height in the207-range according to the input size. Note that adaptive height is not compatible208-with top/bottom margin and padding given in percent size. It is also not209-compatible with a negative height value.210+range according to the input size.211 212 # Will not take up 100% of the screen213 seq 5 | fzf --height=~100%214 215+Adaptive height has the following limitations:216+.br217+* Cannot be used with top/bottom margin and padding given in percent size218+.br219+* Negative value is not allowed220+.br221+* It will not find the right size when there are multi-line items222+223 .TP224 .BI "--min-height=" "HEIGHT"225 Minimum height when \fB--height\fR is given in percent (default: 10).226 Ignored when \fB--height\fR is not specified.1376 unmodified linesmodified:src/util/chars.go+30 added
1 package util2 3 import (4+ "bytes"5 "fmt"6 "unicode"7 "unicode/utf8"8 "unsafe"65 unmodified lines74 func (chars *Chars) Bytes() []byte {75 return chars.slice76 }77 78+func (chars *Chars) NumLines(atMost int) int {79+ lines := 180+ if runes := chars.optionalRunes(); runes != nil {81+ for _, r := range runes {82+ if r == '\n' {83+ lines++84+ }85+ if lines >= atMost {86+ return atMost87+ }88+ }89+ return lines90+ }91+92+ for idx := 0; idx < len(chars.slice); idx++ {93+ found := bytes.IndexByte(chars.slice[idx:], '\n')94+ if found < 0 {95+ break96+ }97+98+ idx += found99+ lines++100+ if lines >= atMost {101+ return atMost102+ }103+ }104+ return lines105+}106+107 func (chars *Chars) optionalRunes() []rune {108 if chars.inBytes {109 return nil110 }118 unmodified linesmodified:src/options.go+7 added
43 unmodified lines44 -m, --multi[=MAX] Enable multi-select with tab/shift-tab45 --no-mouse Disable mouse46 --bind=KEYBINDS Custom key bindings. Refer to the man page.47 --cycle Enable cyclic scroll48+ --no-multi-line Disable multi-line display of items when using --read049 --keep-right Keep the right end of the line visible on overflow50 --scroll-off=LINES Number of screen lines to keep above or below when51 scrolling to the top or to the bottom (default: 0)52 --no-hscroll Disable horizontal scroll356 unmodified lines409 Height heightSpec410 MinHeight int411 Layout layoutType412 Cycle bool413+ MultiLine bool414 CursorLine bool415 KeepRight bool416 Hscroll bool417 HscrollOff int89 unmodified lines507 Bold: true,508 MinHeight: 10,509 Layout: layoutDefault,510 Cycle: false,511+ MultiLine: true,512 KeepRight: false,513 Hscroll: true,514 HscrollOff: 10,515 ScrollOff: 0,1548 unmodified lines2064 case "--no-highlight-line":2065 opts.CursorLine = false2066 case "--no-cycle":2067 opts.Cycle = false2068+ case "--multi-line":2069+ opts.MultiLine = true2070+ case "--no-multi-line":2071+ opts.MultiLine = false2072 case "--keep-right":2073 opts.KeepRight = true2074 case "--no-keep-right":2075 opts.KeepRight = false700 unmodified linesmodified:src/result.go+5 added-3 removed
13 unmodified lines14 15 type colorOffset struct {16 offset [2]int3217 color tui.ColorPair18+ match bool19 }20 21 type Result struct {22 item *Item86 unmodified lines109 // No ANSI codes110 if len(itemColors) == 0 {111 var offsets []colorOffset112 for _, off := range matchOffsets {112- offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch})113+ offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch, match: true})114 }115 return offsets116 }117 75 unmodified lines193 color = origColor.MergeNonDefault(color)194 }195 }196 colors = append(colors, colorOffset{196- offset: [2]int32{int32(start), int32(idx)}, color: color})197+ offset: [2]int32{int32(start), int32(idx)}, color: color, match: true})198 } else {199 ansi := itemColors[curr-1]200 colors = append(colors, colorOffset{201 offset: [2]int32{int32(start), int32(idx)},201- color: ansiToColorPair(ansi, colBase)})202+ color: ansiToColorPair(ansi, colBase),203+ match: false})204 }205 }206 }207 for idx, col := range cols {56 unmodified linesmodified:src/terminal.go+350 added-174 removed
146 unmodified lines147 err error148 }149 150 type itemLine struct {151+ firstLine int151- offset int152- current bool153- selected bool154- label string155- queryLen int156- width int157- bar bool158- result Result152+ cy int153+ current bool154+ selected bool155+ label string156+ queryLen int157+ width int158+ hasBar bool159+ result Result160+ empty bool161+ other bool162 }163 164+func (t *Terminal) markEmptyLine(line int) {165+ t.prevLines[line] = itemLine{firstLine: line, empty: true}166+}167+168+func (t *Terminal) markOtherLine(line int) {169+ t.prevLines[line] = itemLine{firstLine: line, other: true}170+}171+172 type fitpad struct {173 fit int174 pad int175 }176 166-var emptyLine = itemLine{}167-177 type labelPrinter func(tui.Window, int)178 179 type StatusItem struct {180 Index int `json:"index"`51 unmodified lines232 xoffset int233 yanked []rune234 input []rune235 multi int236+ multiLine bool237 sort bool238 toggleSort bool239 track trackOption240 delimiter Delimiter507 unmodified lines748 xoffset: 0,749 yanked: []rune{},750 input: input,751 multi: opts.Multi,752+ multiLine: opts.ReadZero && opts.MultiLine,753 sort: opts.Sort > 0,754 toggleSort: opts.ToggleSort,755 track: opts.Track,756 delimiter: opts.Delimiter,262 unmodified lines1019 item.colors = &colors1020 }1021 }1022 output := func() {1023+ line := t.promptLine()1024 t.printHighlighted(1013- Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false)1025+ Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, line, line, true, nil, nil)1026 }1027 _, promptLen := t.processTabs([]rune(trimmed), 0)1028 1029 return output, promptLen12 unmodified lines1042 func (t *Terminal) noSeparatorLine() bool {1043 return noSeparatorLine(t.infoStyle, t.separatorLen > 0)1044 }1045 1034-func getScrollbar(total int, height int, offset int) (int, int) {1035- if total == 0 || total <= height {1046+func getScrollbar(perLine int, total int, height int, offset int) (int, int) {1047+ if total == 0 || total*perLine <= height {1048 return 0, 01049 }1038- barLength := util.Max(1, height*height/total)1050+ barLength := util.Max(1, height*height/(total*perLine))1051 var barStart int1052 if total == height {1053 barStart = 01054 } else {1043- barStart = (height - barLength) * offset / (total - height)1055+ barStart = util.Min(height-barLength, (height*perLine-barLength)*offset/(total*perLine-height))1056 }1057 return barLength, barStart1058 }1059 1060+// Estimate the average number of lines per item. Instead of going through all1061+// items, we only check a few items around the current cursor position.1062+func (t *Terminal) avgNumLines() int {1063+ if !t.multiLine {1064+ return 11065+ }1066+1067+ maxItems := t.maxItems()1068+ numLines := 01069+ count := 01070+ total := t.merger.Length()1071+ offset := util.Max(0, util.Min(t.offset, total-maxItems-1))1072+ for idx := 0; idx < maxItems && idx+offset < total; idx++ {1073+ item := t.merger.Get(idx + offset)1074+ numLines += item.item.text.NumLines(maxItems)1075+ count++1076+ }1077+ if count == 0 {1078+ return 11079+ }1080+ return numLines / count1081+}1082+1083 func (t *Terminal) getScrollbar() (int, int) {1049- return getScrollbar(t.merger.Length(), t.maxItems(), t.offset)1084+ return getScrollbar(t.avgNumLines(), t.merger.Length(), t.maxItems(), t.offset)1085 }1086 1087 // Input returns current query string1088 func (t *Terminal) Input() (bool, []rune) {567 unmodified lines1656 t.move(t.promptLine(), t.promptLen+t.queryLen[0], false)1657 }1658 1659 func (t *Terminal) printPrompt() {1625- t.move(t.promptLine(), 0, true)1660 t.prompt()1661 1662 before, after := t.updatePromptOffset()1663 color := tui.ColInput14 unmodified lines1678 1679 func (t *Terminal) printInfo() {1680 pos := 01681 line := t.promptLine()1682+ move := func(y int, x int, clear bool) {1683+ t.move(y, x, clear)1684+ t.markOtherLine(y)1685+ }1686 printSpinner := func() {1687 if t.reading {1688 duration := int64(spinnerDuration)1689 idx := (time.Now().UnixNano() % (duration * int64(len(t.spinner)))) / duration10 unmodified lines1700 trimmed, _ := t.trimRight([]rune(str), maxWidth)1701 str = string(trimmed)1702 width = maxWidth1703 }1666- t.move(line, pos, t.separatorLen == 0)1704+ move(line, pos, t.separatorLen == 0)1705 if t.reading {1706 t.window.CPrint(tui.ColSpinner, str)1707 } else {1708 t.window.CPrint(tui.ColPrompt, str)1709 }1710 pos += width1711 }1712 printSeparator := func(fillLength int, pad bool) {1675- // --------_1713 if t.separatorLen > 0 {1714 t.separator(t.window, fillLength)1715 t.window.Print(" ")1716 } else if pad {1 unmodified line1718 }1719 }1720 switch t.infoStyle {1721 case infoDefault:1685- t.move(line+1, 0, t.separatorLen == 0)1722+ move(line+1, 0, t.separatorLen == 0)1723 printSpinner()1724 t.window.Print(" ") // Margin1725 pos = 21726 case infoRight:1690- t.move(line+1, 0, false)1727+ move(line+1, 0, false)1728 case infoInlineRight:1729 pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11730 case infoInline:1731 pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11732 printInfoPrefix()1733 case infoHidden:1734 if t.separatorLen > 0 {1698- t.move(line+1, 0, false)1735+ move(line+1, 0, false)1736 printSeparator(t.window.Width()-1, false)1737 }1738 return1739 }51 unmodified lines1791 }1792 1793 if t.infoStyle == infoInlineRight {1794 if len(t.infoPrefix) == 0 {1758- t.move(line, pos, false)1795+ move(line, pos, false)1796 newPos := util.Max(pos, t.window.Width()-util.StringWidth(output)-3)1797 t.window.Print(strings.Repeat(" ", newPos-pos))1798 pos = newPos1799 if pos < t.window.Width() {15 unmodified lines1815 t.window.CPrint(tui.ColInfo, output)1816 1817 if t.infoStyle == infoInlineRight {1818 if t.separatorLen > 0 {1782- t.move(line+1, 0, false)1819+ move(line+1, 0, false)1820 printSeparator(t.window.Width()-1, false)1821 }1822 return1823 }41 unmodified lines1865 item := &Item{1866 text: util.ToChars([]byte(trimmed)),1867 colors: colors}1868 1832- t.move(line, 0, true)1833- t.window.Print(" ")1869 t.printHighlighted(Result{item: item},1835- tui.ColHeader, tui.ColHeader, false, false)1870+ tui.ColHeader, tui.ColHeader, false, false, line, line, true,1871+ func(int) { t.window.Print(" ") }, nil)1872 }1873 }1874 1875 func (t *Terminal) printList() {1876 t.constrain()1877 barLength, barStart := t.getScrollbar()1878 1843- maxy := t.maxItems()1879+ maxy := t.maxItems() - 11880 count := t.merger.Length() - t.offset1845- for j := 0; j < maxy; j++ {1846- i := j1847- if t.layout == layoutDefault {1848- i = maxy - 1 - j1849- }1850- line := i + 2 + t.visibleHeaderLines()1851- if t.noSeparatorLine() {1852- line--1853- }1854- if i < count {1855- t.printItem(t.merger.Get(i+t.offset), line, i, i == t.cy-t.offset, i >= barStart && i < barStart+barLength)1856- } else if t.prevLines[i] != emptyLine || t.prevLines[i].offset != line {1857- t.prevLines[i] = emptyLine1881+1882+ // Start line1883+ startLine := 2 + t.visibleHeaderLines()1884+ if t.noSeparatorLine() {1885+ startLine--1886+ }1887+ maxy += startLine1888+1889+ barRange := [2]int{startLine + barStart, startLine + barStart + barLength}1890+ for line, itemCount := startLine, 0; line <= maxy; line, itemCount = line+1, itemCount+1 {1891+ if itemCount < count {1892+ item := t.merger.Get(itemCount + t.offset)1893+ line = t.printItem(item, line, maxy, itemCount, itemCount == t.cy-t.offset, barRange)1894+ } else if !t.prevLines[line].empty {1895 t.move(line, 0, true)1896+ t.markEmptyLine(line)1897+ // If the screen is not filled with the list in non-multi-line mode,1898+ // scrollbar is not visible at all. But in multi-line mode, we may need1899+ // to redraw the scrollbar character at the end.1900+ if t.multiLine {1901+ t.prevLines[line].hasBar = t.printBar(line, true, barRange)1902+ }1903 }1904 }1905 }1906 1863-func (t *Terminal) printItem(result Result, line int, i int, current bool, bar bool) {1907+func (t *Terminal) printBar(lineNum int, forceRedraw bool, barRange [2]int) bool {1908+ hasBar := lineNum >= barRange[0] && lineNum < barRange[1]1909+ if len(t.scrollbar) > 0 && (hasBar != t.prevLines[lineNum].hasBar || forceRedraw) {1910+ t.move(lineNum, t.window.Width()-1, true)1911+ if hasBar {1912+ t.window.CPrint(tui.ColScrollbar, t.scrollbar)1913+ }1914+ }1915+ return hasBar1916+}1917+1918+func (t *Terminal) printItem(result Result, line int, maxLine int, index int, current bool, barRange [2]int) int {1919 item := result.item1920 _, selected := t.selected[item.Index()]1921 label := ""1922 if t.jumping != jumpDisabled {1868- if i < len(t.jumpLabels) {1923+ if index < len(t.jumpLabels) {1924 // Striped1870- current = i%2 == 01871- label = t.jumpLabels[i:i+1] + strings.Repeat(" ", t.pointerLen-1)1925+ current = index%2 == 01926+ label = t.jumpLabels[index:index+1] + strings.Repeat(" ", t.pointerLen-1)1927 }1928 } else if current {1929 label = t.pointer1930 }1931 1932 // Avoid unnecessary redraw1878- newLine := itemLine{offset: line, current: current, selected: selected, label: label,1879- result: result, queryLen: len(t.input), width: 0, bar: bar}1880- prevLine := t.prevLines[i]1881- forceRedraw := prevLine.offset != newLine.offset1882- printBar := func() {1883- if len(t.scrollbar) > 0 && (bar != prevLine.bar || forceRedraw) {1884- t.prevLines[i].bar = bar1885- t.move(line, t.window.Width()-1, true)1886- if bar {1887- t.window.CPrint(tui.ColScrollbar, t.scrollbar)1888- }1889- }1933+ newLine := itemLine{firstLine: line, cy: index + t.offset, current: current, selected: selected, label: label,1934+ result: result, queryLen: len(t.input), width: 0, hasBar: line >= barRange[0] && line < barRange[1]}1935+ prevLine := t.prevLines[line]1936+ forceRedraw := prevLine.other || prevLine.firstLine != newLine.firstLine1937+ printBar := func(lineNum int, forceRedraw bool) bool {1938+ return t.printBar(lineNum, forceRedraw, barRange)1939 }1940 1941 if !forceRedraw &&1942 prevLine.current == newLine.current &&1943 prevLine.selected == newLine.selected &&1944 prevLine.label == newLine.label &&1945 prevLine.queryLen == newLine.queryLen &&1946 prevLine.result == newLine.result {1898- printBar()1899- return1947+ t.prevLines[line].hasBar = printBar(line, false)1948+ if !t.multiLine {1949+ return line1950+ }1951+ return line + item.text.NumLines(maxLine-line+1) - 11952 }1953 1902- t.move(line, 0, forceRedraw)1903- if current {1904- if len(label) == 0 {1905- t.window.CPrint(tui.ColCurrentCursorEmpty, t.pointerEmpty)1954+ maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)1955+ postTask := func(lineNum int, width int) {1956+ if (current || selected) && t.highlightLine {1957+ color := tui.ColSelected1958+ if current {1959+ color = tui.ColCurrent1960+ }1961+ fillSpaces := maxWidth - width1962+ if fillSpaces > 0 {1963+ t.window.CPrint(color, strings.Repeat(" ", fillSpaces))1964+ }1965+ newLine.width = maxWidth1966 } else {1907- t.window.CPrint(tui.ColCurrentCursor, label)1967+ fillSpaces := t.prevLines[lineNum].width - width1968+ if fillSpaces > 0 {1969+ t.window.Print(strings.Repeat(" ", fillSpaces))1970+ }1971+ newLine.width = width1972 }1973+ // When width is 0, line is completely cleared. We need to redraw scrollbar1974+ newLine.hasBar = printBar(lineNum, forceRedraw || width == 0)1975+ t.prevLines[lineNum] = newLine1976+ }1977+1978+ var finalLineNum int1979+ if current {1980+ preTask := func(lineOffset int) {1981+ if len(label) == 0 {1982+ t.window.CPrint(tui.ColCurrentCursorEmpty, t.pointerEmpty)1983+ } else {1984+ t.window.CPrint(tui.ColCurrentCursor, label)1985+ }1909- if selected {1910- t.window.CPrint(tui.ColCurrentMarker, t.marker)1911- } else {1912- t.window.CPrint(tui.ColCurrentSelectedEmpty, t.markerEmpty)1986+ if selected {1987+ t.window.CPrint(tui.ColCurrentMarker, t.marker)1988+ } else {1989+ t.window.CPrint(tui.ColCurrentSelectedEmpty, t.markerEmpty)1990+ }1991 }1914- newLine.width = t.printHighlighted(result, tui.ColCurrent, tui.ColCurrentMatch, true, true)1992+ finalLineNum = t.printHighlighted(result, tui.ColCurrent, tui.ColCurrentMatch, true, true, line, maxLine, forceRedraw, preTask, postTask)1993 } else {1994+ preTask := func(lineOffset int) {1916- if len(label) == 0 {1917- t.window.CPrint(tui.ColCursorEmpty, t.pointerEmpty)1918- } else {1919- t.window.CPrint(tui.ColCursor, label)1995+ if len(label) == 0 {1996+ t.window.CPrint(tui.ColCursorEmpty, t.pointerEmpty)1997+ } else {1998+ t.window.CPrint(tui.ColCursor, label)1999+ }2000+ if selected {2001+ t.window.CPrint(tui.ColMarker, t.marker)2002+ } else {2003+ t.window.Print(t.markerEmpty)2004+ }2005 }1921- if selected {1922- t.window.CPrint(tui.ColMarker, t.marker)1923- } else {1924- t.window.Print(t.markerEmpty)1925- }2006 var base, match tui.ColorPair2007 if selected {2008 base = tui.ColSelected2009 match = tui.ColSelectedMatch2010 } else {2011 base = tui.ColNormal2012 match = tui.ColMatch2013 }1934- newLine.width = t.printHighlighted(result, base, match, false, true)2014+ finalLineNum = t.printHighlighted(result, base, match, false, true, line, maxLine, forceRedraw, preTask, postTask)2015 }1936- if (current || selected) && t.highlightLine {1937- color := tui.ColSelected1938- if current {1939- color = tui.ColCurrent1940- }1941- maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)1942- fillSpaces := maxWidth - newLine.width1943- newLine.width = maxWidth1944- if fillSpaces > 0 {1945- t.window.CPrint(color, strings.Repeat(" ", fillSpaces))1946- }1947- } else {1948- fillSpaces := prevLine.width - newLine.width1949- if fillSpaces > 0 {1950- t.window.Print(strings.Repeat(" ", fillSpaces))1951- }1952- }1953- printBar()1954- t.prevLines[i] = newLine2016+ return finalLineNum2017 }2018 2019 func (t *Terminal) trimRight(runes []rune, width int) ([]rune, bool) {2020 // We start from the beginning to handle tab characters32 unmodified lines2053 func (t *Terminal) overflow(runes []rune, max int) bool {2054 return t.displayWidthWithLimit(runes, 0, max) > max2055 }2056 1995-func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool) int {2057+func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(int), postTask func(int, int)) int {2058+ var displayWidth int2059 item := result.item1997-1998- // Overflow1999- text := make([]rune, item.text.Length())2000- copy(text, item.text.ToRunes())2060 matchOffsets := []Offset{}2061 var pos *[]int2062 if match && t.merger.pattern != nil {2063 _, matchOffsets, pos = t.merger.pattern.MatchItem(item, true, t.slab)6 unmodified lines2070 charOffsets[idx] = offset2071 }2072 sort.Sort(ByOrder(charOffsets))2073 }2015- var maxe int2016- for _, offset := range charOffsets {2017- maxe = util.Max(maxe, int(offset[1]))2074+ allOffsets := result.colorOffsets(charOffsets, t.theme, colBase, colMatch, current)2075+2076+ from := 02077+ text := make([]rune, item.text.Length())2078+ copy(text, item.text.ToRunes())2079+2080+ finalLineNum := lineNum2081+ numItemLines := 12082+ cutoff := 02083+ if t.multiLine && t.layout == layoutDefault {2084+ maxLines := maxLineNum - lineNum + 12085+ numItemLines = item.text.NumLines(maxLines)2086+ // Cut off the upper lines in the 'default' layout2087+ if !current && maxLines == numItemLines {2088+ cutoff = item.text.NumLines(math.MaxInt32) - maxLines2089+ }2090 }2091+ for lineOffset := 0; from <= len(text) && (lineNum <= maxLineNum || maxLineNum == 0); lineOffset++ {2092+ finalLineNum = lineNum2093 2020- offsets := result.colorOffsets(charOffsets, t.theme, colBase, colMatch, current)2021- maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)2022- ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)2023- maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(text))2024- displayWidth := t.displayWidthWithLimit(text, 0, maxWidth)2025- if displayWidth > maxWidth {2026- transformOffsets := func(diff int32, rightTrim bool) {2027- for idx, offset := range offsets {2028- b, e := offset.offset[0], offset.offset[1]2029- el := int32(len(ellipsis))2030- b += el - diff2031- e += el - diff2032- b = util.Max32(b, el)2033- if rightTrim {2034- e = util.Min32(e, int32(maxWidth-ellipsisWidth))2094+ line := text[from:]2095+ if t.multiLine {2096+ for idx, r := range text[from:] {2097+ if r == '\n' {2098+ line = line[:idx]2099+ break2100 }2036- offsets[idx].offset[0] = b2037- offsets[idx].offset[1] = util.Max32(b, e)2101 }2102 }2040- if t.hscroll {2041- if t.keepRight && pos == nil {2042- trimmed, diff := t.trimLeft(text, maxWidth-ellipsisWidth)2043- transformOffsets(diff, false)2044- text = append(ellipsis, trimmed...)2045- } else if !t.overflow(text[:maxe], maxWidth-ellipsisWidth) {2046- // Stri..2047- text, _ = t.trimRight(text, maxWidth-ellipsisWidth)2048- text = append(text, ellipsis...)2103+2104+ offsets := []colorOffset{}2105+ for _, offset := range allOffsets {2106+ if offset.offset[0] >= int32(from) && offset.offset[1] <= int32(from+len(line)) {2107+ offset.offset[0] -= int32(from)2108+ offset.offset[1] -= int32(from)2109+ offsets = append(offsets, offset)2110 } else {2050- // Stri..2051- rightTrim := false2052- if t.overflow(text[maxe:], ellipsisWidth) {2053- text = append(text[:maxe], ellipsis...)2054- rightTrim = true2055- }2056- // ..ri..2057- var diff int322058- text, diff = t.trimLeft(text, maxWidth-ellipsisWidth)2111+ allOffsets = allOffsets[len(offsets):]2112+ break2113+ }2114+ }2115 2060- // Transform offsets2061- transformOffsets(diff, rightTrim)2062- text = append(ellipsis, text...)2116+ from += len(line) + 12117+2118+ if cutoff > 0 {2119+ cutoff--2120+ lineOffset--2121+ continue2122+ }2123+2124+ var maxe int2125+ for _, offset := range offsets {2126+ if offset.match {2127+ maxe = util.Max(maxe, int(offset.offset[1]))2128 }2064- } else {2065- text, _ = t.trimRight(text, maxWidth-ellipsisWidth)2066- text = append(text, ellipsis...)2129+ }2130 2068- for idx, offset := range offsets {2069- offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))2070- offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))2131+ actualLineNum := lineNum2132+ if t.layout == layoutDefault {2133+ actualLineNum = (lineNum - lineOffset) + (numItemLines - lineOffset) - 12134+ }2135+ t.move(actualLineNum, 0, forceRedraw)2136+2137+ if preTask != nil {2138+ preTask(lineOffset)2139+ }2140+2141+ maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)2142+ ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)2143+ maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(line))2144+ displayWidth = t.displayWidthWithLimit(line, 0, maxWidth)2145+ if displayWidth > maxWidth {2146+ transformOffsets := func(diff int32, rightTrim bool) {2147+ for idx, offset := range offsets {2148+ b, e := offset.offset[0], offset.offset[1]2149+ el := int32(len(ellipsis))2150+ b += el - diff2151+ e += el - diff2152+ b = util.Max32(b, el)2153+ if rightTrim {2154+ e = util.Min32(e, int32(maxWidth-ellipsisWidth))2155+ }2156+ offsets[idx].offset[0] = b2157+ offsets[idx].offset[1] = util.Max32(b, e)2158+ }2159 }2160+ if t.hscroll {2161+ if t.keepRight && pos == nil {2162+ trimmed, diff := t.trimLeft(line, maxWidth-ellipsisWidth)2163+ transformOffsets(diff, false)2164+ line = append(ellipsis, trimmed...)2165+ } else if !t.overflow(line[:maxe], maxWidth-ellipsisWidth) {2166+ // Stri..2167+ line, _ = t.trimRight(line, maxWidth-ellipsisWidth)2168+ line = append(line, ellipsis...)2169+ } else {2170+ // Stri..2171+ rightTrim := false2172+ if t.overflow(line[maxe:], ellipsisWidth) {2173+ line = append(line[:maxe], ellipsis...)2174+ rightTrim = true2175+ }2176+ // ..ri..2177+ var diff int322178+ line, diff = t.trimLeft(line, maxWidth-ellipsisWidth)2179+2180+ // Transform offsets2181+ transformOffsets(diff, rightTrim)2182+ line = append(ellipsis, line...)2183+ }2184+ } else {2185+ line, _ = t.trimRight(line, maxWidth-ellipsisWidth)2186+ line = append(line, ellipsis...)2187+2188+ for idx, offset := range offsets {2189+ offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))2190+ offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))2191+ }2192+ }2193+ displayWidth = t.displayWidthWithLimit(line, 0, displayWidth)2194 }2073- displayWidth = t.displayWidthWithLimit(text, 0, displayWidth)2195+2196+ t.printColoredString(t.window, line, offsets, colBase)2197+ if postTask != nil {2198+ postTask(actualLineNum, displayWidth)2199+ } else {2200+ t.markOtherLine(actualLineNum)2201+ }2202+ lineNum += 12203 }2204 2076- t.printColoredString(t.window, text, offsets, colBase)2077- return displayWidth2205+ return finalLineNum2206 }2207 2208 func (t *Terminal) printColoredString(window tui.Window, text []rune, offsets []colorOffset, colBase tui.ColorPair) {2209 var index int3289 unmodified lines2299 return2300 }2301 2302 effectiveHeight := height - headerLines2175- barLength, barStart := getScrollbar(len(body), effectiveHeight, util.Min(len(body)-effectiveHeight, t.previewer.offset-headerLines))2303+ barLength, barStart := getScrollbar(1, len(body), effectiveHeight, util.Min(len(body)-effectiveHeight, t.previewer.offset-headerLines))2304 t.renderPreviewScrollbar(headerLines, barLength, barStart)2305 }2306 2307 func (t *Terminal) makeImageBorder(width int, top bool) string {1841 unmodified lines4149 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())4150 if pbarDragging {4151 effectiveHeight := t.pwindow.Height() - headerLines4152 numLines := len(t.previewer.lines) - headerLines4025- barLength, _ := getScrollbar(numLines, effectiveHeight, util.Min(numLines-effectiveHeight, t.previewer.offset-headerLines))4153+ barLength, _ := getScrollbar(1, numLines, effectiveHeight, util.Min(numLines-effectiveHeight, t.previewer.offset-headerLines))4154 if barLength > 0 {4155 y := my - t.pwindow.Top() - headerLines - barLength/24156 y = util.Constrain(y, 0, effectiveHeight-barLength)4157 // offset = (total - maxItems) * barStart / (maxItems - barLength)37 unmodified lines4195 if newBarStart := util.Constrain(my-min-barLength/2, 0, maxItems-barLength); newBarStart != barStart {4196 total := t.merger.Length()4197 prevOffset := t.offset4198 // barStart = (maxItems - barLength) * t.offset / (total - maxItems)4199+ perLine := t.avgNumLines()4071- t.offset = int(math.Ceil(float64(newBarStart) * float64(total-maxItems) / float64(maxItems-barLength)))4200+ t.offset = int(math.Ceil(float64(newBarStart) * float64(total*perLine-maxItems) / float64(maxItems*perLine-barLength)))4201 t.cy = t.offset + t.cy - prevOffset4202 req(reqList)4203 }4204 }4205 break4206 }4207 4208+ // There can be empty lines after the list in multi-line mode4209+ prevLine := t.prevLines[my]4210+ if prevLine.empty {4211+ break4212+ }4213+4214 // Double-click on an item4215+ cy := prevLine.cy4216 if me.Double && mx < t.window.Width()-1 {4217 // Double-click4218 if my >= min {4083- if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {4219+ if t.vset(cy) && t.cy < t.merger.Length() {4220 return doActions(actionsFor(tui.DoubleClick))4221 }4222 }4223 break4 unmodified lines4228 if my == t.promptLine() && mxCons >= 0 {4229 // Prompt4230 t.cx = mxCons + t.xoffset4231 } else if my >= min {4096- t.vset(t.offset + my - min)4232+ t.vset(cy)4233 req(reqList)4234 evt := tui.RightClick4235 if me.Mod {4236 evt = tui.SRightClick210 unmodified lines4447 4448 func (t *Terminal) constrain() {4449 // count of items to display allowed by filtering4450 count := t.merger.Length()4315- // count of lines can be displayed4316- height := t.maxItems()4451+ maxItems := t.maxItems()4452 4318- t.cy = util.Constrain(t.cy, 0, util.Max(0, count-1))4453+ // May need to try again after adjusting the offset4454+ for tries := 0; tries < maxItems; tries++ {4455+ height := maxItems4456+ // How many items can be fit on screen including the current item?4457+ if t.multiLine && t.merger.Length() > 0 {4458+ actualHeight := 04459+ linesSum := 04460 4320- minOffset := util.Max(t.cy-height+1, 0)4321- maxOffset := util.Max(util.Min(count-height, t.cy), 0)4322- t.offset = util.Constrain(t.offset, minOffset, maxOffset)4323- if t.scrollOff == 0 {4324- return4325- }4461+ add := func(i int) bool {4462+ lines := t.merger.Get(i).item.text.NumLines(height - linesSum)4463+ linesSum += lines4464+ if linesSum >= height {4465+ if actualHeight == 0 {4466+ actualHeight = 14467+ }4468+ return false4469+ }4470+ actualHeight++4471+ return true4472+ }4473 4327- scrollOff := util.Min(height/2, t.scrollOff)4328- for {4474+ for i := t.offset; i < t.merger.Length(); i++ {4475+ if !add(i) {4476+ break4477+ }4478+ }4479+4480+ // We can possibly fit more items "before" the offset on screen4481+ if linesSum < height {4482+ for i := t.offset - 1; i >= 0; i-- {4483+ if !add(i) {4484+ break4485+ }4486+ }4487+ }4488+4489+ height = actualHeight4490+ }4491+4492+ t.cy = util.Constrain(t.cy, 0, util.Max(0, count-1))4493+ minOffset := util.Max(t.cy-height+1, 0)4494+ maxOffset := util.Max(util.Min(count-height, t.cy), 0)4495 prevOffset := t.offset4496+ t.offset = util.Constrain(t.offset, minOffset, maxOffset)4497+ if t.scrollOff > 0 {4498+ scrollOff := util.Min(height/2, t.scrollOff)4499+ for {4500+ prevOffset := t.offset4330- if t.cy-t.offset < scrollOff {4331- t.offset = util.Max(minOffset, t.offset-1)4501+ if t.cy-t.offset < scrollOff {4502+ t.offset = util.Max(minOffset, t.offset-1)4503+ }4504+ if t.cy-t.offset >= height-scrollOff {4505+ t.offset = util.Min(maxOffset, t.offset+1)4506+ }4507+ if t.offset == prevOffset {4508+ break4509+ }4510+ }4511 }4333- if t.cy-t.offset >= height-scrollOff {4334- t.offset = util.Min(maxOffset, t.offset+1)4335- }4512 if t.offset == prevOffset {4513 break4514 }4515 }79 unmodified linesmodified:test/test_go.rb+2 added-2 removed
2126 unmodified lines2127 end2128 end2129 2130 def test_keep_right2131- tmux.send_keys "seq 10000 | #{FZF} --read0 --keep-right", :Enter2131+ tmux.send_keys "seq 10000 | #{FZF} --read0 --keep-right --no-multi-line", :Enter2132 tmux.until { |lines| assert lines.any_include?('9999␊10000') }2133 end2134 2135 def test_backward_eof1261 unmodified lines3397 end3398 3399 def test_ctrl_r_multiline3400 # NOTE: Current bash implementation shows an extra new line if there's3401- # only enty in the history3401+ # only entry in the history3402 tmux.send_keys ':', :Enter3403 tmux.send_keys 'echo "foo', :Enter, 'bar"', :Enter3404 tmux.until { |lines| assert_equal %w[foo bar], lines[-2..] }3405 tmux.prepare396 unmodified lines