summary refs log tree commit diff
path: root/.config/waybar/modules/todo.go
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-02-23 13:36:27 +0100
committerRobert Günzler <r@gnzler.io>2021-04-26 02:09:45 +0200
commit4b71908c30807c9d726e83378e04ccae28dff334 (patch)
treeab3c37125a69803ffdaba7cdcac9014c437f3215 /.config/waybar/modules/todo.go
parent6cb604cdeb78b151d7e179d4f34f00101c912282 (diff)
waybar: update 2021-02-23T13:36:34Z+01:00
Diffstat (limited to '.config/waybar/modules/todo.go')
-rw-r--r--.config/waybar/modules/todo.go68
1 files changed, 22 insertions, 46 deletions
diff --git a/.config/waybar/modules/todo.go b/.config/waybar/modules/todo.go
index 449f991..4f403f5 100644
--- a/.config/waybar/modules/todo.go
+++ b/.config/waybar/modules/todo.go
@@ -11,17 +11,14 @@
 package main
 
 import (
-	"bytes"
+	"bufio"
 	"encoding/json"
-	"fmt"
-	"io/ioutil"
 	"log"
 	"os"
 	"os/exec"
 	"os/signal"
 	"regexp"
 	"strings"
-	"text/scanner"
 	"time"
 
 	"github.com/fsnotify/fsnotify"
@@ -29,7 +26,7 @@ import (
 )
 
 const (
-	TODO_LINE_CHAR       rune   = '*'
+	TODO_LINE_CHAR       string = "* "
 	TODO_INACTIVE_STRING string = ""
 	TODO_LABEL_REGEX     string = `\[\[!(.*)\]\]`
 )
@@ -56,47 +53,26 @@ type todoItem struct {
 	label string
 }
 
-func readTodofile(path string) ([]todoItem, error) {
-	content, err := ioutil.ReadFile(path)
+func readTodofile2(path string) ([]todoItem, error) {
+	f, err := os.Open(path)
 	if err != nil {
-		return nil, fmt.Errorf("reading file: %v", err)
+		return nil, err
 	}
-
 	var (
-		sc        scanner.Scanner
-		validLine bool
-		todos     []todoItem
+		todos []todoItem
 	)
-	sc.Init(bytes.NewReader(content))
-	sc.Filename = path
-	sc.Whitespace ^= 1<<'\n' | 1<<' ' // don't skip newlines or spaces
-	sc.Mode ^= scanner.GoTokens
-	for tok := sc.Scan(); tok != scanner.EOF; tok = sc.Scan() {
-		switch {
-		// a valid line stats with TODO_LINE_CHAR
-		// followed by a space
-		// we strip them before appending the line array
-		case tok == TODO_LINE_CHAR && sc.Peek() == ' ':
-			validLine = true
-			_ = sc.Scan()                     // swallow the space
-			todos = append(todos, todoItem{}) // grow the array
-
-		case tok == '\n':
-			validLine = false
-			if len(todos) > 0 {
-				idx := len(todos) - 1
-				matches := labelre.FindAllStringSubmatch(todos[idx].text, -1)
-				if matches != nil && len(matches) > 0 {
-					todos[idx].label = matches[0][1]
-					todos[idx].text = strings.TrimSpace(
-						strings.Replace(todos[idx].text, matches[0][0], "", 1))
-				}
-			}
-
-		case validLine:
-			todos[len(todos)-1].text = todos[len(todos)-1].text + sc.TokenText()
+	for lr := bufio.NewScanner(f); lr.Scan(); {
+		if !strings.HasPrefix(lr.Text(), TODO_LINE_CHAR) {
+			continue
 		}
-
+		ti := todoItem{text: lr.Text()[2:]}
+		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
 }
@@ -115,15 +91,15 @@ func update() error {
 		active = false
 	}
 	if !active {
-		return printJson(TODO_INACTIVE_STRING, "* inactive *", []string{"off"}, "todo-off", 0)
+		return printJson(TODO_INACTIVE_STRING, "* inactive *", []string{"off"}, "off", 0)
 	}
-	todos, err := readTodofile(todofile)
+	todos, err := readTodofile2(todofile)
 	if err != nil {
 		return err
 	}
 	var (
 		text       string
-		tooltip    string
+		tooltip    []string
 		extraClass string
 	)
 	for _, item := range todos {
@@ -136,9 +112,9 @@ func update() error {
 			continue
 		}
 		// render out the rest of the todos into the tooltip
-		tooltip = strings.Join([]string{tooltip, item.text}, "\n")
+		tooltip = append(tooltip, item.text)
 	}
-	return printJson(text, tooltip, []string{"on", extraClass}, "todo-on", 1)
+	return printJson(text, strings.Join(tooltip, "\n"), []string{"on", extraClass}, "on", 1)
 }
 
 var (