From 50020e807a42db5dcf8e3f9d74108a53b5c3c133 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 20:05:55 +0000 Subject: [PATCH] termtest: emulate the escape sequences bubbletea v2 emits bubbletea v2's cell renderer moves and edits cells with REP, ECH, ICH, DCH, IRM, IL/DL, SU/SD, DECSTBM, RI and tab stops. The emulator now handles them, ignores prefixed and intermediate CSIs (kitty keyboard, modifyOtherKeys, DECRQM), and maps LF to column 0 the way v2 expects when its input is not a TTY (off Windows). Scrolls and cell shifts keep the styles grid aligned with the text. Esc disambiguation is 100 ms. v2's reader holds a lone Esc for ultraviolet's 50 ms timeout, which starts when the byte is read. No v1 golden changes. Co-authored-by: Isaac --- libs/cmdio/cmdiotest/termtest/emulator.go | 223 +++++++++++++++--- .../cmdio/cmdiotest/termtest/emulator_test.go | 187 ++++++++++++++- libs/cmdio/cmdiotest/termtest/termtest.go | 18 +- 3 files changed, 388 insertions(+), 40 deletions(-) diff --git a/libs/cmdio/cmdiotest/termtest/emulator.go b/libs/cmdio/cmdiotest/termtest/emulator.go index 9b3b22efe7a..330f059be9b 100644 --- a/libs/cmdio/cmdiotest/termtest/emulator.go +++ b/libs/cmdio/cmdiotest/termtest/emulator.go @@ -1,6 +1,7 @@ package termtest import ( + "runtime" "strings" "github.com/charmbracelet/x/ansi" @@ -10,18 +11,17 @@ import ( // byte stream bubbletea writes to its output into a 2D screen of plain text // so tests can match against `screen text` goldens. // -// Scope is deliberately narrow: bubbletea's stock renderer (which is what -// the cmdio prompt/select models run through) emits a small subset of CSI -// sequences — cursor moves, erase-line, set/reset-mode for cursor -// visibility — plus printable runes, CR, LF, and BS. Alt-screen switches, -// scroll regions, charsets, DCS, and friends are recognised by the -// underlying ansi parser but intentionally ignored: they do not affect cell -// text. SGR styling, DEC private modes, keyboard protocol switches and -// terminal queries are recorded alongside the text for the styled, modes -// and queries goldens (see observe.go), without changing what String -// returns. Cursor movement covers the usual CUU/CUD/CUP family and the -// unprefixed SCO save/restore pair (CSI s and CSI u); prefixed forms such as -// the kitty keyboard CSI ? u and CSI > n u do not move the cursor. +// Scope is deliberately narrow. bubbletea v1's standard renderer only moves +// the cursor with CUU, EL, CUP and CRLF (its IL and DECSTBM live in the +// deprecated insertTop, which cmdio does not use). The other cell and cursor +// sequences handled here (REP, ECH, ICH, DCH, IRM, IL, DL, SU, SD, DECSTBM, +// RI, CHT, CBT) are the ones the cell renderer of charm.land/bubbletea/v2 +// v2.1.1 emits through github.com/charmbracelet/ultraviolet +// (terminal_renderer.go, terminal_renderer_hardscroll.go). SGR styling, +// alt-screen switches, charsets and OSC are recognised by the underlying ansi +// parser but do not affect cell text. Styles, modes and queries are recorded +// alongside the text for the styled, modes and queries goldens (see +// observe.go), without changing what String returns. type emulator struct { width, height int grid [][]rune @@ -30,8 +30,23 @@ type emulator struct { // Saved cursor (DECSC/DECRC; ESC 7 / ESC 8). savedRow, savedCol int - // mapNewline makes LF also return to column 0, like a cooked tty's - // ONLCR. Off by default: bubbletea v1 writes CRLF itself. + // Scroll region rows (DECSTBM), inclusive. + top, bottom int + + // insert is IRM (CSI 4 h): printed runes shift the rest of the line right. + insert bool + + // lastRune is the most recently printed rune, repeated by REP. Zero + // until something is printed. + lastRune rune + + // mapNewline makes LF also return to column 0, like a cooked tty's ONLCR + // output translation. bubbletea v2 assumes that mapping whenever its input + // is not a TTY, except on Windows (charm.land/bubbletea/v2 v2.1.1 + // tea.go:1107 "mapNl := runtime.GOOS != \"windows\" && p.ttyInput == nil"; + // ultraviolet terminal_renderer.go:1645-1649). termtest feeds input + // through a pipe, so it follows the same rule. v1 always writes CRLF, so + // the setting does not affect it. mapNewline bool // Observation state for the styled, modes and queries goldens. None of @@ -48,7 +63,7 @@ type emulator struct { } func newEmulator(width, height int) *emulator { - e := &emulator{width: width, height: height} + e := &emulator{width: width, height: height, mapNewline: runtime.GOOS != "windows"} e.reset() e.parser = ansi.NewParser() e.parser.SetHandler(ansi.Handler{ @@ -77,6 +92,9 @@ func (e *emulator) reset() { e.privateModes = map[int]bool{} e.row, e.col = 0, 0 e.savedRow, e.savedCol = 0, 0 + e.top, e.bottom = 0, e.height-1 + e.insert = false + e.lastRune = 0 } // Write feeds bytes into the parser. @@ -107,9 +125,13 @@ func (e *emulator) handlePrint(r rune) { return } if e.inBounds() { + if e.insert { + e.insertCells(1) + } e.grid[e.row][e.col] = r e.styles[e.row][e.col] = e.pen } + e.lastRune = r e.col++ } @@ -123,10 +145,8 @@ func (e *emulator) handleControl(b byte) { case 0x09: // HT: next tab stop (every 8 columns). stop := ((e.col / 8) + 1) * 8 e.col = min(stop, e.width-1) - case 0x0a: // LF: move down one line, no scroll. - if e.row+1 < e.height { - e.row++ - } + case 0x0a: // LF: move down a line (scrolling at the bottom margin), then to column 0 if mapped. + e.index() if e.mapNewline { e.col = 0 } @@ -140,9 +160,78 @@ func (e *emulator) handleControl(b byte) { // the mode and query sequences do not move the cursor or change cell text; // observeCsi records the ones the goldens pin. func (e *emulator) handleCsi(cmd ansi.Cmd, params ansi.Params) { + // Prefixed and intermediate CSIs are terminal queries and mode switches + // (DEC private modes, DECRQM "CSI ? 2026 $ p", kitty keyboard + // "CSI > 1 u" / "CSI < u" / "CSI ? u", modifyOtherKeys "CSI > 4;2 m"), + // never cursor or cell operations. Without this check "CSI > 1 u" would + // be read as SCO restore-cursor. e.observeCsi(cmd, params) + if cmd.Prefix() != 0 || cmd.Intermediate() != 0 { + return + } n, _, _ := params.Param(0, 1) switch cmd.Final() { + case 'b': // REP: repeat the last printed rune n times. + // A real terminal ignores REP before anything has been printed. + if e.lastRune == 0 { + break + } + for range n { + e.handlePrint(e.lastRune) + } + case 'X': // ECH: erase n cells from the cursor without moving it. + if e.inBounds() { + for i := e.col; i < min(e.col+n, e.width); i++ { + e.clearCell(e.row, i) + } + } + case '@': // ICH: insert n blank cells at the cursor. + if e.inBounds() { + e.insertCells(n) + } + case 'P': // DCH: delete n cells at the cursor, shifting the rest left. + if e.inBounds() { + e.deleteCells(n) + } + case 'L': // IL: insert n blank lines at the cursor row. + if e.row >= e.top && e.row <= e.bottom { + e.scrollDown(e.row, n) + e.col = 0 + } + case 'M': // DL: delete n lines at the cursor row. + if e.row >= e.top && e.row <= e.bottom { + e.scrollUp(e.row, n) + e.col = 0 + } + case 'S': // SU: scroll the region up n lines. + e.scrollUp(e.top, n) + case 'T': // SD: scroll the region down n lines. + // The five-parameter form is xterm mouse highlight tracking. + if len(params) <= 1 { + e.scrollDown(e.top, n) + } + case 'r': // DECSTBM: set scroll region (1-based, inclusive); homes the cursor. + top, _, _ := params.Param(0, 1) + bottom, _, _ := params.Param(1, e.height) + top, bottom = clamp(top-1, 0, e.height-1), clamp(bottom-1, 0, e.height-1) + if top < bottom { + e.top, e.bottom = top, bottom + e.row, e.col = 0, 0 + } + case 'h', 'l': // SM / RM: only IRM (mode 4) affects cell text. + for i := range params { + if params[i].Param(0) == 4 { + e.insert = cmd.Final() == 'h' + } + } + case 'I': // CHT: forward n tab stops. + for range n { + e.handleControl(0x09) + } + case 'Z': // CBT: back n tab stops. + for range n { + e.col = max(((e.col+7)/8-1)*8, 0) + } case 'A': // CUU: cursor up e.row = max(e.row-n, 0) case 'B', 'e': // CUD / VPR: cursor down @@ -173,22 +262,26 @@ func (e *emulator) handleCsi(cmd ansi.Cmd, params ansi.Params) { n, _, _ := params.Param(0, 0) e.eraseLine(n) case 's': // SCO save cursor - if cmd.Prefix() == 0 { - e.savedRow, e.savedCol = e.row, e.col - } - case 'u': // SCO restore cursor; prefixed forms are kitty keyboard sequences. - if cmd.Prefix() == 0 { - e.row, e.col = e.savedRow, e.savedCol - } - // All other CSI commands (SGR 'm', mode set/reset 'h'/'l', cursor - // show/hide '?25h/l', etc.) are no-ops for cell text. + e.savedRow, e.savedCol = e.row, e.col + case 'u': // SCO restore cursor + e.row, e.col = e.savedRow, e.savedCol + // All other CSI commands (SGR 'm', etc.) are no-ops for cell text. } } -// handleEsc handles ESC sequences: only the C1-equivalent cursor save/restore -// (ESC 7 / ESC 8) matter for cell positions. +// handleEsc handles the ESC sequences that matter for cell positions: +// cursor save/restore (ESC 7 / ESC 8) and reverse index (ESC M). func (e *emulator) handleEsc(cmd ansi.Cmd) { + if cmd.Intermediate() != 0 { + return // charset designations etc. + } switch cmd.Final() { + case 'M': // RI: up one line, scrolling down at the top margin. + if e.row == e.top { + e.scrollDown(e.top, 1) + } else if e.row > 0 { + e.row-- + } case '7': // DECSC: save cursor e.savedRow, e.savedCol = e.row, e.col case '8': // DECRC: restore cursor @@ -255,6 +348,76 @@ func (e *emulator) clearCell(r, c int) { e.styles[r][c] = cellStyle{} } +// index moves the cursor down one row, scrolling the region up when the +// cursor sits on its bottom margin. +func (e *emulator) index() { + switch { + case e.row == e.bottom: + e.scrollUp(e.top, 1) + case e.row+1 < e.height: + e.row++ + } +} + +// insertCells shifts the cells from the cursor right by n, dropping cells +// pushed past the right margin, and blanks the gap. +func (e *emulator) insertCells(n int) { + line, styles := e.grid[e.row], e.styles[e.row] + n = min(n, e.width-e.col) + copy(line[e.col+n:], line[e.col:e.width-n]) + copy(styles[e.col+n:], styles[e.col:e.width-n]) + for i := e.col; i < e.col+n; i++ { + e.clearCell(e.row, i) + } +} + +// deleteCells removes n cells at the cursor, shifting the rest of the line +// left and blanking the right margin. +func (e *emulator) deleteCells(n int) { + line, styles := e.grid[e.row], e.styles[e.row] + n = min(n, e.width-e.col) + copy(line[e.col:], line[e.col+n:]) + copy(styles[e.col:], styles[e.col+n:]) + for i := e.width - n; i < e.width; i++ { + e.clearCell(e.row, i) + } +} + +// scrollUp removes n lines at row from, shifting the lines below it up to +// the bottom margin and blanking the bottom n lines of the region. +func (e *emulator) scrollUp(from, n int) { + n = min(n, e.bottom-from+1) + region, styles := e.grid[from:e.bottom+1], e.styles[from:e.bottom+1] + copy(region, region[n:]) + copy(styles, styles[n:]) + for i := len(region) - n; i < len(region); i++ { + region[i] = blankRow(e.width) + styles[i] = make([]cellStyle, e.width) + } +} + +// scrollDown inserts n blank lines at row from, shifting the lines below it +// down and dropping those pushed past the bottom margin. +func (e *emulator) scrollDown(from, n int) { + n = min(n, e.bottom-from+1) + region, styles := e.grid[from:e.bottom+1], e.styles[from:e.bottom+1] + copy(region[n:], region) + copy(styles[n:], styles) + for i := range n { + region[i] = blankRow(e.width) + styles[i] = make([]cellStyle, e.width) + } +} + +// blankRow returns a row of width spaces. +func blankRow(width int) []rune { + row := make([]rune, width) + for i := range row { + row[i] = ' ' + } + return row +} + func (e *emulator) inBounds() bool { return e.row >= 0 && e.row < e.height && e.col >= 0 && e.col < e.width } diff --git a/libs/cmdio/cmdiotest/termtest/emulator_test.go b/libs/cmdio/cmdiotest/termtest/emulator_test.go index 93f7464dda5..288855be2a4 100644 --- a/libs/cmdio/cmdiotest/termtest/emulator_test.go +++ b/libs/cmdio/cmdiotest/termtest/emulator_test.go @@ -1,10 +1,13 @@ package termtest import ( + "os" "strings" "testing" + "github.com/databricks/cli/libs/cmdio" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // testCols and testRows match a classic VT100 default. The emulator's @@ -21,8 +24,13 @@ var blankLine = strings.Repeat(" ", testCols) // run constructs a fresh 80x25 emulator, feeds it the byte stream, and // returns it. Tests then assert on e.String(), e.row, e.col, etc. +// +// mapNewline is pinned on so LF expectations don't depend on the OS the +// tests run on; TestEmulator_LineFeedWithoutMapNewline covers the Windows +// setting. func feed(input string) *emulator { e := newEmulator(testCols, testRows) + e.mapNewline = true _, _ = e.Write([]byte(input)) return e } @@ -103,18 +111,31 @@ func TestEmulator_TabAdvancesToNextEighthColumn(t *testing.T) { func TestEmulator_LineFeed(t *testing.T) { e := feed("ab\ncd") - // LF moves down one line; it does not return to col 0. + // LF moves down one line and, like a cooked tty's ONLCR translation, + // returns to col 0. + assert.Equal(t, "ab", line(e, 0)) + assert.Equal(t, "cd", line(e, 1)) + pos(t, e, 1, 2) +} + +func TestEmulator_LineFeedWithoutMapNewline(t *testing.T) { + // bubbletea v2 on Windows doesn't map LF to CR LF, so the column stays. + e := newEmulator(testCols, testRows) + e.mapNewline = false + _, _ = e.Write([]byte("ab\ncd")) assert.Equal(t, "ab", line(e, 0)) assert.Equal(t, " cd", line(e, 1)) pos(t, e, 1, 4) } -func TestEmulator_LineFeedDoesNotScroll(t *testing.T) { - // Park at the bottom row and try to LF off the end. - e := newEmulator(testCols, testRows) - e.row = testRows - 1 +func TestEmulator_LineFeedScrollsAtBottom(t *testing.T) { + // Park at the bottom row and LF off the end: the screen scrolls up. + e := feed("top\x1b[25;1Hbottom") _, _ = e.Write([]byte{0x0a}) - pos(t, e, testRows-1, 0) // stays at last row + pos(t, e, testRows-1, 0) + assert.Empty(t, line(e, 0)) + assert.Equal(t, "bottom", line(e, testRows-2)) + assert.Empty(t, line(e, testRows-1)) } func TestEmulator_CarriageReturn(t *testing.T) { @@ -327,6 +348,9 @@ func TestEmulator_ResetClearsGridAndCursor(t *testing.T) { assert.Equal(t, blankLine, strings.Split(e.String(), "\n")[i], "row %d", i) } pos(t, e, 0, 0) + // REP after a reset must not repeat the rune printed before it. + _, _ = e.Write([]byte("\x1b[2b")) + assert.Empty(t, line(e, 0)) } func TestEmulator_WriteIsAdditive(t *testing.T) { @@ -338,3 +362,154 @@ func TestEmulator_WriteIsAdditive(t *testing.T) { pos(t, e, 4, 14) // (4,9) + "hello" assert.Equal(t, " hello", line(e, 4)) } + +func TestEmulator_RepeatPreviousCharacter(t *testing.T) { + e := feed("a-\x1b[3b!") + assert.Equal(t, "a----!", line(e, 0)) + pos(t, e, 0, 6) +} + +func TestEmulator_RepeatBeforeAnyPrintIsNoop(t *testing.T) { + e := feed("\x1b[3b") + assert.Empty(t, line(e, 0)) + assert.NotContains(t, e.String(), "\x00") + pos(t, e, 0, 0) +} + +func TestEmulator_EraseCharacter(t *testing.T) { + e := feed("abcdef\x1b[3G\x1b[2X") + assert.Equal(t, "ab ef", line(e, 0)) + pos(t, e, 0, 2) +} + +func TestEmulator_InsertCharacter(t *testing.T) { + e := feed("abcd\x1b[2G\x1b[2@") + assert.Equal(t, "a bcd", line(e, 0)) + pos(t, e, 0, 1) +} + +func TestEmulator_InsertCharacterDropsPastMargin(t *testing.T) { + e := feed(strings.Repeat("x", testCols-1) + "Y\x1b[1G\x1b[@") + assert.Equal(t, " "+strings.Repeat("x", testCols-1), line(e, 0)) +} + +func TestEmulator_DeleteCharacter(t *testing.T) { + e := feed("abcdef\x1b[2G\x1b[2P") + assert.Equal(t, "adef", line(e, 0)) + pos(t, e, 0, 1) +} + +func TestEmulator_InsertMode(t *testing.T) { + e := feed("abc\x1b[2G\x1b[4hXY\x1b[4lZ") + // IRM shifts "bc" right for X and Y; after RM 4, Z overwrites. + assert.Equal(t, "aXYZc", line(e, 0)) +} + +func TestEmulator_InsertLine(t *testing.T) { + e := feed("one\r\ntwo\r\nthree\x1b[2;3H\x1b[L") + assert.Equal(t, "one", line(e, 0)) + assert.Empty(t, line(e, 1)) + assert.Equal(t, "two", line(e, 2)) + assert.Equal(t, "three", line(e, 3)) + pos(t, e, 1, 0) +} + +func TestEmulator_DeleteLine(t *testing.T) { + e := feed("one\r\ntwo\r\nthree\x1b[1;1H\x1b[M") + assert.Equal(t, "two", line(e, 0)) + assert.Equal(t, "three", line(e, 1)) + assert.Empty(t, line(e, 2)) +} + +func TestEmulator_ScrollUpAndDown(t *testing.T) { + e := feed("one\r\ntwo\x1b[S") + assert.Equal(t, "two", line(e, 0)) + assert.Empty(t, line(e, 1)) + _, _ = e.Write([]byte("\x1b[2T")) + assert.Empty(t, line(e, 0)) + assert.Empty(t, line(e, 1)) + assert.Equal(t, "two", line(e, 2)) +} + +func TestEmulator_ScrollRegion(t *testing.T) { + e := feed("a\r\nb\r\nc\r\nd") + // Region rows 2-3; DECSTBM homes the cursor. + _, _ = e.Write([]byte("\x1b[2;3r")) + pos(t, e, 0, 0) + // LF at the region's bottom margin scrolls only the region. + _, _ = e.Write([]byte("\x1b[3;1H\n")) + assert.Equal(t, "a", line(e, 0)) + assert.Equal(t, "c", line(e, 1)) + assert.Empty(t, line(e, 2)) + assert.Equal(t, "d", line(e, 3)) + pos(t, e, 2, 0) +} + +func TestEmulator_ReverseIndex(t *testing.T) { + e := feed("a\r\nb\x1bM") + pos(t, e, 0, 1) + // At the top margin RI scrolls the screen down instead. + _, _ = e.Write([]byte("\x1bM")) + pos(t, e, 0, 1) + assert.Empty(t, line(e, 0)) + assert.Equal(t, "a", line(e, 1)) + assert.Equal(t, "b", line(e, 2)) +} + +func TestEmulator_TabStopForwardAndBack(t *testing.T) { + e := feed("\x1b[2I") + pos(t, e, 0, 16) + _, _ = e.Write([]byte("x\x1b[Z")) + pos(t, e, 0, 16) + _, _ = e.Write([]byte("\x1b[2Z")) + pos(t, e, 0, 0) +} + +func TestEmulator_PrefixedCSIsAreIgnored(t *testing.T) { + // Kitty keyboard push/pop/query, modifyOtherKeys, and DECRQM must not + // be mistaken for SCO restore-cursor ('u') or SGR. + e := feed("ab\x1b[s\x1b[1;5Hcd" + + "\x1b[>1u\x1b[4;2m\x1b[?2026$p\x1b[?2027$p") + assert.Equal(t, "ab cd", line(e, 0)) + pos(t, e, 0, 6) +} + +// v2SelectPageDownStream is the byte stream charm.land/bubbletea/v2 v2.1.1 +// wrote for TestSelectBaseline_VimNavOutsideSearch up to its +// 04-after-l-pagedown step, captured on Linux under TERM=xterm-256color with +// tm.Raw(). It relies on the LF column reset (mapNewline) and starts with the +// prefixed CSIs v2 sends at startup. To refresh it after a bubbletea bump, +// run that test's steps on the upgraded tree and paste +// fmt.Sprintf("%q", tm.Raw()) here. +const v2SelectPageDownStream = "\x1b[?2026$p\x1b[?2027$p\x1b[?25l\x1b[?2004h\x1b[>4;2m\x1b[>1u\x1b[?u\r\x1b[J" + + "Use the arrow keys to navigate: ↓ ↑ → ← and / toggles search\n" + + "? Pick one:\n" + + " > item-01 (id01)\n" + + "\x1b[4Citem-02 (id02)\n" + + "\x1b[4Citem-03 (id03)\n" + + "\x1b[4Citem-04 (id04)\n" + + "↓ item-05 (id05)\r\x1b[4A↑ > item-06 (id06\n" + + "\x1b[10C7 (id07\n" + + "\x1b[10C8 (id08\n" + + "\x1b[10C9 (id09\n" + + "\x1b[9C10 (id10" + +// TestEmulator_RendersCapturedV2Stream checks the emulator against output +// bubbletea v2 really produced: the screen must match the golden that v1 +// already produces for the same step. +func TestEmulator_RendersCapturedV2Stream(t *testing.T) { + golden, err := os.ReadFile("../testdata/TestSelectBaseline_VimNavOutsideSearch/04-after-l-pagedown.golden") + require.NoError(t, err) + + e := newEmulator(cmdio.TestTerminalWidth, cmdio.TestTerminalHeight) + e.mapNewline = true + _, _ = e.Write([]byte(v2SelectPageDownStream)) + + lines := strings.Split(e.String(), "\n") + for i, l := range lines { + lines[i] = strings.TrimRight(l, " ") + } + got := strings.TrimRight(strings.Join(lines, "\n"), "\n") + assert.Equal(t, strings.TrimRight(string(golden), "\n"), got) +} diff --git a/libs/cmdio/cmdiotest/termtest/termtest.go b/libs/cmdio/cmdiotest/termtest/termtest.go index 4940bfc6640..8cf2443e3dc 100644 --- a/libs/cmdio/cmdiotest/termtest/termtest.go +++ b/libs/cmdio/cmdiotest/termtest/termtest.go @@ -117,9 +117,15 @@ const ( // Other Key* constants are single non-Esc bytes or complete "\x1b[..." // CSI sequences, whose terminator commits the parse before any follow-up // byte matters. - // 20 ms matches the ESC-disambiguation timeouts neovim and tmux use - // for the same problem. - escDisambiguationDelay = 20 * time.Millisecond + // bubbletea v2 additionally holds a lone Esc until ultraviolet's + // EscTimeout expires, and that timer starts when the reader receives the + // byte, not when Type writes it. bubbletea leaves the timeout at + // ultraviolet's DefaultEscTimeout of 50 ms and doesn't expose it + // (charm.land/bubbletea/v2 v2.1.1 tty.go:75; ultraviolet + // terminal_reader.go:27,49). 100 ms, twice that, leaves room for a + // reader running late under CI load. v1 has no such timeout, so there it + // only needs to beat the next write. + escDisambiguationDelay = 100 * time.Millisecond // settleQuiet is how long [Term.Settle] waits for output to stop. A // keystroke's redraw lands well within it. @@ -325,7 +331,11 @@ func RunWith[T any](t *testing.T, opts Options, fn func(ctx context.Context) (T, finished: make(chan struct{}), term: newEmulator(cmdio.TestTerminalWidth, screenRows(opts.ScreenRows)), } - tt.term.mapNewline = opts.MapNewline + // MapNewline only turns LF mapping on: newEmulator already maps LF to CR LF + // off Windows, which bubbletea v2 relies on when its input is a pipe. + if opts.MapNewline { + tt.term.mapNewline = true + } ctx := t.Context() for k, v := range opts.Env { ctx = env.Set(ctx, k, v) //nolint:fatcontext // one value per variable, set once before the run