-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrols.go
64 lines (52 loc) · 1.25 KB
/
controls.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
package main
import (
"fmt"
"html/template"
"net/http"
"go.tmthrgd.dev/spork/dbus"
"go.tmthrgd.dev/spork/web"
)
var controlTmpl = web.NewTemplate("control.tmpl", template.FuncMap{
"FormatLength": formatLength,
})
func controlsHandler() http.HandlerFunc {
return web.ErrorHandler(func(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
volume, err := dbus.GetVolume(ctx)
if err != nil {
return err
}
pos, err := dbus.GetPlaylistPosition(ctx)
if err != nil {
return err
}
title, err := dbus.GetSongTitle(ctx, pos)
if err != nil {
return err
}
length, err := dbus.GetSongLength(ctx, pos)
if err != nil {
return err
}
shuffle, err := dbus.GetShuffle(ctx)
if err != nil {
return err
}
repeat, err := dbus.GetRepeat(ctx)
if err != nil {
return err
}
return web.WriteTemplateResponse(w, controlTmpl, &struct {
Pos uint32
Volume int32
Title string
Length int32
Shuffle, Repeat bool
}{pos, volume, title, length, shuffle, repeat})
})
}
// formatLength formats a given song length in seconds into a display friendly
// minute:seconds format.
func formatLength(length int32) string {
return fmt.Sprintf("%d:%02d", length/60, length%60)
}