-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickcount.go
58 lines (48 loc) · 1.08 KB
/
clickcount.go
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
// This is an example blocklet that doesn't have much sense.
// Use it as a template.
package blocks
import (
"context"
"fmt"
. "github.com/kraftwerk28/gost/core"
"github.com/kraftwerk28/gost/core/formatting"
)
type ClickcountConfig struct {
Format *ConfigFormat `yaml:"format"`
}
type Clickcount struct {
ClickcountConfig
clicks int
ch UpdateChan
}
func NewClickcountBlock() I3barBlocklet {
b := Clickcount{}
b.Format = NewConfigFormatFromString("{clicks}")
return &b
}
func (c *Clickcount) Run(ch UpdateChan, ctx context.Context) {
c.ch = ch
}
func (c *Clickcount) GetConfig() interface{} {
return &c.ClickcountConfig
}
func (t *Clickcount) Render(cfg *AppConfig) []I3barBlock {
txt := t.Format.Expand(formatting.NamedArgs{
"clicks": fmt.Sprintf("%d", t.clicks),
})
return []I3barBlock{{FullText: txt}}
}
func (t *Clickcount) OnEvent(e *I3barClickEvent, ctx context.Context) {
if e.Button == ButtonScrollDown {
t.clicks--
} else {
t.clicks++
}
if t.clicks < 0 {
t.clicks = 0
}
t.ch.SendUpdate()
}
func init() {
RegisterBlocklet("clicks", NewClickcountBlock)
}