From 34b35796291cd56eb867d4519fe8cd13ae7a4f7c Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Tue, 22 Jul 2025 17:52:49 -0400 Subject: [PATCH 1/6] Updated playerctl and mac player modules Made sure that both modules have scrollable text as an option as exposed several other parameters that the user can use to customize --- docs/CONFIG.md | 15 +++++++++ scripts/mac-player.sh | 73 ++++++++++++++++++++++++++++++------------- scripts/playerctl.sh | 56 ++++++++++++++++++--------------- scripts/utils.sh | 26 ++++++++++++++- 4 files changed, 122 insertions(+), 48 deletions(-) diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 3f3f5bba..9cde5969 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -577,6 +577,13 @@ set -g @dracula-mac-player-remote-back "R" set -g @dracula-mac-player-remote-next "N" ``` +To add a scrolling effect to the player instead of a truncated text: + +```bash +set -g @dracula-mac-player-scroll true +set -g @dracula-mac-player-scroll-speed 0.08 # Lower speeds means faster scroll between renders +``` + ### mpc - [up](#table-of-contents) This widget displays music information provided by mpc. @@ -681,6 +688,14 @@ Set the playerctl metadata format like so: set -g @dracula-playerctl-format "► {{ artist }} - {{ title }}" ``` +To set the player to scroll the text: + +```bash +set -g @dracula-playerctl-scroll true # on by default +set -g @dracula-playerctl-width 25 +set -g @dracula-playerctl-speed 0.08 # Small speeds = faster scrolling +``` + ### ram-usage - [up](#table-of-contents) This widget displays the currently used ram as GB per GB. diff --git a/scripts/mac-player.sh b/scripts/mac-player.sh index f4ae62c8..81bf3183 100755 --- a/scripts/mac-player.sh +++ b/scripts/mac-player.sh @@ -3,15 +3,14 @@ export LC_ALL=en_US.UTF-8 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "$current_dir/utils.sh" - +source $current_dir/utils.sh function trackStatus() { - local active_player + local active_player local pause_icon="$1" local play_icon="$2" - active_player=$(osascript -e " + active_player=$(osascript -e " property playerList : {\"Spotify\", \"Music\", \"Safari\", \"Google Chrome\"} property nativePlayerList : {\"Spotify\", \"Music\"} @@ -128,20 +127,18 @@ function trackStatus() { detectPlayer() ") + case "$active_player" in + "not running") echo "not running" ;; + "stopped") echo "stopped" ;; + "can't encode") echo "unable to encode" ;; + "Not Supported") echo "not supported" ;; -case "$active_player" in - "not running") echo "not running" ;; - "stopped") echo "stopped" ;; - "can't encode") echo "unable to encode" ;; - "Not Supported") echo "not supported" ;; - - *) echo "$active_player" ;; - esac + *) echo "$active_player" ;; + esac } -function sliceTrack() -{ +function sliceTrack() { local str="$1" local std="$2" local len=${#str} @@ -158,7 +155,6 @@ function sliceTrack() echo "$result" } - function remoteControl() { toggle_button="$1" back_button="$2" @@ -185,10 +181,33 @@ function remoteControl() { fi } +# Scroll the text +function scroll() { + local str=$1 + local width=$2 + local speed=$3 + + local scrolling_text="" + local i=0 + local len=${#str} + + for ((i = 0; i <= len; i++)); do + scrolling_text=$(slice_text "$str" "$i" "$width") + echo -ne "\r" + echo "$scrolling_text" + echo -ne "\r" + + sleep "$speed" + done + + echo -ne "\r" + echo "$scrolling_text" + echo -ne "\r" +} main() { # save buffer to prevent lag - local cache_file="/tmp/tmux_mac_player_cache" + local cache_file="/tmp/tmux_mac_player_cache" RATE=$(get_tmux_option "@dracula-refresh-rate" 5) @@ -206,7 +225,9 @@ main() { BACK_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-back" "R") NEXT_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-next" "N") - + # Scroll + SCROLL=$(get_tmux_option "@dracula-mac-player-scroll" false) + SCROLL_SPEED=$(get_tmux_option "@dracula-mac-player-scroll-speed" 0.08) # os checker if [[ "$OSTYPE" != "darwin"* ]]; then @@ -217,16 +238,24 @@ main() { # Remote Access if [[ "$REMOTE_ACCESS" == true ]]; then remoteControl "$PLAY_PAUSE_BUTTON" "$BACK_BUTTON" "$NEXT_BUTTON" "$REMOTE_APP" - fi if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -f%c "$cache_file"))) -ge "$RATE" ]; then - trackStatus "$PAUSE_ICON" "$PLAY_ICON" > "$cache_file" - sliceTrack "$(cat $cache_file)" "$MAX_LENGTH" > "$cache_file" + trackStatus "$PAUSE_ICON" "$PLAY_ICON" >"$cache_file" + + if [ "$SCROLL" = false ]; then + sliceTrack "$(cat $cache_file)" "$MAX_LENGTH" >"$cache_file" + fi + fi + + # Allow scrolling + local str=$(cat "$cache_file") + if [ "$SCROLL" = true ]; then + scroll "$str" "$MAX_LENGTH" "$SCROLL_SPEED" + else + echo "$str" fi - cat "$cache_file" } main - diff --git a/scripts/playerctl.sh b/scripts/playerctl.sh index 67a14e65..b7b99d5c 100755 --- a/scripts/playerctl.sh +++ b/scripts/playerctl.sh @@ -5,21 +5,33 @@ export LC_ALL=en_US.UTF-8 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source $current_dir/utils.sh -function slice_loop() { - local str="$1" - local start="$2" - local how_many="$3" +# Set the configuration + +# Scroll the text +# arg1: text +# arg2: width +# arg3: speed +scroll() { + local str=$1 + local width=$2 + local speed=$2 + + local scrolling_text="" + local i=0 local len=${#str} - local result="" + for ((i = 0; i <= len; i++)); do + scrolling_text=$(slice_text "$str" "$i" "$width") + echo -ne "\r" + echo "$scrolling_text " + echo -ne "\r" - for ((i = 0; i < how_many; i++)); do - local index=$(((start + i) % len)) - local char="${str:index:1}" - result="$result$char" + sleep "$speed" done - echo "$result" + echo -ne "\r" + echo "$scrolling_text " + echo -ne "\r" } main() { @@ -31,12 +43,13 @@ main() { fi FORMAT=$(get_tmux_option "@dracula-playerctl-format" "Now playing: {{ artist }} - {{ album }} - {{ title }}") + SCROLL=$(get_tmux_option "@dracula-playerctl-scroll" true) + WIDTH=$(get_tmux_option "@dracula-playerctl-width" 25) + SPEED=$(get_tmux_option "@dracula-playerctl-speed" 0.08) + playerctl_playback=$(playerctl metadata --format "${FORMAT}") playerctl_playback="${playerctl_playback} " - # Adjust width of string - terminal_width=25 - # Initial start point for scrolling start=0 len=${#playerctl_playback} @@ -50,18 +63,11 @@ main() { scrolling_text="" - for ((start = 0; start <= len; start++)); do - scrolling_text=$(slice_loop "$playerctl_playback" "$start" "$terminal_width") - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" - - sleep 0.08 - done - - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" + if [ "$SCROLL" = true ]; then + scroll "$playerctl_playback" "$WIDTH" "$SPEED" + else + echo "$playerctl_playback" + fi } # run the main driver diff --git a/scripts/utils.sh b/scripts/utils.sh index a6a46fb6..cf6800e7 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -29,8 +29,32 @@ normalize_percent_len() { percent_len=${#1} let diff_len=$max_len-$percent_len # if the diff_len is even, left will have 1 more space than right - let left_spaces=($diff_len+1)/2 + let left_spaces=($diff_len + 1)/2 let right_spaces=($diff_len)/2 printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" } +# Create a slice of the text to show currently in the module +# arg1: The full string +# arg2: Where to start the loop from +# arg3: The length of characters to display +slice_text() { + local str="$1" + local start="$2" + local how_many="$3" + + local len=${#str} + + local result="" + + # Caputre the strings to show + for ((i = 0; i < how_many; i++)); do + local index=$(((start + i) % len)) + local char="${str:index:1}" + + # Append the character to show + result="$result$char" + done + + echo "$result" +} From 8763e9d6abf816929c9813470b02a1bc9a25eb37 Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Tue, 22 Jul 2025 23:41:40 -0400 Subject: [PATCH 2/6] formatting updated --- scripts/utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/utils.sh b/scripts/utils.sh index cf6800e7..06ba49a0 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -29,7 +29,7 @@ normalize_percent_len() { percent_len=${#1} let diff_len=$max_len-$percent_len # if the diff_len is even, left will have 1 more space than right - let left_spaces=($diff_len + 1)/2 + let left_spaces=($diff_len+1)/2 let right_spaces=($diff_len)/2 printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" } From fc5efb13de37f06c6026264d3206a96616d608d0 Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Wed, 23 Jul 2025 11:35:57 -0400 Subject: [PATCH 3/6] Formatting fixes Reverted some formatting changes to previous code --- scripts/mac-player.sh | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/scripts/mac-player.sh b/scripts/mac-player.sh index 81bf3183..28603597 100755 --- a/scripts/mac-player.sh +++ b/scripts/mac-player.sh @@ -6,11 +6,11 @@ current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source $current_dir/utils.sh function trackStatus() { - local active_player + local active_player local pause_icon="$1" local play_icon="$2" - active_player=$(osascript -e " + active_player=$(osascript -e " property playerList : {\"Spotify\", \"Music\", \"Safari\", \"Google Chrome\"} property nativePlayerList : {\"Spotify\", \"Music\"} @@ -127,18 +127,20 @@ function trackStatus() { detectPlayer() ") - case "$active_player" in - "not running") echo "not running" ;; - "stopped") echo "stopped" ;; - "can't encode") echo "unable to encode" ;; - "Not Supported") echo "not supported" ;; - *) echo "$active_player" ;; - esac +case "$active_player" in + "not running") echo "not running" ;; + "stopped") echo "stopped" ;; + "can't encode") echo "unable to encode" ;; + "Not Supported") echo "not supported" ;; + + *) echo "$active_player" ;; + esac } -function sliceTrack() { +function sliceTrack() +{ local str="$1" local std="$2" local len=${#str} @@ -155,6 +157,7 @@ function sliceTrack() { echo "$result" } + function remoteControl() { toggle_button="$1" back_button="$2" @@ -194,20 +197,20 @@ function scroll() { for ((i = 0; i <= len; i++)); do scrolling_text=$(slice_text "$str" "$i" "$width") echo -ne "\r" - echo "$scrolling_text" + echo "$scrolling_text " echo -ne "\r" sleep "$speed" done echo -ne "\r" - echo "$scrolling_text" + echo "$scrolling_text " echo -ne "\r" } main() { # save buffer to prevent lag - local cache_file="/tmp/tmux_mac_player_cache" + local cache_file="/tmp/tmux_mac_player_cache" RATE=$(get_tmux_option "@dracula-refresh-rate" 5) @@ -255,7 +258,7 @@ main() { else echo "$str" fi - } main + From a8deaec1f121298a2eebfd3fb8f25bb71275c742 Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Wed, 23 Jul 2025 12:03:25 -0400 Subject: [PATCH 4/6] small typo --- scripts/playerctl.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/playerctl.sh b/scripts/playerctl.sh index b7b99d5c..2bfd18ff 100755 --- a/scripts/playerctl.sh +++ b/scripts/playerctl.sh @@ -14,7 +14,7 @@ source $current_dir/utils.sh scroll() { local str=$1 local width=$2 - local speed=$2 + local speed=$3 local scrolling_text="" local i=0 From f829cc0d4737322dfd7daae5effa91103ab3caa3 Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Wed, 23 Jul 2025 12:10:19 -0400 Subject: [PATCH 5/6] Code rabbit typo fixes Added a couple fixes suggested by code rabbit --- scripts/mac-player.sh | 8 ++------ scripts/playerctl.sh | 9 +++------ scripts/utils.sh | 9 +++++++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/scripts/mac-player.sh b/scripts/mac-player.sh index 28603597..df37bc4e 100755 --- a/scripts/mac-player.sh +++ b/scripts/mac-player.sh @@ -196,16 +196,12 @@ function scroll() { for ((i = 0; i <= len; i++)); do scrolling_text=$(slice_text "$str" "$i" "$width") - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" + printf "\r%s " "$scrolling_text" sleep "$speed" done - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" + printf "\r%s " "$scrolling_text" } main() { diff --git a/scripts/playerctl.sh b/scripts/playerctl.sh index 2bfd18ff..157c1667 100755 --- a/scripts/playerctl.sh +++ b/scripts/playerctl.sh @@ -22,16 +22,13 @@ scroll() { for ((i = 0; i <= len; i++)); do scrolling_text=$(slice_text "$str" "$i" "$width") - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" + + printf "\r%s " "$scrolling_text" sleep "$speed" done - echo -ne "\r" - echo "$scrolling_text " - echo -ne "\r" + printf "\r%s " "$scrolling_text" } main() { diff --git a/scripts/utils.sh b/scripts/utils.sh index 06ba49a0..adae49e4 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -43,11 +43,16 @@ slice_text() { local start="$2" local how_many="$3" + # Check that the string is not empty + if [[ -z $str ]]; then + echo "" + return 0 + fi + local len=${#str} local result="" - - # Caputre the strings to show + # Capture the strings to show for ((i = 0; i < how_many; i++)); do local index=$(((start + i) % len)) local char="${str:index:1}" From a149f17bfc8d6fb3b018755c7398ffd6b3098150 Mon Sep 17 00:00:00 2001 From: marcosFIF Date: Fri, 25 Jul 2025 16:10:23 -0400 Subject: [PATCH 6/6] Made sure max length is respected for the player --- scripts/mac-player.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mac-player.sh b/scripts/mac-player.sh index df37bc4e..c546df85 100755 --- a/scripts/mac-player.sh +++ b/scripts/mac-player.sh @@ -249,7 +249,7 @@ main() { # Allow scrolling local str=$(cat "$cache_file") - if [ "$SCROLL" = true ]; then + if [ "$SCROLL" = true ] && [ "${#str}" -ge $MAX_LENGTH ]; then scroll "$str" "$MAX_LENGTH" "$SCROLL_SPEED" else echo "$str"