diff --git a/CHANGELOG.md b/CHANGELOG.md index 283446d5..5b4d55eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ CHANGELOG ``` - To disable multi-line display, use `--no-multi-line` - The default `--pointer` and `--marker` have been changed from `>` to Unicode bar characters as they look better with multi-line items +- Added `--marker-multi-line` to customize the select marker for multi-line entries with the default set to `╻┃╹` + ``` + ╻First line + ┃... + ╹Last line + ``` - Native `--tmux` integration to replace fzf-tmux script ```sh # --tmux [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]] diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index ef88d69a..d1c2cdc0 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -455,6 +455,10 @@ Pointer to the current line (default: '▌' or '>' depending on \fB--no-unicode\ .BI "--marker=" "STR" Multi-select marker (default: '┃' or '>' depending on \fB--no-unicode\fR) .TP +.BI "--marker-multi-line=" "STR" +Multi-select marker for multi-line entries. 3 elements for top, middle, and bottom. +(default: '╻┃╹' or '.|'' depending on \fB--no-unicode\fR) +.TP .BI "--header=" "STR" The given string will be printed as the sticky header. The lines are displayed in the given order from top to bottom regardless of \fB--layout\fR option, and diff --git a/src/options.go b/src/options.go index c5b939e4..461db313 100644 --- a/src/options.go +++ b/src/options.go @@ -27,136 +27,137 @@ Author: Junegunn Choi Usage: fzf [options] Search - -x, --extended Extended-search mode - (enabled by default; +x or --no-extended to disable) - -e, --exact Enable Exact-match - -i, --ignore-case Case-insensitive match (default: smart-case match) - +i, --no-ignore-case Case-sensitive match - --scheme=SCHEME Scoring scheme [default|path|history] - --literal Do not normalize latin script letters before matching - -n, --nth=N[,..] Comma-separated list of field index expressions - for limiting search scope. Each can be a non-zero - integer or a range expression ([BEGIN]..[END]). - --with-nth=N[,..] Transform the presentation of each line using - field index expressions - -d, --delimiter=STR Field delimiter regex (default: AWK-style) - +s, --no-sort Do not sort the result - --track Track the current selection when the result is updated - --tac Reverse the order of the input - --disabled Do not perform search - --tiebreak=CRI[,..] Comma-separated list of sort criteria to apply - when the scores are tied [length|chunk|begin|end|index] - (default: length) - + -x, --extended Extended-search mode + (enabled by default; +x or --no-extended to disable) + -e, --exact Enable Exact-match + -i, --ignore-case Case-insensitive match (default: smart-case match) + +i, --no-ignore-case Case-sensitive match + --scheme=SCHEME Scoring scheme [default|path|history] + --literal Do not normalize latin script letters before matching + -n, --nth=N[,..] Comma-separated list of field index expressions + for limiting search scope. Each can be a non-zero + integer or a range expression ([BEGIN]..[END]). + --with-nth=N[,..] Transform the presentation of each line using + field index expressions + -d, --delimiter=STR Field delimiter regex (default: AWK-style) + +s, --no-sort Do not sort the result + --track Track the current selection when the result is updated + --tac Reverse the order of the input + --disabled Do not perform search + --tiebreak=CRI[,..] Comma-separated list of sort criteria to apply + when the scores are tied [length|chunk|begin|end|index] + (default: length) Interface - -m, --multi[=MAX] Enable multi-select with tab/shift-tab - --no-mouse Disable mouse - --bind=KEYBINDS Custom key bindings. Refer to the man page. - --cycle Enable cyclic scroll - --no-multi-line Disable multi-line display of items when using --read0 - --keep-right Keep the right end of the line visible on overflow - --scroll-off=LINES Number of screen lines to keep above or below when - scrolling to the top or to the bottom (default: 0) - --no-hscroll Disable horizontal scroll - --hscroll-off=COLS Number of screen columns to keep to the right of the - highlighted substring (default: 10) - --filepath-word Make word-wise movements respect path separators - --jump-labels=CHARS Label characters for jump mode + -m, --multi[=MAX] Enable multi-select with tab/shift-tab + --no-mouse Disable mouse + --bind=KEYBINDS Custom key bindings. Refer to the man page. + --cycle Enable cyclic scroll + --no-multi-line Disable multi-line display of items when using --read0 + --keep-right Keep the right end of the line visible on overflow + --scroll-off=LINES Number of screen lines to keep above or below when + scrolling to the top or to the bottom (default: 0) + --no-hscroll Disable horizontal scroll + --hscroll-off=COLS Number of screen columns to keep to the right of the + highlighted substring (default: 10) + --filepath-word Make word-wise movements respect path separators + --jump-labels=CHARS Label characters for jump mode Layout - --height=[~]HEIGHT[%] Display fzf window below the cursor with the given - height instead of using fullscreen. - A negative value is calculated as the terminal height - minus the given value. - If prefixed with '~', fzf will determine the height - according to the input size. - --min-height=HEIGHT Minimum height when --height is given in percent - (default: 10) - --tmux=OPTS Start fzf in a tmux popup (requires tmux 3.3+) - [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]] - --layout=LAYOUT Choose layout: [default|reverse|reverse-list] - --border[=STYLE] Draw border around the finder - [rounded|sharp|bold|block|thinblock|double|horizontal|vertical| - top|bottom|left|right|none] (default: rounded) - --border-label=LABEL Label to print on the border - --border-label-pos=COL Position of the border label - [POSITIVE_INTEGER: columns from left| - NEGATIVE_INTEGER: columns from right][:bottom] - (default: 0 or center) - --margin=MARGIN Screen margin (TRBL | TB,RL | T,RL,B | T,R,B,L) - --padding=PADDING Padding inside border (TRBL | TB,RL | T,RL,B | T,R,B,L) - --info=STYLE Finder info style - [default|right|hidden|inline[-right][:PREFIX]] - --separator=STR String to form horizontal separator on info line - --no-separator Hide info line separator - --scrollbar[=C1[C2]] Scrollbar character(s) (each for main and preview window) - --no-scrollbar Hide scrollbar - --prompt=STR Input prompt (default: '> ') - --pointer=STR Pointer to the current line (default: '▌' or '>') - --marker=STR Multi-select marker (default: '┃' or '>') - --header=STR String to print as header - --header-lines=N The first N lines of the input are treated as header - --header-first Print header before the prompt line - --ellipsis=STR Ellipsis to show when line is truncated (default: '..') + --height=[~]HEIGHT[%] Display fzf window below the cursor with the given + height instead of using fullscreen. + A negative value is calculated as the terminal height + minus the given value. + If prefixed with '~', fzf will determine the height + according to the input size. + --min-height=HEIGHT Minimum height when --height is given in percent + (default: 10) + --tmux=OPTS Start fzf in a tmux popup (requires tmux 3.3+) + [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]] + --layout=LAYOUT Choose layout: [default|reverse|reverse-list] + --border[=STYLE] Draw border around the finder + [rounded|sharp|bold|block|thinblock|double|horizontal|vertical| + top|bottom|left|right|none] (default: rounded) + --border-label=LABEL Label to print on the border + --border-label-pos=COL Position of the border label + [POSITIVE_INTEGER: columns from left| + NEGATIVE_INTEGER: columns from right][:bottom] + (default: 0 or center) + --margin=MARGIN Screen margin (TRBL | TB,RL | T,RL,B | T,R,B,L) + --padding=PADDING Padding inside border (TRBL | TB,RL | T,RL,B | T,R,B,L) + --info=STYLE Finder info style + [default|right|hidden|inline[-right][:PREFIX]] + --separator=STR String to form horizontal separator on info line + --no-separator Hide info line separator + --scrollbar[=C1[C2]] Scrollbar character(s) (each for main and preview window) + --no-scrollbar Hide scrollbar + --prompt=STR Input prompt (default: '> ') + --pointer=STR Pointer to the current line (default: '▌' or '>') + --marker=STR Multi-select marker (default: '┃' or '>') + --marker-multi-line=STR Multi-select marker for multi-line entries; + 3 elements for top, middle, and bottom (default: '╻┃╹') + --header=STR String to print as header + --header-lines=N The first N lines of the input are treated as header + --header-first Print header before the prompt line + --ellipsis=STR Ellipsis to show when line is truncated (default: '..') Display - --ansi Enable processing of ANSI color codes - --tabstop=SPACES Number of spaces for a tab character (default: 8) - --color=COLSPEC Base scheme (dark|light|16|bw) and/or custom colors - --highlight-line Highlight the whole current line - --no-bold Do not use bold text + --ansi Enable processing of ANSI color codes + --tabstop=SPACES Number of spaces for a tab character (default: 8) + --color=COLSPEC Base scheme (dark|light|16|bw) and/or custom colors + --highlight-line Highlight the whole current line + --no-bold Do not use bold text History - --history=FILE History file - --history-size=N Maximum number of history entries (default: 1000) + --history=FILE History file + --history-size=N Maximum number of history entries (default: 1000) Preview - --preview=COMMAND Command to preview highlighted line ({}) - --preview-window=OPT Preview window layout (default: right:50%) - [up|down|left|right][,SIZE[%]] - [,[no]wrap][,[no]cycle][,[no]follow][,[no]hidden] - [,border-BORDER_OPT] - [,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES] - [,default][, markerLen { + padded := *opts.Marker + " " + opts.Marker = &padded + } else if markerMultiLen < markerLen { + for idx := range opts.MarkerMulti { + opts.MarkerMulti[idx] += " " + } } // Default actions for CTRL-N / CTRL-P when --history is set diff --git a/src/terminal.go b/src/terminal.go index e2ea49fd..d5ad72a5 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -177,6 +177,15 @@ type fitpad struct { type labelPrinter func(tui.Window, int) +type markerClass int + +const ( + markerSingle markerClass = iota + markerTop + markerMiddle + markerBottom +) + type StatusItem struct { Index int `json:"index"` Text string `json:"text"` @@ -218,6 +227,7 @@ type Terminal struct { marker string markerLen int markerEmpty string + markerMultiLine [3]string queryLen [2]int layout layoutType fullscreen bool @@ -755,6 +765,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor pointerLen: uniseg.StringWidth(*opts.Pointer), marker: *opts.Marker, markerLen: uniseg.StringWidth(*opts.Marker), + markerMultiLine: opts.MarkerMulti, wordRubout: wordRubout, wordNext: wordNext, cx: len(input), @@ -1084,7 +1095,8 @@ func (t *Terminal) avgNumLines() int { offset := util.Max(0, util.Min(t.offset, total-maxItems-1)) for idx := 0; idx < maxItems && idx+offset < total; idx++ { item := t.merger.Get(idx + offset) - numLines += item.item.text.NumLines(maxItems) + lines, _ := item.item.text.NumLines(maxItems) + numLines += lines count++ } if count == 0 { @@ -1884,7 +1896,7 @@ func (t *Terminal) printHeader() { t.printHighlighted(Result{item: item}, tui.ColHeader, tui.ColHeader, false, false, line, line, true, - func(int) { t.window.Print(" ") }, nil) + func(markerClass) { t.window.Print(" ") }, nil) } } @@ -1964,7 +1976,8 @@ func (t *Terminal) printItem(result Result, line int, maxLine int, index int, cu if !t.multiLine { return line } - return line + item.text.NumLines(maxLine-line+1) - 1 + lines, _ := item.text.NumLines(maxLine - line + 1) + return line + lines - 1 } maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1) @@ -1992,29 +2005,41 @@ func (t *Terminal) printItem(result Result, line int, maxLine int, index int, cu } var finalLineNum int + markerFor := func(markerClass markerClass) string { + marker := t.marker + switch markerClass { + case markerTop: + marker = t.markerMultiLine[0] + case markerMiddle: + marker = t.markerMultiLine[1] + case markerBottom: + marker = t.markerMultiLine[2] + } + return marker + } if current { - preTask := func(lineOffset int) { + preTask := func(marker markerClass) { if len(label) == 0 { t.window.CPrint(tui.ColCurrentCursorEmpty, t.pointerEmpty) } else { t.window.CPrint(tui.ColCurrentCursor, label) } if selected { - t.window.CPrint(tui.ColCurrentMarker, t.marker) + t.window.CPrint(tui.ColCurrentMarker, markerFor(marker)) } else { t.window.CPrint(tui.ColCurrentSelectedEmpty, t.markerEmpty) } } finalLineNum = t.printHighlighted(result, tui.ColCurrent, tui.ColCurrentMatch, true, true, line, maxLine, forceRedraw, preTask, postTask) } else { - preTask := func(lineOffset int) { + preTask := func(marker markerClass) { if len(label) == 0 { t.window.CPrint(tui.ColCursorEmpty, t.pointerEmpty) } else { t.window.CPrint(tui.ColCursor, label) } if selected { - t.window.CPrint(tui.ColMarker, t.marker) + t.window.CPrint(tui.ColMarker, markerFor(marker)) } else { t.window.Print(t.markerEmpty) } @@ -2070,7 +2095,7 @@ func (t *Terminal) overflow(runes []rune, max int) bool { return t.displayWidthWithLimit(runes, 0, max) > max } -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 { +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 { var displayWidth int item := result.item matchOffsets := []Offset{} @@ -2096,12 +2121,16 @@ func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMat finalLineNum := lineNum numItemLines := 1 cutoff := 0 - if t.multiLine && t.layout == layoutDefault { + overflow := false + topCutoff := false + if t.multiLine { maxLines := maxLineNum - lineNum + 1 - numItemLines = item.text.NumLines(maxLines) + numItemLines, overflow = item.text.NumLines(maxLines) // Cut off the upper lines in the 'default' layout - if !current && maxLines == numItemLines { - cutoff = item.text.NumLines(math.MaxInt32) - maxLines + if t.layout == layoutDefault && !current && maxLines == numItemLines && overflow { + actualLines, _ := item.text.NumLines(math.MaxInt32) + cutoff = actualLines - maxLines + topCutoff = true } } for lineOffset := 0; from <= len(text) && (lineNum <= maxLineNum || maxLineNum == 0); lineOffset++ { @@ -2151,7 +2180,34 @@ func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMat t.move(actualLineNum, 0, forceRedraw) if preTask != nil { - preTask(lineOffset) + var marker markerClass + if numItemLines == 1 { + if !overflow { + marker = markerSingle + } else if topCutoff { + marker = markerBottom + } else { + marker = markerTop + } + } else { + if lineOffset == 0 { // First line + if topCutoff { + marker = markerMiddle + } else { + marker = markerTop + } + } else if lineOffset == numItemLines-1 { // Last line + if topCutoff || !overflow { + marker = markerBottom + } else { + marker = markerMiddle + } + } else { + marker = markerMiddle + } + } + + preTask(marker) } maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1) @@ -4502,7 +4558,7 @@ func (t *Terminal) constrain() { linesSum := 0 add := func(i int) bool { - lines := t.merger.Get(i).item.text.NumLines(numItems - linesSum) + lines, _ := t.merger.Get(i).item.text.NumLines(numItems - linesSum) linesSum += lines if linesSum >= numItems { if numItemsFound == 0 { @@ -4547,13 +4603,14 @@ func (t *Terminal) constrain() { numItems := t.merger.Length() itemLines := 1 if t.multiLine && t.cy < numItems { - itemLines = t.merger.Get(t.cy).item.text.NumLines(maxLines) + itemLines, _ = t.merger.Get(t.cy).item.text.NumLines(maxLines) } linesBefore := t.cy - newOffset if t.multiLine { linesBefore = 0 for i := newOffset; i < t.cy && i < numItems; i++ { - linesBefore += t.merger.Get(i).item.text.NumLines(maxLines - linesBefore - itemLines) + lines, _ := t.merger.Get(i).item.text.NumLines(maxLines - linesBefore - itemLines) + linesBefore += lines } } linesAfter := maxLines - (linesBefore + itemLines) diff --git a/src/util/chars.go b/src/util/chars.go index 7c706ae8..82773f40 100644 --- a/src/util/chars.go +++ b/src/util/chars.go @@ -75,18 +75,18 @@ func (chars *Chars) Bytes() []byte { return chars.slice } -func (chars *Chars) NumLines(atMost int) int { +func (chars *Chars) NumLines(atMost int) (int, bool) { lines := 1 if runes := chars.optionalRunes(); runes != nil { for _, r := range runes { if r == '\n' { lines++ } - if lines >= atMost { - return atMost + if lines > atMost { + return atMost, true } } - return lines + return lines, false } for idx := 0; idx < len(chars.slice); idx++ { @@ -97,11 +97,11 @@ func (chars *Chars) NumLines(atMost int) int { idx += found lines++ - if lines >= atMost { - return atMost + if lines > atMost { + return atMost, true } } - return lines + return lines, false } func (chars *Chars) optionalRunes() []rune { diff --git a/test/test_go.rb b/test/test_go.rb index 06c3aefd..dfb338e8 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -2752,8 +2752,9 @@ class TestGoFZF < TestBase def assert_block(expected, lines) cols = expected.lines.map(&:chomp).map(&:length).max - actual = lines.reverse.take(expected.lines.length).reverse.map { _1[0, cols].rstrip + "\n" }.join - assert_equal_org expected, actual + top = lines.take(expected.lines.length).map { _1[0, cols].rstrip + "\n" }.join + bottom = lines.reverse.take(expected.lines.length).reverse.map { _1[0, cols].rstrip + "\n" }.join + assert_includes [top, bottom], expected end def test_height_range_fit @@ -3268,6 +3269,55 @@ class TestGoFZF < TestBase tmux.send_keys '99' tmux.until { |lines| assert(lines.any? { |line| line.include?('0 / 0') }) } end + + def test_fzf_multi_line + tmux.send_keys %[(echo -en '0\\0'; echo -en '1\\n2\\0'; seq 1000) | fzf --read0 --multi --bind load:select-all --border rounded], :Enter + block = <<~BLOCK + │ ┃998 + │ ┃999 + │ ┃1000 + │ ╹ + │ ╻1 + │ ╹2 + │ >>0 + │ 3/3 (3) + │ > + ╰─────────── + BLOCK + tmux.until { assert_block(block, _1) } + tmux.send_keys :Up, :Up + block = <<~BLOCK + ╭─────── + │ >╻1 + │ >┃2 + │ >┃3 + BLOCK + tmux.until { assert_block(block, _1) } + + block = <<~BLOCK + │ >┃ + │ + │ > + ╰─── + BLOCK + tmux.until { assert_block(block, _1) } + end + + def test_fzf_multi_line_reverse + tmux.send_keys %[(echo -en '0\\0'; echo -en '1\\n2\\0'; seq 1000) | fzf --read0 --multi --bind load:select-all --border rounded --reverse], :Enter + block = <<~BLOCK + ╭─────────── + │ > + │ 3/3 (3) + │ >>0 + │ ╻1 + │ ╹2 + │ ╻1 + │ ┃2 + │ ┃3 + BLOCK + tmux.until { assert_block(block, _1) } + end end module TestShell