build JSON string instead of json.Marshal-ing
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
not that this was a problem, but the output is simple enough to just create it by hand.
This commit is contained in:
parent
c235dd60dd
commit
a2164b0173
37
main.go
37
main.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -43,12 +42,13 @@ var (
|
|||||||
|
|
||||||
// JSON returns json for waybar to consume.
|
// JSON returns json for waybar to consume.
|
||||||
func playerJSON(p *mpris2.Player) string {
|
func playerJSON(p *mpris2.Player) string {
|
||||||
data := map[string]string{}
|
|
||||||
symbol := PLAY
|
symbol := PLAY
|
||||||
data["class"] = "paused"
|
out := "{\"class\": \""
|
||||||
if p.Playing {
|
if p.Playing {
|
||||||
symbol = PAUSE
|
symbol = PAUSE
|
||||||
data["class"] = "playing"
|
out += "playing"
|
||||||
|
} else {
|
||||||
|
out += "paused"
|
||||||
}
|
}
|
||||||
var pos string
|
var pos string
|
||||||
if SHOW_POS {
|
if SHOW_POS {
|
||||||
@ -96,21 +96,23 @@ func playerJSON(p *mpris2.Player) string {
|
|||||||
}
|
}
|
||||||
text += v + right
|
text += v + right
|
||||||
}
|
}
|
||||||
|
out += "\",\"text\":\"" + text + "\","
|
||||||
data["tooltip"] = fmt.Sprintf(
|
out += "\"tooltip\":\"" + fmt.Sprintf(
|
||||||
"%s\nby %s\n",
|
"%s\\nby %s\\n",
|
||||||
strings.ReplaceAll(p.Title, "&", "&"),
|
strings.ReplaceAll(p.Title, "&", "&"),
|
||||||
strings.ReplaceAll(p.Artist, "&", "&"))
|
strings.ReplaceAll(p.Artist, "&", "&"),
|
||||||
|
)
|
||||||
if p.Album != "" {
|
if p.Album != "" {
|
||||||
data["tooltip"] += "from " + strings.ReplaceAll(p.Album, "&", "&") + "\n"
|
out += "from " + strings.ReplaceAll(p.Album, "&", "&") + "\\n"
|
||||||
}
|
}
|
||||||
data["tooltip"] += "(" + p.Name + ")"
|
out += "(" + p.Name + ")\"}"
|
||||||
data["text"] = text
|
return out
|
||||||
out, err := json.Marshal(data)
|
// return fmt.Sprintf("{\"class\":\"%s\",\"text\":\"%s\",\"tooltip\":\"%s\"}", data["class"], data["text"], data["tooltip"])
|
||||||
if err != nil {
|
// out, err := json.Marshal(data)
|
||||||
return "{}"
|
// if err != nil {
|
||||||
}
|
// return "{}"
|
||||||
return string(out)
|
// }
|
||||||
|
// return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
type players struct {
|
type players struct {
|
||||||
@ -204,10 +206,11 @@ func duplicateOutput(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
str := string(l)
|
str := string(l)
|
||||||
|
// Trim extra newline is necessary
|
||||||
if str[len(str)-2:] == "\n\n" {
|
if str[len(str)-2:] == "\n\n" {
|
||||||
fmt.Print(str[:len(str)-1])
|
fmt.Print(str[:len(str)-1])
|
||||||
} else {
|
} else {
|
||||||
fmt.Print(string(l))
|
fmt.Print(str)
|
||||||
}
|
}
|
||||||
f.Seek(0, 0)
|
f.Seek(0, 0)
|
||||||
}
|
}
|
||||||
|
BIN
waybar-mpris
BIN
waybar-mpris
Binary file not shown.
Loading…
Reference in New Issue
Block a user