Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204package termview
import ( "image" "testing" "time"
"gioui.org/gpu/headless" "gioui.org/layout" "gioui.org/op" "gioui.org/unit"
"go.mitchellh.com/libghostty")
// TestCursorShapes sets each cursor shape a program can ask for with// DECSCUSR and checks the shape of what's drawn in the cursor's colour.func TestCursorShapes(t *testing.T) { const w, h = 240, 120 win, err := headless.NewWindow(w, h) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release() term, err := libghostty.NewTerminal(libghostty.WithSize(20, 4)) if err != nil { t.Fatal(err) } defer term.Close() scr := NewScreen(t, term) defer scr.Close() cursor := libghostty.ColorRGB{R: 0xff, G: 0x00, B: 0xff} if err := term.SetColorCursor(&cursor); err != nil { t.Fatal(err) } v, err := New(testGlyphs()) if err != nil { t.Fatal(err) } defer v.Close() v.Padding = 0
var ops op.Ops now := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC) // draw returns the bounds of the cursor's colour, and how many pixels // of it there are. draw := func(focused bool) (image.Rectangle, int) { 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: now} if err := v.Update(gtx, scr); err != nil { t.Fatal(err) } if _, err := v.Layout(gtx); 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) } var box image.Rectangle n := 0 for y := range h { for x := range w { if c := img.RGBAAt(x, y); c.R == 0xff && c.G == 0 && c.B == 0xff { box = box.Union(image.Rect(x, y, x+1, y+1)) n++ } } } return box, n }
block, n := draw(true) cw, ch := block.Dx(), block.Dy() if n != cw*ch || cw < 4 || ch < 8 { t.Fatalf("block cursor is %d pixels in %v, want a solid cell", n, block) } for _, tc := range []struct { name, seq string focused bool ok func(box image.Rectangle, n int) bool }{ {"bar", "\x1b[6 q", true, func(b image.Rectangle, n int) bool { return b.Min == block.Min && b.Dy() == ch && b.Dx() < cw/2 && n == b.Dx()*b.Dy() }}, {"underline", "\x1b[4 q", true, func(b image.Rectangle, n int) bool { return b.Max == block.Max && b.Dx() == cw && b.Dy() < ch/4 && n == b.Dx()*b.Dy() }}, {"blinking block", "\x1b[1 q", true, func(b image.Rectangle, n int) bool { return b == block && n == cw*ch }}, {"unfocused bar", "\x1b[6 q", false, func(b image.Rectangle, n int) bool { return b == block && n < cw*ch/2 }}, } { term.VTWrite([]byte(tc.seq)) if box, n := draw(tc.focused); !tc.ok(box, n) { t.Errorf("%s cursor is %d pixels in %v; the cell is %v", tc.name, n, box, block) } }}
// TestCursorBlinks checks that a blinking cursor shows and hides in turn,// that it shows at once when it moves, and that one that doesn't blink, or// is in an unfocused view, always shows.func TestCursorBlinks(t *testing.T) { const w, h = 240, 120 win, err := headless.NewWindow(w, h) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release() term, err := libghostty.NewTerminal(libghostty.WithSize(20, 4)) if err != nil { t.Fatal(err) } defer term.Close() scr := NewScreen(t, term) defer scr.Close() cursor := libghostty.ColorRGB{R: 0xff, G: 0x00, B: 0xff} if err := term.SetColorCursor(&cursor); err != nil { t.Fatal(err) } v, err := New(testGlyphs()) if err != nil { t.Fatal(err) } defer v.Close()
start := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC) var ops op.Ops // shows draws the view at start+at and reports whether the cursor is // on screen. shows := func(at time.Duration, focused bool) bool { 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: start.Add(at)} if err := v.Update(gtx, scr); err != nil { t.Fatal(err) } if _, err := v.Layout(gtx); 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) } for i := 0; i < len(img.Pix); i += 4 { if img.Pix[i] == 0xff && img.Pix[i+1] == 0 && img.Pix[i+2] == 0xff { return true } } return false } ms := time.Millisecond check := func(what string, at time.Duration, focused, want bool) { t.Helper() if got := shows(at, focused); got != want { t.Errorf("%s, at %v: cursor shows %v, want %v", what, at, got, want) } }
check("a steady cursor", 0, true, true) check("a steady cursor", 700*ms, true, true) term.VTWrite([]byte("\x1b[1 q")) // a blinking block check("a cursor starting to blink", 1000*ms, true, true) check("a blinking cursor", 1500*ms, true, true) check("a blinking cursor", 1700*ms, true, false) check("a blinking cursor", 2300*ms, true, true) term.VTWrite([]byte("\x1b[2;3H")) check("a blinking cursor that moved", 2900*ms, true, true) check("a blinking cursor after moving", 3500*ms, true, false) term.VTWrite([]byte("x")) check("a blinking cursor after typing", 3500*ms, true, true) check("a blinking cursor in an unfocused view", 4100*ms, false, true) check("a blinking cursor focused again", 4200*ms, true, true) check("a blinking cursor focused again", 4900*ms, true, false) term.VTWrite([]byte("\x1b[2 q")) // steady again check("a cursor that stopped blinking", 4900*ms, true, true)}
func TestNextBlink(t *testing.T) { since := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC) for _, tc := range []struct{ now, want time.Duration }{ {0, blinkEvery}, {blinkEvery - 1, blinkEvery}, {blinkEvery, 2 * blinkEvery}, {5*blinkEvery + 1, 6 * blinkEvery}, } { if got := nextBlink(since, since.Add(tc.now)); !got.Equal(since.Add(tc.want)) { t.Errorf("nextBlink at %v = %v, want %v", tc.now, got.Sub(since), tc.want) } }}