Eric Bower
·
2025-03-17
kv.go
1package tui
2
3import (
4 "git.sr.ht/~rockorager/vaxis"
5 "git.sr.ht/~rockorager/vaxis/vxfw"
6 "git.sr.ht/~rockorager/vaxis/vxfw/text"
7)
8
9type KVBuilder func(uint16) (vxfw.Widget, vxfw.Widget)
10
11type Kv struct {
12 Key string
13 Value string
14 Style vaxis.Style
15}
16
17type KvData struct {
18 Data []Kv
19 KeyColWidth int
20}
21
22func NewKv(data []Kv) *KvData {
23 return &KvData{
24 Data: data,
25 KeyColWidth: 15,
26 }
27}
28
29func (m *KvData) HandleEvent(vaxis.Event, vxfw.EventPhase) (vxfw.Command, error) {
30 return nil, nil
31}
32
33func (m *KvData) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
34 root := vxfw.NewSurface(ctx.Max.Width, ctx.Max.Height, m)
35 lng := m.KeyColWidth
36 left := vxfw.NewSurface(uint16(lng), ctx.Max.Height, m)
37 right := vxfw.NewSurface(ctx.Max.Width-uint16(lng), ctx.Max.Height, m)
38
39 ah := 0
40 var idx uint16 = 0
41 for _, data := range m.Data {
42 key := text.New(data.Key)
43 key.Style = data.Style
44 value := text.New(data.Value)
45 value.Style = data.Style
46 lft, _ := key.Draw(ctx)
47 left.AddChild(0, ah, lft)
48 rht, _ := value.Draw(ctx)
49 right.AddChild(0, ah, rht)
50 idx += 1
51 ah += 1
52 }
53 root.AddChild(0, 0, left)
54 root.AddChild(lng, 0, right)
55
56 root.Size.Width = left.Size.Width + right.Size.Width - 2
57 root.Size.Height = uint16(ah)
58
59 return root, nil
60}