#!/usr/bin/env bash TMUX_TMPDIR="/tmp/clerk/tmux" # set default settings script="$0 $1" random_artist="albumartist" shuf=$([[ "$OSTYPE" == "darwin"* ]] && echo 'gshuf' || echo 'shuf') value=20 # table customization track_width="2" title_width="40" artist_width="50" date_width="6" # album is much longer in order to keep filename off screen album_width="200" pl_title_width="110" # read global config if [[ -f "/etc/clerk_fzf.conf" ]]; then source /etc/clerk_fzf.conf fi # read user config if [[ -f "$HOME/.config/clerk/clerk_fzf.conf" ]]; then source $HOME/.config/clerk/clerk_fzf.conf fi if [[ -n $mpd_host ]]; then if [[ -n $mpd_password ]]; then export MPD_HOST="${mpd_password}@${mpd_host}" else export MPD_HOST="${mpd_host}" fi fi if [[ -n $mpd_port ]]; then ┆ export MPD_PORT="${mpd_port}" fi albums () { script="$0 --albums" tracklist="$(cat ~/.config/clerk/albums.cache | fzf --no-sort -m -e --reverse -i --with-nth=1,2,3 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all")" val=$? if [[ $val -eq 130 ]]; then exit fi track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')" actions } latest () { script="$0 --latest" tracklist=$(cat ~/.config/clerk/latest.cache | fzf --no-sort -m -e --reverse -i --with-nth=1,2,3 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all") val=$? if [[ $val -eq 130 ]]; then exit fi track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')" actions } tracks () { script="$0 --tracks" tracklist=$(cat ~/.config/clerk/tracks.cache | fzf -m --reverse -i -e --with-nth=1,2,3,4,5 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all") val=$? if [[ $val -eq 130 ]]; then exit fi if [[ $tracklist == "< Go to Albumlist"* ]]; then latest fi track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')" actions } actions () { action=$(echo -e "1. Replace\n2. Add\n3. Insert" | fzf --reverse -i --header="Choose Action or quit with ESC" --inline-info) if [[ $action == "1. Replace" ]]; then mpc clear echo "${track}" | while read line; do mpc add "${line}" done mpc play > /dev/null elif [[ $action == "2. Add" ]]; then echo "${track}" | while read line; do mpc add "${line}" done elif [[ $action == "3. Insert" ]]; then echo "${track}" | while read line; do mpc insert "${line}" done fi tmux findw -t music queue $(${script}) } playRandomAlbum () { mpc clear > /dev/null artist="$(mpc list "albumartist" | $shuf -n 1)" album="$(mpc list album "albumartist" "$artist" | $shuf -n 1)" mpc findadd album "$album" "albumartist" "$artist"; mpc play > /dev/null tmux findw -t music queue } # same for tracks, no artist should be preferred because it has more tracks. playRandomTracks () { mpc clear > /dev/null artist="$(mpc list "$random_artist" | $shuf -n 1)" album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)" title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)" mpc findadd album "$album" "$random_artist" "$artist" title "$title"; mpc play > /dev/null n=0; while (( n++ < $value -1 )); do artist="$(mpc list "$random_artist" | $shuf -n 1)" album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)" title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)" mpc findadd album "$album" "$random_artist" "$artist" title "$title" done mpc play > /dev/null tmux findw -t music queue } fix_date_format() { # provided by Thorsten Wißmann: # https://github.com/t-wissmann/dotfiles/blob/master/menu/rofi-mpd.sh # reformats the date given in column $1 from format %c of locale LC_TIME=C # (see man strftime) to the format specified in argument $2 column indices # start with 1 column_index="$1" gawk -F $'\t' ' # parse a date which was formated using %c to unix time BEGIN { # generated by the following bash one-liner: # for i in {1..12} ; do LC_TIME=C date -d "1972-$i-01" +month2num[\"%b\"]\ =\ %_m ; done month2num["Jan"] = 1 month2num["Feb"] = 2 month2num["Mar"] = 3 month2num["Apr"] = 4 month2num["May"] = 5 month2num["Jun"] = 6 month2num["Jul"] = 7 month2num["Aug"] = 8 month2num["Sep"] = 9 month2num["Oct"] = 10 month2num["Nov"] = 11 month2num["Dec"] = 12 } function reformat_c_date(str) { monthname = gensub(/^([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)$/, "\\2", "g", str) monthnum = month2num[monthname] nicedate = gensub(/^([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)$/, "\\5 " monthnum " \\3 \\4", "g", str) nicedate = gensub(/:/, " ", "g", nicedate) return nicedate } { # modify the $column_index-th field $'"$1"' = strftime("'"$2"'", mktime(reformat_c_date($'"$1"'))) print $0 } ' } updateCache () { cd $HOME/.config/clerk rm -f *.cache mpc --format '%mtime%\t[%albumartist%|%artist%] ○ (%date%) ○ %album% [(CD %disc%)] ○ %file%' \ search filename '' \ | fix_date_format 1 '%Y-%m-%d-%H:%M:%S' \ | sort -nr \ | gawk '{for (i=2; i $HOME/.config/clerk/latest.cache mpc --format '[%albumartist%|%artist%]\t(%date%)\t%album% [(CD %disc%)]\t%file%' \ search filename '' \ | sed 's:/[^/]*$::' \ | gawk -F '\t' '!seen[$1 $2 $3 $4]++' \ | sort \ | gawk -F '\t' '{ printf "%."'${artist_width}'"s\t%."'${date_width}'"s\t%."'${album_width}'"s\t%.300s\n", $1, $2, $3, $4 }' \ | column -t -s $'\t' -o $'\t' > $HOME/.config/clerk/albums.cache mpc --format '%track%\t%title%\t%artist%\t(%date%)\t%album%\t%file%' \ search filename '' \ | gawk -F '\t' '{ printf "%."'${track_width}'"s\t%."'${title_width}'"s\t%."'${artist_width}'"s\t%."'${date_width}'"s\t%."'${album_width}'"s\t%.500s\n", $1, $2, $3, $4, $5, $6 }' \ | column -s $'\t' -t -o $'\t' \ > $HOME/.config/clerk/tracks.cache date=$(mpc stats | grep 'DB Updated: ') file="$HOME/.config/clerk/.lastupdate" echo "${date}" > "${file}" # | gawk -F '\t' '{ printf "%.2s\t%.40s\t%.40s\t%.200s\t%.500s\n", $1, $2, $3, $4, $5, $6 }' \ if [[ $forceupdate == "true" ]]; then exit fi } if [[ $1 == "--update" ]]; then : else date=$(mpc stats | grep 'DB Updated: ') file="$HOME/.config/clerk/.lastupdate" if [ "$(< $file)" = "$date" ] && [ -f "$file" ] ; then : else updateCache date=$(mpc stats | grep 'DB Updated: ') echo "${date}" > "${file}" fi fi if [[ $1 == "--albums" ]]; then albums elif [[ $1 == "--tracks" ]]; then tracks elif [[ $1 == "--latest" ]]; then latest elif [[ $1 == "--random_album" ]]; then playRandomAlbum elif [[ $1 == "--random_tracks" ]]; then playRandomTracks elif [[ $1 == "--update" ]]; then forceupdate=true updateCache fi