This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume_bar.go
52 lines (42 loc) · 1.14 KB
/
volume_bar.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
// This file is a part of git.thekondor.net/zvuchno.git (mirror: github.com/thekondor/zvuchno)
package main
import (
"bytes"
"fmt"
"github.com/cheggaaa/pb"
"log"
"text/template"
)
type VolumeBar struct {
progressBar *pb.ProgressBar
tpl *template.Template
}
func NewVolumeBar(width byte, formatTpl, barFormat string) VolumeBar {
bar := pb.New(100)
bar.ManualUpdate = true
bar.ShowCounters = false
bar.ShowTimeLeft = false
bar.ShowFinalTime = false
bar.ShowPercent = false
bar.SetWidth(int(width))
bar.Format(barFormat)
return VolumeBar{bar, template.Must(template.New("full format").Parse(formatTpl))}
}
func (vb VolumeBar) Update(value int) string {
percent := value / 655
vb.progressBar.Set(percent)
vb.progressBar.Update()
return vb.toString(percent)
}
func (vb VolumeBar) toString(percent int) string {
var output bytes.Buffer
progress := vb.progressBar.String()
if err := vb.tpl.Execute(&output, struct {
Percent int
Bar string
}{percent, progress}); nil != err {
log.Printf("W: template formatting error = %s", err)
return fmt.Sprintf("(fallback): %d%% %s", percent, progress)
}
return output.String()
}