| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
|
| 7 |
"git.sr.ht/~rockorager/vaxis" |
| 8 |
"git.sr.ht/~rockorager/vaxis/vxfw" |
| 9 |
"git.sr.ht/~rockorager/vaxis/vxfw/list" |
| 10 |
"git.sr.ht/~rockorager/vaxis/vxfw/text" |
| 11 |
) |
| 12 |
|
| 13 |
type App struct { |
| 14 |
list list.Dynamic |
| 15 |
} |
| 16 |
|
| 17 |
func redrawAndConsume() vxfw.BatchCmd { |
| 18 |
return []vxfw.Command{ |
| 19 |
vxfw.RedrawCmd{}, |
| 20 |
vxfw.ConsumeEventCmd{}, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func (a *App) CaptureEvent(ev vaxis.Event) (vxfw.Command, error) { |
| 25 |
switch ev := ev.(type) { |
| 26 |
case vaxis.Key: |
| 27 |
if ev.Matches('c', vaxis.ModCtrl) { |
| 28 |
return vxfw.QuitCmd{}, nil |
| 29 |
} |
| 30 |
if ev.Matches('l', vaxis.ModCtrl) { |
| 31 |
return []vxfw.Command{vxfw.DebugCmd{}, vxfw.RedrawCmd{}}, nil |
| 32 |
} |
| 33 |
} |
| 34 |
return nil, nil |
| 35 |
} |
| 36 |
|
| 37 |
func (a *App) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) { |
| 38 |
switch ev.(type) { |
| 39 |
case vxfw.Init: |
| 40 |
return vxfw.FocusWidgetCmd(&a.list), nil |
| 41 |
} |
| 42 |
return nil, nil |
| 43 |
} |
| 44 |
|
| 45 |
func (a *App) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) { |
| 46 |
s, err := a.list.Draw(ctx) |
| 47 |
if err != nil { |
| 48 |
return vxfw.Surface{}, err |
| 49 |
} |
| 50 |
|
| 51 |
root := vxfw.NewSurface(s.Size.Width, s.Size.Height, a) |
| 52 |
root.AddChild(0, 0, s) |
| 53 |
|
| 54 |
return root, nil |
| 55 |
} |
| 56 |
|
| 57 |
func getWidget(i uint, cursor uint) vxfw.Widget { |
| 58 |
var style vaxis.Style |
| 59 |
if i == cursor { |
| 60 |
style.Attribute = vaxis.AttrReverse |
| 61 |
} |
| 62 |
content := fmt.Sprintf("Row %d", i) |
| 63 |
for n := uint(0); n < i; n += 1 { |
| 64 |
content += "\n Multiline" |
| 65 |
} |
| 66 |
return &text.Text{ |
| 67 |
Content: content, |
| 68 |
Style: style, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func main() { |
| 73 |
app, err := vxfw.NewApp(vaxis.Options{}) |
| 74 |
if err != nil { |
| 75 |
log.Fatalf("Couldn't create a new app: %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
root := &App{ |
| 79 |
list: list.Dynamic{ |
| 80 |
Builder: getWidget, |
| 81 |
DrawCursor: true, |
| 82 |
Gap: 1, |
| 83 |
}, |
| 84 |
} |
| 85 |
|
| 86 |
app.Run(root) |
| 87 |
} |