forked from issadarkthing/gomu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playingbar.go
146 lines (116 loc) · 2.82 KB
/
playingbar.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
// Copyright (C) 2020 Raziman
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/spf13/viper"
"github.com/ztrue/tracerr"
)
type PlayingBar struct {
*tview.Frame
full int
limit int
progress chan int
_progress int
skip bool
text *tview.TextView
}
func (p *PlayingBar) help() []string {
return []string{}
}
// Playing bar shows progress of the song and the title of the song
func newPlayingBar() *PlayingBar {
textView := tview.NewTextView().SetTextAlign(tview.AlignCenter)
frame := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
frame.SetBorder(true).SetTitle(" Now Playing ")
p := &PlayingBar{
Frame: frame,
text: textView,
progress: make(chan int),
}
p.setDefault()
textView.SetChangedFunc(func() {
gomu.app.Draw()
if !gomu.player.isRunning {
p.setDefault()
}
})
return p
}
// Start processing progress bar
func (p *PlayingBar) run() error {
// When app is suspending, we want the progress bar to stop progressing
// because it causes screen to hang-up when app starts to stop suspending
// accumulate when app is suspending
acc := 0
wasSuspended := false
for {
// stop progressing if song ends or skipped
if p._progress > p.full || p.skip {
p.skip = false
p._progress = 0
break
}
if gomu.isSuspend {
// channel the progress to acc
acc += <-p.progress
wasSuspended = true
continue
} else {
// normal progressing
p._progress += <-p.progress
}
if wasSuspended {
// add back so that we dont lose track in progress bar
p._progress += acc
wasSuspended = false
acc = 0
}
p.text.Clear()
start, err := time.ParseDuration(strconv.Itoa(p._progress) + "s")
if err != nil {
return tracerr.Wrap(err)
}
end, err := time.ParseDuration(strconv.Itoa(p.full) + "s")
if err != nil {
return tracerr.Wrap(err)
}
progressBar := progresStr(p._progress, p.full, p.limit, "█", "━")
// our progress bar
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s",
fmtDuration(start),
progressBar,
fmtDuration(end),
))
}
return nil
}
// Updates song title
func (p *PlayingBar) setSongTitle(title string) {
p.Clear()
titleColor := viper.GetString("color.now_playing_title")
p.AddText(title, true, tview.AlignCenter, tcell.GetColor(titleColor))
}
// Resets progress bar, ready for execution
func (p *PlayingBar) newProgress(songTitle string, full, limit int) {
p.full = full
p.limit = limit
p._progress = 0
p.setSongTitle(songTitle)
}
// Sets default title and progress bar
func (p *PlayingBar) setDefault() {
p.setSongTitle("---------:---------")
text := fmt.Sprintf(
"%s ┣%s┫ %s", "00:00", strings.Repeat("━", 100), "00:00",
)
p.text.SetText(text)
}
// Skips the current playing song
func (p *PlayingBar) stop() {
p.skip = true
}