Skip to content

Commit

Permalink
Merge pull request #2 from Nox04/feature/add-metadata-format-support
Browse files Browse the repository at this point in the history
Add support for metadata format
  • Loading branch information
abenz1267 authored Apr 6, 2023
2 parents e7b9746 + 1fdb780 commit 8c973e6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ f.e. `sudo GOBIN=/usr/bin/ go install github.com/abenz1267/pww@latest`

Additionally a `-p` flag can be provided to specify a placeholder for empty text. F.e. when the player isn't running.

You can also provide `-f` flag to specify the format of the text output. F.e. `pww -w spotify -f '{{title}} - {{artist}}'` will emit `{"class": "Playing", "text": "Song Title - Artist Name"}`. You can find more information about supported formats [here](https://github.com/altdesktop/playerctl#printing-properties-and-metadata).

### Waybar Example

This is how you'd use it as a custom module in [Waybar](https://github.com/Alexays/Waybar).
Expand Down
9 changes: 7 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ func status(player string) string {
return strings.TrimSpace(string(out))
}

func metadata(player, data string) string {
cmd := exec.Command("playerctl", fmt.Sprintf("--player=%s", player), "metadata", data)
func metadata(player, data, format string) string {
params := []string{fmt.Sprintf("--player=%s", player), "metadata", data}
if format != "" {
params = append(params, "-f", format)
}

cmd := exec.Command("playerctl", params...)

out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ func main() {
var watch string
var placeholder string
var toggle string
var format string

pflag.StringVarP(&autopauseplayers, "autopause", "a", "", "players to autopause")
pflag.StringVarP(&watch, "watch", "w", "", "metadata to watch (<player>:<data>)")
pflag.StringVarP(&placeholder, "placeholder", "p", "", "placeholder for empty text")
pflag.StringVarP(&toggle, "toggle", "t", "", "toggles play/pause for the given player. Starts the player otherwise.")
pflag.StringVarP(&format, "format", "f", "", "format string for the output. More info: https://github.com/altdesktop/playerctl#printing-properties-and-metadata")
pflag.Parse()

if toggle != "" {
Expand All @@ -33,13 +35,24 @@ func main() {
return
}

if watch == "" {
return
player := ""
data := ""

if watch != "" {
info := strings.Split(watch, ":")
player = info[0]
// data could be empty when using a custom format
if len(info) > 1 {
data = info[1]
// if data is empty, format should be provided
} else if format == "" {
return
}
}

info := strings.Split(watch, ":")
player := info[0]
data := info[1]
if player == "" {
return
}

watchPlayerMetaData(player, data, placeholder)
watchPlayerMetaData(player, data, placeholder, format)
}
15 changes: 10 additions & 5 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"time"
)

func watchPlayerMetaData(player, data, placeholder string) {
func watchPlayerMetaData(player, data, placeholder, format string) {
infoChannel := make(chan string)

go watchMetaData(player, data, infoChannel)
go watchMetaData(player, data, format, infoChannel)
go watchMetaDataStatus(player, infoChannel)

s := status(player)
Expand All @@ -37,7 +37,7 @@ func watchPlayerMetaData(player, data, placeholder string) {
if val == "STATUSCHANGED" {
s = status(player)
time.Sleep(100 * time.Millisecond)
text = metadata(player, data)
text = metadata(player, data, format)
} else {
text = val
}
Expand All @@ -60,8 +60,13 @@ func watchPlayerMetaData(player, data, placeholder string) {
}
}

func watchMetaData(player, data string, infoChannel chan string) {
cmd := exec.Command("playerctl", fmt.Sprintf("--player=%s", player), "metadata", data, "-F")
func watchMetaData(player, data, format string, infoChannel chan string) {
params := []string{fmt.Sprintf("--player=%s", player), "metadata", data, "-F"}
if format != "" {
params = append(params, "-f", format)
}

cmd := exec.Command("playerctl", params...)

pipe, err := cmd.StdoutPipe()
if err != nil {
Expand Down

0 comments on commit 8c973e6

Please sign in to comment.