aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarvey Tindall <hrfee@protonmail.ch>2021-04-24 16:37:44 +0100
committerHarvey Tindall <hrfee@protonmail.ch>2021-04-24 16:37:44 +0100
commita2164b0173c5ef052ec7758ab4ddb4e301fd9c0d (patch)
tree3837e56d1236c87fe7fb2159506068bd46324216
parentc235dd60ddb1b6359c839835b9e44cd9d6726f38 (diff)
downloadwaybar-mpris-a2164b0173c5ef052ec7758ab4ddb4e301fd9c0d.tar.gz
waybar-mpris-a2164b0173c5ef052ec7758ab4ddb4e301fd9c0d.zip
build JSON string instead of json.Marshal-ing
not that this was a problem, but the output is simple enough to just create it by hand.
-rw-r--r--main.go37
-rwxr-xr-xwaybar-mprisbin4362867 -> 4203301 bytes
2 files changed, 20 insertions, 17 deletions
diff --git a/main.go b/main.go
index f4bf549..d765d9f 100644
--- a/main.go
+++ b/main.go
@@ -1,7 +1,6 @@
package main
import (
- "encoding/json"
"fmt"
"io"
"log"
@@ -43,12 +42,13 @@ var (
// JSON returns json for waybar to consume.
func playerJSON(p *mpris2.Player) string {
- data := map[string]string{}
symbol := PLAY
- data["class"] = "paused"
+ out := "{\"class\": \""
if p.Playing {
symbol = PAUSE
- data["class"] = "playing"
+ out += "playing"
+ } else {
+ out += "paused"
}
var pos string
if SHOW_POS {
@@ -96,21 +96,23 @@ func playerJSON(p *mpris2.Player) string {
}
text += v + right
}
-
- data["tooltip"] = fmt.Sprintf(
- "%s\nby %s\n",
+ out += "\",\"text\":\"" + text + "\","
+ out += "\"tooltip\":\"" + fmt.Sprintf(
+ "%s\\nby %s\\n",
strings.ReplaceAll(p.Title, "&", "&amp;"),
- strings.ReplaceAll(p.Artist, "&", "&amp;"))
+ strings.ReplaceAll(p.Artist, "&", "&amp;"),
+ )
if p.Album != "" {
- data["tooltip"] += "from " + strings.ReplaceAll(p.Album, "&", "&amp;") + "\n"
- }
- data["tooltip"] += "(" + p.Name + ")"
- data["text"] = text
- out, err := json.Marshal(data)
- if err != nil {
- return "{}"
+ out += "from " + strings.ReplaceAll(p.Album, "&", "&amp;") + "\\n"
}
- return string(out)
+ out += "(" + p.Name + ")\"}"
+ return out
+ // return fmt.Sprintf("{\"class\":\"%s\",\"text\":\"%s\",\"tooltip\":\"%s\"}", data["class"], data["text"], data["tooltip"])
+ // out, err := json.Marshal(data)
+ // if err != nil {
+ // return "{}"
+ // }
+ // return string(out)
}
type players struct {
@@ -204,10 +206,11 @@ func duplicateOutput(conn net.Conn) {
return
}
str := string(l)
+ // Trim extra newline is necessary
if str[len(str)-2:] == "\n\n" {
fmt.Print(str[:len(str)-1])
} else {
- fmt.Print(string(l))
+ fmt.Print(str)
}
f.Seek(0, 0)
}
diff --git a/waybar-mpris b/waybar-mpris
index a940dcf..71b7cd1 100755
--- a/waybar-mpris
+++ b/waybar-mpris
Binary files differ