diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 3f3f5bba..851f56b2 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -31,6 +31,7 @@ - [playerctl](#playerctl---up) - [ram-usage](#ram-usage---up) - [spotify-tui](#spotify-tui---up) + - [spr](#spr---up) - [ssh-session](#ssh-session---up) - [synchronize-panes](#synchronize-panes---up) - [sys-temp](#sys-temp---up) @@ -516,6 +517,7 @@ set -g @dracula-kubernetes-eks-extract-account true This script retrieves and displays continuous glucose monitoring (CGM) data from the LibreView API. It caches the data to minimize API requests and displays the latest glucose level along with a trend indicator in a Tmux status bar. + ### mac-player - [up](#table-of-contents) This widget and script displays music information provided by the native macOS players. @@ -535,8 +537,6 @@ The supported remote players are: - Spotify - Music - Apple Music -NOTE: `set -g @dracula-refresh-rate 5` affects this widget - To change player icons: ```bash @@ -713,8 +713,47 @@ To limit the maximum length (0 means unlimited length): set -g @dracula-spotify-tui-max-len 30 ``` + `set -g @dracula-refresh-rate 5` affects this widget +### spr - [up](#table-of-contents) + +This widget displays music information provided by [spotify-player](https://github.com/aome510/spotify-player). spotify-player must be installed to use this widget. + +To change player icons: + +```bash +set -g @dracula-spr-play-icon "♪ " +set -g @dracula-spr-pause-icon "❚❚ " + +``` + +This section includes an experimental remote control feature, but it may limit the widget’s display on macOS. + +In order to utilize the remote feature you need to install the [spotify-player-daemon](https://github.com/aome510/spotify-player#daemon) +To activate the remote: + +```bash +set -g @dracula-spr-remote true +``` + +The default keybinds are: + +- ` + P` - Play/Pause +- ` + R` - Back to position 0/previous track +- ` + N` - Next track + +To change the keybinds: + +```bash +set -g @dracula-spr-remote-play-pause "P" +set -g @dracula-spr-remote-back "R" +set -g @dracula-spr-remote-next "N" +``` + +`set -g @dracula-refresh-rate 5` affects this widget + + ### ssh-session - [up](#table-of-contents) This widget displays the current username@host combination, both of the local machine as well as when connected via ssh. diff --git a/scripts/dracula.sh b/scripts/dracula.sh index fb869021..313f146f 100755 --- a/scripts/dracula.sh +++ b/scripts/dracula.sh @@ -278,6 +278,10 @@ main() { IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-mpc-colors" "green dark_gray") script="#($current_dir/mpc.sh)" + elif [ $plugin = "spr" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-spr-colors" "green dark_gray") + script="#($current_dir/spr.sh)" + elif [ $plugin = "spotify-tui" ]; then IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-spotify-tui-colors" "green dark_gray") script="#($current_dir/spotify-tui.sh)" diff --git a/scripts/spr.sh b/scripts/spr.sh new file mode 100755 index 00000000..7dfec955 --- /dev/null +++ b/scripts/spr.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash + +export LC_ALL=en_US.UTF-8 + +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$current_dir/utils.sh" + + +trackStatus() { + local pause_icon="$1" play_icon="$2" + local track_info playback status track_result + + playback=$(spotify_player get key playback) + + + status=$(echo "$playback" | jq -r '.["is_playing"]') + track_info=$(echo "$playback" | jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"') + track_result="" + + if [[ $status == "true" ]]; then + track_result+=$play_icon + track_result+=$track_info + else + track_result+=$pause_icon + track_result+=$track_info + fi + +case "$status" in + "null") echo "spotify not running" ;; + *) echo "$track_result" ;; + esac + +} + +function sliceTrack() +{ + local str="$1" + local std="$2" + local len=${#str} + + local result="" + + if [[ $len > $std ]]; then + result="${str:0:$std}" + result+="..." + else + result=$str + fi + + echo "$result" +} + + +function sprRemoteControl() { + local toggle_button="$1" + local back_button="$2" + local next_button="$3" + + local toggle="spotify_player playback play-pause > /dev/null 2>&1" + local back="spotify_player playback previous > /dev/null 2>&1" + local next="spotify_player playback next > /dev/null 2>&1" + + + tmux unbind-key "$toggle_button" 2>/dev/null + tmux unbind-key "$back_button" 2>/dev/null + tmux unbind-key "$next_button" 2>/dev/null + + tmux bind-key "$toggle_button" run-shell "$toggle" + tmux bind-key "$back_button" run-shell "$back" + tmux bind-key "$next_button" run-shell "$next" + +} + +main() { + # save buffer to prevent lag + local cache_file="/tmp/tmux_spr_cache" + + RATE="$(get_tmux_option "@dracula-refresh-rate" 5)" + + MAX_LENGTH="$(get_tmux_option "@dracula-spr-length" 25)" + + # Remote Control checker + SPR_REMOTE_ACCESS="$(get_tmux_option "@dracula-spr-remote" "false")" + + PLAY_ICON=$(get_tmux_option "@dracula-spr-play-icon" "♪ ") + PAUSE_ICON=$(get_tmux_option "@dracula-spr-pause-icon" "❚❚ ") + + + if ! command -v spotify_player &> /dev/null + then + exit 1 + fi + + # Remote Access + if [[ "$SPR_REMOTE_ACCESS" == "true" ]]; then + PLAY_PAUSE_BUTTON=$(get_tmux_option "@dracula-spr-remote-play-pause" "P") + BACK_BUTTON=$(get_tmux_option "@dracula-spr-remote-back" "R") + NEXT_BUTTON=$(get_tmux_option "@dracula-spr-remote-next" "N") + sprRemoteControl "$PLAY_PAUSE_BUTTON" "$BACK_BUTTON" "$NEXT_BUTTON" + else + tmux set -g @dracula-spr-remote-play-pause "" + tmux set -g @dracula-spr-remote-back "" + tmux set -g @dracula-spr-remote-next "" + tmux unbind-key "$PLAY_PAUSE_BUTTON" 2>/dev/null + tmux unbind-key "$BACK_BUTTON" 2>/dev/null + tmux unbind-key "$NEXT_BUTTON" 2>/dev/null + fi + + + if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -c%Y "$cache_file" 2>/dev/null || stat -f%m "$cache_file"))) -ge "$RATE" ]; then + local full_track + full_track=$(trackStatus "$PAUSE_ICON" "$PLAY_ICON") + sliceTrack "$full_track" "$MAX_LENGTH" > "$cache_file" + fi + + echo "$BACK_BUTTON" +} + +main +