diff --git a/README.md b/README.md index a3a4295..ef01bdc 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Blocks compatible with [i3blocks](https://github.com/vivien/i3blocks). * `tmux` for `app` and `date` blocks * `df` for `du` block * `xdg-open` for `du` block +* `playerctl` for `spotify` block ## Installation @@ -68,6 +69,12 @@ Show indicator of RSS. rss ``` +Show current song played (spotify). + +``` +spotify +``` + Show volume. ``` diff --git a/blocks/spotify/main.go b/blocks/spotify/main.go new file mode 100644 index 0000000..e91d718 --- /dev/null +++ b/blocks/spotify/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "os" + "os/exec" +) + +func GetMetadata(metadata string) string { + output, _ := exec.Command("playerctl", "-p", "spotify", "metadata", metadata).Output() + data := string(output) + + if len(data) > 20 { + data = fmt.Sprintf("%s…", data[0:20]) + } + + return data +} + +func GetTitle() string { + return GetMetadata("xesam:title") +} + +func GetArtist() string { + return GetMetadata("xesam:artist") +} + +func main() { + title := GetTitle() + artist := GetArtist() + + if title == "" { + return + } + + label := r.TextWithPadding(artist, r.FB{ + Foreground: r.Color("grey1"), + Background: r.Color("black3"), + }) + + value := r.TextWithPadding(title, r.FB{ + Foreground: r.Color("grey2"), + Background: r.Color("black1"), + }) + + if os.Getenv("BLOCK_BUTTON") == "1" { + exec.Command("i3-msg", `workspace "6. MEDIA"`).Output() + } + + options := r.NewBlockOptions() + options.FullText = fmt.Sprintf("%s%s", label, value) + block := r.Block("spotify", options) + + fmt.Println(block) +}