package termview import ( "fmt" "image" "image/color" "strings" "testing" "time" "gioui.org/f32" "gioui.org/gpu/headless" "gioui.org/io/input" "gioui.org/io/key" "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op" "gioui.org/unit" "go.mitchellh.com/libghostty" ) // selectHarness lays out a view over a terminal with a Gio input router, so // that tests can drive it with mouse events. type selectHarness struct { t *testing.T v *View term *libghostty.Terminal router input.Router ops op.Ops now time.Duration clock time.Time // the frames' time, for autoscroll } const selW, selH = 400, 200 func newSelectHarness(t *testing.T) *selectHarness { t.Helper() term, err := libghostty.NewTerminal(libghostty.WithSize(30, 5)) if err != nil { t.Fatal(err) } t.Cleanup(term.Close) term.VTWrite([]byte("hello world\r\nsecond line here")) v, err := New(testGlyphs()) if err != nil { t.Fatal(err) } t.Cleanup(v.Close) v.Padding = 6 h := &selectHarness{t: t, v: v, term: term, clock: time.Unix(1_800_000_000, 0)} h.frame() return h } func (h *selectHarness) frame() { h.t.Helper() h.ops.Reset() gtx := layout.Context{ Ops: &h.ops, Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, Constraints: layout.Exact(image.Pt(selW, selH)), Source: h.router.Source(), Now: h.clock, } if _, err := h.v.Layout(gtx, h.term); err != nil { h.t.Fatal(err) } h.router.Frame(&h.ops) } // at is the middle of a cell. func (h *selectHarness) at(col, row int) f32.Point { m, pad := h.v.metrics, 6 return f32.Pt(float32(pad+col*m.width+m.width/2), float32(pad+row*m.height+m.height/2)) } // mouse sends an event as the platform would: a drag is a move with the // button held, which the router turns into a Drag. // past is the right part of a cell: dragging there takes the cell in, as // Ghostty only does once the pointer is past a cell's middle. func (h *selectHarness) past(col, row int) f32.Point { m, pad := h.v.metrics, 6 return f32.Pt(float32(pad+col*m.width+m.width*3/4), float32(pad+row*m.height+m.height/2)) } func (h *selectHarness) mouse(kind pointer.Kind, p f32.Point) { if kind == pointer.Drag { kind = pointer.Move } h.now += 10 * time.Millisecond h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: p, Buttons: pointer.ButtonPrimary, Time: h.now}) h.frame() } func (h *selectHarness) click(p f32.Point) { h.mouse(pointer.Press, p) h.mouse(pointer.Release, p) } func (h *selectHarness) selected() string { h.t.Helper() s, err := SelectionText(h.term) if err != nil { h.t.Fatal(err) } return s } func TestSelection(t *testing.T) { h := newSelectHarness(t) presses := 0 h.v.OnPress = func() { presses++ } // Drag across "hello". h.mouse(pointer.Press, h.at(0, 0)) h.mouse(pointer.Drag, h.at(2, 0)) h.mouse(pointer.Drag, h.past(4, 0)) h.mouse(pointer.Release, h.past(4, 0)) if got := h.selected(); got != "hello" { t.Errorf("drag selection = %q, want %q", got, "hello") } if presses != 1 { t.Errorf("OnPress called %d times, want 1", presses) } // A drag over two lines takes both. h.now += time.Second h.mouse(pointer.Press, h.at(6, 0)) h.mouse(pointer.Drag, h.past(5, 1)) h.mouse(pointer.Release, h.past(5, 1)) if got := h.selected(); got != "world\nsecond" { t.Errorf("two-line selection = %q, want %q", got, "world\nsecond") } // A plain click clears it. h.now += time.Second h.click(h.at(20, 3)) if got := h.selected(); got != "" { t.Errorf("selection after a click = %q, want none", got) } // A double-click selects a word, a triple-click the line. h.now += time.Second h.click(h.at(8, 1)) h.click(h.at(8, 1)) if got := h.selected(); got != "line" { t.Errorf("double-click selection = %q, want %q", got, "line") } h.click(h.at(8, 1)) if got := h.selected(); got != "second line here" { t.Errorf("triple-click selection = %q, want %q", got, "second line here") } } // TestSelectionDrawn checks that selected cells are drawn in the selection // colours. func TestSelectionDrawn(t *testing.T) { win, err := headless.NewWindow(selW, selH) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release() h := newSelectHarness(t) h.v.SelectionBg = color.NRGBA{R: 0xDA, G: 0xB6, B: 0xFF, A: 0xff} h.mouse(pointer.Press, h.at(0, 0)) h.mouse(pointer.Drag, h.past(4, 0)) h.mouse(pointer.Release, h.past(4, 0)) if err := win.Frame(&h.ops); err != nil { t.Fatal(err) } img := image.NewRGBA(image.Rect(0, 0, selW, selH)) if err := win.Screenshot(img); err != nil { t.Fatal(err) } m := h.v.metrics // Sample the top-left corner of a cell, clear of its glyph. corner := func(col, row int) color.RGBA { return img.RGBAAt(6+col*m.width+1, 6+row*m.height+1) } if c := corner(2, 0); c.R != 0xDA || c.G != 0xB6 || c.B != 0xFF { t.Errorf("selected cell is %v, want the selection colour", c) } if c := corner(2, 1); c.R == 0xDA && c.G == 0xB6 && c.B == 0xFF { t.Errorf("unselected cell is the selection colour") } } // history fills the terminal with numbered lines, more than fit. func history(h *selectHarness, n int) { for i := range n { h.term.VTWrite([]byte(fmt.Sprintf("\r\nline %d", i))) } h.frame() } func (h *selectHarness) offset() uint64 { h.t.Helper() sb, err := h.term.Scrollbar() if err != nil { h.t.Fatal(err) } return sb.Offset } func (h *selectHarness) wheel(lines int, mods key.Modifiers) { h.router.Queue(pointer.Event{ Kind: pointer.Scroll, Source: pointer.Mouse, Position: h.at(3, 2), Scroll: f32.Pt(0, float32(lines*h.v.metrics.height)), Modifiers: mods, }) h.frame() } func TestWheel(t *testing.T) { h := newSelectHarness(t) history(h, 50) var toProgram []int h.v.OnWheel = func(col, row uint16, lines int) { toProgram = append(toProgram, lines) } bottom := h.offset() h.wheel(-3, 0) if got := h.offset(); got != bottom-3 { t.Errorf("after wheeling up 3 lines, offset %d, want %d", got, bottom-3) } h.wheel(2, 0) if got := h.offset(); got != bottom-1 { t.Errorf("after wheeling down 2 lines, offset %d, want %d", got, bottom-1) } if len(toProgram) != 0 { t.Errorf("a shell got wheel turns: %v", toProgram) } // A program that reads the mouse gets the wheel, and the scrollback // stays put, unless Shift is held. h.term.VTWrite([]byte("\x1b[?1000h")) before := h.offset() h.wheel(-2, 0) if len(toProgram) != 1 || toProgram[0] != -2 || h.offset() != before { t.Errorf("with mouse tracking: program got %v, offset %d (was %d)", toProgram, h.offset(), before) } h.wheel(-2, key.ModShift) if got := h.offset(); got != before-2 { t.Errorf("Shift-wheel with mouse tracking: offset %d, want %d", got, before-2) } } func TestAutoscroll(t *testing.T) { h := newSelectHarness(t) history(h, 50) bottom := h.offset() selected := 0 h.v.OnSelected = func() { selected++ } // Drag from the second row to above the top, and hold it there. h.mouse(pointer.Press, h.past(2, 1)) h.mouse(pointer.Drag, f32.Pt(30, -20)) for range 10 { h.clock = h.clock.Add(autoscrollEvery) h.frame() } h.mouse(pointer.Release, f32.Pt(30, -20)) // Ten ticks, a line each, scroll back at least ten lines, and the // selection reaches lines that were above the screen when the drag // began: the screen showed lines 45 to 49. if got := h.offset(); got+10 > bottom { t.Fatalf("offset %d after ten ticks above the top, want %d or less", got, bottom-10) } if text := h.selected(); !strings.Contains(text, "line 38\nline 39\nline 40") { t.Errorf("selection %q doesn't reach back into the scrollback", text) } if selected != 1 { t.Errorf("OnSelected called %d times, want 1", selected) } } // TestSelectionPaintsSelectedRows checks that selecting paints again only // the rows the selection changed, though the terminal marks every row // changed: painting a row means uploading it to the GPU again, and on a // large screen every row's upload costs a slow frame. func TestSelectionPaintsSelectedRows(t *testing.T) { h := newSelectHarness(t) painted := func(what string, want int) { t.Helper() if got := Painted(h.v); got != want { t.Errorf("%s: %d rows painted, want %d", what, got, want) } } h.mouse(pointer.Press, h.at(0, 0)) h.mouse(pointer.Drag, h.past(2, 0)) painted("selecting in a row", 1) h.mouse(pointer.Drag, h.past(4, 0)) painted("selecting more of it", 1) h.mouse(pointer.Drag, h.past(3, 1)) painted("selecting into the next row", 2) h.mouse(pointer.Release, h.past(3, 1)) painted("letting go", 0) h.frame() painted("a frame after", 0) h.mouse(pointer.Press, h.at(20, 3)) painted("clearing the selection", 2) h.mouse(pointer.Release, h.at(20, 3)) }