-
Notifications
You must be signed in to change notification settings - Fork 7
/
mainview.go
116 lines (101 loc) · 2.65 KB
/
mainview.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
package main
import (
"fmt"
"github.com/dhowden/tag"
"github.com/eugene-eeo/orchid/liborchid"
"github.com/nsf/termbox-go"
)
const ATTR_DIM = termbox.Attribute(0xf0)
const ATTR_DEFAULT = termbox.ColorDefault
// Layout (50x8)
// ┌────────┐
// │ │ Album (Year)
// │ 8x16 │ <Play/Pause> Title
// │ │ Artist
// │ │ Track [i/n]
// │ │
// └────────┘ Progress
//
type playerView struct {
current *liborchid.Song
image string
metadata tag.Metadata
}
func newPlayerView() *playerView {
return &playerView{
current: nil,
image: DefaultImage,
}
}
func (pv *playerView) drawCurrent(paused, shuffle, repeat bool) {
name := getPlayingIndicator(paused, shuffle) + " " + getSongTitle(pv.current, pv.metadata)
attr := termbox.AttrBold
if repeat {
attr = termbox.AttrReverse
}
drawName(name, 18, 2, attr)
}
func (pv *playerView) drawImage() {
fmt.Print("\033[0;0H" + pv.image + "\u001B[?25l")
}
func (pv *playerView) drawMetaData() {
m := pv.metadata
if m == nil {
return
}
album := defaultString(m.Album(), "Unknown album")
year := defaultString(defaultInt(m.Year()), "?")
artist := defaultString(m.Artist(), "Unknown artist")
track, total := m.Track()
drawName(fmt.Sprintf("%s (%s)", album, year), 20, 1, ATTR_DEFAULT)
drawName(artist, 20, 3, ATTR_DEFAULT)
drawName(fmt.Sprintf("Track [%d/%d]", track, total), 20, 4, ATTR_DEFAULT)
}
func (pv *playerView) drawProgress(progress float64) {
b := int(progress*31) + 18
for x := 18; x < 50; x++ {
a := ATTR_DIM
if x <= b {
a = ATTR_DEFAULT
}
termbox.SetCell(x, 6, '─', a, ATTR_DEFAULT)
}
termbox.SetCell(b, 6, '╼', ATTR_DEFAULT, ATTR_DEFAULT)
}
func (pv *playerView) Update(player *liborchid.Queue, progress float64, paused, shuffle, repeat bool) {
must(termbox.Clear(ATTR_DEFAULT, ATTR_DEFAULT))
song := player.Song()
if song != nil && song != pv.current {
pv.current = song
pv.metadata = song.Metadata()
pv.image = getImage(pv.metadata)
}
pv.drawMetaData()
pv.drawCurrent(paused, shuffle, repeat)
pv.drawProgress(progress)
must(termbox.Flush())
pv.drawImage()
}
func drawName(name string, x int, y int, color termbox.Attribute) {
unicodeCells(name, 49-x, true, func(dx int, r rune) {
termbox.SetCell(x+dx, y, r, color, ATTR_DEFAULT)
})
}
func getSongTitle(song *liborchid.Song, metadata tag.Metadata) string {
if song == nil {
return "<No Songs>"
}
if metadata != nil {
return defaultString(metadata.Title(), song.Name())
}
return song.Name()
}
func getPlayingIndicator(paused, shuffle bool) string {
if paused {
return "Ⅱ"
}
if shuffle {
return "⥮"
}
return "▶"
}