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 /statusbar.go | |
| parent | c02d74186d0622e6b3407b05570c298de48a340e (diff) | |
| download | statusbar-4b575266ca443b1cca945b14f5ebf10de6f87c66.tar.gz | |
Add support for a generic music player
Diffstat (limited to 'statusbar.go')
| -rw-r--r-- | statusbar.go | 59 |
1 files changed, 59 insertions, 0 deletions
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 |
