-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatting.go
56 lines (48 loc) · 1.25 KB
/
formatting.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
package main
import (
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/SoMuchForSubtlety/opendj"
)
func fmtDuration(d time.Duration) string {
return d.String()
}
func durationBar(width int, fraction time.Duration, total time.Duration) string {
base := strings.Repeat("—", width)
pos := float64(fraction.Seconds() / total.Seconds())
newpos := int(float64(utf8.RuneCountInString(base))*pos) - 1
if newpos < 0 {
newpos = 0
} else if newpos >= utf8.RuneCountInString(base) {
newpos = utf8.RuneCountInString(base) - 1
}
return replaceAtIndex(base, '⚫', newpos)
}
func replaceAtIndex(in string, r rune, i int) string {
out := ""
j := 0
for _, c := range in {
if j != i {
out += string(c)
} else {
out += string(r)
}
j++
}
return out
}
func formatPlaylist(queue []opendj.QueueEntry, currentEntry opendj.QueueEntry) string {
lines := fmt.Sprintf(" curretly playing: 🎶 %q 🎶 requested by %s\n\n", currentEntry.Media.Title, currentEntry.Owner)
var maxname int
for _, vid := range queue {
if len(vid.Owner) > maxname {
maxname = len(vid.Owner)
}
}
for i, vid := range queue {
lines += fmt.Sprintf(" %2v | %-*v | %v | %v\n", i+1, maxname, vid.Owner, fmtDuration(vid.Media.Duration), vid.Media.Title)
}
return lines
}