-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
period.go
84 lines (69 loc) · 1.7 KB
/
period.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
package main
import (
"fmt"
"time"
"github.com/getlantern/systray"
"github.com/hako/durafmt"
)
type period struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
func (p *period) duration() time.Duration {
return calculateDuration(p.Start, p.End)
}
func (p *period) string() string {
var format string
if p.Start.Day() == time.Now().Day() {
format = "15:04"
} else {
format = "2 Jan 15:04"
}
duration := p.duration()
limit := 1
if duration > time.Hour {
limit = 2
}
return fmt.Sprintf(
"%s - %s (%s)",
p.Start.Format(format),
p.End.Format(format),
durafmt.Parse(duration).LimitFirstN(limit).String(),
)
}
func (a *app) readPeriodsFromStorage(key string) ([]period, error) {
var periods []period
err := a.defaults.Unmarshal(key, &periods)
return periods, err
}
func (a *app) savePeriodsToStorage(key string, periods []period) error {
return a.defaults.Marshal(key, periods)
}
func updatePeriodMenuItems(
periods []period,
periodsMenuItem *systray.MenuItem,
currentMenuItems []*systray.MenuItem,
) []*systray.MenuItem {
menuItems := currentMenuItems
totalNewMenuItems := len(periods) - len(menuItems)
fmt.Printf("totalNewMenuItems: %v", totalNewMenuItems)
if totalNewMenuItems < 0 {
// Hide redundant menu items.
for i := len(menuItems) - 1; i >= len(menuItems)-(-totalNewMenuItems); i-- {
menuItems[i].Hide()
}
} else {
// Add missing menu items.
for i := 0; i < totalNewMenuItems; i++ {
item := periodsMenuItem.AddSubMenuItem("", "")
item.Disable()
item.Show()
menuItems = append(menuItems, item)
}
}
length := len(periods)
for index, entry := range periods {
menuItems[length-index-1].SetTitle(entry.string())
}
return menuItems
}