Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
139 lines (126 loc) · 4.6 KB
/
Copy pathexample_test.go
File metadata and controls
139 lines (126 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package limoni_test
import (
"context"
"fmt"
"strings"
"github.com/thebanri/limoni"
"github.com/thebanri/limoni/testkit"
)
// printScreen prints a testkit snapshot with each row's trailing blanks cut,
// so the examples' expected output survives editors that strip whitespace.
func printScreen(term *testkit.Terminal) {
for _, row := range strings.Split(term.Snapshot(), "\n") {
fmt.Println(strings.TrimRight(row, " "))
}
}
// The smallest immediate-mode application: draw on every event, return false
// to quit. Run takes over the terminal until then.
func Example() {
limoni.Run(func(f *limoni.Frame, ev *limoni.Event) bool {
if ev != nil && ev.Type == limoni.EventKey && ev.Key.Type == limoni.KeyEsc {
return false
}
f.RenderWidget(limoni.Center(limoni.Label("Hello from Limoni (Esc quits)")), f.Area())
return true
})
}
// counter is a declarative (Elm architecture) model: Update changes state
// and asks for a redraw, View draws it.
type counter struct{ n int }
func (c *counter) Init() []limoni.Cmd { return nil }
func (c *counter) Update(msg limoni.Msg) limoni.UpdateResult {
key, ok := msg.(limoni.KeyPressMsg)
if !ok {
return limoni.UpdateResult{}
}
switch {
case key.Key.Type == limoni.KeyEsc:
return limoni.UpdateResult{Quit: true}
case key.Key.Type == limoni.KeyRune && key.Key.Ch == '+':
c.n++
return limoni.UpdateResult{Redraw: true}
}
return limoni.UpdateResult{}
}
func (c *counter) View(f *limoni.Frame) {
f.RenderWidget(limoni.Label(fmt.Sprintf("count: %d (+ adds, Esc quits)", c.n)), f.Area())
}
// RunProgram drives a Model on the real terminal until Update returns Quit
// or the context is cancelled.
func ExampleRunProgram() {
if err := limoni.RunProgram(context.Background(), &counter{}); err != nil {
fmt.Println(err)
}
}
// Components compose without allocating: a border around a centred label.
// testkit draws into memory, which is also how to test a view.
func ExampleBorder() {
term := testkit.NewTerminal(24, 5)
term.Draw(func(f *limoni.Frame) {
f.RenderWidget(limoni.Border(
limoni.Center(limoni.Label("Hello, Limoni")),
limoni.SymbolsRounded,
limoni.NewStyle(),
), f.Area())
})
printScreen(term)
// Output:
// ╭──────────────────────╮
// │ │
// │ Hello, Limoni │
// │ │
// ╰──────────────────────╯
}
// HStack lays children out side by side; Flex shares the space left over by
// weight.
func ExampleHStack() {
term := testkit.NewTerminal(30, 3)
term.Draw(func(f *limoni.Frame) {
f.RenderWidget(limoni.HStack(
limoni.Flex(1, limoni.Border(limoni.Label("left"), limoni.SymbolsSingle, limoni.NewStyle())),
limoni.Flex(2, limoni.Border(limoni.Label("right: weight 2"), limoni.SymbolsSingle, limoni.NewStyle())),
), f.Area())
})
printScreen(term)
// Output:
// ┌────────┐┌──────────────────┐
// │left ││right: weight 2 │
// └────────┘└──────────────────┘
}
// A table with a header row. Rows can also come from a TableDataSource, which
// is how a million-row table touches only the rows on screen.
func ExampleNewTable() {
table := limoni.NewTable().
WithHeaders("Service", "Status").
WithRow("api", "up").
WithRow("worker", "degraded")
term := testkit.NewTerminal(26, 6)
term.Render(table, term.Area())
printScreen(term)
// Output:
// Service │Status
// ────────────┼────────────
// api │up
// worker │degraded
}
// MessageFromEvent is the conversion a Program applies to terminal input. A
// pointer moving with no button held is motion, not a press.
func ExampleMessageFromEvent() {
hover := limoni.Event{Type: limoni.EventMouse, Mouse: limoni.MouseEvent{X: 10, Y: 2, Button: limoni.MouseNone}}
click := limoni.Event{Type: limoni.EventMouse, Mouse: limoni.MouseEvent{X: 10, Y: 2, Button: limoni.MouseLeft}}
wheel := limoni.Event{Type: limoni.EventMouse, Mouse: limoni.MouseEvent{X: 4, Y: 7, Button: limoni.MouseScrollDown}}
for _, ev := range []limoni.Event{hover, click, wheel} {
switch msg := limoni.MessageFromEvent(ev).(type) {
case limoni.MouseMotionMsg:
fmt.Println("motion at", msg.Position.X, msg.Position.Y)
case limoni.MousePressMsg:
fmt.Println("press at", msg.Position.X, msg.Position.Y)
case limoni.MouseWheelMsg:
fmt.Println("wheel", msg.DeltaY, "at", msg.Position.X, msg.Position.Y)
}
}
// Output:
// motion at 10 2
// press at 10 2
// wheel -1 at 4 7
}