Debian Codegraph @

_examples/vxfw/main.go

raw
1 package main
2
3 import (
4 "log"
5 "math"
6
7 "git.sr.ht/~rockorager/vaxis"
8 "git.sr.ht/~rockorager/vaxis/vxfw"
9 "git.sr.ht/~rockorager/vaxis/vxfw/richtext"
10 )
11
12 const lorem = `Lorem ipsum odor amet, consectetuer adipiscing elit. Nulla viverra ipsum id curae dui etiam massa? Sagittis non morbi ornare penatibus pharetra inceptos dolor posuere. Placerat netus nascetur tellus nec magnis magna. Convallis accumsan sollicitudin dui sem natoque; tristique nam! Condimentum tristique risus diam nisl cursus suscipit mauris. Penatibus viverra mattis nunc maximus curabitur. Aenean mi tempus vivamus amet vitae urna. Orci at senectus ullamcorper suspendisse augue proin.
13 `
14
15 var segments = []vaxis.Segment{
16 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
17 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(2)}},
18 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
19 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(4)}},
20 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(5)}},
21 {Text: lorem, Style: vaxis.Style{Foreground: vaxis.IndexColor(6)}},
22 }
23
24 type App struct {
25 t *richtext.RichText
26 scroll int
27 }
28
29 func redrawAndConsume() vxfw.BatchCmd {
30 return []vxfw.Command{
31 vxfw.RedrawCmd{},
32 vxfw.ConsumeEventCmd{},
33 }
34 }
35
36 func (a *App) CaptureEvent(ev vaxis.Event) (vxfw.Command, error) {
37 switch ev := ev.(type) {
38 case vaxis.Key:
39 if ev.Matches('c', vaxis.ModCtrl) {
40 return vxfw.QuitCmd{}, nil
41 }
42 if ev.Matches('j') {
43 a.scroll -= 1
44 return redrawAndConsume(), nil
45 }
46 if ev.Matches('k') {
47 a.scroll += 1
48 return redrawAndConsume(), nil
49 }
50 }
51 return nil, nil
52 }
53
54 func (a *App) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) {
55 return nil, nil
56 }
57
58 func (a *App) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
59 chCtx := ctx.WithMax(vxfw.Size{Width: 24, Height: math.MaxUint16})
60 s, err := a.t.Draw(chCtx)
61 if err != nil {
62 return vxfw.Surface{}, err
63 }
64
65 root := vxfw.NewSurface(s.Size.Width, s.Size.Height, a)
66 root.AddChild(0, a.scroll, s)
67
68 return root, nil
69 }
70
71 func main() {
72 app, err := vxfw.NewApp(vaxis.Options{})
73 if err != nil {
74 log.Fatalf("Couldn't create a new app: %v", err)
75 }
76
77 root := &App{t: richtext.New(segments)}
78
79 app.Run(root)
80 }