Skip to content

Commit 55dc242

Browse files
author
Mateusz Lopacinski
committed
refactor ui a little, introduce volume widget
1 parent 231fa3c commit 55dc242

File tree

3 files changed

+101
-34
lines changed

3 files changed

+101
-34
lines changed

pkg/ui/events.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"regexp"
77
"strconv"
8+
"strings"
89

910
"github.com/robopuff/goradio/pkg/driver"
1011

@@ -20,11 +21,11 @@ var colorSelected = "(fg:black,bg:white)"
2021

2122
func manageKeyboardEvent(e tui.Event, d driver.Driver) int {
2223
if e.Type == tui.ResizeEvent {
23-
windowResize()
24+
windowResize(e)
2425
}
2526

2627
switch e.ID {
27-
case "<Enter>":
28+
case "<Enter>", "<MouseLeft>":
2829
selected := uiStationsList.SelectedRow
2930
selectedStation := stationsList.GetSelected(selected)
3031
currentStation := stationsList.GetSelected(current)
@@ -64,9 +65,9 @@ func manageKeyboardEvent(e tui.Event, d driver.Driver) int {
6465
d.Mute()
6566
case "p":
6667
d.Pause()
67-
case "k", "<Up>":
68+
case "k", "<Up>", "<MouseWheelUp>":
6869
uiStationsList.ScrollUp()
69-
case "j", "<Down>":
70+
case "j", "<Down>", "<MouseWheelDown>":
7071
uiStationsList.ScrollDown()
7172
case "K", "<PageUp>":
7273
uiStationsList.ScrollPageUp()
@@ -80,8 +81,6 @@ func manageKeyboardEvent(e tui.Event, d driver.Driver) int {
8081
d.IncVolume()
8182
case "-":
8283
d.DecVolume()
83-
//case "D":
84-
//toggleDebug()
8584
case "q", "<C-c>", "<Esc>":
8685
d.Close()
8786
return 1
@@ -114,9 +113,12 @@ func manageDriverLogs(d driver.Driver) {
114113
match = volumeRegex.FindStringSubmatch(data)
115114
if len(match) > 0 {
116115
setVolumeGauge(match[1])
116+
continue
117117
}
118118

119-
sendToLog(data[:len(data)-1])
119+
if data != "" {
120+
sendToLog(strings.Trim(data, "\n"))
121+
}
120122
}
121123
}
122124
}
@@ -125,18 +127,20 @@ func manageDriverLogs(d driver.Driver) {
125127
func setVolumeGauge(value string) {
126128
volume, _ := strconv.Atoi(value)
127129
volumeGauge.Percent = volume
128-
if value == "" {
129-
value = " "
130+
volumeGauge.Visible = true
131+
132+
if volume == 0 {
133+
volumeGauge.Visible = false
130134
}
131135

132-
volumeGauge.Label = value
133136
render()
134137
}
135138

136139
func setCurrentlyPlaying(currently string) {
137140
format := fmt.Sprintf(fullLineFormatter, currentlyPlayingFormat)
138141
if currently == "" {
139-
currently = "None"
142+
format = "%s"
143+
currently = ""
140144
}
141145
uiPlayingParagraph.Text = fmt.Sprintf(format, currently)
142146
render()

pkg/ui/ui.go

+20-23
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ var (
2929
uiFooterParagraph *widgets.Paragraph
3030
uiStationsList *widgets.List
3131
uiLoggerList *widgets.List
32-
volumeGauge *widgets.Gauge
32+
volumeGauge *volume
3333
drawables []tui.Drawable
3434
)
3535

3636
// Init initialize UI
3737
func Init(csvStationsList *stations.List, debugFlag bool) error {
38-
fullLineFormatter = fmt.Sprintf("%%-%ds", w)
39-
debug = debugFlag
40-
stationsList = csvStationsList
41-
4238
if err := tui.Init(); nil != err {
4339
return err
4440
}
4541

42+
w, h = tui.TerminalDimensions()
43+
fullLineFormatter = fmt.Sprintf("%%-%ds", w)
44+
45+
debug = debugFlag
46+
stationsList = csvStationsList
47+
4648
uiPlayingParagraph = widgets.NewParagraph()
4749
uiPlayingParagraph.Border = false
4850
uiPlayingParagraph.TextStyle.Fg = tui.ColorRed
@@ -73,12 +75,16 @@ func Init(csvStationsList *stations.List, debugFlag bool) error {
7375
uiStationsList.BorderStyle.Fg = colorGray
7476
uiStationsList.WrapText = false
7577

76-
volumeGauge = widgets.NewGauge()
78+
volumeGauge = NewVolume()
7779
volumeGauge.Border = false
7880
volumeGauge.Percent = 0
79-
volumeGauge.Label = " "
8081

81-
windowResize()
82+
windowResize(tui.Event{
83+
Payload: tui.Resize{
84+
Width: w,
85+
Height: h,
86+
},
87+
})
8288
drawables = []tui.Drawable{
8389
uiPlayingParagraph,
8490
volumeGauge,
@@ -94,9 +100,13 @@ func Init(csvStationsList *stations.List, debugFlag bool) error {
94100
}
95101

96102
// Init initialize UI
97-
func windowResize() {
103+
func windowResize(e tui.Event) {
104+
payload := e.Payload.(tui.Resize)
98105
tui.Clear()
99-
w, h = tui.TerminalDimensions()
106+
107+
w = payload.Width
108+
h = payload.Height
109+
fullLineFormatter = fmt.Sprintf("%%-%ds", w)
100110

101111
uiPlayingParagraph.SetRect(0, -1, w, 3)
102112
uiFooterParagraph.SetRect(0, h-3, w, h)
@@ -140,19 +150,6 @@ func Run(d driver.Driver) {
140150
}
141151
}
142152

143-
func toggleDebug() {
144-
debug = !debug
145-
146-
if debug {
147-
drawables = append(drawables, uiLoggerList)
148-
} else {
149-
drawables = drawables[:len(drawables)-1]
150-
}
151-
152-
windowResize()
153-
render()
154-
}
155-
156153
func render() {
157154
tui.Render(drawables...)
158155
}

pkg/ui/volume.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ui
2+
3+
import (
4+
"image"
5+
"strconv"
6+
7+
tui "github.com/gizak/termui/v3"
8+
)
9+
10+
type volume struct {
11+
tui.Block
12+
Percent int
13+
Visible bool
14+
BarColor tui.Color
15+
LabelStyle tui.Style
16+
characters []rune
17+
}
18+
19+
//NewVolume create new volume custom widget
20+
func NewVolume() *volume {
21+
return &volume{
22+
Block: *tui.NewBlock(),
23+
BarColor: tui.Theme.Gauge.Bar,
24+
LabelStyle: tui.Theme.Gauge.Label,
25+
characters: []rune{
26+
'▁', '▁', '▂', '▂', '▃',
27+
'▃', '▅', '▅', '▆', '▆',
28+
'▇', '▇', '█', '█',
29+
},
30+
}
31+
}
32+
33+
func (self *volume) Draw(buf *tui.Buffer) {
34+
self.Block.Draw(buf)
35+
36+
if !self.Visible {
37+
return
38+
}
39+
40+
label := strconv.Itoa(self.Percent)
41+
42+
barXCoordinate := self.Inner.Min.X
43+
barYCoordinate := self.Inner.Min.Y + ((self.Inner.Dy() - 1) / 2)
44+
barDxCoordinate := self.Inner.Max.X - barXCoordinate - (len(label) + 1)
45+
46+
// plot bar
47+
barWidth := int((float64(self.Percent) / 100) * float64(barDxCoordinate))
48+
barStyle := tui.NewStyle(self.BarColor, tui.ColorClear)
49+
for i, char := range self.characters {
50+
if i > barWidth {
51+
break
52+
}
53+
54+
buf.SetCell(tui.NewCell(char, barStyle), image.Pt(barXCoordinate+i, barYCoordinate))
55+
}
56+
57+
// plot label
58+
labelXCoordinate := self.Inner.Max.X - len(label)
59+
labelYCoordinate := self.Inner.Min.Y + ((self.Inner.Dy() - 1) / 2)
60+
if labelYCoordinate < self.Inner.Max.Y {
61+
style := self.LabelStyle
62+
for i, char := range label {
63+
buf.SetCell(tui.NewCell(char, style), image.Pt(labelXCoordinate+i, labelYCoordinate))
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)