Skip to content

Commit 9cfb5af

Browse files
author
Mateusz Lopacinski
committed
#2 implement currently playing and volume
1 parent ee501e0 commit 9cfb5af

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

pkg/stations/stations.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Load(path string) *List {
5555
if os.IsNotExist(err) {
5656
err = downloadStations(path)
5757
if err != nil {
58-
log.Fatalf("Cannot find or install stations ")
58+
log.Fatalf("cannot find or install stations: %v", err)
5959
}
6060
}
6161
}
@@ -91,10 +91,12 @@ func Load(path string) *List {
9191
func downloadStations(path string) error {
9292
dir := filepath.Dir(path)
9393
if _, err := os.Stat(dir); os.IsNotExist(err) {
94-
err := os.Chdir(dir)
94+
log.Printf("pyradio config dir not found, trying to create")
95+
err := os.MkdirAll(dir, os.ModePerm)
9596
if err != nil {
9697
return err
9798
}
99+
log.Printf("created directory: %v", dir)
98100
}
99101

100102
out, err := os.Create(path)

pkg/ui/events.go

+41-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@ package ui
33
import (
44
"bufio"
55
"fmt"
6+
"log"
7+
"regexp"
8+
"strconv"
69

7-
tui "github.com/gizak/termui/v3"
810
"github.com/robopuff/goradio/pkg/driver"
11+
12+
tui "github.com/gizak/termui/v3"
13+
)
14+
15+
const (
16+
regexTitle = `(?m)^ICY Info: StreamTitle='(.*?)';`
17+
regexVolume = `(?m)Volume: (\d+)`
918
)
1019

1120
var colorSelected = "(fg:black,bg:white)"
1221

1322
func manageKeyboardEvent(e tui.Event, d driver.Driver) int {
1423
if e.Type == tui.ResizeEvent {
1524
tui.Clear()
16-
Init(stationsList, debug)
25+
if err := Init(stationsList, debug); err != nil {
26+
log.Fatalf("failed to initialize ui: %v", err)
27+
}
1728
}
1829

1930
switch e.ID {
@@ -80,9 +91,8 @@ func manageKeyboardEvent(e tui.Event, d driver.Driver) int {
8091
}
8192

8293
func manageDriverLogs(d driver.Driver) {
83-
if !debug {
84-
return
85-
}
94+
titleRegex := regexp.MustCompile(regexTitle)
95+
volumeRegex := regexp.MustCompile(regexVolume)
8696

8797
for {
8898
select {
@@ -91,15 +101,39 @@ func manageDriverLogs(d driver.Driver) {
91101
for {
92102
data, err := reader.ReadString('\n')
93103
if err != nil {
94-
log(fmt.Sprintf("Pipe closed: %v", err.Error()))
104+
setCurrentlyPlaying("")
105+
sendToLog(fmt.Sprintf("Pipe closed: %v", err.Error()))
95106
break
96107
}
97-
log(data[:len(data)-1])
108+
109+
match := titleRegex.FindStringSubmatch(data)
110+
if len(match) > 0 {
111+
setCurrentlyPlaying(match[1])
112+
}
113+
114+
match = volumeRegex.FindStringSubmatch(data)
115+
if len(match) > 0 {
116+
volume, _ := strconv.Atoi(match[1])
117+
volumeGauge.Percent = volume
118+
volumeGauge.Label = match[1]
119+
render()
120+
}
121+
122+
sendToLog(data[:len(data)-1])
98123
}
99124
}
100125
}
101126
}
102127

128+
func setCurrentlyPlaying(currently string) {
129+
format := fmt.Sprintf(fullLineFormatter, currentlyPlayingFormat)
130+
if currently == "" {
131+
currently = "None"
132+
}
133+
uiPlayingParagraph.Text = fmt.Sprintf(format, currently)
134+
render()
135+
}
136+
103137
func addStationSelection() {
104138
uiStationsList.Rows[current] = fmt.Sprintf("[%v]%s", uiStationsList.Rows[current], colorSelected)
105139
}

pkg/ui/ui.go

+27-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,26 @@ import (
99
"github.com/gizak/termui/v3/widgets"
1010
)
1111

12+
const (
13+
currentlyPlayingFormat = "Currently playing: %s"
14+
helpFooter = "k/↑ : Up | j/↓: Down | Enter: Select | p: Pause | m: Mute | s: Stop | +: Louder | -: Quieter | R: Refresh | q: Quit"
15+
)
16+
17+
const (
18+
colorGray tui.Color = 8
19+
)
20+
1221
var (
1322
current = -1
1423
debug bool
1524
w, h int
25+
fullLineFormatter string
1626
stationsList *stations.List
1727
uiPlayingParagraph *widgets.Paragraph
1828
uiFooterParagraph *widgets.Paragraph
1929
uiStationsList *widgets.List
2030
uiLoggerList *widgets.List
31+
volumeGauge *widgets.Gauge
2132
drawables []tui.Drawable
2233
)
2334

@@ -32,29 +43,29 @@ func Init(csvStationsList *stations.List, debugFlag bool) error {
3243

3344
stationsList = csvStationsList
3445

35-
formatter := fmt.Sprintf("%%-%ds", w)
46+
fullLineFormatter = fmt.Sprintf("%%-%ds", w)
3647

3748
uiPlayingParagraph = widgets.NewParagraph()
38-
uiPlayingParagraph.Text = fmt.Sprintf(formatter, "Currently playing: None")
3949
uiPlayingParagraph.SetRect(0, -1, w, 3)
4050
uiPlayingParagraph.Border = false
4151
uiPlayingParagraph.TextStyle.Fg = tui.ColorRed
52+
setCurrentlyPlaying("")
4253

4354
uiFooterParagraph = widgets.NewParagraph()
44-
uiFooterParagraph.Text = fmt.Sprintf(formatter, "k/↑ : Up | j/↓: Down | Enter: Select | p: Pause | m: Mute | s: Stop | +: Louder | -: Quieter | R: Refresh | q: Quit")
55+
uiFooterParagraph.Text = fmt.Sprintf(fullLineFormatter, helpFooter)
4556
uiFooterParagraph.WrapText = false
4657
uiFooterParagraph.PaddingLeft = -1
4758
uiFooterParagraph.PaddingRight = -1
4859
uiFooterParagraph.SetRect(0, h-3, w, h)
4960
uiFooterParagraph.Border = false
5061
uiFooterParagraph.TextStyle.Fg = tui.ColorBlack
51-
uiFooterParagraph.TextStyle.Bg = 8
62+
uiFooterParagraph.TextStyle.Bg = colorGray
5263

5364
uiLoggerList = widgets.NewList()
54-
uiLoggerList.Title = "[ log ]"
65+
uiLoggerList.Title = "[ sendToLog ]"
5566
uiLoggerList.SetRect(w/2, 1, w-1, h-2)
5667
uiLoggerList.TextStyle.Fg = tui.ColorBlue
57-
uiLoggerList.BorderStyle.Fg = 8
68+
uiLoggerList.BorderStyle.Fg = colorGray
5869
uiLoggerList.SelectedRowStyle.Fg = uiLoggerList.TextStyle.Fg
5970
uiLoggerList.Rows = []string{""}
6071

@@ -64,10 +75,16 @@ func Init(csvStationsList *stations.List, debugFlag bool) error {
6475
uiStationsList.TextStyle.Modifier = tui.ModifierBold
6576
uiStationsList.SelectedRowStyle.Modifier = tui.ModifierBold
6677
uiStationsList.SelectedRowStyle.Fg = tui.ColorWhite
67-
uiStationsList.SelectedRowStyle.Bg = 8
68-
uiStationsList.BorderStyle.Fg = 8
78+
uiStationsList.SelectedRowStyle.Bg = colorGray
79+
uiStationsList.BorderStyle.Fg = colorGray
6980
uiStationsList.WrapText = false
7081

82+
volumeGauge = widgets.NewGauge()
83+
volumeGauge.Border = false
84+
volumeGauge.SetRect(w-21, -1, w-1, 2)
85+
volumeGauge.Percent = 25
86+
volumeGauge.Label = "25"
87+
7188
if debug {
7289
uiStationsList.SetRect(0, 1, (w/2)-1, h-2)
7390
} else {
@@ -78,6 +95,7 @@ func Init(csvStationsList *stations.List, debugFlag bool) error {
7895

7996
drawables = []tui.Drawable{
8097
uiPlayingParagraph,
98+
volumeGauge,
8199
uiFooterParagraph,
82100
uiStationsList,
83101
}
@@ -117,7 +135,7 @@ func render() {
117135
tui.Render(drawables...)
118136
}
119137

120-
func log(m string) {
138+
func sendToLog(m string) {
121139
if !debug {
122140
return
123141
}

0 commit comments

Comments
 (0)