summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--statusbar.go59
3 files changed, 65 insertions, 1 deletions
diff --git a/go.mod b/go.mod
index 65bb318..dcbea9e 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module statusbar
go 1.14
-require github.com/BurntSushi/toml v0.3.1 // indirect
+require (
+ github.com/BurntSushi/toml v0.3.1 // indirect
+ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
+)
diff --git a/go.sum b/go.sum
index 9cb2df8..4233566 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,4 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
diff --git a/statusbar.go b/statusbar.go
index 549ebfe..73f3cb2 100644
--- a/statusbar.go
+++ b/statusbar.go
@@ -11,8 +11,10 @@ import (
"io/ioutil"
"time"
"flag"
+ "errors"
"github.com/BurntSushi/toml"
+ "github.com/google/shlex"
)
type MusicPlayer interface {
@@ -20,6 +22,11 @@ type MusicPlayer interface {
status() int
}
+type GenericPlayer struct {
+ NowPlayingCmd string
+ StatusCmd string
+}
+
type MPD struct{
Host string
Port int
@@ -57,6 +64,8 @@ type MusicConfig struct {
Type string
Host string
Port int
+ NowPlayingCmd string `toml:"nowplayingcmd"`
+ StatusCmd string `toml:"statuscmd"`
}
type ClockConfig struct {
@@ -153,6 +162,54 @@ func clock(format string) string {
return t.Format(format)
}
+func (player GenericPlayer) nowPlaying() string {
+ split, err := shlex.Split(player.NowPlayingCmd)
+ if err != nil {
+ panic(err)
+ }
+ if len(split) == 0 {
+ panic(errors.New("Empty \"now playing\" command for generic player"))
+ }
+ var args []string
+ if len(split) > 1 {
+ args = append(args, split[1:]...)
+ }
+ cmd := exec.Command(split[0], args...)
+ out, err := cmd.Output()
+ if err != nil {
+ panic(err)
+ }
+ sOut := string(out)
+ sOut = strings.ReplaceAll(sOut, "\n", "")
+ return sOut
+}
+
+func (player GenericPlayer) status() int {
+ split, err := shlex.Split(player.StatusCmd)
+ if err != nil {
+ panic(err)
+ }
+ if len(split) == 0 {
+ panic(errors.New("Empty \"status\" command for generic player"))
+ }
+ var args []string
+ if len(split) > 1 {
+ args = append(args, split[1:]...)
+ }
+ cmd := exec.Command(split[0], args...)
+ out, err := cmd.Output()
+ if err != nil {
+ panic(err)
+ }
+ sOut := string(out)
+ sOut = strings.ReplaceAll(sOut, "\n", "")
+ iOut, err := strconv.Atoi(sOut)
+ if err != nil {
+ panic(err)
+ }
+ return iOut
+}
+
func (mpd MPD) nowPlaying() string {
cmd := exec.Command("mpc", "-h", mpd.Host, "-p", strconv.Itoa(mpd.Port), "current")
out, err := cmd.Output()
@@ -275,6 +332,8 @@ func buildMusic(config MusicConfig) string {
player = MPD{Host: host, Port: port}
case "mocp":
player = MOCP{}
+ case "generic":
+ player = GenericPlayer{NowPlayingCmd: config.NowPlayingCmd, StatusCmd: config.StatusCmd}
default:
}
exists := player != nil