Skip to content

Commit 3987eec

Browse files
Merge pull request #344 from iberniex/spr
feat:spr plugin
2 parents f711cfe + 7cf2f55 commit 3987eec

File tree

3 files changed

+165
-2
lines changed

3 files changed

+165
-2
lines changed

docs/CONFIG.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [playerctl](#playerctl---up)
3232
- [ram-usage](#ram-usage---up)
3333
- [spotify-tui](#spotify-tui---up)
34+
- [spr](#spr---up)
3435
- [ssh-session](#ssh-session---up)
3536
- [synchronize-panes](#synchronize-panes---up)
3637
- [sys-temp](#sys-temp---up)
@@ -535,6 +536,7 @@ set -g @dracula-kubernetes-eks-extract-account true
535536
This script retrieves and displays continuous glucose monitoring (CGM) data from the LibreView API.
536537
It caches the data to minimize API requests and displays the latest glucose level along with a trend indicator in a Tmux status bar.
537538

539+
538540
### mac-player - [up](#table-of-contents)
539541

540542
This widget and script displays music information provided by the native macOS players.
@@ -554,8 +556,6 @@ The supported remote players are:
554556
- Spotify
555557
- Music - Apple Music
556558

557-
NOTE: `set -g @dracula-refresh-rate 5` affects this widget
558-
559559
To change player icons:
560560

561561
```bash
@@ -732,8 +732,47 @@ To limit the maximum length (0 means unlimited length):
732732
set -g @dracula-spotify-tui-max-len 30
733733
```
734734

735+
735736
`set -g @dracula-refresh-rate 5` affects this widget
736737

738+
### spr - [up](#table-of-contents)
739+
740+
This widget displays music information provided by [spotify-player](https://github.com/aome510/spotify-player). spotify-player must be installed to use this widget.
741+
742+
To change player icons:
743+
744+
```bash
745+
set -g @dracula-spr-play-icon ""
746+
set -g @dracula-spr-pause-icon "❚❚ "
747+
748+
```
749+
750+
This section includes an experimental remote control feature, but it may limit the widget’s display on macOS.
751+
752+
In order to utilize the remote feature you need to install the [spotify-player-daemon](https://github.com/aome510/spotify-player#daemon)
753+
To activate the remote:
754+
755+
```bash
756+
set -g @dracula-spr-remote true
757+
```
758+
759+
The default keybinds are:
760+
761+
- `<prefix> + P` - Play/Pause
762+
- `<prefix> + R` - Back to position 0/previous track
763+
- `<prefix> + N` - Next track
764+
765+
To change the keybinds:
766+
767+
```bash
768+
set -g @dracula-spr-remote-play-pause "P"
769+
set -g @dracula-spr-remote-back "R"
770+
set -g @dracula-spr-remote-next "N"
771+
```
772+
773+
`set -g @dracula-refresh-rate 5` affects this widget
774+
775+
737776
### ssh-session - [up](#table-of-contents)
738777

739778
This widget displays the current username@host combination, both of the local machine as well as when connected via ssh.

scripts/dracula.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ main() {
280280
IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-mpc-colors" "green dark_gray")
281281
script="#($current_dir/mpc.sh)"
282282

283+
elif [ $plugin = "spr" ]; then
284+
IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-spr-colors" "green dark_gray")
285+
script="#($current_dir/spr.sh)"
286+
283287
elif [ $plugin = "spotify-tui" ]; then
284288
IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-spotify-tui-colors" "green dark_gray")
285289
script="#($current_dir/spotify-tui.sh)"

scripts/spr.sh

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)