Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ set -g @dracula-mac-player-remote-back "R"
set -g @dracula-mac-player-remote-next "N"
```

To add a scrolling effect to the player instead of a truncated text:

```bash
set -g @dracula-mac-player-scroll true
set -g @dracula-mac-player-scroll-speed 0.08 # Lower speeds means faster scroll between renders
```

### mpc - [up](#table-of-contents)

This widget displays music information provided by mpc.
Expand Down Expand Up @@ -681,6 +688,14 @@ Set the playerctl metadata format like so:
set -g @dracula-playerctl-format "► {{ artist }} - {{ title }}"
```

To set the player to scroll the text:

```bash
set -g @dracula-playerctl-scroll true # on by default
set -g @dracula-playerctl-width 25
set -g @dracula-playerctl-speed 0.08 # Small speeds = faster scrolling
```

### ram-usage - [up](#table-of-contents)

This widget displays the currently used ram as GB per GB.
Expand Down
73 changes: 51 additions & 22 deletions scripts/mac-player.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
export LC_ALL=en_US.UTF-8

current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir/utils.sh"

source $current_dir/utils.sh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Quote the sourced path to avoid breakage on paths containing spaces
Unquoted expansions break if the script is located in a directory with whitespace (e.g. “/Users/al ice/tmux”). Always quote path-like variables when sourcing or executing.

-source $current_dir/utils.sh
+source "$current_dir/utils.sh"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
source $current_dir/utils.sh
source "$current_dir/utils.sh"
🤖 Prompt for AI Agents
In scripts/mac-player.sh at line 6, the sourced path variable $current_dir is
not quoted, which can cause the script to break if the directory path contains
spaces. Fix this by enclosing $current_dir/utils.sh in double quotes when using
source, like source "$current_dir/utils.sh", to ensure the path is correctly
interpreted even with spaces.


function trackStatus() {
local active_player
local active_player
local pause_icon="$1"
local play_icon="$2"

active_player=$(osascript -e "
active_player=$(osascript -e "
property playerList : {\"Spotify\", \"Music\", \"Safari\", \"Google Chrome\"}
property nativePlayerList : {\"Spotify\", \"Music\"}

Expand Down Expand Up @@ -128,20 +127,18 @@ function trackStatus() {
detectPlayer()
")

case "$active_player" in
"not running") echo "not running" ;;
"stopped") echo "stopped" ;;
"can't encode") echo "unable to encode" ;;
"Not Supported") echo "not supported" ;;

case "$active_player" in
"not running") echo "not running" ;;
"stopped") echo "stopped" ;;
"can't encode") echo "unable to encode" ;;
"Not Supported") echo "not supported" ;;

*) echo "$active_player" ;;
esac
*) echo "$active_player" ;;
esac

}

function sliceTrack()
{
function sliceTrack() {
local str="$1"
local std="$2"
local len=${#str}
Expand All @@ -158,7 +155,6 @@ function sliceTrack()
echo "$result"
}


function remoteControl() {
toggle_button="$1"
back_button="$2"
Expand All @@ -185,10 +181,33 @@ function remoteControl() {
fi
}

# Scroll the text
function scroll() {
local str=$1
local width=$2
local speed=$3

local scrolling_text=""
local i=0
local len=${#str}

for ((i = 0; i <= len; i++)); do
scrolling_text=$(slice_text "$str" "$i" "$width")
echo -ne "\r"
echo "$scrolling_text"
echo -ne "\r"

sleep "$speed"
done

echo -ne "\r"
echo "$scrolling_text"
echo -ne "\r"
}

main() {
# save buffer to prevent lag
local cache_file="/tmp/tmux_mac_player_cache"
local cache_file="/tmp/tmux_mac_player_cache"

RATE=$(get_tmux_option "@dracula-refresh-rate" 5)

Expand All @@ -206,7 +225,9 @@ main() {
BACK_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-back" "R")
NEXT_BUTTON=$(get_tmux_option "@dracula-mac-player-remote-next" "N")


# Scroll
SCROLL=$(get_tmux_option "@dracula-mac-player-scroll" false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget this too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SCROLL=$(get_tmux_option "@dracula-mac-player-scroll" "false")

SCROLL_SPEED=$(get_tmux_option "@dracula-mac-player-scroll-speed" 0.08)

# os checker
if [[ "$OSTYPE" != "darwin"* ]]; then
Expand All @@ -217,16 +238,24 @@ main() {
# Remote Access
if [[ "$REMOTE_ACCESS" == true ]]; then
remoteControl "$PLAY_PAUSE_BUTTON" "$BACK_BUTTON" "$NEXT_BUTTON" "$REMOTE_APP"

fi

if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -f%c "$cache_file"))) -ge "$RATE" ]; then
trackStatus "$PAUSE_ICON" "$PLAY_ICON" > "$cache_file"
sliceTrack "$(cat $cache_file)" "$MAX_LENGTH" > "$cache_file"
trackStatus "$PAUSE_ICON" "$PLAY_ICON" >"$cache_file"

if [ "$SCROLL" = false ]; then
sliceTrack "$(cat $cache_file)" "$MAX_LENGTH" >"$cache_file"
fi
fi

# Allow scrolling
local str=$(cat "$cache_file")
if [ "$SCROLL" = true ]; then
scroll "$str" "$MAX_LENGTH" "$SCROLL_SPEED"
else
echo "$str"
fi

cat "$cache_file"
}

main

56 changes: 31 additions & 25 deletions scripts/playerctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source $current_dir/utils.sh

function slice_loop() {
local str="$1"
local start="$2"
local how_many="$3"
# Set the configuration

# Scroll the text
# arg1: text
# arg2: width
# arg3: speed
scroll() {
local str=$1
local width=$2
local speed=$2

local scrolling_text=""
local i=0
local len=${#str}

local result=""
for ((i = 0; i <= len; i++)); do
scrolling_text=$(slice_text "$str" "$i" "$width")
echo -ne "\r"
echo "$scrolling_text "
echo -ne "\r"

for ((i = 0; i < how_many; i++)); do
local index=$(((start + i) % len))
local char="${str:index:1}"
result="$result$char"
sleep "$speed"
done

echo "$result"
echo -ne "\r"
echo "$scrolling_text "
echo -ne "\r"
}

main() {
Expand All @@ -31,12 +43,13 @@ main() {
fi

FORMAT=$(get_tmux_option "@dracula-playerctl-format" "Now playing: {{ artist }} - {{ album }} - {{ title }}")
SCROLL=$(get_tmux_option "@dracula-playerctl-scroll" true)
WIDTH=$(get_tmux_option "@dracula-playerctl-width" 25)
SPEED=$(get_tmux_option "@dracula-playerctl-speed" 0.08)

playerctl_playback=$(playerctl metadata --format "${FORMAT}")
playerctl_playback="${playerctl_playback} "

# Adjust width of string
terminal_width=25

# Initial start point for scrolling
start=0
len=${#playerctl_playback}
Expand All @@ -50,18 +63,11 @@ main() {

scrolling_text=""

for ((start = 0; start <= len; start++)); do
scrolling_text=$(slice_loop "$playerctl_playback" "$start" "$terminal_width")
echo -ne "\r"
echo "$scrolling_text "
echo -ne "\r"

sleep 0.08
done

echo -ne "\r"
echo "$scrolling_text "
echo -ne "\r"
if [ "$SCROLL" = true ]; then
scroll "$playerctl_playback" "$WIDTH" "$SPEED"
else
echo "$playerctl_playback"
fi
}

# run the main driver
Expand Down
24 changes: 24 additions & 0 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,27 @@ normalize_percent_len() {
printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 ""
}

# Create a slice of the text to show currently in the module
# arg1: The full string
# arg2: Where to start the loop from
# arg3: The length of characters to display
slice_text() {
local str="$1"
local start="$2"
local how_many="$3"

local len=${#str}

local result=""

# Caputre the strings to show
for ((i = 0; i < how_many; i++)); do
local index=$(((start + i) % len))
local char="${str:index:1}"

# Append the character to show
result="$result$char"
done

echo "$result"
}