Add --wrap option and 'toggle-wrap' action (#3887)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* `--wrap` * `--wrap-sign` * `toggle-wrap` Close #3619 Close #2236 Close #577 Close #461
authored by Junegunn Choi · committed by GitHub · parent 724f8a1 · Browse files · 7 files·+354 added−117 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+354 added−117 removed
- other:+354 added−117 removed
Provenance
- Commit
70bf8bc- Tree
02d7481- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
724f8a1
7 files changed+354 added−117 removed
modified:man/man1/fzf.1+8 added
196 unmodified lines197 .TP198 .B "\-\-cycle"199 Enable cyclic scroll200 .TP201+.B "\-\-wrap"202+Enable line wrap203+.TP204+.BI "\-\-wrap\-sign" "=INDICATOR"205+Indicator for wrapped lines. The default is '↳ ' or '> ' depending on206+\fB\-\-no\-unicode\fR.207+.TP208 .B "\-\-no\-multi\-line"209 Disable multi-line display of items when using \fB\-\-read0\fR210 .TP211 .B "\-\-keep\-right"1284 unmodified lines1496 \fBtoggle\-search\fR (toggle search functionality)1497 \fBtoggle\-sort\fR1498 \fBtoggle\-track\fR (toggle global tracking option (\fB\-\-track\fR))1499 \fBtoggle\-track\-current\fR (toggle tracking of the current item)1500+ \fBtoggle\-wrap\fR1501 \fBtoggle+up\fR \fIbtab (shift\-tab)\fR1502 \fBtrack\-current\fR (track the current item; automatically disabled if focus changes)1503 \fBtransform(...)\fR (transform states using the output of an external command)1504 \fBtransform\-border\-label(...)\fR (transform border label using an external command)183 unmodified linesmodified:src/util/chars_test.go+38 added-1 removed
1 package util2 3-import "testing"3+import (4+ "fmt"5+ "testing"6+)7 8 func TestToCharsAscii(t *testing.T) {9 chars := ToChars([]byte("foobar"))10 if !chars.inBytes || chars.ToString() != "foobar" || !chars.inBytes {35 unmodified lines46 check("h o", 5)47 check(" h o ", 5)48 check(" ", 0)49 }50+51+func TestCharsLines(t *testing.T) {52+ chars := ToChars([]byte("abcdef\n가나다\n\tdef"))53+ check := func(multiLine bool, maxLines int, wrapCols int, wrapSignWidth int, tabstop int, expectedNumLines int, expectedOverflow bool) {54+ lines, overflow := chars.Lines(multiLine, maxLines, wrapCols, wrapSignWidth, tabstop)55+ fmt.Println(lines, overflow)56+ if len(lines) != expectedNumLines || overflow != expectedOverflow {57+ t.Errorf("Invalid result: %d %v (expected %d %v)", len(lines), overflow, expectedNumLines, expectedOverflow)58+ }59+ }60+61+ // No wrap62+ check(true, 1, 0, 0, 8, 1, true)63+ check(true, 2, 0, 0, 8, 2, true)64+ check(true, 3, 0, 0, 8, 3, false)65+66+ // Wrap (2)67+ check(true, 4, 2, 0, 8, 4, true)68+ check(true, 5, 2, 0, 8, 5, true)69+ check(true, 6, 2, 0, 8, 6, true)70+ check(true, 7, 2, 0, 8, 7, true)71+ check(true, 8, 2, 0, 8, 8, true)72+ check(true, 9, 2, 0, 8, 9, false)73+ check(true, 9, 2, 0, 1, 8, false) // Smaller tab size74+75+ // With wrap sign (3 + 1)76+ check(true, 100, 3, 1, 1, 8, false)77+78+ // With wrap sign (3 + 2)79+ check(true, 100, 3, 2, 1, 12, false)80+81+ // With wrap sign (3 + 2) and no multi-line82+ check(false, 100, 3, 2, 1, 13, false)83+}modified:src/util/chars.go+82 added
224 unmodified lines225 } else {226 chars.slice = append([]byte(prefix), chars.slice...)227 }228 }229+230+func (chars *Chars) Lines(multiLine bool, maxLines int, wrapCols int, wrapSignWidth int, tabstop int) ([][]rune, bool) {231+ text := make([]rune, chars.Length())232+ copy(text, chars.ToRunes())233+234+ lines := [][]rune{}235+ overflow := false236+ if !multiLine {237+ lines = append(lines, text)238+ } else {239+ from := 0240+ for off := 0; off < len(text); off++ {241+ if text[off] == '\n' {242+ lines = append(lines, text[from:off+1]) // Include '\n'243+ from = off + 1244+ if len(lines) >= maxLines {245+ break246+ }247+ }248+ }249+250+ var lastLine []rune251+ if from < len(text) {252+ lastLine = text[from:]253+ }254+255+ overflow = false256+ if len(lines) >= maxLines {257+ overflow = true258+ } else {259+ lines = append(lines, lastLine)260+ }261+ }262+263+ // If wrapping is disabled, we're done264+ if wrapCols == 0 {265+ return lines, overflow266+ }267+268+ wrapped := [][]rune{}269+ for _, line := range lines {270+ // Remove trailing '\n' and remember if it was there271+ newline := len(line) > 0 && line[len(line)-1] == '\n'272+ if newline {273+ line = line[:len(line)-1]274+ }275+276+ for {277+ cols := wrapCols278+ if len(wrapped) > 0 {279+ cols -= wrapSignWidth280+ }281+ _, overflowIdx := RunesWidth(line, 0, tabstop, cols)282+ if overflowIdx >= 0 {283+ // Might be a wide character284+ if overflowIdx == 0 {285+ overflowIdx = 1286+ }287+ if len(wrapped) >= maxLines {288+ return wrapped, true289+ }290+ wrapped = append(wrapped, line[:overflowIdx])291+ line = line[overflowIdx:]292+ continue293+ }294+295+ // Restore trailing '\n'296+ if newline {297+ line = append(line, '\n')298+ }299+300+ if len(wrapped) >= maxLines {301+ return wrapped, true302+ }303+304+ wrapped = append(wrapped, line)305+ break306+ }307+ }308+309+ return wrapped, false310+}modified:src/actiontype_string.go+65 added-64 removed
56 unmodified lines57 _ = x[actToggleOut-46]58 _ = x[actToggleTrack-47]59 _ = x[actToggleTrackCurrent-48]60 _ = x[actToggleHeader-49]61- _ = x[actTrackCurrent-50]62- _ = x[actUntrackCurrent-51]63- _ = x[actDown-52]64- _ = x[actUp-53]65- _ = x[actPageUp-54]66- _ = x[actPageDown-55]67- _ = x[actPosition-56]68- _ = x[actHalfPageUp-57]69- _ = x[actHalfPageDown-58]70- _ = x[actOffsetUp-59]71- _ = x[actOffsetDown-60]72- _ = x[actOffsetMiddle-61]73- _ = x[actJump-62]74- _ = x[actJumpAccept-63]75- _ = x[actPrintQuery-64]76- _ = x[actRefreshPreview-65]77- _ = x[actReplaceQuery-66]78- _ = x[actToggleSort-67]79- _ = x[actShowPreview-68]80- _ = x[actHidePreview-69]81- _ = x[actTogglePreview-70]82- _ = x[actTogglePreviewWrap-71]83- _ = x[actTransform-72]84- _ = x[actTransformBorderLabel-73]85- _ = x[actTransformHeader-74]86- _ = x[actTransformPreviewLabel-75]87- _ = x[actTransformPrompt-76]88- _ = x[actTransformQuery-77]89- _ = x[actPreview-78]90- _ = x[actChangePreview-79]91- _ = x[actChangePreviewWindow-80]92- _ = x[actPreviewTop-81]93- _ = x[actPreviewBottom-82]94- _ = x[actPreviewUp-83]95- _ = x[actPreviewDown-84]96- _ = x[actPreviewPageUp-85]97- _ = x[actPreviewPageDown-86]98- _ = x[actPreviewHalfPageUp-87]99- _ = x[actPreviewHalfPageDown-88]100- _ = x[actPrevHistory-89]101- _ = x[actPrevSelected-90]102- _ = x[actPrint-91]103- _ = x[actPut-92]104- _ = x[actNextHistory-93]105- _ = x[actNextSelected-94]106- _ = x[actExecute-95]107- _ = x[actExecuteSilent-96]108- _ = x[actExecuteMulti-97]109- _ = x[actSigStop-98]110- _ = x[actFirst-99]111- _ = x[actLast-100]112- _ = x[actReload-101]113- _ = x[actReloadSync-102]114- _ = x[actDisableSearch-103]115- _ = x[actEnableSearch-104]116- _ = x[actSelect-105]117- _ = x[actDeselect-106]118- _ = x[actUnbind-107]119- _ = x[actRebind-108]120- _ = x[actBecome-109]121- _ = x[actShowHeader-110]122- _ = x[actHideHeader-111]61+ _ = x[actToggleWrap-50]62+ _ = x[actTrackCurrent-51]63+ _ = x[actUntrackCurrent-52]64+ _ = x[actDown-53]65+ _ = x[actUp-54]66+ _ = x[actPageUp-55]67+ _ = x[actPageDown-56]68+ _ = x[actPosition-57]69+ _ = x[actHalfPageUp-58]70+ _ = x[actHalfPageDown-59]71+ _ = x[actOffsetUp-60]72+ _ = x[actOffsetDown-61]73+ _ = x[actOffsetMiddle-62]74+ _ = x[actJump-63]75+ _ = x[actJumpAccept-64]76+ _ = x[actPrintQuery-65]77+ _ = x[actRefreshPreview-66]78+ _ = x[actReplaceQuery-67]79+ _ = x[actToggleSort-68]80+ _ = x[actShowPreview-69]81+ _ = x[actHidePreview-70]82+ _ = x[actTogglePreview-71]83+ _ = x[actTogglePreviewWrap-72]84+ _ = x[actTransform-73]85+ _ = x[actTransformBorderLabel-74]86+ _ = x[actTransformHeader-75]87+ _ = x[actTransformPreviewLabel-76]88+ _ = x[actTransformPrompt-77]89+ _ = x[actTransformQuery-78]90+ _ = x[actPreview-79]91+ _ = x[actChangePreview-80]92+ _ = x[actChangePreviewWindow-81]93+ _ = x[actPreviewTop-82]94+ _ = x[actPreviewBottom-83]95+ _ = x[actPreviewUp-84]96+ _ = x[actPreviewDown-85]97+ _ = x[actPreviewPageUp-86]98+ _ = x[actPreviewPageDown-87]99+ _ = x[actPreviewHalfPageUp-88]100+ _ = x[actPreviewHalfPageDown-89]101+ _ = x[actPrevHistory-90]102+ _ = x[actPrevSelected-91]103+ _ = x[actPrint-92]104+ _ = x[actPut-93]105+ _ = x[actNextHistory-94]106+ _ = x[actNextSelected-95]107+ _ = x[actExecute-96]108+ _ = x[actExecuteSilent-97]109+ _ = x[actExecuteMulti-98]110+ _ = x[actSigStop-99]111+ _ = x[actFirst-100]112+ _ = x[actLast-101]113+ _ = x[actReload-102]114+ _ = x[actReloadSync-103]115+ _ = x[actDisableSearch-104]116+ _ = x[actEnableSearch-105]117+ _ = x[actSelect-106]118+ _ = x[actDeselect-107]119+ _ = x[actUnbind-108]120+ _ = x[actRebind-109]121+ _ = x[actBecome-110]122+ _ = x[actShowHeader-111]123+ _ = x[actHideHeader-112]124 }125 125-const _actionType_name = "actIgnoreactStartactClickactInvalidactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactCancelactChangeBorderLabelactChangeHeaderactChangeMultiactChangePreviewLabelactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactKillLineactKillWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactTrackCurrentactUntrackCurrentactDownactUpactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactOffsetMiddleactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTransformactTransformBorderLabelactTransformHeaderactTransformPreviewLabelactTransformPromptactTransformQueryactPreviewactChangePreviewactChangePreviewWindowactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactBecomeactShowHeaderactHideHeader"126+const _actionType_name = "actIgnoreactStartactClickactInvalidactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactCancelactChangeBorderLabelactChangeHeaderactChangeMultiactChangePreviewLabelactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactKillLineactKillWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactToggleWrapactTrackCurrentactUntrackCurrentactDownactUpactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactOffsetMiddleactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTransformactTransformBorderLabelactTransformHeaderactTransformPreviewLabelactTransformPromptactTransformQueryactPreviewactChangePreviewactChangePreviewWindowactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactBecomeactShowHeaderactHideHeader"127 127-var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 42, 50, 68, 76, 85, 102, 123, 138, 159, 183, 198, 207, 227, 242, 256, 277, 292, 306, 320, 333, 350, 358, 371, 387, 399, 407, 421, 435, 446, 457, 475, 492, 499, 518, 530, 544, 553, 568, 580, 593, 604, 615, 627, 641, 662, 677, 692, 709, 716, 721, 730, 741, 752, 765, 780, 791, 804, 819, 826, 839, 852, 869, 884, 897, 911, 925, 941, 961, 973, 996, 1014, 1038, 1056, 1073, 1083, 1099, 1121, 1134, 1150, 1162, 1176, 1192, 1210, 1230, 1252, 1266, 1281, 1289, 1295, 1309, 1324, 1334, 1350, 1365, 1375, 1383, 1390, 1399, 1412, 1428, 1443, 1452, 1463, 1472, 1481, 1490, 1503, 1516}128+var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 42, 50, 68, 76, 85, 102, 123, 138, 159, 183, 198, 207, 227, 242, 256, 277, 292, 306, 320, 333, 350, 358, 371, 387, 399, 407, 421, 435, 446, 457, 475, 492, 499, 518, 530, 544, 553, 568, 580, 593, 604, 615, 627, 641, 662, 677, 690, 705, 722, 729, 734, 743, 754, 765, 778, 793, 804, 817, 832, 839, 852, 865, 882, 897, 910, 924, 938, 954, 974, 986, 1009, 1027, 1051, 1069, 1086, 1096, 1112, 1134, 1147, 1163, 1175, 1189, 1205, 1223, 1243, 1265, 1279, 1294, 1302, 1308, 1322, 1337, 1347, 1363, 1378, 1388, 1396, 1403, 1412, 1425, 1441, 1456, 1465, 1476, 1485, 1494, 1503, 1516, 1529}129 130 func (i actionType) String() string {131 if i < 0 || i >= actionType(len(_actionType_index)-1) {132 return "actionType(" + strconv.FormatInt(int64(i), 10) + ")"3 unmodified linesmodified:src/options.go+19 added
51 unmodified lines52 -m, --multi[=MAX] Enable multi-select with tab/shift-tab53 --no-mouse Disable mouse54 --bind=KEYBINDS Custom key bindings. Refer to the man page.55 --cycle Enable cyclic scroll56+ --wrap Enable line wrap57+ --wrap-sign=STR Indicator for wrapped lines58 --no-multi-line Disable multi-line display of items when using --read059 --keep-right Keep the right end of the line visible on overflow60 --scroll-off=LINES Number of screen lines to keep above or below when61 scrolling to the top or to the bottom (default: 0)374 unmodified lines436 Height heightSpec437 MinHeight int438 Layout layoutType439 Cycle bool440+ Wrap bool441+ WrapSign *string442 MultiLine bool443 CursorLine bool444 KeepRight bool445 Hscroll bool100 unmodified lines546 Bold: true,547 MinHeight: 10,548 Layout: layoutDefault,549 Cycle: false,550+ Wrap: false,551 MultiLine: true,552 KeepRight: false,553 Hscroll: true,554 HscrollOff: 10,815 unmodified lines1370 case "toggle-track-current":1371 appendAction(actToggleTrackCurrent)1372 case "toggle-header":1373 appendAction(actToggleHeader)1374+ case "toggle-wrap":1375+ appendAction(actToggleWrap)1376 case "show-header":1377 appendAction(actShowHeader)1378 case "hide-header":1379 appendAction(actHideHeader)789 unmodified lines2169 case "--no-highlight-line":2170 opts.CursorLine = false2171 case "--no-cycle":2172 opts.Cycle = false2173+ case "--wrap":2174+ opts.Wrap = true2175+ case "--no-wrap":2176+ opts.Wrap = false2177+ case "--wrap-sign":2178+ str, err := nextString(allArgs, &i, "wrap sign required")2179+ if err != nil {2180+ return err2181+ }2182+ opts.WrapSign = &str2183 case "--multi-line":2184 opts.MultiLine = true2185 case "--no-multi-line":2186 opts.MultiLine = false342 unmodified lines2529 } else if match, value := optString(arg, "--preview-label-pos="); match {2530 if err := parseLabelPosition(&opts.PreviewLabel, value); err != nil {2531 return err2532 }2533+ } else if match, value := optString(arg, "--wrap-sign="); match {2534+ opts.WrapSign = &value2535 } else if match, value := optString(arg, "--prompt="); match {2536 opts.Prompt = value2537 } else if match, value := optString(arg, "--pointer="); match {2538 str := firstLine(value)443 unmodified linesmodified:src/terminal.go+132 added-52 removed
153 unmodified lines154 }155 156 type itemLine struct {157 firstLine int158+ numLines int159 cy int160 current bool161 selected bool162 label string52 unmodified lines215 initDelay time.Duration216 infoCommand string217 infoStyle infoStyle218 infoPrefix string219+ wrap bool220+ wrapSign string221+ wrapSignWidth int222 separator labelPrinter223 separatorLen int224 spinner []string225 promptString string223 unmodified lines449 actToggleOut450 actToggleTrack451 actToggleTrackCurrent452 actToggleHeader453+ actToggleWrap454 actTrackCurrent455 actUntrackCurrent456 actDown457 actUp333 unmodified lines791 yanked: []rune{},792 input: input,793 multi: opts.Multi,794 multiLine: opts.ReadZero && opts.MultiLine,795+ wrap: opts.Wrap,796 sort: opts.Sort > 0,797 toggleSort: opts.ToggleSort,798 track: opts.Track,799 delimiter: opts.Delimiter,81 unmodified lines881 }882 t.separator, t.separatorLen = t.ansiLabelPrinter(bar, &tui.ColSeparator, true)883 }884 if t.unicode {885+ t.wrapSign = "↳ "886 t.borderWidth = uniseg.StringWidth("│")887+ } else {888+ t.wrapSign = "> "889 }890+ if opts.WrapSign != nil {891+ t.wrapSign = *opts.WrapSign892+ }893+ t.wrapSign, t.wrapSignWidth = t.processTabs([]rune(t.wrapSign), 0)894 if opts.Scrollbar == nil {895 if t.unicode && t.borderWidth == 1 {896 t.scrollbar = "│"897 } else {181 unmodified lines1079 }1080 }1081 output := func() {1082 line := t.promptLine()1083+ wrap := t.wrap1084+ t.wrap = false1085 t.printHighlighted(1086 Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, line, line, true, nil, nil)1087+ t.wrap = wrap1088 }1089 _, promptLen := t.processTabs([]rune(trimmed), 0)1090 1091 return output, promptLen26 unmodified lines1118 }1119 return barLength, barStart1120 }1121 1122+func (t *Terminal) wrapCols() int {1123+ if !t.wrap {1124+ return 0 // No wrap1125+ }1126+ return util.Max(t.window.Width()-(t.pointerLen+t.markerLen+1), 1)1127+}1128+1129+func (t *Terminal) numItemLines(item *Item, atMost int) (int, bool) {1130+ if !t.wrap && !t.multiLine {1131+ return 1, false1132+ }1133+ if !t.wrap && t.multiLine {1134+ return item.text.NumLines(atMost)1135+ }1136+ lines, overflow := item.text.Lines(t.multiLine, atMost, t.wrapCols(), t.wrapSignWidth, t.tabstop)1137+ return len(lines), overflow1138+}1139+1140+func (t *Terminal) itemLines(item *Item, atMost int) ([][]rune, bool) {1141+ if !t.wrap && !t.multiLine {1142+ text := make([]rune, item.text.Length())1143+ copy(text, item.text.ToRunes())1144+ return [][]rune{text}, false1145+ }1146+ return item.text.Lines(t.multiLine, atMost, t.wrapCols(), t.wrapSignWidth, t.tabstop)1147+}1148+1149 // Estimate the average number of lines per item. Instead of going through all1150 // items, we only check a few items around the current cursor position.1151 func (t *Terminal) avgNumLines() int {1109- if !t.multiLine {1152+ if !t.wrap && !t.multiLine {1153 return 11154 }1155 1156 maxItems := t.maxItems()1 unmodified line1158 count := 01159 total := t.merger.Length()1160 offset := util.Max(0, util.Min(t.offset, total-maxItems-1))1161 for idx := 0; idx < maxItems && idx+offset < total; idx++ {1119- item := t.merger.Get(idx + offset)1120- lines, _ := item.item.text.NumLines(maxItems)1162+ result := t.merger.Get(idx + offset)1163+ lines, _ := t.numItemLines(result.item, maxItems)1164 numLines += lines1165 count++1166 }1167 if count == 0 {838 unmodified lines2006 switch t.layout {2007 case layoutDefault, layoutReverseList:2008 needReverse = true2009 }2010+ // Wrapping is not supported for header2011+ wrap := t.wrap2012+ t.wrap = false2013 for idx, lineStr := range append(append([]string{}, t.header0...), t.header...) {2014 line := idx2015 if needReverse && idx < len(t.header0) {2016 line = len(t.header0) - idx - 116 unmodified lines2033 t.printHighlighted(Result{item: item},2034 tui.ColHeader, tui.ColHeader, false, false, line, line, true,2035 func(markerClass) { t.window.Print(" ") }, nil)2036 }2037+ t.wrap = wrap2038 }2039 2040 func (t *Terminal) printList() {2041 t.constrain()19 unmodified lines2061 t.markEmptyLine(line)2062 // If the screen is not filled with the list in non-multi-line mode,2063 // scrollbar is not visible at all. But in multi-line mode, we may need2064 // to redraw the scrollbar character at the end.2018- if t.multiLine {2065+ if t.multiLine || t.wrap {2066 t.prevLines[line].hasBar = t.printBar(line, true, barRange)2067 }2068 }2069 }24 unmodified lines2094 label = t.pointer2095 }2096 2097 // Avoid unnecessary redraw2098+ numLines, _ := t.numItemLines(item, maxLine-line+1)2051- newLine := itemLine{firstLine: line, cy: index + t.offset, current: current, selected: selected, label: label,2099+ newLine := itemLine{firstLine: line, numLines: numLines, cy: index + t.offset, current: current, selected: selected, label: label,2100 result: result, queryLen: len(t.input), width: 0, hasBar: line >= barRange[0] && line < barRange[1]}2101 prevLine := t.prevLines[line]2102 forceRedraw := prevLine.other || prevLine.firstLine != newLine.firstLine2103 printBar := func(lineNum int, forceRedraw bool) bool {2104 return t.printBar(lineNum, forceRedraw, barRange)2105 }2106 2107 if !forceRedraw &&2108+ prevLine.numLines == newLine.numLines &&2109 prevLine.current == newLine.current &&2110 prevLine.selected == newLine.selected &&2111 prevLine.label == newLine.label &&2112 prevLine.queryLen == newLine.queryLen &&2113 prevLine.result == newLine.result {2114 t.prevLines[line].hasBar = printBar(line, false)2066- if !t.multiLine {2115+ if !t.multiLine && !t.wrap {2116 return line2117 }2069- lines, _ := item.text.NumLines(maxLine - line + 1)2070- return line + lines - 12118+ return line + numLines - 12119 }2120 2121 maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)2074- postTask := func(lineNum int, width int) {2122+ postTask := func(lineNum int, width int, wrapped bool) {2123 if (current || selected) && t.highlightLine {2124 color := tui.ColSelected2125 if current {2126 color = tui.ColCurrent2127 }2128 fillSpaces := maxWidth - width2129+ if wrapped {2130+ fillSpaces -= t.wrapSignWidth2131+ }2132 if fillSpaces > 0 {2133 t.window.CPrint(color, strings.Repeat(" ", fillSpaces))2134 }2135 newLine.width = maxWidth2136 } else {2137 fillSpaces := t.prevLines[lineNum].width - width2138+ if wrapped {2139+ fillSpaces -= t.wrapSignWidth2140+ }2141 if fillSpaces > 0 {2142 t.window.Print(strings.Repeat(" ", fillSpaces))2143 }2144 newLine.width = width2145+ if wrapped {2146+ newLine.width += t.wrapSignWidth2147+ }2148 }2149 // When width is 0, line is completely cleared. We need to redraw scrollbar2150 newLine.hasBar = printBar(lineNum, forceRedraw || width == 0)2151 t.prevLines[lineNum] = newLine89 unmodified lines2241 func (t *Terminal) overflow(runes []rune, max int) bool {2242 return t.displayWidthWithLimit(runes, 0, max) > max2243 }2244 2188-func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass), postTask func(int, int)) int {2245+func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass), postTask func(int, int, bool)) int {2246 var displayWidth int2247 item := result.item2248 matchOffsets := []Offset{}2249 var pos *[]int10 unmodified lines2260 sort.Sort(ByOrder(charOffsets))2261 }2262 allOffsets := result.colorOffsets(charOffsets, t.theme, colBase, colMatch, current)2263 2207- from := 02208- text := make([]rune, item.text.Length())2209- copy(text, item.text.ToRunes())2264+ maxLines := 12265+ if t.multiLine || t.wrap {2266+ maxLines = maxLineNum - lineNum + 12267+ }2268+ lines, overflow := t.itemLines(item, maxLines)2269+ numItemLines := len(lines)2270 2271 finalLineNum := lineNum2212- numItemLines := 12213- cutoff := 02214- overflow := false2272 topCutoff := false2216- if t.multiLine {2217- maxLines := maxLineNum - lineNum + 12218- numItemLines, overflow = item.text.NumLines(maxLines)2273+ wrapped := false2274+ if t.multiLine || t.wrap {2275 // Cut off the upper lines in the 'default' layout2276 if t.layout == layoutDefault && !current && maxLines == numItemLines && overflow {2221- actualLines, _ := item.text.NumLines(math.MaxInt32)2222- cutoff = actualLines - maxLines2277+ lines, _ = t.itemLines(item, math.MaxInt)2278+2279+ // To see if the first visible line is wrapped, we need to check the last cut-off line2280+ prevLine := lines[len(lines)-maxLines-1]2281+ if len(prevLine) == 0 || prevLine[len(prevLine)-1] != '\n' {2282+ wrapped = true2283+ }2284+2285+ lines = lines[len(lines)-maxLines:]2286 topCutoff = true2287 }2288 }2289+ from := 02226- for lineOffset := 0; from <= len(text) && (lineNum <= maxLineNum || maxLineNum == 0); lineOffset++ {2290+ for lineOffset := 0; lineOffset < len(lines) && (lineNum <= maxLineNum || maxLineNum == 0); lineOffset++ {2291+ line := lines[lineOffset]2292 finalLineNum = lineNum2293+ offsets := []colorOffset{}2294+ for _, offset := range allOffsets {2295+ if offset.offset[0] >= int32(from+len(line)) {2296+ allOffsets = allOffsets[len(offsets):]2297+ break2298+ }2299 2229- line := text[from:]2230- if t.multiLine {2231- for idx, r := range text[from:] {2232- if r == '\n' {2233- line = line[:idx]2234- break2235- }2300+ if offset.offset[0] < int32(from) {2301+ continue2302 }2237- }2303 2239- offsets := []colorOffset{}2240- for _, offset := range allOffsets {2241- if offset.offset[0] >= int32(from) && offset.offset[1] <= int32(from+len(line)) {2304+ if offset.offset[1] < int32(from+len(line)) {2305 offset.offset[0] -= int32(from)2306 offset.offset[1] -= int32(from)2307 offsets = append(offsets, offset)2308 } else {2246- allOffsets = allOffsets[len(offsets):]2309+ dupe := offset2310+ dupe.offset[0] = int32(from + len(line))2311+2312+ offset.offset[0] -= int32(from)2313+ offset.offset[1] = int32(from + len(line))2314+ offsets = append(offsets, offset)2315+2316+ allOffsets = append([]colorOffset{dupe}, allOffsets[len(offsets):]...)2317 break2318 }2319 }2320+ from += len(line)2321 2251- from += len(line) + 12252-2253- if cutoff > 0 {2254- cutoff--2255- lineOffset--2256- continue2257- }2258-2322 var maxe int2323 for _, offset := range offsets {2324 if offset.match {2325 maxe = util.Max(maxe, int(offset.offset[1]))37 unmodified lines2363 preTask(marker)2364 }2365 2366 maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)2304- ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)2305- maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(line))2367+ wasWrapped := false2368+ if wrapped {2369+ maxWidth -= t.wrapSignWidth2370+ t.window.CPrint(colBase.WithAttr(tui.Dim), t.wrapSign)2371+ wrapped = false2372+ wasWrapped = true2373+ }2374+2375+ if len(line) > 0 && line[len(line)-1] == '\n' {2376+ line = line[:len(line)-1]2377+ } else {2378+ wrapped = true2379+ }2380+2381 displayWidth = t.displayWidthWithLimit(line, 0, maxWidth)2307- if displayWidth > maxWidth {2382+ if !t.wrap && displayWidth > maxWidth {2383+ ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)2384+ maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(line))2385 transformOffsets := func(diff int32, rightTrim bool) {2386 for idx, offset := range offsets {2387 b, e := offset.offset[0], offset.offset[1]2388 el := int32(len(ellipsis))44 unmodified lines2433 }2434 2435 t.printColoredString(t.window, line, offsets, colBase)2436 if postTask != nil {2360- postTask(actualLineNum, displayWidth)2437+ postTask(actualLineNum, displayWidth, wasWrapped)2438 } else {2439 t.markOtherLine(actualLineNum)2440 }2441 lineNum += 12003 unmodified lines4445 req(reqList, reqInfo, reqPrompt, reqHeader)4446 case actToggleHeader:4447 t.headerVisible = !t.headerVisible4448 req(reqList, reqInfo, reqPrompt, reqHeader)4449+ case actToggleWrap:4450+ t.wrap = !t.wrap4451+ req(reqList, reqHeader)4452 case actTrackCurrent:4453 if t.track == trackDisabled {4454 t.track = trackCurrent4455 }374 unmodified lines4830 t.offset = util.Constrain(t.offset, 0, count)4831 for tries := 0; tries < maxLines; tries++ {4832 numItems := maxLines4833 // How many items can be fit on screen including the current item?4754- if t.multiLine && t.merger.Length() > 0 {4834+ if (t.multiLine || t.wrap) && t.merger.Length() > 0 {4835 numItemsFound := 04836 linesSum := 04837 4838 add := func(i int) bool {4759- lines, _ := t.merger.Get(i).item.text.NumLines(numItems - linesSum)4839+ lines, _ := t.numItemLines(t.merger.Get(i).item, numItems-linesSum)4840 linesSum += lines4841 if linesSum >= numItems {4842 if numItemsFound == 0 {4843 numItemsFound = 135 unmodified lines4879 for {4880 prevOffset := newOffset4881 numItems := t.merger.Length()4882 itemLines := 14803- if t.multiLine && t.cy < numItems {4804- itemLines, _ = t.merger.Get(t.cy).item.text.NumLines(maxLines)4883+ if (t.multiLine || t.wrap) && t.cy < numItems {4884+ itemLines, _ = t.numItemLines(t.merger.Get(t.cy).item, maxLines)4885 }4886 linesBefore := t.cy - newOffset4807- if t.multiLine {4887+ if t.multiLine || t.wrap {4888 linesBefore = 04889 for i := newOffset; i < t.cy && i < numItems; i++ {4810- lines, _ := t.merger.Get(i).item.text.NumLines(maxLines - linesBefore - itemLines)4890+ lines, _ := t.numItemLines(t.merger.Get(i).item, maxLines-linesBefore-itemLines)4891 linesBefore += lines4892 }4893 }4894 linesAfter := maxLines - (linesBefore + itemLines)123 unmodified linesmodified:CHANGELOG.md+10 added
1 unmodified line2 =========3 4 0.54.05 ------6+- Implemented line wrap of long items7+ - `--wrap` option enables line wrap8+ - `--wrap-sign` customizes the sign for wrapped lines (default: `↳ `)9+ - `toggle-wrap` action toggles line wrap10+ ```sh11+ history | fzf --tac --wrap --bind 'ctrl-/:toggle-wrap'12+13+ # You can press CTRL-/ to toggle line wrap in CTRL-R binding14+ export FZF_CTRL_R_OPTS=$'--bind ctrl-/:toggle-wrap --wrap-sign "\t↳ "'15+ ```16 - Added `--info-command` option for customizing the info line17 ```sh18 # Prepend the current cursor position in yellow19 fzf --info-command='echo -e "\x1b[33;1m$FZF_POS\x1b[m/$FZF_INFO 💛"'2188 unmodified lines