diff --git a/scripts/playerctl.sh b/scripts/playerctl.sh index a813a5ea..70e94729 100755 --- a/scripts/playerctl.sh +++ b/scripts/playerctl.sh @@ -2,23 +2,59 @@ # setting the locale, some users have issues with different locales, this forces the correct one export LC_ALL=en_US.UTF-8 -current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source $current_dir/utils.sh -main() -{ +function slice_loop() { + local str="$1" + local start="$2" + local how_many="$3" + local len=${#str} + + local result="" + + for ((i = 0; i < how_many; i++)); do + local index=$(((start + i) % len)) + local char="${str:index:1}" + result="$result$char" + done + + echo "$result" +} + +main() { # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@dracula-refresh-rate" 5) - if ! command -v playerctl &> /dev/null - then + if ! command -v playerctl &>/dev/null; then exit 1 fi FORMAT=$(get_tmux_option "@dracula-playerctl-format" "Now playing: {{ artist }} - {{ album }} - {{ title }}") playerctl_playback=$(playerctl metadata --format "${FORMAT}") - echo ${playerctl_playback} + playerctl_playback="${playerctl_playback} " + + # Adjust width of string + terminal_width=25 + + # Initial start point for scrolling + start=0 + len=${#playerctl_playback} + + 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" } # run the main driver