/ scripts / playerctl.sh
playerctl.sh
 1  #!/usr/bin/env bash
 2  # setting the locale, some users have issues with different locales, this forces the correct one
 3  export LC_ALL=en_US.UTF-8
 4  
 5  current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 6  source $current_dir/utils.sh
 7  
 8  function slice_loop() {
 9    local str="$1"
10    local start="$2"
11    local how_many="$3"
12    local len=${#str}
13  
14    local result=""
15  
16    for ((i = 0; i < how_many; i++)); do
17      local index=$(((start + i) % len))
18      local char="${str:index:1}"
19      result="$result$char"
20    done
21  
22    echo "$result"
23  }
24  
25  main() {
26    # storing the refresh rate in the variable RATE, default is 5
27    RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
28  
29    if ! command -v playerctl &>/dev/null; then
30      exit 1
31    fi
32  
33    FORMAT=$(get_tmux_option "@dracula-playerctl-format" "Now playing: {{ artist }} - {{ album }} - {{ title }}")
34    playerctl_playback=$(playerctl metadata --format "${FORMAT}")
35    playerctl_playback="${playerctl_playback} "
36  
37    # Adjust width of string
38    terminal_width=25
39  
40    # Initial start point for scrolling
41    start=0
42    len=${#playerctl_playback}
43  
44    # previously we have appended a space to playerctl_playback
45    # if there is no player, len sees only one space
46    # exit the script and output nothing if there is just that space
47    if [[ $len == 1 ]]; then
48      exit
49    fi
50  
51    scrolling_text=""
52  
53    for ((start = 0; start <= len; start++)); do
54      scrolling_text=$(slice_loop "$playerctl_playback" "$start" "$terminal_width")
55      echo -ne "\r"
56      echo "$scrolling_text "
57      echo -ne "\r"
58  
59      sleep 0.08
60    done
61  
62    echo -ne "\r"
63    echo "$scrolling_text "
64    echo -ne "\r"
65  }
66  
67  # run the main driver
68  main