Add flags, executable and improve README.md

This commit is contained in:
Harvey Tindall 2020-08-22 15:40:24 +01:00
parent 5020e1c929
commit 54dcac02e6
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
6 changed files with 85 additions and 22 deletions

View File

@ -1,3 +1,17 @@
## waybar-mpris
a custom waybar component for displaying info from MPRIS2 players. Currently WIP.
a custom waybar component for displaying info from MPRIS2 players. It automatically focuses on currently playing music players, and can easily be customized.
## Usage
When running, the program will pipe out json in waybar's format. Make a custom component in your configuration and set `return-type` to `json`, and `exec` to the path to the program.
```
Usage of ./waybar-mpris:
--order string Element order. (default "SYMBOL:ARTIST:ALBUM:TITLE")
--pause string Pause symbol/text to use. (default "")
--play string Play symbol/text to use. (default "▶")
--separator string Separator string to use between artist, album, and title. (default " - ")
```
* Modify the order of components with `--order`. `SYMBOL` is the play/paused icon or text, other options are self explanatory.
* `--play/--pause` specify the symbols or text to display when music is paused/playing respectively.
* --separator specifies a string to separate the artist, album and title text.

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/hrfee/waybar-mpris
go 1.15
require github.com/godbus/dbus/v5 v5.0.3
require (
github.com/godbus/dbus/v5 v5.0.3
github.com/spf13/pflag v1.0.5
)

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

BIN
main Executable file

Binary file not shown.

84
main.go
View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/godbus/dbus/v5"
flag "github.com/spf13/pflag"
)
var knownPlayers = map[string]string{
@ -25,14 +26,19 @@ type Player struct {
const (
INTERFACE = "org.mpris.MediaPlayer2"
PATH = "/org/mpris/MediaPlayer2"
PLAY = "▶"
PAUSE = ""
// NameOwnerChanged
MATCH_NOC = "type='signal',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
// PropertiesChanged
MATCH_PC = "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'"
)
var (
PLAY = "▶"
PAUSE = ""
SEP = " - "
ORDER = "SYMBOL:ARTIST:ALBUM:TITLE"
)
// NewPlayer returns a new player object.
func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
playerName := strings.ReplaceAll(name, INTERFACE+".", "")
@ -108,16 +114,48 @@ func (p *Player) Refresh() (err error) {
}
func (p *Player) JSON() string {
data := map[string]string{}
symbol := PLAY
data["class"] = "paused"
if p.playing {
symbol = PAUSE
data["class"] = "playing"
}
var items []string
for _, v := range []string{p.artist, p.album, p.title} {
if v != "" {
items = append(items, v)
order := strings.Split(ORDER, ":")
for _, v := range order {
if v == "SYMBOL" {
items = append(items, symbol)
} else if v == "ARTIST" {
if p.artist != "" {
items = append(items, p.artist)
}
} else if v == "ALBUM" {
if p.album != "" {
items = append(items, p.album)
}
} else if v == "TITLE" {
if p.album != "" {
items = append(items, p.title)
}
}
}
if len(items) == 0 {
return "{}"
}
data := map[string]string{}
text := ""
for i, v := range items {
right := ""
if v == symbol && i != len(items)-1 {
right = " "
} else if i != len(items)-1 && items[i+1] != symbol {
right = SEP
} else {
right = " "
}
text += v + right
}
data["tooltip"] = fmt.Sprintf(
"%s\nby %s\n",
p.title,
@ -126,18 +164,12 @@ func (p *Player) JSON() string {
data["tooltip"] += "from " + p.album + "\n"
}
data["tooltip"] += "(" + p.name + ")"
symbol := PLAY
data["class"] = "paused"
if p.playing {
symbol = PAUSE
data["class"] = "playing"
}
data["text"] = symbol + " " + strings.Join(items, " - ")
text, err := json.Marshal(data)
data["text"] = text
out, err := json.Marshal(data)
if err != nil {
return "{}"
}
return string(text)
return string(out)
}
type PlayerList struct {
@ -221,6 +253,12 @@ func (pl *PlayerList) JSON() string {
}
func main() {
flag.StringVar(&PLAY, "play", PLAY, "Play symbol/text to use.")
flag.StringVar(&PAUSE, "pause", PAUSE, "Pause symbol/text to use.")
flag.StringVar(&SEP, "separator", SEP, "Separator string to use between artist, album, and title.")
flag.StringVar(&ORDER, "order", ORDER, "Element order.")
flag.Parse()
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
@ -230,7 +268,10 @@ func main() {
}
players.Reload()
players.Sort()
fmt.Println("New array", players)
players.Refresh()
fmt.Println(players.JSON())
lastLine := ""
// fmt.Println("New array", players)
conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_NOC)
conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_PC)
c := make(chan *dbus.Signal, 10)
@ -244,10 +285,10 @@ func main() {
conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
if strings.Contains(name, INTERFACE) {
if pid == 0 {
fmt.Println("Removing", name)
// fmt.Println("Removing", name)
players.Remove(name)
} else {
fmt.Println("Adding", name)
// fmt.Println("Adding", name)
players.New(name)
}
}
@ -255,8 +296,11 @@ func main() {
} else if strings.Contains(v.Name, "PropertiesChanged") && strings.Contains(v.Body[0].(string), INTERFACE+".Player") {
players.Refresh()
players.Sort()
fmt.Println(players.JSON())
if l := players.JSON(); l != lastLine {
lastLine = l
fmt.Println(l)
}
fmt.Println("New array", players)
}
// fmt.Println("New array", players)
}
}

BIN
waybar-mpris Executable file

Binary file not shown.