-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremind.go
175 lines (149 loc) · 4.91 KB
/
remind.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
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
package main
import (
"fmt"
"bytes"
"strings"
"os"
"os/exec"
"strconv"
"time"
"encoding/json"
"path/filepath"
)
// Gets reminders for today and puts the output into array of lines ([]string)
// If lines are longer than maxLineWidth they are cut off and the remainder(s) added as new line(s)
func getToday(filename string, year int, month int, day int, maxLineWidth int) []string {
var outb, errb bytes.Buffer
dateStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
cmd := exec.Command("remind", filename, dateStr)
cmd.Stdout = &outb
cmd.Stderr = &errb
err := cmd.Run()
if err != nil {
if errb.Len() > 0 { err = fmt.Errorf(errb.String()) }
panic(err)
}
if outb.Len() <= 0 {
panic(fmt.Errorf("remind did not return any output"))
}
// entire outb is transformed into lines wrapped if too long
lines := []string{}
for i, line := range strings.Split(outb.String(), "\n") {
if i == 0 {
// patching first line because it is so long
lines = append(lines, "Todays Reminders:")
continue
}
for {
if len(line) <= maxLineWidth {
lines = append(lines, line)
break
} else {
lines = append(lines, line[:maxLineWidth])
line = line[maxLineWidth:]
}
}
}
return lines
}
// Calls remind -pppn -g filename date and parses returned reminders into []Event
// All returned dates are valid
func getEvents(filename string, year int, month int, nrOfMonth int) (eventsArr []Event) {
var outb, errb bytes.Buffer
dateStr := fmt.Sprintf("%04d-%02d-%02d", year, month, 1)
cmd := exec.Command("remind", "-ppp" + strconv.Itoa(nrOfMonth), "-g", filename, dateStr)
cmd.Stdout = &outb
cmd.Stderr = &errb
err := cmd.Run()
if err != nil {
if errb.Len() > 0 { err = fmt.Errorf(errb.String()) }
panic(err)
}
if outb.Len() <= 0 {
panic(fmt.Errorf("remind did not return any output"))
}
eventsArr, err = parseRemindEventsJSON(outb.String())
if err != nil { panic(err) }
return eventsArr
}
// Parses pure JSON output ( remind -ppp )
// This is more useful than simple format since it also includes
// Event information like Filename and Lineno
func parseRemindEventsJSON(str string) ([]Event, error) {
eventsArr := []Event{}
type Entry struct {
Date string
Filename string
Lineno int
Body string
}
type MonthDescriptor struct { Entries []Entry }
mds := []MonthDescriptor{}
err := json.Unmarshal([]byte(str), &mds)
if err != nil { return eventsArr, err }
for _, md := range mds {
for _, entry := range md.Entries {
t, err := time.Parse("2006-01-02", entry.Date)
if err != nil {
return eventsArr, fmt.Errorf("Could not parse REM Event date %s: %w", entry.Date, err)
}
event, err := NewEvent(t.Year(), int(t.Month()), t.Day(), entry.Body)
if err != nil { return eventsArr, err }
event.Filename = entry.Filename
event.Lineno = entry.Lineno
eventsArr = append(eventsArr, event)
}
}
return eventsArr, nil
}
// Parses line of simple format specified according to remind source code ( remind -s )
// General structure: date passhtru tags duration time body
// all fields are seperated by 1 space char ' '
// date: YEAR/MONTH/DAY of format %04d/%02d/%02d
// passthru: Out of band reminders when SPECIAL keyword used
// possible values are MOON, SHADE, COLOR, ...
// it does not seem possible to use multiple keywords at once
// tags: A tag can consist of any char except ',' and ' '. Tags are seperated by ','
// duration: single number, in minutes
// time: single number, in minutes e.g. 8:30am = 8*60+30
// body: event message
//
// Currently just using date and body
func parseRemindEventSimpleFormat(str string) (Event, error) {
dateStr, rest, found := strings.Cut(str, " ")
if !found { return Event{}, fmt.Errorf("Invalid REM Event: %s", str) }
t, err := time.Parse("2006/01/02", dateStr)
if err != nil {
return Event{}, fmt.Errorf("Could not parse REM Event date %s: %w", str[:10], err)
}
// passthru
_, rest, found = strings.Cut(rest, " ")
if !found { return Event{}, fmt.Errorf("Invalid REM Event: %s", str) }
// tags
_, rest, found = strings.Cut(rest, " ")
if !found { return Event{}, fmt.Errorf("Invalid REM Event: %s", str) }
// duration
_, rest, found = strings.Cut(rest, " ")
if !found { return Event{}, fmt.Errorf("Invalid REM Event: %s", str) }
// time
_, rest, found = strings.Cut(rest, " ")
if !found { return Event{}, fmt.Errorf("Invalid REM Event: %s", str) }
return NewEvent(t.Year(), int(t.Month()), t.Day(), rest)
}
///////////////// OTHER ////////////////////////////////
func openEditor(filename string, lineno int) {
editor := os.Getenv("EDITOR")
if editor == "" { editor = "vim" }
// for certain editors jump to lineno as well
var cmd *exec.Cmd
switch filepath.Base(editor) {
case "vi", "vim", "nano", "emacs":
cmd = exec.Command(editor, "+" + strconv.Itoa(lineno), filename)
default:
cmd = exec.Command(editor, filename)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil { panic(err) }
}