Skip to content
Myr

Break preview lines on IND to support chafa in preview window on tmux

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

chafa ends each row of a Kitty Unicode placeholder image with CUB + IND instead of a newline, so that an image drawn at a column offset survives ONLCR, which would rewrite the newline as CR NL and pull the cursor back to column 0. Both sequences were dropped, so the image arrived as a single line and was re-wrapped, shifting every row after the first by the width of the wrap sign. Without wrapping it was truncated to its first row. The column is tracked across the breaks and restored with padding, so an image indented by the preview command keeps its indent. Fix #4885

authored by Junegunn Choi · committed by Junegunn Choi · parent 3337be9 · Browse files · 3 files+123 added−1 removed
This commit

State

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

Diff stats

3 files+123 added−1 removed

  • other:+123 added−1 removed

3 files+123 added−1 removed

modified:src/terminal_test.go+70 added
849 unmodified lines
850 if len(result) != 2 || result[0] != "hello" || result[1] != "world" {851 t.Errorf("Tab wrap: %q", result)852 }853}854855func TestSplitOnIND(t *testing.T) {856 term := &Terminal{tabstop: 8}857 for _, tc := range []struct {858 name string859 line string860 want []string861 }{862 {863 // Nothing to do, so the caller keeps the line as it read it864 name: "no IND",865 line: "foo\n",866 want: nil,867 },868 {869 // chafa: CUB returns to the column the row started on870 name: "rows at column 0",871 line: "AAA\x1b[3D\x1bDBBB\x1b[3D\x1bDCCC\n",872 want: []string{"AAA\x1b[3D\n", "BBB\x1b[3D\n", "CCC\n"},873 },874 {875 // 'printf " "; chafa ...'876 name: "indented rows",877 line: " AAA\x1b[3D\x1bDBBB\x1b[3D\x1bDCCC\n",878 want: []string{" AAA\x1b[3D\n", " BBB\x1b[3D\n", " CCC\n"},879 },880 {881 name: "tab indent expands to the tab stop",882 line: "\tAAA\x1b[3D\x1bDBBB\x1b[3D\x1bDCCC\n",883 want: []string{"\tAAA\x1b[3D\n", " BBB\x1b[3D\n", " CCC\n"},884 },885 {886 // IND on its own keeps the column887 name: "bare IND",888 line: "AB\x1bDCD\n",889 want: []string{"AB\n", " CD\n"},890 },891 {892 name: "CUB without a parameter moves back one",893 line: "AB\x1b[D\x1bDCD\n",894 want: []string{"AB\x1b[D\n", " CD\n"},895 },896 {897 name: "CUB past the left edge is clamped",898 line: "AB\x1b[9D\x1bDCD\n",899 want: []string{"AB\x1b[9D\n", "CD\n"},900 },901 {902 name: "SGR codes take no column",903 line: "\x1b[31mAB\x1b[m\x1b[2D\x1bDCD\n",904 want: []string{"\x1b[31mAB\x1b[m\x1b[2D\n", "CD\n"},905 },906 {907 name: "pass-throughs take no column",908 line: "\x1b_Ga=T,c=2,r=1\x1b\\AB\x1b[2D\x1bDCD\n",909 want: []string{"\x1b_Ga=T,c=2,r=1\x1b\\AB\x1b[2D\n", "CD\n"},910 },911 } {912 got := term.splitOnIND(tc.line)913 if len(got) != len(tc.want) {914 t.Errorf("%s: got %q, want %q", tc.name, got, tc.want)915 continue916 }917 for idx, line := range got {918 if line != tc.want[idx] {919 t.Errorf("%s: line %d: got %q, want %q", tc.name, idx, line, tc.want[idx])920 }921 }922 }923}
modified:src/terminal.go+49 added-1 removed
56 unmodified lines
57var offsetTrimCharsRegex *regexp.Regexp58var passThroughBeginRegex *regexp.Regexp59var passThroughEndTmuxRegex *regexp.Regexp60var sixelBeginRegex *regexp.Regexp61var cursorBackRegex *regexp.Regexp62var ttyin *os.File6364var inTmux = len(os.Getenv("TMUX")) > 065
30 unmodified lines
96 */97 passThroughBeginRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b|\x1b(_G|P[0-9;]*q)|\x1b]1337;`)98 passThroughEndTmuxRegex = regexp.MustCompile(`[^\x1b]\x1b\\`)99 sixelBeginRegex = regexp.MustCompile(`^\x1bP[0-9;]*q`)100101 // CUB right before an IND, used to return to the column a row started on102 cursorBackRegex = regexp.MustCompile(`\x1b\[([0-9]*)D$`)103}104105type jumpMode int106
2172 unmodified lines
2279 width, _ := util.RunesWidth([]rune(str), prefixWidth, t.tabstop, math.MaxInt32)2280 return width2281}22822283// displayWidthWithoutEscapes is displayWidthWithPrefix for a string that may2284// still carry pass-throughs and ANSI codes, neither of which take any column.2285func (t *Terminal) displayWidthWithoutEscapes(str string, prefixWidth int) int {2286 _, text := extractPassThroughs(str)2287 stripped, _, _ := extractColor(text, nil, nil)2288 return t.displayWidthWithPrefix(stripped, prefixWidth)2289}22902291const (2292 minWidth = 42293 minHeight = 32294)
2565 unmodified lines
48604861 return passThroughs, transformed4862}48634864// splitOnIND breaks a preview line on IND (ESC D), which moves the cursor4865// down one line, keeping the column. A program drawing at a column offset ends4866// its rows with IND instead of a newline, because ONLCR would rewrite a newline4867// as CR NL and snap the cursor to column 0. chafa does this for Kitty Unicode4868// placeholders, and without the break the whole image collapses into a single4869// line.4870//4871// The column is tracked and re-created with padding so that an indented image4872// keeps its indent, and a CUB right before the IND is subtracted, which is how4873// chafa returns to the column its rows start on.4874func (t *Terminal) splitOnIND(line string) []string {4875 chunks := strings.Split(line, "\x1bD")4876 if len(chunks) == 1 {4877 return nil4878 }48794880 lines := make([]string, 0, len(chunks))4881 col := 04882 for _, chunk := range chunks[:len(chunks)-1] {4883 lines = append(lines, strings.Repeat(" ", col)+chunk+"\n")4884 col += t.displayWidthWithoutEscapes(chunk, col)4885 if match := cursorBackRegex.FindStringSubmatch(chunk); match != nil {4886 back := 14887 if len(match[1]) > 0 {4888 back, _ = strconv.Atoi(match[1])4889 }4890 col = max(0, col-back)4891 }4892 }4893 return append(lines, strings.Repeat(" ", col)+chunks[len(chunks)-1])4894}48954896// followOffset computes the correct content-line offset for follow mode,4897// accounting for line wrapping in the preview window.4898func (t *Terminal) followOffset() int {4899 lines := t.previewer.lines
1564 unmodified lines
6464 line = line[clearIndex+len(clearCode):]6465 version--6466 offset = 06467 }6468 if split := t.splitOnIND(line); split != nil {6469 lines = append(lines, split...)6470 } else {6424 lines = append(lines, line)6471 lines = append(lines, line)6472 }6473 }6474 if err != nil {6475 t.reqBox.Set(reqPreviewDisplay, previewResult{version, lines, offset, ""})6476 rendered.Set(true)
2361 unmodified lines
modified:CHANGELOG.md+4 added
1CHANGELOG2=========340.74.35------6- Fixed an image from a preview command being torn apart when its rows are separated by IND instead of newlines, as `chafa` does under tmux (#4885)780.74.29------10- Performance optimizations for short queries11 - Short queries scan the largest candidate sets, and the first keystroke scans the whole input
3407 unmodified lines