/
7d.go
220 lines (201 loc) · 4.58 KB
/
7d.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
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
// Seven Days (7d) is a simple calendar software.
//
// Main point is that quite often I find myself being only interested in the stuff that is
// happening during the next seven days.
// I also tend to have weekly entries and I dislike complex syntax or having to click through
// pile of menus before I can add simple entry.
//
// 7d reads a simple plaintext file that has calendar entries formatted in simple syntax:
// each|every <weekday> <description>
// or
// <date> <description>
//
// Everything else is discarded so you can store whatever else you want in the same file.
//
// <date> can be formatted in three ways:
// 25.3.2012
// 2012-03-25
// 25.3 <- Uses the _current_ year.
//
// One thing to note: If current day is Wednesday for example,
// the displayed Monday and Tuesday will be the _next_ weeks, not the previous days.
// You will never see past dates. (Unless you offset the week that is)
//
//author: Erno Hopearuoho ( https://github.com/errnoh )
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)
var filename *string = flag.String("file", "", "file to read the data from")
// A single calendar entry
//
// text: the description
// priority: 0 if entry is weekly, 2 if entry is marked within 2 days
type entry struct {
text string
priority int
}
func (e entry) String() string {
var s string
switch e.priority {
case 0:
s += "\x1b[1;32m"
case 1:
return e.text
case 2:
s += "\x1b[1;31m"
}
s += " " + e.text + "\x1b[0m\n"
return s
}
// A single day. :)
//
// number: int identifier of the weekday, starting from Sunday being 0.
// entries: slice of entries
type Day struct {
number int
entries []*entry
}
func (d Day) String() (s string) {
if d.number == today {
s += "\x1b[1;37m" + time.Weekday(d.number%7).String() + "\x1b[0m:\n"
} else {
s += time.Weekday(d.number%7).String() + ":\n"
}
for _, e := range d.entries {
s += e.String()
}
return
}
// Week contains days. Usually seven.
type Week []Day
var week Week = make([]Day, 7)
var today int
func main() {
flag.Parse()
now := time.Now()
hour, min, sec := now.Clock()
extra, _ := time.ParseDuration(fmt.Sprintf("-%sh%sm%ss", hour, min, sec))
now = now.Add(extra)
today = int(now.Weekday())
initWeek()
parseData(*filename)
//println(week.String())
println(week.StringFromToday())
}
func chkerr(err error) {
if err != nil {
println("<ERROR> " + err.Error())
os.Exit(1)
}
}
func initWeek() {
for i := 0; i < len(week); i++ {
d := new(Day)
d.entries = make([]*entry, 0, 5)
d.number = i
week[i] = *d
}
}
/*
* Reads a plaintext file with calendar entries.
* Entries should be formatted as either:
* each|every <weekday> <description>
* or
* <date> <description>
*/
func parseData(target string) {
var day int
contents, err := ioutil.ReadFile(target)
chkerr(err)
rows := strings.Split(string(contents), "\n")
for _, r := range rows {
row := strings.Split(r, " ")
if len(row) < 2 {
continue
}
if strings.ToLower(row[0]) == "each" || strings.ToLower(row[0]) == "every" {
day = getDay(row[1])
if day == 7 {
continue
}
addEntry(row[2:], day, 0)
} else {
date, err := parseTime(row[0])
if err != nil {
continue
}
since := time.Since(date.Add(time.Hour*23 + time.Minute*59))
// no need for entries in the past
if since > 0 {
continue
}
untildate := (time.Hour * 24 * 7) + since
if untildate > (time.Hour * 24 * 4) {
addEntry(row[1:], int(date.Weekday()), 2)
} else if untildate > 0 {
addEntry(row[1:], int(date.Weekday()), 1)
}
}
}
}
func addEntry(text []string, day int, priority int) {
s := ""
for _, word := range text {
s += word + " "
}
week[day].entries = append(week[day].entries, &entry{text: s, priority: priority})
}
/*
* Parses the <date>, supported formatting:
* 25.3.2012
* 2012-03-25
* 25.3 <- Uses the _current_ year.
*/
func parseTime(s string) (date time.Time, err error) {
date, err = time.Parse("2006-01-02", s)
if err == nil {
return
}
date, err = time.Parse("2.1.2006", s)
if err == nil {
return
}
date, err = time.Parse("2.1", s)
if err == nil {
today := time.Now()
date = date.AddDate(today.Year(), 0, 0)
if today.After(date) {
date = date.AddDate(1, 0, 0)
}
}
return
}
/*
* Returns the <weekday> as integer, Sunday being 0
*/
func getDay(day string) int {
day = strings.ToLower(day)
switch day {
case "monday":
return 1
case "tuesday":
return 2
case "wednesday":
return 3
case "thursday":
return 4
case "friday":
return 5
case "saturday":
return 6
case "sunday":
return 0
}
return 7
}