summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst92@gmail.com>2020-07-06 10:43:13 +0200
committerJulian Hurst <julian.hurst92@gmail.com>2020-07-06 10:43:13 +0200
commit8d3395aa20fd273f42d8526298bf47f6fd795dba (patch)
tree250c03473b12e7ddcf75e9ec136faca6b24671b9
parentf2dc3c9a50358ab8d5f3afd64da852f01f540eee (diff)
downloadstatusbar-8d3395aa20fd273f42d8526298bf47f6fd795dba.tar.gz
Define an interface to support different music players
-rw-r--r--statusbar.conf1
-rw-r--r--statusbar.go78
2 files changed, 62 insertions, 17 deletions
diff --git a/statusbar.conf b/statusbar.conf
index 197383c..82802d0 100644
--- a/statusbar.conf
+++ b/statusbar.conf
@@ -12,6 +12,7 @@ label = "mounts: "
mountpoints = [ "/", "/mnt/storage", "/mnt/storage1" ]
[music]
+# type can be mpd or mocp
type = "mpd"
labelplaying = "▶ "
labelpaused = "⏸ "
diff --git a/statusbar.go b/statusbar.go
index 86b0310..a12c2ce 100644
--- a/statusbar.go
+++ b/statusbar.go
@@ -14,6 +14,14 @@ import (
"github.com/BurntSushi/toml"
)
+type MusicPlayer interface {
+ nowPlaying() string
+ status() int
+}
+
+type MPD struct{}
+type MOCP struct{}
+
type Config struct {
Sections []string
Delay int
@@ -126,7 +134,7 @@ func clock(format string) string {
return t.Format(format)
}
-func nowPlayingMPD() string {
+func (mpd MPD) nowPlaying() string {
cmd := exec.Command("mpc", "current")
out, err := cmd.Output()
if err != nil {
@@ -137,8 +145,8 @@ func nowPlayingMPD() string {
return sOut
}
-// Returns 0 if playing, 1 if paused and 2 if stopped
-func statusMPD() int {
+// Returns 0 if playing, 1 if paused and 2 otherwise
+func (mpd MPD) status() int {
cmd := exec.Command("mpc", "status")
out, err := cmd.Output()
if err != nil {
@@ -154,6 +162,35 @@ func statusMPD() int {
}
}
+func (mocp MOCP) nowPlaying() string {
+ cmd := exec.Command("mocp", "-Q", "%artist - %song")
+ out, err := cmd.Output()
+ if err != nil {
+ panic(err)
+ }
+ sOut := string(out)
+ sOut = strings.ReplaceAll(sOut, "\n", "")
+ return sOut
+}
+
+// Returns 0 if playing, 1 if paused and 2 otherwise
+func (mocp MOCP) status() int {
+ cmd := exec.Command("mocp", "-i")
+ out, err := cmd.Output()
+ if err != nil {
+ return -1
+ }
+ sOut := string(out)
+ if strings.Contains(sOut, "PLAY") {
+ return 0
+ } else if strings.Contains(sOut, "PAUSE") {
+ return 1
+ } else {
+ return 2
+ }
+}
+
+
func buildClock(config ClockConfig) string {
var b strings.Builder
b.WriteString(config.Label)
@@ -181,24 +218,31 @@ func buildMounts(config MountConfig) string {
func buildMusic(config MusicConfig) string {
var b strings.Builder
+ var player MusicPlayer
switch config.Type {
case "mpd":
- isMPD := true
- switch statusMPD() {
- case 0:
- b.WriteString(config.LabelPlaying)
- case 1:
- b.WriteString(config.LabelPaused)
- case 2:
- b.WriteString(config.LabelStopped)
- default:
- isMPD = false
- }
- if isMPD {
- b.WriteString(nowPlayingMPD())
- }
+ player = MPD{}
+ case "mocp":
+ player = MOCP{}
default:
}
+ exists := player != nil
+ if !exists {
+ return "Unknown music player type"
+ }
+ switch player.status() {
+ case 0:
+ b.WriteString(config.LabelPlaying)
+ case 1:
+ b.WriteString(config.LabelPaused)
+ case 2:
+ b.WriteString(config.LabelStopped)
+ default:
+ exists = false
+ }
+ if exists {
+ b.WriteString(player.nowPlaying())
+ }
return b.String()
}