-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
widget_time.go
97 lines (82 loc) · 1.88 KB
/
widget_time.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
package main
import (
"image"
"image/color"
"strings"
"time"
)
// TimeWidget is a widget displaying the current time/date.
type TimeWidget struct {
*BaseWidget
formats []string
fonts []string
colors []color.Color
frames []image.Rectangle
}
// NewTimeWidget returns a new TimeWidget.
func NewTimeWidget(bw *BaseWidget, opts WidgetConfig) *TimeWidget {
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second/2)
var formats, fonts, frameReps []string
_ = ConfigValue(opts.Config["format"], &formats)
_ = ConfigValue(opts.Config["font"], &fonts)
_ = ConfigValue(opts.Config["layout"], &frameReps)
var colors []color.Color
_ = ConfigValue(opts.Config["color"], &colors)
layout := NewLayout(int(bw.dev.Pixels))
frames := layout.FormatLayout(frameReps, len(formats))
for i := 0; i < len(formats); i++ {
if len(fonts) < i+1 {
fonts = append(fonts, "regular")
}
if len(colors) < i+1 {
colors = append(colors, DefaultColor)
}
}
return &TimeWidget{
BaseWidget: bw,
formats: formats,
fonts: fonts,
colors: colors,
frames: frames,
}
}
// Update renders the widget.
func (w *TimeWidget) Update() error {
size := int(w.dev.Pixels)
img := image.NewRGBA(image.Rect(0, 0, size, size))
for i := 0; i < len(w.formats); i++ {
str := formatTime(time.Now(), w.formats[i])
font := fontByName(w.fonts[i])
drawString(img,
w.frames[i],
font,
str,
w.dev.DPI,
-1,
w.colors[i],
image.Pt(-1, -1))
}
return w.render(w.dev, img)
}
func formatTime(t time.Time, format string) string {
tm := map[string]string{
"%Y": "2006",
"%y": "06",
"%F": "January",
"%M": "Jan",
"%m": "01",
"%l": "Monday",
"%D": "Mon",
"%d": "02",
"%h": "03",
"%H": "15",
"%i": "04",
"%s": "05",
"%a": "PM",
"%t": "MST",
}
for k, v := range tm {
format = strings.ReplaceAll(format, k, v)
}
return t.Format(format)
}