Skip to content

Commit f74270c

Browse files
πŸ“ Add docstrings to mac-player
Docstrings generation was requested by @ethancedwards8. * #343 (comment) The following files were modified: * `scripts/dracula.sh` * `scripts/mac-player.sh`
1 parent 3323120 commit f74270c

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed

β€Žscripts/dracula.shβ€Ž

100755100644
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export LC_ALL=en_US.UTF-8
55
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66
source $current_dir/utils.sh
77

8+
# main configures the tmux status bar with Dracula theme options, colors, and plugins.
9+
#
10+
# Reads user-defined tmux options to customize the status bar's appearance, color palette, icons, separators, refresh rate, and enabled plugins. Dynamically builds and applies tmux configuration for the status bar, including left and right sections, window status formats, and plugin outputs. Supports powerline styling, transparency, and a wide range of system and informational plugins.
811
main() {
912
# set configuration option variables
1013
show_krbtgt_label=$(get_tmux_option "@dracula-krbtgt-context-label" "")
@@ -56,6 +59,7 @@ main() {
5659
green="#50fa7b"
5760
orange="#ffb86c"
5861
red="#ff5555"
62+
purple="#b166cc"
5963
pink="#ff79c6"
6064
yellow="#f1fa8c"
6165

@@ -268,6 +272,11 @@ main() {
268272
IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-attached-clients-colors" "cyan dark_gray")
269273
script="#($current_dir/attached_clients.sh)"
270274

275+
276+
elif [ $plugin = "mac-player" ]; then
277+
IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-mac-player-colors" "purple dark_gray")
278+
script="#($current_dir/mac-player.sh)"
279+
271280
elif [ $plugin = "mpc" ]; then
272281
IFS=' ' read -r -a colors <<<$(get_tmux_option "@dracula-mpc-colors" "green dark_gray")
273282
script="#($current_dir/mpc.sh)"

β€Žscripts/mac-player.shβ€Ž

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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 detects the currently active media player (Spotify, Music, Safari, or Google Chrome) and outputs the current track information with play or pause icons, or a status message if no supported player is active.
10+
function trackStatus() {
11+
local active_player
12+
local pause_icon="$1"
13+
local play_icon="$2"
14+
15+
active_player=$(osascript -e "
16+
property playerList : {\"Spotify\", \"Music\", \"Safari\", \"Google Chrome\"}
17+
property nativePlayerList : {\"Spotify\", \"Music\"}
18+
19+
20+
-- Detect the Player being used
21+
on detectPlayer()
22+
repeat with appName in playerList
23+
set currentApp to contents of appName
24+
-- process checker
25+
-- native and browser checker
26+
if (running of application currentApp) then
27+
if (currentApp is not in nativePlayerList) then
28+
return browserPlayer(currentApp)
29+
else
30+
return nativePlayer(currentApp)
31+
end if
32+
end if
33+
end repeat
34+
35+
return \"No App Supported\"
36+
end detectPlayer
37+
38+
39+
-- nativePlayer Function: supports spotify and apple-music
40+
on nativePlayer(nativeName)
41+
if not (running of application nativeName) then return \"not running\"
42+
if nativeName is \"Spotify\" then
43+
tell application \"Spotify\"
44+
if player state is stopped then return \"stopped\"
45+
set trackArtist to artist of current track
46+
set trackName to name of current track
47+
if player state is paused then
48+
return \"$pause_icon \" & trackArtist & \" - \" & trackName
49+
else
50+
return \"$play_icon \" & trackArtist & \" - \" & trackName
51+
end if
52+
53+
end tell
54+
else if nativeName is \"Music\" then
55+
tell application \"Music\"
56+
if player state is stopped then return \"stopped\"
57+
set trackArtist to artist of current track
58+
set trackName to name of current track
59+
if player state is paused then
60+
return \"$pause_icon \" & trackArtist & \" - \" & trackName
61+
else
62+
return \"$play_icon \" & trackArtist & \" - \" & trackName
63+
end if
64+
65+
end tell
66+
end if
67+
68+
end nativePlayer
69+
70+
-- browserPlayer Function: supports Safari and Google Chrome
71+
on browserPlayer(browserName)
72+
if (running of application \"Safari\") and (browserName is \"Safari\") then
73+
tell application \"Safari\"
74+
set currentTab to the front document
75+
set currentURL to URL of currentTab
76+
set pageTitle to name of currentTab
77+
end tell
78+
else if (running of application \"Google Chrome\") and (browserName is \"Google Chrome\") then
79+
tell application \"Google Chrome\"
80+
set currentTab to active tab of front window
81+
--- set currentTab to active tab of front window
82+
set currentURL to URL of currentTab
83+
set pageTitle to title of currentTab
84+
end tell
85+
else
86+
return \"Not Supported\"
87+
end if
88+
89+
90+
-- Check if it's a YouTube video page
91+
if currentURL contains \"youtube.com/watch\" then
92+
-- YouTube video titles usually follow this format: \"Artist - Track Name\"
93+
set AppleScript's text item delimiters to \" - \"
94+
set titleParts to text items of pageTitle
95+
96+
if (count of titleParts) β‰₯ 2 then
97+
set artistName to item 1 of titleParts
98+
set trackName to item 2 of titleParts
99+
--- display dialog \"Artist: \" & artistName & return & \"Track: \" & trackName
100+
return artistName & \" - \" & trackName
101+
else
102+
--- display dialog \"Could not parse artist and track from: \" & pageTitle
103+
return \"can't encode\"
104+
end if
105+
-- Check if it's a Spotify video page
106+
else if currentURL contains \"open.spotify.com\" then
107+
-- Spotify video titles usually follow this format: \"Artist β€’ Track Name\"
108+
set AppleScript's text item delimiters to \" β€’ \"
109+
set titleParts to text items of pageTitle
110+
111+
if (count of titleParts) β‰₯ 2 then
112+
set artistName to item 1 of titleParts
113+
set trackName to item 2 of titleParts
114+
--- display dialog \"Artist: \" & artistName & return & \"Track: \" & trackName
115+
return artistName & \" - \" & trackName
116+
else
117+
--- display dialog \"Could not parse artist and track from: \" & pageTitle
118+
return \"can't encode\"
119+
end if
120+
121+
122+
else
123+
return \"No active Tab\"
124+
end if
125+
126+
end browserPlayer
127+
128+
129+
detectPlayer()
130+
")
131+
132+
133+
case "$active_player" in
134+
"not running") echo "not running" ;;
135+
"stopped") echo "stopped" ;;
136+
"can't encode") echo "unable to encode" ;;
137+
"Not Supported") echo "not supported" ;;
138+
139+
*) echo "$active_player" ;;
140+
esac
141+
142+
}
143+
144+
# sliceTrack truncates a string to a specified maximum length and appends "..." if it exceeds that length.
145+
function sliceTrack()
146+
{
147+
local str="$1"
148+
local std="$2"
149+
local len=${#str}
150+
151+
local result=""
152+
153+
if [[ $len > $std ]]; then
154+
result="${str:0:$std}"
155+
result+="..."
156+
else
157+
result=$str
158+
fi
159+
160+
echo "$result"
161+
}
162+
163+
164+
# remoteControl configures tmux key bindings for media playback control of Spotify or Music, or unbinds them for unsupported apps.
165+
function remoteControl() {
166+
toggle_button="$1"
167+
back_button="$2"
168+
next_button="$3"
169+
app_controlled="$4"
170+
171+
if [[ $app_controlled == "Spotify" ]] || [[ $app_controlled == "Music" ]]; then
172+
173+
toggle="osascript -e 'tell application \"$app_controlled\" to playpause'"
174+
back="osascript -e 'tell application \"$app_controlled\" to back track'"
175+
next="osascript -e 'tell application \"$app_controlled\" to play next track'"
176+
177+
tmux unbind-key "$toggle_button"
178+
tmux unbind-key "$back_button"
179+
tmux unbind-key "$next_button"
180+
181+
tmux bind-key "$toggle_button" run-shell "$toggle"
182+
tmux bind-key "$back_button" run-shell "$back"
183+
tmux bind-key "$next_button" run-shell "$next"
184+
else
185+
tmux unbind-key "$toggle_button"
186+
tmux unbind-key "$back_button"
187+
tmux unbind-key "$next_button"
188+
fi
189+
}
190+
191+
192+
# main is the entry point for the script, managing configuration, caching, remote control setup, and output of current media track information for tmux integration on macOS.
193+
main() {
194+
# save buffer to prevent lag
195+
local cache_file="/tmp/tmux_mac_player_cache"
196+
197+
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
198+
199+
# Active Player variables
200+
PLAY_ICON=$(get_tmux_option "@dracula-mac-player-play-icon" "β™ͺ")
201+
PAUSE_ICON=$(get_tmux_option "@dracula-mac-player-pause-icon" "❚❚ ")
202+
MAX_LENGTH=$(get_tmux_option "@dracula-mac-player-length" 25)
203+
204+
# Remote variables
205+
REMOTE_ACCESS=$(get_tmux_option "@dracula-mac-player-remote" false)
206+
REMOTE_APP=$(get_tmux_option "@dracula-mac-player-app" "Spotify")
207+
208+
# Remote Control Buttons Customizations
209+
PLAY_PAUSE_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-play-pause" "P")
210+
BACK_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-back" "R")
211+
NEXT_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-next" "N")
212+
213+
214+
215+
# os checker
216+
if [[ "$OSTYPE" != "darwin"* ]]; then
217+
echo ""
218+
exit 1
219+
fi
220+
221+
# Remote Access
222+
if [[ "$REMOTE_ACCESS" == true ]]; then
223+
remoteControl "$PLAY_PAUSE_BUTTON" "$BACK_BUTTON" "$NEXT_BUTTON" "$REMOTE_APP"
224+
225+
fi
226+
227+
if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -f%c "$cache_file"))) -ge "$RATE" ]; then
228+
trackStatus "$PAUSE_ICON" "$PLAY_ICON" > "$cache_file"
229+
sliceTrack "$(cat $cache_file)" "$MAX_LENGTH" > "$cache_file"
230+
fi
231+
232+
cat "$cache_file"
233+
}
234+
235+
main
236+

0 commit comments

Comments
Β (0)