mpd-status

Utility to show mpd status in tmux
git clone https://www.brianlane.com/git/mpd-status
Log | Files | Refs | README | LICENSE

commit 2d1c7dd71014cf022f2bd83cffce0b2bc9fd9599
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Sat, 22 Jun 2019 10:48:07 -0700

Initial commit

Diffstat:
AREADME.md | 14++++++++++++++
Ago.mod | 5+++++
Ago.sum | 2++
Ampd-status.go | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,14 @@ +Simple mpd status display + +* MPD protocol - https://www.musicpd.org/doc/html/protocol.html +* mpd package docs - https://godoc.org/github.com/fhs/gompd/mpd + +Add this to the tmux status line with: + + set -g status-right "#[fg=green]#(~/bin/mpd-status)#[fg=default] | [#H] %H:%M %e-%b-%g" + +Fields available: + + STATUS- map[audio:44100:24:2 bitrate:320 consume:0 duration:223.791 elapsed:160.651 mixrampdb:0.000000 nextsong:5 nextsongid:6 playlist:2 playlistlength:19 random:1 repeat:0 single:0 song:0 songid:1 state:pause time:161:224 volume:75] + + SONG- map[Album:Joe Bonamassa Live From the Royal Albert Hall AlbumArtist:Joe Bonamassa Artist:Joe Bonamassa Date:2009 Disc:1 Genre:Blues Id:1 Last-Modified:2017-10-09T18:21:04Z Pos:0 Time:224 Title:Django (Live) Track:1 duration:223.791 file:Joe Bonamassa/Live From The Royal Albert Hall/01 - Django (Live).mp3] diff --git a/go.mod b/go.mod @@ -0,0 +1,5 @@ +module github.com/bcl/mpd-status + +go 1.12 + +require github.com/fhs/gompd v2.0.0+incompatible diff --git a/go.sum b/go.sum @@ -0,0 +1,2 @@ +github.com/fhs/gompd v2.0.0+incompatible h1:pv5XKTatya1k3r1woaWLwFQiF0BfAsgWSe5ev2XZ0UM= +github.com/fhs/gompd v2.0.0+incompatible/go.mod h1:UVZXd9wmFBH5tIXLYeI+CGUIt15ZvtGQvVO6SDHy1os= diff --git a/mpd-status.go b/mpd-status.go @@ -0,0 +1,99 @@ +package main + +import ( + "flag" + "fmt" + "github.com/fhs/gompd/mpd" + "log" + "strings" + "time" + "unicode/utf8" +) + +/* commandline flags */ +type cmdlineArgs struct { + Volume bool // Show volume percentage 0-100 + Elapsed bool // Show the duration:elapsed time + Width int // Maximum width + Debug bool // Output debugging info +} + +/* commandline defaults */ +var cfg = cmdlineArgs{ + Volume: false, + Elapsed: false, + Width: 60, + Debug: false, +} + +/* parseArgs handles parsing the cmdline args and setting values in the global cfg struct */ +func parseArgs() { + flag.BoolVar(&cfg.Volume, "volume", cfg.Volume, "Include the volume percentage 0-100") + flag.BoolVar(&cfg.Elapsed, "elapsed", cfg.Elapsed, "Include the duration:elapsed time") + flag.IntVar(&cfg.Width, "width", cfg.Width, "Maximum width of output") + flag.BoolVar(&cfg.Debug, "debug", cfg.Debug, "Output debug information") + + flag.Parse() +} + +func main() { + parseArgs() + + // Connect to MPD server + conn, err := mpd.Dial("tcp", "localhost:6600") + if err != nil { + log.Fatalln(err) + } + defer conn.Close() + + // Get the status and current song + status, err := conn.Status() + if err != nil { + log.Fatalln(err) + } + song, err := conn.CurrentSong() + if err != nil { + log.Fatalln(err) + } + // fmt.Printf("STATUS- %s\n", status) + // fmt.Printf("SONG- %s\n", song) + + // Build the optional parts of the output + var optional strings.Builder + if cfg.Volume { + fmt.Fprintf(&optional, " %s%%", status["volume"]) + } + if cfg.Elapsed { + duration, _ := time.ParseDuration(status["duration"] + "s") + elapsed, _ := time.ParseDuration(status["elapsed"] + "s") + fmt.Fprintf(&optional, " %s/%s", elapsed.Truncate(time.Second), duration.Truncate(time.Second)) + } + + // Build the final output string + var s strings.Builder + switch status["state"] { + case "play": + s.WriteString("▶ ") + case "stop": + s.WriteString("◼ ") + case "pause": + s.WriteString("‖ ") + default: + s.WriteString(" ") + } + + // Build the Artist + title part (do I want to make artist optional? album?) + songStr := fmt.Sprintf("%s - %s", song["Artist"], song["Title"]) + + // Calculate how much title to trim + trim := cfg.Width - utf8.RuneCountInString(optional.String()) - utf8.RuneCountInString(s.String()) + trim = utf8.RuneCountInString(songStr) - trim + if trim < 0 { + trim = 0 + } else if trim > utf8.RuneCountInString(songStr) { + trim = utf8.RuneCountInString(songStr) + } + + // Trim the title so that it will fit into the width + fmt.Printf("%s%s%s", s.String(), songStr[trim:], optional.String()) +}