aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarvey Tindall <hrfee@protonmail.ch>2020-08-22 15:40:24 +0100
committerHarvey Tindall <hrfee@protonmail.ch>2020-08-22 15:40:24 +0100
commit54dcac02e69545743c4cb07942428e4fdaea515e (patch)
tree9978381010d037cf70554e53e00185fe2096e8a9
parent5020e1c9298bfa76e89016be1bd2dd157563ea41 (diff)
downloadwaybar-mpris-54dcac02e69545743c4cb07942428e4fdaea515e.tar.gz
waybar-mpris-54dcac02e69545743c4cb07942428e4fdaea515e.zip
Add flags, executable and improve README.md
-rw-r--r--README.md16
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rwxr-xr-xmainbin0 -> 4413239 bytes
-rw-r--r--main.go84
-rwxr-xr-xwaybar-mprisbin0 -> 4413239 bytes
6 files changed, 85 insertions, 22 deletions
diff --git a/README.md b/README.md
index 8ae1301..a527fb3 100644
--- a/README.md
+++ b/README.md
@@ -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.
+
diff --git a/go.mod b/go.mod
index 04302e1..9326004 100644
--- a/go.mod
+++ b/go.mod
@@ -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
+)
diff --git a/go.sum b/go.sum
index 4eefd05..b9efde1 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
diff --git a/main b/main
new file mode 100755
index 0000000..ca8a9fc
--- /dev/null
+++ b/main
Binary files differ
diff --git a/main.go b/main.go
index 95d04cb..a6f7688 100644
--- a/main.go
+++ b/main.go
@@ -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)
}
}
diff --git a/waybar-mpris b/waybar-mpris
new file mode 100755
index 0000000..31c553d
--- /dev/null
+++ b/waybar-mpris
Binary files differ