Skip to content

Commit

Permalink
Add an optional log to the now_playing segment
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoMeyer441 committed May 5, 2024
1 parent 397b631 commit 05ad070
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion segments/now_playing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD_DEFAULT="trim"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT="2"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE:-repeat}"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR:-  }"
TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH_DEFAULT="\${HOME}/.now_playing.log"
TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES_DEFAULT="100"
TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST_DEFAULT="localhost"
TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_PORT_DEFAULT="6600"
TMUX_POWERLINE_SEG_NOW_PLAYING_LASTFM_UPDATE_PERIOD_DEFAULT="30"
Expand All @@ -35,6 +37,10 @@ export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED="${TMUX_POWERLINE_SEG_NOW_PLAYI
# export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE}"
# Separator for "repeat" roll mode
# export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR}"
# If set to a non-empty string, played tracks will be logged to the following file.
# export TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH="${TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH_DEFAULT}"
# Maximum number of logged song entries. Set to "unlimited" for unlimited entries.
# export TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES="${TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES_DEFAULT}"
# Hostname for MPD server in the format "[password@]host"
export TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST="${TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST_DEFAULT}"
Expand All @@ -61,7 +67,7 @@ export TMUX_POWERLINE_SEG_NOW_PLAYING_LASTFM_UPDATE_PERIOD="${TMUX_POWERLINE_SEG
# Fancy char to display before now playing track
export TMUX_POWERLINE_SEG_NOW_PLAYING_NOTE_CHAR="${TMUX_POWERLINE_SEG_NOW_PLAYING_NOTE_CHAR_DEFAULT}"
# Plexamp
# Plexamp
# Set up steps for Plexamp
# 1. Make sure jq(1) is installed on the system.
# 2. Make sure you have an instance of Tautulli that is accessible by the computer running tmux-powerline.
Expand Down Expand Up @@ -120,6 +126,33 @@ run_segment() {
return ${exitcode}
fi
if [ -n "$np" ]; then
if [[ -n "$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH" && -n "$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES" ]]; then
local last_track
last_track=$(
awk -F': ' 'END { for (i = 2; i <= NF; ++i) printf("%s%s", $i, (i < NF) ? FS : "") }' \
<"$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH" 2>/dev/null
)
if [[ "$np" != "$last_track" ]]; then
local next_entry
next_entry="$(date +"%Y-%m-%d %H:%M:%S"): ${np}"
if [[ "${TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES}" == "unlimited" ]]; then
echo "$next_entry" >>"$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH"
else
local tmp_log_file
tmp_log_file="$(mktemp)" &&
{
tail -n $((TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_MAX_ENTRIES - 1)) \
"$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH" 2>/dev/null
echo "$next_entry"
} >"$tmp_log_file" && \
if [[ -f "$tmp_log_file" ]]; then
# Use `cat` to preserve destination file attributes rather than source file attributes
cat "$tmp_log_file" >"$TMUX_POWERLINE_SEG_NOW_PLAYING_TRACK_LOG_FILEPATH"
rm "$tmp_log_file"
fi
fi
fi
fi
case "$TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD" in
"roll")
np=$(roll_text "${np}" "${TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN}" "${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT}" "${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE}" "${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR}")
Expand Down

0 comments on commit 05ad070

Please sign in to comment.