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..c546df85 100755 --- a/scripts/mac-player.sh +++ b/scripts/mac-player.sh @@ -3,8 +3,7 @@ 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 @@ -185,6 +184,25 @@ 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") + printf "\r%s " "$scrolling_text" + + sleep "$speed" + done + + printf "\r%s " "$scrolling_text" +} main() { # save buffer to prevent lag @@ -206,7 +224,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,15 +237,23 @@ 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 - cat "$cache_file" + # Allow scrolling + local str=$(cat "$cache_file") + if [ "$SCROLL" = true ] && [ "${#str}" -ge $MAX_LENGTH ]; then + scroll "$str" "$MAX_LENGTH" "$SCROLL_SPEED" + else + echo "$str" + fi } main diff --git a/scripts/playerctl.sh b/scripts/playerctl.sh index 67a14e65..157c1667 100755 --- a/scripts/playerctl.sh +++ b/scripts/playerctl.sh @@ -5,21 +5,30 @@ 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=$3 + + 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") - for ((i = 0; i < how_many; i++)); do - local index=$(((start + i) % len)) - local char="${str:index:1}" - result="$result$char" + printf "\r%s " "$scrolling_text" + + sleep "$speed" done - echo "$result" + printf "\r%s " "$scrolling_text" } main() { @@ -31,12 +40,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 +60,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..adae49e4 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -34,3 +34,32 @@ normalize_percent_len() { 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" + + # Check that the string is not empty + if [[ -z $str ]]; then + echo "" + return 0 + fi + + local len=${#str} + + local result="" + # Capture 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" +}