Skip to content
Myr

Implement word wrapping in the preview window

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

Example: fzf --preview 'bat --style=plain --color=always {}' \ --preview-window wrap-word \ --bind space:toggle-preview-wrap-word Close https://github.com/junegunn/fzf/discussions/3383

authored by Junegunn Choi · committed by Junegunn Choi · parent b56d614 · Browse files · 12 files+481 added−220 removed
This commit

State

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

Diff stats

12 files+481 added−220 removed

  • other:+481 added−220 removed

12 files+481 added−220 removed

modified:man/man1/fzf.1+4 added-2 removed
924 unmodified lines
925default value 0 (or \fBcenter\fR) will put the label at the center of the926border line.927928.TP929.BI "\-\-preview\-window=" "[POSITION][,SIZE[%]][,border\-STYLE][,[no]wrap][,[no]follow][,[no]cycle][,[no]info][,[no]hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default][,<SIZE_THRESHOLD(ALTERNATIVE_LAYOUT)]"929.BI "\-\-preview\-window=" "[POSITION][,SIZE[%]][,border\-STYLE][,[no]wrap][,wrap\-word][,[no]follow][,[no]cycle][,[no]info][,[no]hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default][,<SIZE_THRESHOLD(ALTERNATIVE_LAYOUT)]"930931.RS932.B POSITION: (default: right)933 \fBup
9 unmodified lines
943* If size is given as 0, preview window will not be visible, but fzf will still944execute the command in the background.945946* Long lines are truncated by default. Line wrap can be enabled with947\fBwrap\fR flag.947\fBwrap\fR flag. \fBwrap\-word\fR flag enables word-level wrapping, which948breaks lines at word boundaries instead of mid-word.949950* Preview window will automatically scroll to the bottom when \fBfollow\fR951flag is set, similarly to how \fBtail \-f\fR works.952
1008 unmodified lines
1961 \fBtoggle\-input\fR1962 \fBtoggle\-multi\-line\fR1963 \fBtoggle\-preview\fR1964 \fBtoggle\-preview\-wrap\fR1965 \fBtoggle\-preview\-wrap\-word\fR1966 \fBtoggle\-raw\fR (toggle raw mode for displaying non-matching items)1967 \fBtoggle\-search\fR (toggle search functionality)1968 \fBtoggle\-sort\fR1969 \fBtoggle\-track\fR (toggle global tracking option (\fB\-\-track\fR))
226 unmodified lines
modified:src/tui/light.go+3 added-43 removed
11 unmodified lines
12 "time"13 "unicode/utf8"1415 "github.com/junegunn/fzf/src/util"16 "github.com/rivo/uniseg"1617 "golang.org/x/term"18)19
1397 unmodified lines
1417 }1418 w.stderrInternal(cleanse(text), false, code)1419}14201422type wrappedLine struct {1423 text string1424 displayWidth int1425}14261427func wrapLine(input string, prefixLength int, initialMax int, tabstop int, wrapSignWidth int) []wrappedLine {1428 lines := []wrappedLine{}1429 width := 01430 line := ""1431 gr := uniseg.NewGraphemes(input)1432 max := initialMax1433 for gr.Next() {1434 rs := gr.Runes()1435 str := string(rs)1436 var w int1437 if len(rs) == 1 && rs[0] == '\t' {1438 w = tabstop - (prefixLength+width)%tabstop1439 str = repeat(' ', w)1440 } else if rs[0] == '\r' {1441 w++1442 } else {1443 w = uniseg.StringWidth(str)1444 }1445 width += w14461447 if prefixLength+width <= max {1448 line += str1449 } else {1450 lines = append(lines, wrappedLine{string(line), width - w})1451 line = str1452 prefixLength = 01453 width = w1454 max = initialMax - wrapSignWidth1455 }1456 }1457 lines = append(lines, wrappedLine{string(line), width})1458 return lines1459}14601421func (w *LightWindow) fill(str string, resetCode string) FillReturn {1422 allLines := strings.Split(str, "\n")1423 for i, line := range allLines {1464 lines := wrapLine(line, w.posx, w.width, w.tabstop, w.wrapSignWidth)1424 lines := WrapLine(line, w.posx, w.width, w.tabstop, w.wrapSignWidth)1425 for j, wl := range lines {1466 w.stderrInternal(wl.text, false, resetCode)1467 w.posx += wl.displayWidth1426 w.stderrInternal(wl.Text, false, resetCode)1427 w.posx += wl.DisplayWidth14281429 // Wrap line1430 if j < len(lines)-1 || i < len(allLines)-1 {1431 if w.posy+1 >= w.height {
104 unmodified lines
modified:src/tui/tcell_test.go+6 added-6 removed
251 unmodified lines
252 {giveKey{tcell.KeyPrint, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled253 {giveKey{tcell.KeyPause, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // unhandled254255 }256 r := NewFullscreenRenderer(&ColorTheme{}, false, false)256 r := NewFullscreenRenderer(&ColorTheme{}, false, false, 8)257 r.Init()258259 // run and evaluate the tests260 initialResizeAsInvalid := true
3 unmodified lines
264 _screen.PostEventWait(giveEvent)265 t.Logf("giveEvent = %T{key: %v, ch: %q (%[3]v), mod: %#04b}\n", giveEvent, giveEvent.Key(), giveEvent.Rune(), giveEvent.Modifiers())266267 // process the event in fzf and evaluate the test268 gotEvent := r.GetChar()268 gotEvent := r.GetChar(true)269 // skip Resize events, those are sometimes put in the buffer outside of this test270 if initialResizeAsInvalid && gotEvent.Type == Invalid {271 t.Logf("Resize as Invalid swallowed")272 initialResizeAsInvalid = false273 gotEvent = r.GetChar()273 gotEvent = r.GetChar(true)274 }275 if gotEvent.Type == Resize {276 t.Logf("Resize swallowed")277 gotEvent = r.GetChar()277 gotEvent = r.GetChar(true)278 }279 t.Logf("wantEvent = %T{Type: %v, Char: %q (%[3]v)}\n", test.wantKey, test.wantKey.Type, test.wantKey.Char)280 t.Logf("gotEvent = %T{Type: %v, Char: %q (%[3]v)}\n", gotEvent, gotEvent.Type, gotEvent.Char)281282 assert(t, "r.GetChar().Type", gotEvent.Type, test.wantKey.Type)283 assert(t, "r.GetChar().Char", gotEvent.Char, test.wantKey.Char)282 assert(t, "r.GetChar(true).Type", gotEvent.Type, test.wantKey.Type)283 assert(t, "r.GetChar(true).Char", gotEvent.Char, test.wantKey.Char)284 }285286 r.Close()287}
186 unmodified lines
modified:src/tui/tcell.go+49 added-48 removed
3 unmodified lines
45import (6 "os"7 "regexp"8 "strings"9 "time"1011 "github.com/gdamore/tcell/v2"12 "github.com/junegunn/fzf/src/util"
40 unmodified lines
53 params *string54 showCursor bool55 wrapSign string56 wrapSignWidth int57 tabstop int58}5960func (w *TcellWindow) Top() int {61 return w.top
696 unmodified lines
758 width: width,759 height: height,760 normal: normal,761 borderStyle: borderStyle,760 showCursor: r.showCursor}762 showCursor: r.showCursor,763 tabstop: r.tabstop}764 w.Erase()765 return w766}767
128 unmodified lines
896func (w *TcellWindow) CPrint(pair ColorPair, text string) {897 w.printString(text, pair)898}899897func (w *TcellWindow) fillString(text string, pair ColorPair) FillReturn {898 lx := 0900func (w *TcellWindow) pairStyle(pair ColorPair) tcell.Style {901 a := pair.Attr()900902 var style tcell.Style903 if w.color {904 style = pair.style()905 } else {
13 unmodified lines
919 }920 } else {921 style = style.Underline(false)922 }922 style = w.withUrl(style)923 return w.withUrl(style)924}925926func (w *TcellWindow) renderGraphemes(text string, style tcell.Style) {927 gr := uniseg.NewGraphemes(text)925Loop:928 for gr.Next() {929 st := style930 rs := gr.Runes()929 if len(rs) == 1 {930 r := rs[0]931 switch r {932 case '\r':933 st = style.Dim(true)934 rs[0] = '␍'935 case '\n':936 w.lastY++937 w.lastX = 0938 lx = 0939 continue Loop940 }931 if len(rs) == 1 && rs[0] == '\r' {932 st = style.Dim(true)933 rs[0] = '␍'934 }935943 // word wrap:944 xPos := w.left + w.lastX + lx936 xPos := w.left + w.lastX945 if xPos >= w.left+w.width {946 w.lastY++947 if w.lastY >= w.height {948 return FillSuspend949 }950 w.lastX = 0951 lx = 0952 xPos = w.left953 sign := w.wrapSign954 if w.wrapSignWidth > w.width {955 runes, _ := util.Truncate(sign, w.width)956 sign = string(runes)957 }958 wgr := uniseg.NewGraphemes(sign)959 for wgr.Next() {960 rs := wgr.Runes()961 _screen.SetContent(w.left+lx, w.top+w.lastY, rs[0], rs[1:], style.Dim(true))962 lx += uniseg.StringWidth(string(rs))963 }964 xPos = w.left + lx965 }966937 yPos := w.top + w.lastY968 if yPos >= (w.top + w.height) {969 return FillSuspend938 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {939 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)940 }941 w.lastX += util.StringWidth(string(rs))942 }943}944972 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)973 lx += util.StringWidth(string(rs))945func (w *TcellWindow) renderWrapSign(style tcell.Style) {946 sign := w.wrapSign947 if w.wrapSignWidth > w.width {948 runes, _ := util.Truncate(sign, w.width)949 sign = string(runes)950 }975 w.lastX += lx976 if w.lastX == w.width {951 gr := uniseg.NewGraphemes(sign)952 for gr.Next() {953 rs := gr.Runes()954 _screen.SetContent(w.left+w.lastX, w.top+w.lastY, rs[0], rs[1:], style.Dim(true))955 w.lastX += uniseg.StringWidth(string(rs))956 }957}958959func (w *TcellWindow) fillString(text string, pair ColorPair) FillReturn {960 style := w.pairStyle(pair)961962 for i, segment := range strings.Split(text, "\n") {963 for j, wl := range WrapLine(segment, w.lastX, w.width, w.tabstop, w.wrapSignWidth) {964 if i > 0 || j > 0 {965 w.lastY++966 if w.lastY >= w.height {967 return FillSuspend968 }969 w.lastX = 0970 if j > 0 {971 w.renderWrapSign(style)972 }973 }974 w.renderGraphemes(wl.Text, style)975 }976 }977 if w.lastX >= w.width {978 w.lastY++979 w.lastX = 0980 return FillNextLine981 }
119 unmodified lines
modified:src/tui/tui_test.go+40 added
1package tui23import "testing"45func TestWrapLine(t *testing.T) {6 // Basic wrapping7 lines := WrapLine("hello world", 0, 7, 8, 2)8 if len(lines) != 2 || lines[0].Text != "hello w" || lines[1].Text != "orld" {9 t.Errorf("Basic wrap: %v", lines)10 }1112 // Exact fit — no wrapping needed13 lines = WrapLine("hello", 0, 5, 8, 2)14 if len(lines) != 1 || lines[0].Text != "hello" || lines[0].DisplayWidth != 5 {15 t.Errorf("Exact fit: %v", lines)16 }1718 // With prefix length19 lines = WrapLine("hello", 3, 5, 8, 2)20 if len(lines) != 2 || lines[0].Text != "he" || lines[1].Text != "llo" {21 t.Errorf("Prefix length: %v", lines)22 }2324 // Empty string25 lines = WrapLine("", 0, 10, 8, 2)26 if len(lines) != 1 || lines[0].Text != "" || lines[0].DisplayWidth != 0 {27 t.Errorf("Empty string: %v", lines)28 }2930 // Continuation lines account for wrapSignWidth31 lines = WrapLine("abcdefghij", 0, 5, 8, 2)32 // First line: "abcde" (5 chars fit in width 5)33 // Continuation max: 5-2=3, so "fgh" then "ij"34 if len(lines) != 3 || lines[0].Text != "abcde" || lines[1].Text != "fgh" || lines[2].Text != "ij" {35 t.Errorf("Continuation: %v", lines)36 }3738 // Tab expansion39 lines = WrapLine("\there", 0, 10, 4, 2)40 if len(lines) != 1 || lines[0].DisplayWidth != 8 {41 t.Errorf("Tab: %v", lines)42 }43}4445func TestHexToColor(t *testing.T) {46 assert := func(expr string, r, g, b int) {47 color := HexToColor(expr)48 if !color.is24() ||
12 unmodified lines
modified:src/tui/tui.go+46 added-1 removed
1package tui23import (4 "strconv"5 "strings"6 "time"78 "github.com/junegunn/fzf/src/util"9 "github.com/rivo/uniseg"
819 unmodified lines
829type FullscreenRenderer struct {830 theme *ColorTheme831 mouse bool832 forceBlack bool833 tabstop int834 prevDownTime time.Time835 clicks [][2]int836 showCursor bool837}838837func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool) Renderer {839func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool, tabstop int) Renderer {840 r := &FullscreenRenderer{841 theme: theme,842 mouse: mouse,843 forceBlack: forceBlack,844 tabstop: tabstop,845 prevDownTime: time.Unix(0, 0),846 clicks: [][2]int{},847 showCursor: true}848 return r
513 unmodified lines
13621363func runeWidth(r rune) int {1364 return uniseg.StringWidth(string(r))1365}13661367// WrappedLine represents a single visual line after character-level wrapping.1368type WrappedLine struct {1369 Text string1370 DisplayWidth int1371}13721373// WrapLine splits a single line (no embedded \n) into visual lines1374// that fit within initialMax columns. Character-level wrapping only.1375func WrapLine(input string, prefixLength int, initialMax int, tabstop int, wrapSignWidth int) []WrappedLine {1376 lines := []WrappedLine{}1377 width := 01378 line := ""1379 gr := uniseg.NewGraphemes(input)1380 max := initialMax1381 for gr.Next() {1382 rs := gr.Runes()1383 str := string(rs)1384 var w int1385 if len(rs) == 1 && rs[0] == '\t' {1386 w = tabstop - (prefixLength+width)%tabstop1387 str = strings.Repeat(" ", w)1388 } else if rs[0] == '\r' {1389 w++1390 } else {1391 w = uniseg.StringWidth(str)1392 }1393 width += w13941395 if prefixLength+width <= max {1396 line += str1397 } else {1398 lines = append(lines, WrappedLine{string(line), width - w})1399 line = str1400 prefixLength = 01401 width = w1402 max = initialMax - wrapSignWidth1403 }1404 }1405 lines = append(lines, WrappedLine{string(line), width})1406 return lines1407}
modified:src/actiontype_string.go+74 added-73 removed
105 unmodified lines
106 _ = x[actShowPreview-95]107 _ = x[actHidePreview-96]108 _ = x[actTogglePreview-97]109 _ = x[actTogglePreviewWrap-98]110 _ = x[actTransform-99]111 _ = x[actTransformBorderLabel-100]112 _ = x[actTransformGhost-101]113 _ = x[actTransformHeader-102]114 _ = x[actTransformFooter-103]115 _ = x[actTransformHeaderLabel-104]116 _ = x[actTransformFooterLabel-105]117 _ = x[actTransformInputLabel-106]118 _ = x[actTransformListLabel-107]119 _ = x[actTransformNth-108]120 _ = x[actTransformPointer-109]121 _ = x[actTransformPreviewLabel-110]122 _ = x[actTransformPrompt-111]123 _ = x[actTransformQuery-112]124 _ = x[actTransformSearch-113]125 _ = x[actTrigger-114]126 _ = x[actBgTransform-115]127 _ = x[actBgTransformBorderLabel-116]128 _ = x[actBgTransformGhost-117]129 _ = x[actBgTransformHeader-118]130 _ = x[actBgTransformFooter-119]131 _ = x[actBgTransformHeaderLabel-120]132 _ = x[actBgTransformFooterLabel-121]133 _ = x[actBgTransformInputLabel-122]134 _ = x[actBgTransformListLabel-123]135 _ = x[actBgTransformNth-124]136 _ = x[actBgTransformPointer-125]137 _ = x[actBgTransformPreviewLabel-126]138 _ = x[actBgTransformPrompt-127]139 _ = x[actBgTransformQuery-128]140 _ = x[actBgTransformSearch-129]141 _ = x[actBgCancel-130]142 _ = x[actSearch-131]143 _ = x[actPreview-132]144 _ = x[actPreviewTop-133]145 _ = x[actPreviewBottom-134]146 _ = x[actPreviewUp-135]147 _ = x[actPreviewDown-136]148 _ = x[actPreviewPageUp-137]149 _ = x[actPreviewPageDown-138]150 _ = x[actPreviewHalfPageUp-139]151 _ = x[actPreviewHalfPageDown-140]152 _ = x[actPrevHistory-141]153 _ = x[actPrevSelected-142]154 _ = x[actPrint-143]155 _ = x[actPut-144]156 _ = x[actNextHistory-145]157 _ = x[actNextSelected-146]158 _ = x[actExecute-147]159 _ = x[actExecuteSilent-148]160 _ = x[actExecuteMulti-149]161 _ = x[actSigStop-150]162 _ = x[actBest-151]163 _ = x[actFirst-152]164 _ = x[actLast-153]165 _ = x[actReload-154]166 _ = x[actReloadSync-155]167 _ = x[actDisableSearch-156]168 _ = x[actEnableSearch-157]169 _ = x[actSelect-158]170 _ = x[actDeselect-159]171 _ = x[actUnbind-160]172 _ = x[actRebind-161]173 _ = x[actToggleBind-162]174 _ = x[actBecome-163]175 _ = x[actShowHeader-164]176 _ = x[actHideHeader-165]177 _ = x[actBell-166]178 _ = x[actExclude-167]179 _ = x[actExcludeMulti-168]180 _ = x[actAsync-169]110 _ = x[actTogglePreviewWrapWord-99]111 _ = x[actTransform-100]112 _ = x[actTransformBorderLabel-101]113 _ = x[actTransformGhost-102]114 _ = x[actTransformHeader-103]115 _ = x[actTransformFooter-104]116 _ = x[actTransformHeaderLabel-105]117 _ = x[actTransformFooterLabel-106]118 _ = x[actTransformInputLabel-107]119 _ = x[actTransformListLabel-108]120 _ = x[actTransformNth-109]121 _ = x[actTransformPointer-110]122 _ = x[actTransformPreviewLabel-111]123 _ = x[actTransformPrompt-112]124 _ = x[actTransformQuery-113]125 _ = x[actTransformSearch-114]126 _ = x[actTrigger-115]127 _ = x[actBgTransform-116]128 _ = x[actBgTransformBorderLabel-117]129 _ = x[actBgTransformGhost-118]130 _ = x[actBgTransformHeader-119]131 _ = x[actBgTransformFooter-120]132 _ = x[actBgTransformHeaderLabel-121]133 _ = x[actBgTransformFooterLabel-122]134 _ = x[actBgTransformInputLabel-123]135 _ = x[actBgTransformListLabel-124]136 _ = x[actBgTransformNth-125]137 _ = x[actBgTransformPointer-126]138 _ = x[actBgTransformPreviewLabel-127]139 _ = x[actBgTransformPrompt-128]140 _ = x[actBgTransformQuery-129]141 _ = x[actBgTransformSearch-130]142 _ = x[actBgCancel-131]143 _ = x[actSearch-132]144 _ = x[actPreview-133]145 _ = x[actPreviewTop-134]146 _ = x[actPreviewBottom-135]147 _ = x[actPreviewUp-136]148 _ = x[actPreviewDown-137]149 _ = x[actPreviewPageUp-138]150 _ = x[actPreviewPageDown-139]151 _ = x[actPreviewHalfPageUp-140]152 _ = x[actPreviewHalfPageDown-141]153 _ = x[actPrevHistory-142]154 _ = x[actPrevSelected-143]155 _ = x[actPrint-144]156 _ = x[actPut-145]157 _ = x[actNextHistory-146]158 _ = x[actNextSelected-147]159 _ = x[actExecute-148]160 _ = x[actExecuteSilent-149]161 _ = x[actExecuteMulti-150]162 _ = x[actSigStop-151]163 _ = x[actBest-152]164 _ = x[actFirst-153]165 _ = x[actLast-154]166 _ = x[actReload-155]167 _ = x[actReloadSync-156]168 _ = x[actDisableSearch-157]169 _ = x[actEnableSearch-158]170 _ = x[actSelect-159]171 _ = x[actDeselect-160]172 _ = x[actUnbind-161]173 _ = x[actRebind-162]174 _ = x[actToggleBind-163]175 _ = x[actBecome-164]176 _ = x[actShowHeader-165]177 _ = x[actHideHeader-166]178 _ = x[actBell-167]179 _ = x[actExclude-168]180 _ = x[actExcludeMulti-169]181 _ = x[actAsync-170]182}183183const _actionType_name = "actIgnoreactStartactClickactInvalidactBracketedPasteBeginactBracketedPasteEndactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactBackwardSubWordactCancelactChangeBorderLabelactChangeGhostactChangeHeaderactChangeFooteractChangeHeaderLabelactChangeFooterLabelactChangeInputLabelactChangeListLabelactChangeMultiactChangeNthactChangePointeractChangePreviewactChangePreviewLabelactChangePreviewWindowactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactForwardSubWordactKillLineactKillWordactKillSubWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactBackwardKillSubWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactToggleWrapactToggleMultiLineactToggleHscrollactToggleRawactEnableRawactDisableRawactTrackCurrentactToggleInputactHideInputactShowInputactUntrackCurrentactDownactDownMatchactUpactUpMatchactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactOffsetMiddleactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTransformactTransformBorderLabelactTransformGhostactTransformHeaderactTransformFooteractTransformHeaderLabelactTransformFooterLabelactTransformInputLabelactTransformListLabelactTransformNthactTransformPointeractTransformPreviewLabelactTransformPromptactTransformQueryactTransformSearchactTriggeractBgTransformactBgTransformBorderLabelactBgTransformGhostactBgTransformHeaderactBgTransformFooteractBgTransformHeaderLabelactBgTransformFooterLabelactBgTransformInputLabelactBgTransformListLabelactBgTransformNthactBgTransformPointeractBgTransformPreviewLabelactBgTransformPromptactBgTransformQueryactBgTransformSearchactBgCancelactSearchactPreviewactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactBestactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactToggleBindactBecomeactShowHeaderactHideHeaderactBellactExcludeactExcludeMultiactAsync"184const _actionType_name = "actIgnoreactStartactClickactInvalidactBracketedPasteBeginactBracketedPasteEndactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactBackwardSubWordactCancelactChangeBorderLabelactChangeGhostactChangeHeaderactChangeFooteractChangeHeaderLabelactChangeFooterLabelactChangeInputLabelactChangeListLabelactChangeMultiactChangeNthactChangePointeractChangePreviewactChangePreviewLabelactChangePreviewWindowactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactForwardSubWordactKillLineactKillWordactKillSubWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactBackwardKillSubWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactToggleWrapactToggleMultiLineactToggleHscrollactToggleRawactEnableRawactDisableRawactTrackCurrentactToggleInputactHideInputactShowInputactUntrackCurrentactDownactDownMatchactUpactUpMatchactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactOffsetMiddleactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTogglePreviewWrapWordactTransformactTransformBorderLabelactTransformGhostactTransformHeaderactTransformFooteractTransformHeaderLabelactTransformFooterLabelactTransformInputLabelactTransformListLabelactTransformNthactTransformPointeractTransformPreviewLabelactTransformPromptactTransformQueryactTransformSearchactTriggeractBgTransformactBgTransformBorderLabelactBgTransformGhostactBgTransformHeaderactBgTransformFooteractBgTransformHeaderLabelactBgTransformFooterLabelactBgTransformInputLabelactBgTransformListLabelactBgTransformNthactBgTransformPointeractBgTransformPreviewLabelactBgTransformPromptactBgTransformQueryactBgTransformSearchactBgCancelactSearchactPreviewactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactBestactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactToggleBindactBecomeactShowHeaderactHideHeaderactBellactExcludeactExcludeMultiactAsync"185185var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 57, 77, 84, 92, 110, 118, 127, 144, 165, 180, 201, 225, 240, 258, 267, 287, 301, 316, 331, 351, 371, 390, 408, 422, 434, 450, 466, 487, 509, 524, 538, 552, 565, 582, 590, 603, 619, 631, 639, 653, 667, 684, 695, 706, 720, 738, 755, 762, 781, 803, 815, 829, 838, 853, 865, 878, 889, 900, 912, 926, 947, 962, 975, 993, 1009, 1021, 1033, 1046, 1061, 1075, 1087, 1099, 1116, 1123, 1135, 1140, 1150, 1159, 1170, 1181, 1194, 1209, 1220, 1233, 1248, 1255, 1268, 1281, 1298, 1313, 1326, 1340, 1354, 1370, 1390, 1402, 1425, 1442, 1460, 1478, 1501, 1524, 1546, 1567, 1582, 1601, 1625, 1643, 1660, 1678, 1688, 1702, 1727, 1746, 1766, 1786, 1811, 1836, 1860, 1883, 1900, 1921, 1947, 1967, 1986, 2006, 2017, 2026, 2036, 2049, 2065, 2077, 2091, 2107, 2125, 2145, 2167, 2181, 2196, 2204, 2210, 2224, 2239, 2249, 2265, 2280, 2290, 2297, 2305, 2312, 2321, 2334, 2350, 2365, 2374, 2385, 2394, 2403, 2416, 2425, 2438, 2451, 2458, 2468, 2483, 2491}186var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 57, 77, 84, 92, 110, 118, 127, 144, 165, 180, 201, 225, 240, 258, 267, 287, 301, 316, 331, 351, 371, 390, 408, 422, 434, 450, 466, 487, 509, 524, 538, 552, 565, 582, 590, 603, 619, 631, 639, 653, 667, 684, 695, 706, 720, 738, 755, 762, 781, 803, 815, 829, 838, 853, 865, 878, 889, 900, 912, 926, 947, 962, 975, 993, 1009, 1021, 1033, 1046, 1061, 1075, 1087, 1099, 1116, 1123, 1135, 1140, 1150, 1159, 1170, 1181, 1194, 1209, 1220, 1233, 1248, 1255, 1268, 1281, 1298, 1313, 1326, 1340, 1354, 1370, 1390, 1414, 1426, 1449, 1466, 1484, 1502, 1525, 1548, 1570, 1591, 1606, 1625, 1649, 1667, 1684, 1702, 1712, 1726, 1751, 1770, 1790, 1810, 1835, 1860, 1884, 1907, 1924, 1945, 1971, 1991, 2010, 2030, 2041, 2050, 2060, 2073, 2089, 2101, 2115, 2131, 2149, 2169, 2191, 2205, 2220, 2228, 2234, 2248, 2263, 2273, 2289, 2304, 2314, 2321, 2329, 2336, 2345, 2358, 2374, 2389, 2398, 2409, 2418, 2427, 2440, 2449, 2462, 2475, 2482, 2492, 2507, 2515}187188func (i actionType) String() string {189 if i < 0 || i >= actionType(len(_actionType_index)-1) {190 return "actionType(" + strconv.FormatInt(int64(i), 10) + ")"
3 unmodified lines
modified:src/options_test.go+14 added
446 unmodified lines
447 opts.Preview.size.percent == true &&448 opts.Preview.size.size == 70) {449 t.Error(opts.Preview)450 }451452 // wrap-word tests453 opts = optsFor("--preview-window=wrap-word")454 if !(opts.Preview.wrap == true && opts.Preview.wrapWord == true) {455 t.Errorf("wrap-word: wrap=%v, wrapWord=%v", opts.Preview.wrap, opts.Preview.wrapWord)456 }457 opts = optsFor("--preview-window=wrap-word,nowrap")458 if !(opts.Preview.wrap == false && opts.Preview.wrapWord == false) {459 t.Errorf("wrap-word,nowrap: wrap=%v, wrapWord=%v", opts.Preview.wrap, opts.Preview.wrapWord)460 }461 opts = optsFor("--preview-window=wrap-word,wrap")462 if !(opts.Preview.wrap == true && opts.Preview.wrapWord == false) {463 t.Errorf("wrap-word,wrap: wrap=%v, wrapWord=%v", opts.Preview.wrap, opts.Preview.wrapWord)464 }465}466467func TestAdditiveExpect(t *testing.T) {468 opts := optsFor("--expect=a", "--expect", "b", "--expect=c")
59 unmodified lines
modified:src/options.go+17 added-3 removed
155 unmodified lines
156 PREVIEW WINDOW157 --preview=COMMAND Command to preview highlighted line ({})158 --preview-window=OPT Preview window layout (default: right:50%)159 [up|down|left|right][,SIZE[%]]160 [,[no]wrap][,[no]cycle][,[no]follow][,[no]info]160 [,[no]wrap[-word]][,[no]cycle][,[no]follow][,[no]info]161 [,[no]hidden][,border-STYLE]162 [,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES]163 [,default][,<SIZE_THRESHOLD(ALTERNATIVE_LAYOUT)]164 --preview-border[=STYLE] Short for --preview-window=border-STYLE
201 unmodified lines
366 size sizeSpec367 scroll string368 hidden bool369 wrap bool370 wrapWord bool371 cycle bool372 follow bool373 info bool374 border tui.BorderShape
168 unmodified lines
543 if !(a.size == b.size && a.position == b.position && a.border == b.border && a.hidden == b.hidden) {544 return previewOptsDifferentLayout545 }546546 if a.wrap == b.wrap && a.headerLines == b.headerLines && a.info == b.info && a.scroll == b.scroll {547 if a.wrap == b.wrap && a.wrapWord == b.wrapWord && a.headerLines == b.headerLines && a.info == b.info && a.scroll == b.scroll {548 return previewOptsSame549 }550551 return previewOptsDifferentContentLayout
139 unmodified lines
691 return output692}693694func defaultPreviewOpts(command string) previewOpts {694 return previewOpts{command, posRight, sizeSpec{50, true}, "", false, false, false, false, true, defaultBorderShape, 0, 0, nil}695 return previewOpts{696 command: command,697 position: posRight,698 size: sizeSpec{50, true},699 info: true,700 border: defaultBorderShape,701 }702}703704func defaultOptions() *Options {705 var theme, baseTheme *tui.ColorTheme
1163 unmodified lines
1869 case "toggle-preview":1870 appendAction(actTogglePreview)1871 case "toggle-preview-wrap":1872 appendAction(actTogglePreviewWrap)1873 case "toggle-preview-wrap-word":1874 appendAction(actTogglePreviewWrapWord)1875 case "toggle-sort":1876 appendAction(actToggleSort)1877 case "offset-up":1878 appendAction(actOffsetUp)
403 unmodified lines
2282 case "nohidden":2283 opts.hidden = false2284 case "wrap":2285 opts.wrap = true2286 opts.wrapWord = false2287 case "wrap-word":2288 opts.wrap = true2289 opts.wrapWord = true2290 case "nowrap":2291 opts.wrap = false2292 opts.wrapWord = false2293 case "cycle":2294 opts.cycle = true2295 case "nocycle":2296 opts.cycle = false
1555 unmodified lines
modified:src/terminal_test.go+69 added
697 unmodified lines
698 return data, err699 }700 }701}702703func TestWordWrapAnsiLine(t *testing.T) {704 term := &Terminal{}705706 // Simple wrapping707 result := term.wordWrapAnsiLine("hello world", 7, 2)708 if len(result) != 2 || result[0] != "hello" || result[1] != "world" {709 t.Errorf("Simple: %q", result)710 }711712 // No wrapping needed713 result = term.wordWrapAnsiLine("hello", 10, 2)714 if len(result) != 1 || result[0] != "hello" {715 t.Errorf("No wrap: %q", result)716 }717718 // ANSI codes preserved across split719 result = term.wordWrapAnsiLine("\x1b[31mhello \x1b[32mworld", 8, 2)720 if len(result) != 2 || result[0] != "\x1b[31mhello" || result[1] != "\x1b[32mworld" {721 t.Errorf("ANSI: %q", result)722 }723724 // Long word (no space) — no break, let character wrapping handle it725 result = term.wordWrapAnsiLine("abcdefghij", 5, 2)726 if len(result) != 1 || result[0] != "abcdefghij" {727 t.Errorf("Long word: %q", result)728 }729730 // Multiple words with continuation wrapSignWidth731 result = term.wordWrapAnsiLine("aa bb cc dd", 5, 2)732 // max=5 for first line, max=3 for continuations (5-2)733 // "aa bb" (5 wide), split at second space -> "aa bb" | "cc" | "dd"734 if len(result) != 3 || result[0] != "aa bb" || result[1] != "cc" || result[2] != "dd" {735 t.Errorf("Multiple words: %q", result)736 }737738 // Empty string739 result = term.wordWrapAnsiLine("", 10, 2)740 if len(result) != 1 || result[0] != "" {741 t.Errorf("Empty: %q", result)742 }743744 // OSC 8 hyperlink preserved745 result = term.wordWrapAnsiLine("\x1b]8;;http://example.com\x1b\\click here\x1b]8;;\x1b\\", 8, 2)746 if len(result) != 2 {747 t.Errorf("Hyperlink split count: %d, %q", len(result), result)748 }749750 // Tab handling: tab expands to tabstop-aligned width751 term.tabstop = 8752 // "\thi there" — tab at column 0 expands to 8, total "hi" starts at 8753 // maxWidth=15: "\thi" = 10 wide, "there" = 5 wide, total 16 > 15, wrap at space754 result = term.wordWrapAnsiLine("\thi there", 15, 2)755 if len(result) != 2 || result[0] != "\thi" || result[1] != "there" {756 t.Errorf("Tab: %q", result)757 }758759 // Tab as word boundary: "hello"(5) + tab(3→col8) + "world"(5) = 13 total760 // maxWidth=13: fits without wrapping761 result = term.wordWrapAnsiLine("hello\tworld", 13, 2)762 if len(result) != 1 || result[0] != "hello\tworld" {763 t.Errorf("Tab no wrap: %q", result)764 }765 // maxWidth=12: 13 > 12, wraps at tab766 result = term.wordWrapAnsiLine("hello\tworld", 12, 2)767 if len(result) != 2 || result[0] != "hello" || result[1] != "world" {768 t.Errorf("Tab wrap: %q", result)769 }770}
modified:src/terminal.go+145 added-38 removed
615 unmodified lines
616 actShowPreview617 actHidePreview618 actTogglePreview619 actTogglePreviewWrap620 actTogglePreviewWrapWord621622 actTransform623 actTransformBorderLabel624 actTransformGhost
316 unmodified lines
941 }942 }943 if fullscreen {944 if tui.HasFullscreenRenderer() {944 renderer = tui.NewFullscreenRenderer(opts.Theme, opts.Black, opts.Mouse)945 renderer = tui.NewFullscreenRenderer(opts.Theme, opts.Black, opts.Mouse, opts.Tabstop)946 } else {947 renderer, err = tui.NewLightRenderer(opts.TtyDefault, ttyin, opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit,948 true, func(h int) int { return h })949 }
3139 unmodified lines
40894090 return passThroughs, transformed4091}40924093func (t *Terminal) wordWrapAnsiLine(line string, maxWidth int, wrapSignWidth int) []string {4094 if maxWidth <= 0 {4095 return []string{line}4096 }40974098 var result []string4099 lineStart := 04100 width := 04101 lastSpaceStart := -14102 lastSpaceEnd := -14103 widthBeforeLastSpace := 04104 lastSpaceWidth := 04105 max := maxWidth4106 pos := 041074108 for pos < len(line) {4109 // Find next ANSI escape sequence4110 start, end := nextAnsiEscapeSequence(line[pos:])41114112 // Determine the end of printable text before the next escape4113 var printableEnd int4114 if start < 0 {4115 printableEnd = len(line)4116 } else {4117 printableEnd = pos + start4118 }41194120 // Process printable characters using grapheme clusters4121 gr := uniseg.NewGraphemes(line[pos:printableEnd])4122 for gr.Next() {4123 gStart, gEnd := gr.Positions()4124 w := gr.Width()4125 str := gr.Str()41264127 if str == "\t" {4128 w = t.tabstop - (width % t.tabstop)4129 }41304131 if str == " " || str == "\t" {4132 lastSpaceStart = pos + gStart4133 lastSpaceEnd = pos + gEnd4134 widthBeforeLastSpace = width4135 lastSpaceWidth = w4136 }41374138 width += w41394140 if width > max && lastSpaceEnd > lineStart {4141 result = append(result, line[lineStart:lastSpaceStart])4142 lineStart = lastSpaceEnd4143 width -= widthBeforeLastSpace + lastSpaceWidth4144 lastSpaceStart = -14145 lastSpaceEnd = -14146 widthBeforeLastSpace = 04147 max = maxWidth - wrapSignWidth4148 }4149 }4150 pos = printableEnd41514152 // Skip the ANSI escape sequence4153 if start >= 0 {4154 pos += end - start4155 }4156 }41574158 result = append(result, line[lineStart:])4159 return result4160}41614162func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unchanged bool) {4163 maxWidth := t.pwindow.Width()4164 var ansi *ansiState4165 spinnerRedraw := t.pwindow.Y() == 0
85 unmodified lines
4251 if len(passThroughs) > 0 && len(line) == 0 {4252 continue4253 }42544255 // Pre-split line into sub-lines for word wrapping4256 var subLines []string4257 if t.activePreviewOpts.wrapWord {4258 subLines = t.wordWrapAnsiLine(line, maxWidth, t.wrapSignWidth)4259 } else {4260 subLines = []string{line}4261 }42624263 var fillRet tui.FillReturn4186 prefixWidth := 04187 var url *url4188 _, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {4189 trimmed := []rune(str)4190 isTrimmed := false4191 if !t.activePreviewOpts.wrap {4192 trimmed, isTrimmed = t.trimRight(trimmed, maxWidth-t.pwindow.X())4264 wrap := t.activePreviewOpts.wrap4265 for subIdx, subLine := range subLines {4266 // Render wrap sign for continuation sub-lines4267 if subIdx > 0 {4268 if fillRet == tui.FillContinue {4269 fillRet = t.pwindow.Fill("\n")4270 if fillRet == tui.FillSuspend {4271 t.previewed.filled = true4272 break Loop4273 }4274 }4275 t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), -1, tui.Dim, t.wrapSign)4276 }4194 if url == nil && ansi != nil && ansi.url != nil {4195 url = ansi.url4196 t.pwindow.LinkBegin(url.uri, url.params)4197 }4198 if url != nil && (ansi == nil || ansi.url == nil) {4199 url = nil4200 t.pwindow.LinkEnd()4201 }4202 if ansi != nil {4203 lbg = ansi.lbg4204 } else {4205 lbg = -14206 }4207 str, width := t.processTabs(trimmed, prefixWidth)4208 if width > prefixWidth {4209 prefixWidth = width4210 colored := ansi != nil && ansi.colored()4211 if t.theme.Colored && colored {4212 fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.ul, ansi.attr, str)42774278 prefixWidth := t.pwindow.X()4279 var url *url4280 _, _, ansi = extractColor(subLine, ansi, func(str string, ansi *ansiState) bool {4281 trimmed := []rune(str)4282 isTrimmed := false4283 if !wrap {4284 trimmed, isTrimmed = t.trimRight(trimmed, maxWidth-t.pwindow.X())4285 }4286 if url == nil && ansi != nil && ansi.url != nil {4287 url = ansi.url4288 t.pwindow.LinkBegin(url.uri, url.params)4289 }4290 if url != nil && (ansi == nil || ansi.url == nil) {4291 url = nil4292 t.pwindow.LinkEnd()4293 }4294 if ansi != nil {4295 lbg = ansi.lbg4296 } else {4297 lbg = -14298 }4299 str, width := t.processTabs(trimmed, prefixWidth)4300 if width > prefixWidth {4301 prefixWidth = width4302 colored := ansi != nil && ansi.colored()4303 if t.theme.Colored && colored {4304 fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.ul, ansi.attr, str)4305 } else {4214 attr := tui.AttrRegular4215 if colored {4216 attr = ansi.attr4306 attr := tui.AttrRegular4307 if colored {4308 attr = ansi.attr4309 }4310 fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), -1, attr, str)4311 }4218 fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), -1, attr, str)4312 }4313 return !isTrimmed &&4314 (fillRet == tui.FillContinue || wrap && fillRet == tui.FillNextLine)4315 })4316 if url != nil {4317 t.pwindow.LinkEnd()4318 }4221 return !isTrimmed &&4222 (fillRet == tui.FillContinue || t.activePreviewOpts.wrap && fillRet == tui.FillNextLine)4223 })4224 if url != nil {4225 t.pwindow.LinkEnd()43194320 if fillRet == tui.FillSuspend {4321 t.previewed.filled = true4322 break Loop4323 }4324 }43254326 t.previewer.scrollable = t.previewer.scrollable || t.pwindow.Y() == height-1 && t.pwindow.X() == t.pwindow.Width()4327 if fillRet == tui.FillNextLine {4328 continue4329 } else if fillRet == tui.FillSuspend {
1740 unmodified lines
6070 // Also kill the preview process if it's still running6071 t.cancelPreview()6072 }6073 }5975 case actTogglePreviewWrap:6074 case actTogglePreviewWrap, actTogglePreviewWrapWord:6075 if t.hasPreviewWindow() {6076 if a.t == actTogglePreviewWrapWord {6077 t.activePreviewOpts.wrapWord = !t.activePreviewOpts.wrapWord6078 t.activePreviewOpts.wrap = t.activePreviewOpts.wrapWord6079 } else {5977 t.activePreviewOpts.wrap = !t.activePreviewOpts.wrap6080 t.activePreviewOpts.wrap = !t.activePreviewOpts.wrap6081 if !t.activePreviewOpts.wrap {6082 t.activePreviewOpts.wrapWord = false6083 }6084 }6085 // Reset preview version so that full redraw occurs6086 t.previewed.version = 06087 req(reqPreviewRefresh)6088 }
1460 unmodified lines
modified:CHANGELOG.md+14 added-6 removed
1 unmodified line
2=========340.68.05------6- Implemented word wrapping in the preview window7 - Added `wrap-word` flag for `--preview-window` to enable word-level wrapping8 - Added `toggle-preview-wrap-word` action9 ```sh10 fzf --preview 'bat --style=plain --color=always {}' \11 --preview-window wrap-word \12 --bind space:toggle-preview-wrap-word13 ```14- Added support for underline style variants in `--color`:15 `underline-double`, `underline-curly`, `underline-dotted`, `underline-dashed`16 ```sh17 fzf --color 'fg:underline-curly,current-fg:underline-dashed'
11 unmodified lines
29- zsh: Handle multi-line history selection (#4595) (@LangLangBart)30- Bug fixes31 - Fixed symlinks to directories being returned as files (#4676) (@skk64)32 - Fixed SIGHUP signal handling (#4668) (@LangLangBart)25 - Fixed preview process not killed on exit26 - Fixed coloring of items with zero-width characters27 - Fixed `track-current` unset after a combined movement action33 - Fixed preview process not killed on exit (#4667)34 - Fixed coloring of items with zero-width characters (#4620)35 - Fixed `track-current` unset after a combined movement action (#4649)36 - Fixed `--accept-nth` being ignored in filter mode (#4636) (@charemma)37 - Fixed display width calculation with `maxWidth` (#4596) (@LangLangBart)30 - Fixed clearing of the rest of the current line on start31 - Fixed `x-api-key` header not required for GET requests38 - Fixed clearing of the rest of the current line on start (#4652)39 - Fixed `x-api-key` header not required for GET requests (#4627)40 - Fixed key reading not cancelled when `execute` triggered via a server request (#4653)41 - Fixed rebind of readline command `redraw-current-line` (#4635) (@jameslazo)42 - Fixed `fzf-tmux` `TERM` quoting and added `mktemp` usage (#4664) (@Goofygiraffe06)35 - Do not allow very long queries in `FuzzyMatchV2`43 - Do not allow very long queries in `FuzzyMatchV2` (#4608)44450.67.046------47- Added `--freeze-left=N` option to keep the leftmost N columns always visible.
3113 unmodified lines