repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / tui
Antonio Mika  ·  2025-03-12

pager.go

 1package tui
 2
 3import (
 4	"git.sr.ht/~rockorager/vaxis"
 5	"git.sr.ht/~rockorager/vaxis/vxfw"
 6)
 7
 8type Pager struct {
 9	Surface vxfw.Surface
10	pos     int
11}
12
13func NewPager() *Pager {
14	return &Pager{}
15}
16
17func (m *Pager) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command, error) {
18	switch msg := ev.(type) {
19	case vaxis.Key:
20		if msg.Matches('j') || msg.Matches(vaxis.KeyDown) {
21			if m.pos == -1*int(m.Surface.Size.Height) {
22				return nil, nil
23			}
24			m.pos -= 1
25			return vxfw.RedrawCmd{}, nil
26		}
27		if msg.Matches('k') || msg.Matches(vaxis.KeyUp) {
28			if m.pos == 0 {
29				return nil, nil
30			}
31			m.pos += 1
32			return vxfw.RedrawCmd{}, nil
33		}
34	}
35	return nil, nil
36}
37
38func (m *Pager) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
39	root := vxfw.NewSurface(ctx.Max.Width, ctx.Max.Height, m)
40	root.AddChild(0, m.pos, m.Surface)
41	return root, nil
42}