|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 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 | + |
| 9 | +trackStatus() { |
| 10 | + local pause_icon="$1" play_icon="$2" |
| 11 | + local track_info playback status track_result |
| 12 | + |
| 13 | + playback=$(spotify_player get key playback) |
| 14 | + |
| 15 | + |
| 16 | + status=$(echo "$playback" | jq -r '.["is_playing"]') |
| 17 | + track_info=$(echo "$playback" | jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"') |
| 18 | + track_result="" |
| 19 | + |
| 20 | + if [[ $status == "true" ]]; then |
| 21 | + track_result+=$play_icon |
| 22 | + track_result+=$track_info |
| 23 | + else |
| 24 | + track_result+=$pause_icon |
| 25 | + track_result+=$track_info |
| 26 | + fi |
| 27 | + |
| 28 | +case "$status" in |
| 29 | + "null") echo "spotify not running" ;; |
| 30 | + *) echo "$track_result" ;; |
| 31 | + esac |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +function sliceTrack() |
| 36 | +{ |
| 37 | + local str="$1" |
| 38 | + local std="$2" |
| 39 | + local len=${#str} |
| 40 | + |
| 41 | + local result="" |
| 42 | + |
| 43 | + if [[ $len > $std ]]; then |
| 44 | + result="${str:0:$std}" |
| 45 | + result+="..." |
| 46 | + else |
| 47 | + result=$str |
| 48 | + fi |
| 49 | + |
| 50 | + echo "$result" |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +function sprRemoteControl() { |
| 55 | + local toggle_button="$1" |
| 56 | + local back_button="$2" |
| 57 | + local next_button="$3" |
| 58 | + |
| 59 | + local toggle="spotify_player playback play-pause > /dev/null 2>&1" |
| 60 | + local back="spotify_player playback previous > /dev/null 2>&1" |
| 61 | + local next="spotify_player playback next > /dev/null 2>&1" |
| 62 | + |
| 63 | + |
| 64 | + tmux unbind-key "$toggle_button" 2>/dev/null |
| 65 | + tmux unbind-key "$back_button" 2>/dev/null |
| 66 | + tmux unbind-key "$next_button" 2>/dev/null |
| 67 | + |
| 68 | + tmux bind-key "$toggle_button" run-shell "$toggle" |
| 69 | + tmux bind-key "$back_button" run-shell "$back" |
| 70 | + tmux bind-key "$next_button" run-shell "$next" |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +main() { |
| 75 | + # save buffer to prevent lag |
| 76 | + local cache_file="/tmp/tmux_spr_cache" |
| 77 | + |
| 78 | + RATE="$(get_tmux_option "@dracula-refresh-rate" 5)" |
| 79 | + |
| 80 | + MAX_LENGTH="$(get_tmux_option "@dracula-spr-length" 25)" |
| 81 | + |
| 82 | + # Remote Control checker |
| 83 | + SPR_REMOTE_ACCESS="$(get_tmux_option "@dracula-spr-remote" "false")" |
| 84 | + |
| 85 | + PLAY_ICON=$(get_tmux_option "@dracula-spr-play-icon" "♪ ") |
| 86 | + PAUSE_ICON=$(get_tmux_option "@dracula-spr-pause-icon" "❚❚ ") |
| 87 | + |
| 88 | + |
| 89 | + if ! command -v spotify_player &> /dev/null |
| 90 | + then |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + # Remote Access |
| 95 | + if [[ "$SPR_REMOTE_ACCESS" == "true" ]]; then |
| 96 | + PLAY_PAUSE_BUTTON=$(get_tmux_option "@dracula-spr-remote-play-pause" "P") |
| 97 | + BACK_BUTTON=$(get_tmux_option "@dracula-spr-remote-back" "R") |
| 98 | + NEXT_BUTTON=$(get_tmux_option "@dracula-spr-remote-next" "N") |
| 99 | + sprRemoteControl "$PLAY_PAUSE_BUTTON" "$BACK_BUTTON" "$NEXT_BUTTON" |
| 100 | + else |
| 101 | + tmux set -g @dracula-spr-remote-play-pause "" |
| 102 | + tmux set -g @dracula-spr-remote-back "" |
| 103 | + tmux set -g @dracula-spr-remote-next "" |
| 104 | + tmux unbind-key "$PLAY_PAUSE_BUTTON" 2>/dev/null |
| 105 | + tmux unbind-key "$BACK_BUTTON" 2>/dev/null |
| 106 | + tmux unbind-key "$NEXT_BUTTON" 2>/dev/null |
| 107 | + fi |
| 108 | + |
| 109 | + |
| 110 | + if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -c%Y "$cache_file" 2>/dev/null || stat -f%m "$cache_file"))) -ge "$RATE" ]; then |
| 111 | + local full_track |
| 112 | + full_track=$(trackStatus "$PAUSE_ICON" "$PLAY_ICON") |
| 113 | + sliceTrack "$full_track" "$MAX_LENGTH" > "$cache_file" |
| 114 | + fi |
| 115 | + |
| 116 | + echo "$BACK_BUTTON" |
| 117 | +} |
| 118 | + |
| 119 | +main |
| 120 | + |
0 commit comments