diff --git a/docs/CONFIG.md b/docs/CONFIG.md index b015a2fa..c8fa1f91 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -601,6 +601,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. @@ -705,6 +712,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 caba4ed1..add708fd 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 @@ -186,6 +185,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 @@ -202,6 +220,15 @@ main() { REMOTE_ACCESS=$(get_tmux_option "@dracula-mac-player-remote" "false") REMOTE_APP=$(get_tmux_option "@dracula-mac-player-app" "Spotify") + # Remote Control Buttons Customizations + PLAY_PAUSE_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-play-pause" "P") + 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 exit 1 @@ -225,14 +252,22 @@ main() { tmux unbind-key "$NEXT_BUTTON" 2>/dev/null fi + # handle cache separately from the scrolling feature if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -f%c "$cache_file"))) -ge "$RATE" ]; then local full_track - full_track=$(trackStatus "$PAUSE_ICON" "$PLAY_ICON") - sliceTrack "$full_track" "$MAX_LENGTH" > "$cache_file" + + echo "$full_track" >"$cache_file" fi - cat "$cache_file" + # Allow scrolling and if not default to the string stripping concatenation thingy from length + local final_str + final_str=$(cat "$cache_file") + if [ "$SCROLL" = "true" ] && [ "${#final_str}" -ge "$MAX_LENGTH" ]; then + scroll "$final_str" "$MAX_LENGTH" "$SCROLL_SPEED" + else + sliceTrack "$final_str" "$MAX_LENGTH" + 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" +}