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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#!/usr/bin/env yaegi
// # Documentation
// we try to read a file pointed to by WAYBAR_TODO_FILE.
// While no particular format is assumed, we recommend
// Markdown.
// We extract the todos from the top-level list items
// (listings start with a `*` character).
//
// # Usage:
// go build -o waybar-todo todo.go
package main
import (
"bufio"
"encoding/json"
"log"
"os"
"os/signal"
"regexp"
"runtime"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"golang.org/x/sys/unix"
)
const (
TODO_INACTIVE_STRING string = ""
TODO_LABEL_REGEX string = `\[\[!(.*)\]\]`
)
var (
active = true
todofile = os.Getenv("WAYBAR_TODO_FILE")
cookiefile = os.ExpandEnv("${XDG_RUNTIME_DIR}/waybar-todo.cookie")
labelFilter = os.Getenv("WAYBAR_TODO_FILTER")
)
func printJson(text, tooltip string, class []string, alt string, percentage int) error {
return json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
"text": text,
"tooltip": tooltip,
"class": class,
"alt": alt,
"percentage": percentage,
})
}
type todoItem struct {
text string
label string
}
func readTodofile2(path string) ([]todoItem, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
var todos []todoItem
for lr := bufio.NewScanner(f); lr.Scan(); {
skip := 0
switch {
case strings.HasPrefix(lr.Text(), "*"):
skip = 2
case strings.HasPrefix(lr.Text(), "- [ ]"):
skip = 6
case strings.HasPrefix(lr.Text(), "- [x]"):
fallthrough
default:
continue
}
ti := todoItem{text: lr.Text()[skip:]}
matches := labelre.FindAllStringSubmatch(ti.text, -1)
if matches != nil && len(matches) > 0 {
ti.label = matches[0][1]
ti.text = strings.TrimSpace(
strings.Replace(ti.text, matches[0][0], "", 1))
}
todos = append(todos, ti)
}
return todos, nil
}
func setCookie(active bool) error {
if !active {
_, err := os.Create(cookiefile)
return err
}
return os.Remove(cookiefile)
}
func update() error {
if _, err := os.Stat(cookiefile); err == nil {
log.Printf("cookie set, we're inactive")
active = false
}
if !active {
return printJson(TODO_INACTIVE_STRING, "* inactive *", []string{"off"}, "off", 0)
}
todos, err := readTodofile2(todofile)
if err != nil {
return err
}
var (
text string
tooltip []string
extraClass string
)
for _, item := range todos {
if labelFilter != "" && strings.Contains(labelFilter, item.label) {
continue
}
if text == "" {
text = item.text
extraClass = item.label
continue
}
// render out the rest of the todos into the tooltip
tooltip = append(tooltip, item.text)
}
return printJson(text, strings.Join(tooltip, "\n"), []string{"on", extraClass}, "on", 1)
}
var labelre *regexp.Regexp
func init() {
labelre = regexp.MustCompile(TODO_LABEL_REGEX)
}
func main() {
runtime.GOMAXPROCS(1)
var (
updateC = make(chan os.Signal, 1)
cancelC = make(chan os.Signal, 1)
)
signal.Notify(updateC, unix.SIGUSR1, unix.SIGUSR2, unix.SIGHUP)
signal.Notify(cancelC, unix.SIGTERM, unix.SIGINT)
var err error
// we will receive a SIGTERM if our parent dies
err = unix.Prctl(unix.PR_SET_PDEATHSIG, uintptr(unix.SIGTERM), 0, 0, 0)
if err != nil {
log.Fatalf("error: setting prctl: %v", err)
}
if todofile == "" {
panic("missing TODO file")
// // get from nb(1)
// nb := exec.Command("sh", "-c", "nb show --no-color --path todo")
// nb_output, err := nb.Output()
// if err != nil {
// e := err.(*exec.ExitError)
// log.Fatalf("error: finding todofile via nb: %s", e.Stderr)
// }
// todofile = strings.TrimSpace(string(nb_output))
}
log.Printf("watching %q", todofile)
w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("error: creating watcher: %v", err)
}
defer w.Close()
if err := w.Add(todofile); err != nil {
log.Fatalf("error: adding watch: %v", err)
}
if err := update(); err != nil {
// fail here because it's the first time we
// try to read the todofile.
log.Fatalf("error: %v", err)
}
for {
select {
case evt, ok := <-w.Events:
if !ok {
return
}
// see https://github.com/fsnotify/fsnotify/issues/94
// https://github.com/gohugoio/hugo/pull/4720
if evt.Op&fsnotify.Remove == fsnotify.Remove {
counter := 0
for w.Add(todofile) != nil {
counter++
if counter >= 1000 {
log.Panic("error: unable to add file watcher")
}
time.Sleep(100 * time.Millisecond)
}
}
goto force_update
case err, ok := <-w.Errors:
if !ok {
return
}
log.Printf("error: from watcher: %v", err)
case sig := <-updateC:
switch sig {
case unix.SIGUSR1: // on
active = !active
if err := setCookie(active); err != nil {
log.Printf("err: %v", err)
}
goto force_update
case unix.SIGUSR2: //
if strings.Contains(labelFilter, "work") {
labelFilter = strings.Trim(strings.ReplaceAll(labelFilter, "work", ""), ",")
} else {
labelFilter = strings.Join([]string{labelFilter, "work"}, ",")
}
continue
case unix.SIGHUP: // force update
log.Println("force update")
goto force_update
}
case <-cancelC:
log.Printf("%s exiting", os.Args[0])
os.Exit(0)
}
force_update:
_ = update()
}
}
|