This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
popups.go
398 lines (338 loc) · 9.38 KB
/
popups.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"image"
"log"
"math"
"os"
"path"
"strconv"
"time"
"github.com/BurntSushi/xgbutil/xgraphics"
"github.com/IvanMenshykov/MoonPhase"
"github.com/RadhiFadlillah/go-prayer"
"github.com/elliotchance/orderedmap"
"github.com/fhs/gompd/mpd"
"github.com/rkoesters/xdg/userdirs"
"golang.org/x/image/math/fixed"
)
func (bar *Bar) initPopups() {
bar.popups.Set("clock", &Popup{
x: (bar.w / 2) - (184 / 2),
y: bar.h,
w: 184,
h: 118,
update: func() {
popup := bar.popup("clock")
// Color the background.
popup.img.For(func(cx, cy int) xgraphics.BGRA {
return xgraphics.BGRA{B: 238, G: 238, R: 238, A: 0xFF}
})
// Set foreground color.
popup.drawer.Src = image.NewUniform(xgraphics.BGRA{B: 33, G: 27,
R: 2, A: 0xFF})
// Get the current time. Present Day, heh... Present Time! Hahahaha!
n := time.Now()
// Get moon phase.
mc := MoonPhase.New(n)
mn := map[int]string{
0: "Ɔ",
1: "Ƈ",
2: "ƈ",
3: "Ɖ",
4: "Ɗ",
5: "Ƌ",
6: "ƌ",
7: "ƍ",
8: "Ƈ",
}
// Draw moon text.
popup.drawer.Dot = fixed.P(19, 48)
popup.drawer.DrawString("The moon currently looks like: " + mn[int(
math.Floor((mc.Phase()+0.0625)*8))])
// Get the prayers.
pm := (&prayer.Calculator{
Latitude: 52.1277,
Longitude: 5.6686,
Elevation: 21,
CalculationMethod: prayer.MWL,
AsrConvention: prayer.Hanafi,
PreciseToSeconds: false,
}).Init().SetDate(n).Calculate()
// The prayers we want to track.
pom := orderedmap.NewOrderedMap()
pom.Set("Fajr", pm[prayer.Fajr])
pom.Set("Zuhr", pm[prayer.Zuhr])
pom.Set("Asr", pm[prayer.Asr])
pom.Set("Maghrib", pm[prayer.Maghrib])
pom.Set("Isha", pm[prayer.Isha])
// Calculate the dot lenght, this is the length of the line (164px)
// divided by 2400 (minutes in a day).
d := 164.00 / 2400.00
// Calculate elapsed line length.
e := int(math.Round(d*float64(n.Hour()*100+n.Minute()))) + 10
// Draw line.
popup.img.SubImage(image.Rect(10, 101, 10+164, 102)).(*xgraphics.
Image).For(func(x, y int) xgraphics.BGRA {
// Make the line look dashed.
if x%5 == 4 {
return xgraphics.BGRA{B: 238, G: 238, R: 238, A: 0xFF}
}
if x < e {
return xgraphics.BGRA{B: 33, G: 27, R: 2, A: 0xFF}
}
return xgraphics.BGRA{B: 211, G: 167, R: 114, A: 0xFF}
})
// Loop over these prayers and draw stuff for each one.
tm := false
np := false
for p := pom.Front(); p != nil; p = p.Next() {
k := p.Key.(string)
v := p.Value.(time.Time)
// Calculate arrow position.
pd := int(math.Round(d*float64(v.Hour()*100+v.Minute()))) + 9
// Set arrow color.
popup.drawer.Src = image.NewUniform(xgraphics.BGRA{B: 211,
G: 167, R: 114, A: 0xFF})
if tm || (!np && v.Unix() > n.Add(-time.Hour).Unix()) {
np = true
// Set arrow color for next prayer.
popup.drawer.Src = image.NewUniform(xgraphics.BGRA{B: 33,
G: 27, R: 2, A: 0xFF})
// Compose arrow text.
s := k + ", " + v.Format("03:04 PM")
// Calculate X offset, we use some smart logic in order to
// always have nice padding, even with longer strings.
sl := popup.drawer.MeasureString(s).Round()
x := pd + 2 - (sl / 2)
if x < 10 {
x = 10
} else if x > 184-8-sl {
x = 184 - 8 - sl
}
// Draw arrow text.
popup.drawer.Dot = fixed.P(x, 85)
popup.drawer.DrawString(s)
}
// Workaround if the next prayer is tomorrow.
if p.Next() == nil && !np {
tm = true
pom.Delete("Fajr")
pom.Set("Fajr", pm[prayer.Fajr])
}
// Draw arrow.
popup.drawer.Dot = fixed.P(pd+2, 98)
popup.drawer.DrawString("↓")
}
// Redraw the popup.
popup.draw()
},
})
bar.popups.Set("music", &Popup{
x: bar.w - 327 - bar.h,
y: bar.h,
w: 327,
h: 148,
update: func() {
popup := bar.popup("music")
cur, err := bar.store["mpd"].(*mpd.Client).CurrentSong()
if err != nil {
log.Println(err)
return
}
sts, err := bar.store["mpd"].(*mpd.Client).Status()
if err != nil {
log.Println(err)
return
}
// Color the background.
popup.img.For(func(cx, cy int) xgraphics.BGRA {
return xgraphics.BGRA{B: 238, G: 238, R: 238, A: 0xFF}
})
// Set foreground color.
popup.drawer.Src = image.NewUniform(xgraphics.BGRA{B: 33, G: 27,
R: 2, A: 0xFF})
// Draw album text.
album := trim(cur["Album"], 32)
popup.drawer.Dot = fixed.P(-(popup.drawer.MeasureString(album).
Ceil()/2)+90, 48)
popup.drawer.DrawString(album)
// Draw artist text.
artist := trim("Artist: "+cur["AlbumArtist"], 32)
popup.drawer.Dot = fixed.P(-(popup.drawer.MeasureString(artist).
Ceil()/2)+90, 58+16)
popup.drawer.DrawString(artist)
// Draw rlease date text.
date := trim("Release date: "+cur["Date"], 32)
popup.drawer.Dot = fixed.P(-(popup.drawer.MeasureString(date).
Ceil()/2)+90, 58+16+16)
popup.drawer.DrawString(date)
// Check if the cover art file exists.
fp := path.Join(userdirs.Music, path.Dir(cur["file"]),
"cover_popup.png")
if _, err := os.Stat(fp); !os.IsNotExist(err) {
f, err := os.Open(fp)
if err != nil {
log.Println(err)
return
}
defer f.Close()
// Draw cover art.
img, _, err := image.Decode(f)
if err != nil {
log.Println(err)
return
}
xgraphics.Blend(popup.img, xgraphics.NewConvert(X, img), image.
Point{-179, -0})
} else {
popup.drawer.Dot = fixed.P(218, 78)
popup.drawer.DrawString("No cover found!")
}
// Get elapsed and duration times.
se, err := strconv.ParseFloat(sts["elapsed"], 32)
if err != nil {
log.Println(err)
return
}
//e := int(math.Round(se))
// Calculate the dot lenght, this is the length of the line divided
// by the length of the song.
sd, err := strconv.ParseFloat(sts["duration"], 32)
if err != nil {
log.Println(err)
return
}
d := 159.00 / sd
// Calculate elapsed line length.
e := int(math.Round(d*se)) + 10
// Draw line.
popup.img.SubImage(image.Rect(10, 131, 10+159, 132)).(*xgraphics.
Image).For(func(x, y int) xgraphics.BGRA {
// Make the line look dashed.
if x%5 == 4 {
return xgraphics.BGRA{B: 238, G: 238, R: 238, A: 0xFF}
}
if x < e {
return xgraphics.BGRA{B: 33, G: 27, R: 2, A: 0xFF}
}
return xgraphics.BGRA{B: 211, G: 167, R: 114, A: 0xFF}
})
// Redraw the popup.
popup.draw()
},
})
/*bar.popups.Set("clock", &Popup{
x: (bar.w / 2) - (178 / 2),
y: bar.h,
w: 178,
h: 129,
update: func() {
popup := bar.popup("clock")
// Color the background.
f, err := runtime.Open("/images/clock-popup-bg.png")
if err != nil {
log.Println(err)
return
}
defer f.Close()
bg, _, err := image.Decode(f.(io.Reader))
if err != nil {
log.Println(err)
return
}
xgraphics.Blend(popup.img, xgraphics.NewConvert(X, bg), image.Point{
0, 0})
// Redraw the popup.
popup.draw()
// Set foreground color.
popup.drawer.Src = image.NewUniform(xgraphics.BGRA{B: 33, G: 27,
R: 2, A: 0xFF})
// Create http client with a timeout.
c := &http.Client{Timeout: time.Duration(2 * time.Second)}
// Get weather information.
r, err := c.Get(
"https://api.buienradar.nl/data/public/2.0/jsonfeed")
if err != nil {
log.Println(err)
return
}
defer r.Body.Close()
jq := gojsonq.New().Reader(r.Body).From(
"actual.stationmeasurements.[0]").WhereEqual("stationid",
"6260")
// Draw weather information.
popup.drawer.Dot = fixed.P(11, 101)
popup.drawer.DrawString("Rainfall graph, it's " + fmt.Sprint(jq.
Find("feeltemperature")) + "°C.")
// Redraw the popup.
popup.draw()
// Set location.
lat := "52.0646"
lon := "5.2065"
// Get rainfall information.
r, err = c.Get(
"https://gpsgadget.buienradar.nl/data/raintext?lat=" + lat +
"&lon=" + lon)
if err != nil {
log.Println(err)
return
}
defer r.Body.Close()
// Create rainfall tmp files.
td, err := ioutil.TempFile(os.TempDir(), "melonbar-rain-*.dat")
if err != nil {
log.Println(err)
return
}
defer os.Remove(td.Name())
ti, err := ioutil.TempFile(os.TempDir(), "melonbar-rain-*.png")
if err != nil {
log.Println(err)
return
}
defer os.Remove(ti.Name())
// Compose rainfall data tmp file contents.
var d []byte
s := bufio.NewScanner(r.Body)
for s.Scan() {
d = append(d, bytes.Split(s.Bytes(), []byte("|"))[0]...)
d = append(d, []byte("\n")...)
}
// Write rainfall data tmp file.
if _, err = td.Write(d); err != nil {
log.Println(err)
return
}
if err := td.Close(); err != nil {
log.Println(err)
return
}
// Create rainfall graph.
cmd := exec.Command("gnuplot", "-e", `
set terminal png transparent size 205,107;
set output '`+ti.Name()+`';
set yrange [0:255];
set noborder;
set nolabel;
set nokey;
set notics;
set notitle;
plot '`+td.Name()+`' smooth csplines w filledcu x1 fc rgb '#72A7D3', '`+td.Name()+`' smooth csplines w line lc rgb '#5394C9';
`)
if err := cmd.Run(); err != nil {
log.Println(err)
return
}
// Draw rainfall graph.
img, _, err := image.Decode(ti)
if err != nil {
log.Println(err)
return
}
xgraphics.Blend(popup.img, xgraphics.NewConvert(X, img), image.
Point{11, 2})
// Redraw the popup.
popup.draw()
},
})*/
}