-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
118 lines (112 loc) · 1.97 KB
/
example_test.go
File metadata and controls
118 lines (112 loc) · 1.97 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
package barlib_test
import (
"fmt"
"strconv"
"time"
"github.com/BurntSushi/xgb/xproto"
"github.com/pgaskin/barlib"
"github.com/pgaskin/barlib/barproto"
)
type Example struct {
Text string
Rate time.Duration
}
func (c Example) Run(i barlib.Instance) error {
var (
count int64
paused bool
)
for ticker, isEvent := i.Tick(c.Rate), false; ; {
i.Update(isEvent, func(render barlib.Renderer) {
var color uint32
if paused {
color = 0xFFFF00FF
} else {
color = 0x00FF00FF
}
render(barproto.Block{
Instance: "text",
FullText: c.Text + " ",
Separator: false,
Color: color,
})
switch {
case count < 0:
color = 0x000088FF
case count > 0:
color = 0x008800FF
default:
color = 0x880000FF
}
render(barproto.Block{
Instance: "count",
FullText: strconv.FormatInt(count, 10),
MinWidthString: "0000",
Align: "center",
Separator: true,
Background: color,
BorderLeft: 4,
BorderRight: 4,
Border: color,
})
})
select {
case <-i.Stopped():
i.Debug("stopped=%t", i.IsStopped())
case <-ticker:
if !paused {
count += 1
}
case event := <-i.Event():
switch event.Instance {
case "text":
switch event.Button {
case 1:
paused = !paused
case 2:
return fmt.Errorf("fake error")
case 3:
count = 0
case 4:
i.Tick(c.Rate)
case 5:
i.Tick(c.Rate / 2)
}
case "count":
switch event.Button {
case 1:
if event.Modifiers&xproto.ModMaskShift != 0 {
count--
} else {
count++
}
case 2:
return fmt.Errorf("fake error")
case 3:
count = 0
case 4:
count++
case 5:
count--
}
}
isEvent = true
}
}
}
func ExampleMain() {
barlib.Main(time.Second/4,
Example{
Text: "A",
Rate: time.Second,
},
Example{
Text: "B",
Rate: time.Second,
},
Example{
Text: "C",
Rate: time.Second / 2,
},
)
}