diff options
| author | Julian Hurst <julian.hurst92@gmail.com> | 2020-08-03 14:33:01 +0200 |
|---|---|---|
| committer | Julian Hurst <julian.hurst92@gmail.com> | 2020-08-03 14:33:01 +0200 |
| commit | 4b575266ca443b1cca945b14f5ebf10de6f87c66 (patch) | |
| tree | 35efb7a23cc86ec79b1593e659db2b7668db2da4 | |
| parent | c02d74186d0622e6b3407b05570c298de48a340e (diff) | |
| download | statusbar-4b575266ca443b1cca945b14f5ebf10de6f87c66.tar.gz | |
Add support for a generic music player
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | statusbar.go | 59 |
3 files changed, 65 insertions, 1 deletions
@@ -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 +) @@ -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 |
