From a386611f22077ce7825bf8d78e908ae6fd7ae649 Mon Sep 17 00:00:00 2001 From: goodhumored Date: Sat, 3 Aug 2024 22:30:57 +0300 Subject: [PATCH] waybar playerctl --- .config/waybar/config.jsonc | 14 +++-- .config/waybar/custom/playerctl.sh | 27 ++++++++++ .config/waybar/modules.json | 87 +++++++++++++++++++++++++++--- 3 files changed, 117 insertions(+), 11 deletions(-) create mode 100755 .config/waybar/custom/playerctl.sh diff --git a/.config/waybar/config.jsonc b/.config/waybar/config.jsonc index e46874b..2ab8cdb 100644 --- a/.config/waybar/config.jsonc +++ b/.config/waybar/config.jsonc @@ -4,13 +4,21 @@ "height": 20, // Waybar height (to be removed for auto height) "spacing": 4, // Gaps between modules (4px) // Choose the order of the modules - "modules-left": ["custom/appmenuicon","wlr/taskbar", + "modules-left": [ + "custom/appmenuicon", + "wlr/taskbar", ], "modules-center": [ "hyprland/workspaces", ], - "modules-right": ["custom/layout","mpd","bluetooth","pulseaudio","tray","backlight","network","clock", "battery" - + "modules-right": [ + "group/playerctl", + "custom/layout", + "pulseaudio", + "tray", + "backlight", + "clock", + "battery" ], "include": ["~/.config/waybar/modules.json"], } diff --git a/.config/waybar/custom/playerctl.sh b/.config/waybar/custom/playerctl.sh new file mode 100755 index 0000000..93cd58f --- /dev/null +++ b/.config/waybar/custom/playerctl.sh @@ -0,0 +1,27 @@ +title=$(playerctl metadata --format '{{markup_escape(artist)}} {{markup_escape(title)}}') +position=$(playerctl metadata --format '{{position}}') +length=$(playerctl metadata --format '{{mpris:length}}') + +# Check if both position and length are not empty +if [[ -n "$position" && -n "$length" && -n "$title" ]]; then + # Calculate the progress as a fraction + progress=$(echo "scale=2; $position / $length" | bc) + + # Calculate the length of the title + total_slots=$((${#title}/2)) + filled_slots=$(echo "$progress * $total_slots" | bc | awk '{printf("%d", $1 + 0.5)}') + + # Construct the progress bar string + progress_string="" + for ((i=1; i<=total_slots; i++)); do + if [ $i -eq $filled_slots ]; then + progress_string+="╡" + elif [ $i -lt $filled_slots ]; then + progress_string+="═" + else + progress_string+="─" + fi + done +fi + +echo $(playerctl metadata --format "{\"text\": \"$title\r$progress_string\", \"alt\": \"{{status}}\", \"class\": \"{{status}}\"}}") diff --git a/.config/waybar/modules.json b/.config/waybar/modules.json index 88cbbe0..bc1f195 100644 --- a/.config/waybar/modules.json +++ b/.config/waybar/modules.json @@ -8,19 +8,14 @@ "all-outputs": true, "format": "{icon}", "format-icons": { - "1": "", - "2": "", + "2": "", + "1": "", "3": "", + "4": "", "urgent": "", "focused": "", "default": "" }, - // "format": "{icon}", - // "format-icons": { - // "urgent": "!", - // "active": "*", - // "default": "0" - //}, "persistent-workspaces": { "*": 5 } @@ -51,6 +46,82 @@ "tooltip-format": "MPD (connected)", "tooltip-format-disconnected": "MPD (disconnected)" }, + "custom/playerctl": { + "interval": 1, + "tooltip": false, + "return-type": "json", + "format": "", + "format-icons": { + "Playing": "|>", + "Paused": "||" + }, + "exec": "~/.config/waybar/custom/playerctl.sh", + "on-click-right": "playerctl next", + "on-click": "playerctl play-pause", + "on-scroll-up": "playerctl position 1+", + "on-scroll-down": "playerctl position 1-", + "signal": 5 + }, + "group/playerctl": { + "orientation": "inherit", + "children-class": "playerctl", + "modules": [ + "custom/playerctl-prev", + "custom/playerctl-pause-play", + "custom/playerctl-next", + "custom/playerctl-title" + ] + + }, + "custom/playerctl-artist": { + "interval": 1, + "format": "{}", + "return-type": "json", + "exec": "playerctl metadata --format '{\"text\": \"{{markup_escape(artist)}}\", \"alt\": \"{{status}}\", \"class\": \"{{status}}\"}}'", + "on-click": "playerctl play-pause", + }, + "custom/playerctl-title": { + "interval": 1, + "format": "{}", + "return-type": "json", + "exec": "~/.config/waybar/custom/playerctl.sh", + "on-click": "playerctl play-pause", + "on-scroll-up": "playerctl position 1+", + "on-scroll-down": "playerctl position 1-", + }, + "custom/playerctl-prev": { + "interval": 1, + "format": "", + "return-type": "json", + "exec": "playerctl metadata --format '{\"alt\": \"{{status}}\", \"class\": \"{{status}}\"}}'", + "on-click": "playerctl previous", + "on-scroll-up": "playerctl position 1+", + "on-scroll-down": "playerctl position 1-", + }, + "custom/playerctl-pause-play": { + "interval": 1, + "format": "{icon}", + "format-icons": { + "Playing": "", + "Paused": "" + }, + "return-type": "json", + "exec": "playerctl metadata --format '{\"alt\": \"{{status}}\", \"class\": \"{{status}}\"}}'", + "on-click": "playerctl play-pause", + "on-scroll-up": "playerctl position 1+", + "on-scroll-down": "playerctl position 1-", + }, + "custom/playerctl-next": { + "interval": 1, + "format": "", + "return-type": "json", + "exec": "playerctl metadata --format '{\"alt\": \"{{status}}\", \"class\": \"{{status}}\"}}'", + "on-click": "playerctl next", + "on-scroll-up": "playerctl position 1+", + "on-scroll-down": "playerctl position 1-", + }, + + // Taskbar "wlr/taskbar": {