Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from professorjamesmoriarty/songlengthtest.sh
Last active May 31, 2025 04:16
Show Gist options
  • Save bonelifer/b48b0417b9f60440d9659c2c69386401 to your computer and use it in GitHub Desktop.
Save bonelifer/b48b0417b9f60440d9659c2c69386401 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# FILE: songlengthtest.sh
# ROLE: Display the current song title with length constraints for better readability.
# BASED ON: https://gist.github.com/professorjamesmoriarty/2d4fcea1f587750b7bfc
# CREATED: 2015-09-27 06:31:51
# MODIFIED: 2025-05-31
# LICENSE: GNU General Public License v3.0
# Function: mus
# Purpose : Retrieve and display the currently playing song from MPD,
# trimming it to a maximum of 50 characters (with ellipsis)
# if the total length exceeds 30 characters.
# Inputs : None
# Outputs : Echoes a trimmed or full song title depending on length.
mus() {
# Fetch the current song details in format "Artist - Title" or just "Title"
current_song=$(mpc current -f "[[%artist% - ]%title%]")
# Calculate the title length
size=${#current_song}
# Define thresholds
local max_length=50 # Maximum characters to show including truncation
local display_length=30 # When to start truncating
if [ "$size" -gt "$display_length" ]; then
# Truncate and append ellipsis
echo "${current_song:0:$max_length}..."
# echo "${current_song:0:$max_length}..." > currentsong.txt # Write to file instead of echoing
else
# Print full title
echo "$current_song"
# echo "$current_song" > currentsong.txt # Write to file instead of echoing
fi
}
# Invoke the function
mus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment