summaryrefslogtreecommitdiff
path: root/statusbar.go
diff options
context:
space:
mode:
Diffstat (limited to 'statusbar.go')
-rw-r--r--statusbar.go78
1 files changed, 61 insertions, 17 deletions
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()
}