-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhistory.go
113 lines (90 loc) · 2.67 KB
/
history.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
package openpomodoro
import (
"bytes"
"encoding/json"
"sort"
"time"
)
// History is a collection of Pomodoros.
type History struct {
Pomodoros []*Pomodoro `json:"pomodoros"`
}
// sort.Interface
func (h History) Len() int { return len(h.Pomodoros) }
func (h History) Swap(i, j int) { h.Pomodoros[i], h.Pomodoros[j] = h.Pomodoros[j], h.Pomodoros[i] }
func (h History) Less(i, j int) bool { return h.Pomodoros[i].StartTime.Before(h.Pomodoros[j].StartTime) }
// MarshalJSON implements json.Marshaler.
func (h History) MarshalJSON() ([]byte, error) {
// This is required so that json.Marshal ignores that we also implement
// encoding.TextMarshaler via MarshalText.
type alias History
return json.Marshal((alias)(h))
}
// MarshalText implements encoding.TextMarshaler. It returns a byte slice of
// each Pomodoro in the History also marshaled, separated by a newline.
func (h History) MarshalText() ([]byte, error) {
var bs [][]byte
for _, p := range h.Pomodoros {
b, err := p.MarshalText()
if err != nil {
return nil, err
}
bs = append(bs, b)
}
bs = append(bs, nil)
return bytes.Join(bs, charNewline), nil
}
// Latest sorts the collection and then returns the latest Pomodoro.
func (h *History) Latest() *Pomodoro {
sort.Sort(h)
n := len(h.Pomodoros)
if n == 0 {
return nil
}
return h.Pomodoros[n-1]
}
// Count returns the total Pomodoro count.
func (h *History) Count() int {
return len(h.Pomodoros)
}
// Date returns a new History collection for the given date.
func (h *History) Date(date time.Time) *History {
y, m, d := date.Date()
today := time.Date(y, m, d, 0, 0, 0, 0, date.Location())
tomorrow := today.AddDate(0, 0, 1)
return h.Range(today, tomorrow)
}
// Range returns a new History collection between the start and end times.
func (h *History) Range(start time.Time, end time.Time) *History {
result := &History{}
for _, pomodoro := range h.Pomodoros {
if t := pomodoro.StartTime; t.Before(start) || t.After(end) {
continue
}
result.Pomodoros = append(result.Pomodoros, pomodoro)
}
return result
}
// Update replaces a Pomodoro within a History collection in place. If the
// Pomodoro does not exist in the collection, it is appended and then the
// collection is sorted.
func (h *History) Update(p *Pomodoro) {
for i, needle := range h.Pomodoros {
if needle.Matches(p) {
h.Pomodoros[i] = p
return
}
}
h.Pomodoros = append(h.Pomodoros, p)
sort.Sort(h)
}
// Delete removes a Pomodoro from a History collection in place.
func (h *History) Delete(p *Pomodoro) {
new := &History{}
for _, needle := range h.Pomodoros {
if !needle.Matches(p) {
new.Pomodoros = append(new.Pomodoros, needle)
}
}
h.Pomodoros = new.Pomodoros
}