2025-05-29 18:51:00 +02:00

190 lines
6.2 KiB
Plaintext

#compdef mympc
# Fixed list of moods
_fixed_moods=("happy" "sad" "energetic" "calm" "angry" "relaxed" "excited" "melancholic")
_mympc() {
_arguments -s -C \
'--artist=[Search songs by artist (separated by commas)]:artist names:_my_mpc_artists' \
'--genre=[Search songs by genre]:genre name:_my_mpc_genres' \
'--album=[Search songs by album]:album name:_my_mpc_albums' \
'--track=[Search songs by track (separated by commas)]:track names:_my_mpc_tracks' \
'--year=[Filter songs by release year]:year:_mympc_years' \
'--duration=[Filter songs by duration (min:sec)]:duration:_mympc_durations' \
'--number=[Specify the number of songs]:number of songs:_mympc_number' \
'-n=[Specify the number of songs]:number of songs:_mympc_number' \
'--play[Play immediately]' \
'--preview[Preview songs to be added without adding them]' \
'--save-playlist=[Save the current playlist]:playlist name:_my_mpc_playlists' \
'--remove=[Remove songs from the playlist (separated by commas)]:songs to remove:_my_mpc_remove_songs' \
'--list-artists[List all available artists]' \
'--list-genres[List all available genres]' \
'--list-albums[List all available albums]' \
'--random[Activate random mode]' \
'--repeat[Activate repeat mode]' \
'--get-rating[Get the rating of the current song]' \
'--rate=[Set the current song rating (0-5)]:rating value (0-5):_mympc_ratings' \
'--toprated[Generate a list with top-rated tracks (5)]' \
'--add-tag=[Add a custom tag (key=value)]:custom tag:_mympc_add_tags' \
'--delete-tag=[Delete a custom tag]:tag key:_mympc_delete_tags' \
'--list-tags[List all tags of the current song]' \
'--update-tag=[Update a custom tag (key=value)]:custom tag:_mympc_update_tags' \
'--playlist-by-mood=[Generate a playlist based on mood]:mood:_my_mpc_playlist_by_mood' \
'--help[Show help message]' \
'-h[Show help message]'
}
# Custom autocomplete functions
_my_mpc_artists() {
compadd -- ${(f)"$(mpc list artist)"}
}
_my_mpc_genres() {
compadd -- ${(f)"$(mpc list genre)"}
}
_my_mpc_albums() {
local artist=""
local -a albums
# Buscar el artista en los argumentos ya escritos
for arg in ${words[@]}; do
if [[ $arg =~ --artist= ]]; then
# Extraer el nombre del artista (manejando espacios escapados)
artist=${arg#--artist=}
artist=${artist//\\ / }
break
fi
done
if [[ -n $artist ]]; then
# Obtener álbumes específicos del artista (manejando comillas)
albums=(${(f)"$(mpc list album artist "$artist")"})
else
# Obtener todos los álbumes si no hay artista especificado
albums=(${(f)"$(mpc list album)"})
fi
# Mostrar los álbumes como opciones de completado
_describe 'album' albums
}
_my_mpc_tracks() {
compadd -- ${(f)"$(mpc list title)"}
}
_my_mpc_playlists() {
compadd -- ${(f)"$(mpc lsplaylists)"}
}
_my_mpc_remove_songs() {
# Define arrays for predefined tag values if needed
local _fixed_moods=("happy" "sad" "energetic" "calm" "angry" "relaxed" "excited" "melancholic")
local _fixed_genres=("rock" "pop" "jazz" "blues" "classical" "hiphop" "electronic")
local _fixed_ratings=("1" "2" "3" "4" "5")
# Fetch the playlist with position and title
local IFS=$'\n' # Handle song titles with spaces
local songs=($(mpc playlist -f "%position%: %title%"))
# Initialize an array for completions
local -a completions
for song in "${songs[@]}"; do
completions+=("${song}")
done
# Provide the completions
compadd -- "${completions[@]}"
}
_mympc_years() {
local -a years
years=($(mpc list date | grep -E '^(19|20)[0-9]{2}$' | sort -u))
compadd -- "${years[@]}"
}
_mympc_durations() {
compadd -- "1:00" "2:00" "3:00" "4:00" "5:00" "6:00" "7:00" "8:00" "9:00" "10:00" # Adjust durations as needed
}
# Autocomplete for numbers
_mympc_number() {
_numbers
}
# Autocomplete for rating values
_mympc_ratings() {
compadd -- 0 1 2 3 4 5
}
# Autocomplete for adding tags (key=value)
_mympc_add_tags() {
# Get the current word being completed
local tag_word="${words[CURRENT]}"
# Check if the user has already typed '=' indicating key and is now completing the value
if [[ "$tag_word" == " " ]]; then
# Extract the key from 'key=value'
local key="${tag_word%%=*}"
local value="${tag_word#*=}"
if [[ "$key" == "mood" ]]; then
# Suggest only predefined mood values
compadd -- "${_fixed_moods[@]}"
else
# For other keys, do not suggest anything to prevent file completions
return 0
fi
else
# Suggest tag keys
local keys=("mood" "genre" "rating" "custom1" "custom2")
compadd -- "${keys[@]}"
fi
}
# Autocomplete for updating tags (key=value)
_mympc_update_tags() {
# Get the current word being completed
local tag_word="${words[CURRENT]}"
if [[ "$tag_word" == " " ]]; then
# Extract the key from 'key=value'
local key="${tag_word%%=*}"
local value="${tag_word#*=}"
if [[ "$key" == "mood" ]]; then
# Suggest only predefined mood values
compadd -- "${_fixed_moods[@]}"
else
# For other keys, do not suggest anything to prevent file completions
return 0
fi
else
# Suggest existing tag keys from the current song
local song_uri
song_uri=$(mpc --format %file% current)
local -a keys
keys=($(mpc sticker "$song_uri" list 2>/dev/null | cut -d'=' -f1))
compadd -- "${keys[@]}"
fi
}
# Autocomplete for deleting tags (only keys)
_mympc_delete_tags() {
local song_uri
song_uri=$(mpc --format %file% current)
local -a tags
tags=($(mpc sticker "$song_uri" list 2>/dev/null | cut -d'=' -f1))
compadd -- "${tags[@]}"
}
# Autocomplete for generating playlist by mood
_my_mpc_playlist_by_mood() {
# Use the predefined mood list for suggestions
_values "mood" "${_fixed_moods[@]}"
}
# Register the autocompletion function with Zsh
compdef _mympc mympc