package termview import ( "bytes" "fmt" "image" "strings" "testing" "time" "gioui.org/gpu/headless" "gioui.org/layout" "gioui.org/op" "gioui.org/unit" "go.mitchellh.com/libghostty" ) // TestRowsRedrawn changes a terminal in each way a screen changes, and after // each change checks that a view drawing it all along, replaying the rows // that didn't change, shows exactly what a new view drawing every row does. func TestRowsRedrawn(t *testing.T) { const w, h = 480, 200 win, err := headless.NewWindow(w, h) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release() glyphs := testGlyphs() term, err := libghostty.NewTerminal(libghostty.WithSize(40, 8), libghostty.WithMaxScrollbackLines(1000)) if err != nil { t.Fatal(err) } defer term.Close() newView := func() *View { v, err := New(glyphs) if err != nil { t.Fatal(err) } v.Padding = 4 return v } kept := newView() defer kept.Close() focused := true var ops op.Ops draw := func(v *View) *image.RGBA { t.Helper() v.Focused = focused ops.Reset() gtx := layout.Context{Ops: &ops, Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, Constraints: layout.Exact(image.Pt(w, h)), Now: time.Now()} if _, err := v.Layout(gtx, term); err != nil { t.Fatal(err) } if err := win.Frame(&ops); err != nil { t.Fatal(err) } img := image.NewRGBA(image.Rect(0, 0, w, h)) if err := win.Screenshot(img); err != nil { t.Fatal(err) } return img } check := func(what string) { t.Helper() got := draw(kept) fresh := newView() defer fresh.Close() if want := draw(fresh); !bytes.Equal(got.Pix, want.Pix) { t.Errorf("after %s, the view shows something other than a new view does", what) } } write := func(s string) { term.VTWrite([]byte(s)) } var lines []string for i := range 6 { lines = append(lines, fmt.Sprintf("\x1b[3%dmline %d\x1b[0m \x1b[4%dm bg \x1b[0m", i+1, i, (i+3)%8)) } write(strings.Join(lines, "\r\n")) check("the first output") draw(kept) // and replayed as it is check("a frame with no change") // Away from the cursor, whose row is always drawn again. write("\x1b7\x1b[2;3Hxy\x1b8") check("writing on a row without the cursor") write("\x1b7\x1b[4;2H\x1b[45m \x1b[0m\x1b8") check("a background away from the cursor") var rainbow strings.Builder for i := range 16 { fmt.Fprintf(&rainbow, "\x1b[38;5;%dm%c\x1b[48;5;%dm%c", 16+i*13, 'a'+i, 200+i, 'A'+i) } write("\x1b7\x1b[3;1H" + rainbow.String() + "\x1b[0m\x1b8") check("a row of many colours") write("\x1b7\x1b[6;20H\x1b[31m漢字\x1b[0m\x1b8") check("wide characters") write("\x1b[5;10H") check("moving the cursor") write("\x1b[6 q") check("a bar cursor") write("\x1b[4 q") check("an underline cursor") write("\x1b[0 q") check("the default cursor again") focused = false check("losing focus") focused = true sel, err := term.SelectAll() if err != nil || sel == nil { t.Fatalf("select all: %v", err) } if err := term.SetSelection(sel); err != nil { t.Fatal(err) } check("selecting") if err := term.SetSelection(nil); err != nil { t.Fatal(err) } check("clearing the selection") write("\x1b[8;1H\r\nscrolled one\r\nscrolled two") check("output scrolling the screen") if err := term.ScrollViewportDelta(-3); err != nil { t.Fatal(err) } check("scrolling back") if err := term.ScrollViewportBottom(); err != nil { t.Fatal(err) } check("scrolling to the bottom") // Full-screen programs scroll part of the screen in place: the rows' // cells move to rows the terminal counts as new. write("\x1b[2;4Hsame\x1b[3;4Hsame\x1b[2;6r\x1b[6;1H\r\nin the region\x1b[r") check("scrolling within a region") write("\x1b[3;1H\x1b[2L") check("inserting lines") write("\x1b[2;1H\x1b[M") check("deleting a line") write("\x1b[1;7r\x1b[1;1H\x1bM\x1bM\x1b[r") check("scrolling a region down") if err := term.SetColorBackground(&libghostty.ColorRGB{R: 0x20, G: 0x30, B: 0x40}); err != nil { t.Fatal(err) } check("a new background colour") write("\x1b[?1049h\x1b[Halternate screen") check("switching to the alternate screen") write("\x1b[?1049l") check("switching back") if err := term.Resize(30, 6, 8, 16); err != nil { t.Fatal(err) } check("resizing") write("\x1b[2J\x1b[Hcleared") check("clearing the screen") } // TestMovedRowsReused scrolls the screen each way programs do, and checks // that only the row with new cells is painted: the rest moved, and keep // their paintings, though the terminal counts some as new rows. func TestMovedRowsReused(t *testing.T) { for _, c := range []struct{ name, seq string }{ {"output scrolling the screen", "\r\nnew line"}, {"scrolling within a region", "\x1b[1;4r\x1b[4;1H\r\nnew line\x1b[r"}, {"scrolling up", "\x1b[S"}, {"deleting a line", "\x1b[1;1H\x1b[M"}, {"inserting a line", "\x1b[1;1H\x1b[L"}, } { h := newSelectHarness(t) h.term.VTWrite([]byte("\x1b[2J")) for i := range 5 { h.term.VTWrite([]byte(fmt.Sprintf("\x1b[%d;1Hrow %d", i+1, i))) } h.frame() h.term.VTWrite([]byte(c.seq)) h.frame() if got := Painted(h.v); got != 1 { t.Errorf("%s: %d rows painted, want 1", c.name, got) } } }