-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.lib
executable file
·249 lines (221 loc) · 9.84 KB
/
playlist.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
. /$HOME/.config/mpd-local.conf
MPDPASS="$mpdpass"
#servername=""
server="192.168.1.2"
server="${server:-$mpdhost}" #defined in mpd-local.conf
#mpdport="6600"
scriptname=$(realpath "$0")
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
bold="$(tput bold)"
tput0=$(tput sgr0) # reset color
lines=$(tput lines 2> /dev/null) || lines=$(tput -T xterm-256color lines)
export TERM=xterm-256color
editscript(){
local script path; script="${scriptname##*/}"; path="${scriptname%/*}"; swp="$path/.$script.swp"
if [[ ! -e "$swp" ]]; then printf "\n\n%s\n\n" "$swp"; (/usr/bin/nano "$scriptname"); exit
else printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptname" "$swp"; exit;
fi; }
pause(){ read -p "$*" ; }
searchfn(){
local mpccommand="playlist"
[[ "$1" = listall ]] && mpccommand="listall"
[[ "$1" = current ]] && mpccommand="current"
# Define colors using tput
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
bold="$(tput bold)"
tput0=$(tput sgr0) # reset color
# Read the playlist into an array
readarray -t playlistarray < <(
{
if [[ "$mpccommand" = playlist ]]; then
printf '(-pos-)~title~album artist~album~TT-DD~(mm:ss)~file\n'
\mpc ${remote:-} ${mpccommand} -f \(%position%\)~%title%~%albumartist%~%album%~\(%disc%-%track%\)~\(%time%\)~%file%~%artist% | grep -i --color=never "$query"
elif [[ "$mpccommand" = listall ]]; then
printf '(-pos-)~title~album artist~album~TT-DD~(mm:ss)~file\n'
\mpc ${remote:-} ${mpccommand} -f \(%position%\)~%title%~%albumartist%~%album%~\(%disc%-%track%\)~\(%time%\)~%file%~%artist% | grep -i --color=never "$query"
elif [[ "$mpccommand" = current ]]; then
\mpc ${REMOTE:-} playlist -f \(%position%\)~%title%~%albumartist%~%album%~\(%disc%-%track%\)~\(%time%\)~%file%~%artist%|
# { printf '(-POS-)~title~albumartist~album~(dd-tt)~(mm:ss)~file\n' ; \grep \($(\mpc ${REMOTE:-} current -f %position%)\) -m1 -C$(("$lines"/3)) ; }
{ printf '(-POS-)~title~albumartist~album~(dd-tt)~(mm:ss)~file\n' ; \grep \($(\mpc ${REMOTE:-} current -f %position%)\) -m1 -C150 ; }
#|
# column -s "~" -o " | " --table | \grep --color=always -e '' -e ".*$(mpc ${REMOTE:-} -f %position% current).*"
fi
} |
awk -F '~' -v red="$red" -v green="$green" -v cyan="$cyan" -v magenta="$magenta" -v yellow="$yellow" -v white="$white" -v blue="$blue" -v tput0="$tput0" -v bold="$bold" '
function reverse_path(path) {
n = split(path, components, "/")
reversed = components[n]
for (i = n - 1; i >= 1; i--) {
reversed = reversed "/" components[i]
}
return reversed
}
function clean_artist(artist) {
# Strip "with", "feat", and similar terms
gsub(/ with .*| feat.*| ft.*/i, "", artist)
return artist
}
{if (NR == 1) {
# print $0; # Print the header as is
$1 = bold blue $1 tput0
# $2 = bold green $2 tput0
$2 = bold green (length($2) > 40 ? substr($2, 1, 38) "…" : $2) tput0
$3 = bold cyan (length($3) > 30 ? substr($3, 1, 28) "…" : $3) tput0
# $3 = bold cyan $3 tput0
$4 = bold magenta (length($4) > 30 ? substr($4, 1, 28) "…" : $4) tput0
$5 = bold red $5 tput0
# $5 = bold red disc "-" track tput0
$6 = bold yellow $6 tput0
$7 = bold $7 tput0
print $1 "~" $2 "~" $3 "~" $4 "~" $5 "~" $6 "~" $7
} else {
album_artist = $3
primary_artist = $8
if (album_artist == "Various Artists") {
primary_artist = clean_artist(primary_artist)
}
# Extract disc and track numbers -- this works with DD-TT
# gsub(/[()]/,"",$5)
# split($5, nums, "-")
# disc = sprintf("%02d", nums[1])
# track = sprintf("%02d", nums[2])
# Extract disc and track numbers -- this is supposed to add support for TTT - Artist -- Title.ext
# Extract disc and track numbers
gsub(/[()]/,"",$5)
split($5, nums, "-")
disc = sprintf("%02d", nums[1])
track = (length(nums[length(nums)]) == 3) ? sprintf("%03d", nums[length(nums)]) : sprintf("%02d", nums[length(nums)])
$1 = blue $1 tput0
# $2 = green $2 tput0
$2 = green (length($2) > 40 ? substr($2, 1, 38) "…" : $2) tput0
$3 = cyan (length(primary_artist) > 30 ? substr(primary_artist, 1, 28) "…" : primary_artist) tput0
# $3 = cyan (length($3) > 30 ? substr($3, 1, 28) "…" : $3) tput0
# $3 = cyan $3 tput0
$4 = magenta (length($4) > 30 ? substr($4, 1, 28) "…" : $4) tput0
# $5 = red $5 tput0
$5 = red disc "-" track tput0
$6 = yellow $6 tput0
$7 = white $7 tput0
# $7 = white reverse_path($7) tput0
print $1 "~" $2 "~" $3 "~" $4 "~" $5 "~" $6 "~" $7
}
}' |
column -t -s "~" -o " | "
)
argmax=1992844 #worked
argmax=1993500 #fialed
argmax=1993000
n="${#playlistarray[@]}"
## printf 'Hits: %s\n' "$n"
## printf 'Lines: %s; Hits: %s\n\n' $((lines-lines/10-1)) "$n"
## echo "$argmax argmax"
## startsize="$(printf %s "${playlistarray[@]}"|wc -c)"
## echo "$startsize startsize"
if (( n > 7000 )); then
printf '%sResults truncated, refine search futher.%s\n' "$red" "$white"
truncatedarray=("${playlistarray[@]:0:5500}")
totalbytes="$(printf %s "${truncatedarray[@]}"|wc -m)"
for item in "${playlistarray[@]:5500}"; do
item_bytes=${#item}
if (( "$totalbytes" + item_bytes > argmax)); then
break
fi
truncatedarray+=("$item")
totalbytes=$((totalbytes + item_bytes))
done
playlistarray=("${truncatedarray[@]}")
fi
(( n == 0 )) && printf 'Exiting.\n' && exit 1
totalbytes2="$(printf %s "${playlistarray[@]}"|wc -m)"
## echo "$n original hits"
## echo "${#playlistarray[@]} truncated hits"
## echo "$argmax argmax"
printf 'Hits: %s (%s) - ' "$n" "$mpccommand"
printf '%s/%s playlist array size\n' "$totalbytes2" "$argmax"
if [[ "$mpccommand" = playlist ]]; then
i=$(gum choose --header.bold "${playlistarray[@]:1:$n}" --cursor.italic --height=40 --header="${playlistarray[0]}" --header.padding="0 2")
elif [[ "$mpccommand" = current ]]; then
timeout=$(( $(sex2sec $(\mpc status %totaltime%)) - $(sex2sec $(\mpc status %currenttime%)) ))
i=$(gum choose --position=middle --timeout="${timeout}s" --selected=">>" --header.bold "${playlistarray[@]:1:$n}" --cursor.italic --height=40 --header="${playlistarray[0]}" --header.padding="0 2")
fi
(( n == 0 )) && printf 'Exiting.\n' && exit 1
selection="$i"
i="${i%%\)*}"
i="${i#*\(}"
$(pause "Play $selection, y/n? ") && \mpc ${remote:-} play "$i"
}
sex2sec(){
local time="$1";
# [[ ! "$time" =~ [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2} ]] && time='00:00:00' #&& pause "${time@A}"
[[ ! "$time" =~ [[:digit:]]{1,2}:[[:digit:]]{2} ]] && time='00:00' #&& pause "${time@A}"
# local hours=$(echo "$time" | awk -F: '{print $1}'); # pause "$hours"
local minutes=$(echo "$time" | awk -F: '{print $1}'); # pause "$minutes"
local seconds=$(echo "$time" | awk -F: '{print $2}'); # pause "$seconds"
local total_seconds=$(( ( 10#$minutes * 60) + 10#$seconds ));
# pause "no sec $(local total_seconds=$(( (10#$hours * 3600) + (10#$minutes * 60) )) )"
# pause "no min $(local total_seconds=$(( (10#$hours * 3600) + (10#$seconds) )) )"
# pause "no hours $(local total_seconds=$(( (10#$minutes * 60) + (10#$seconds) )) )"
echo "$total_seconds" ; }
##--> sexagesimal_to_sec() <--##################################################################
searchfn_backup(){
red="$(tput setaf 1)"
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
white='\033[0m' # No Color
readarray -t playlistarray < <(\mpc ${REMOTE:-} playlist -f \(%position%\)~%artist%~%album%~\(%track%\)~%title%~\(%time%\)|grep -i --color=always "$query"|
{ printf '%s(-POS-)~%sArtist~%sAlbum~%s(%s#%s)~%sTitle%s~(mm:ss)\n' "${blue}" "${green}" "${cyan}" "${white}" "${magenta}" "${white}" "${yellow}" "${white}"; \grep \($(\mpc ${REMOTE:-} current -f %position%)\) -m1 -C$(("$lines"/3)) ; } |
column -s "~" -o " | " --table | \grep --color=always -e '' -e ".*$(mpc ${REMOTE:-} -f %position% current).*")
n="${#playlistarray[@]}"
printf 'Hits: %s\n' "$n"
# printf '$((lines-lines/10-1))=%s' $((lines-lines/10-1))
printf 'Lines: %s; Hits: %s\n\n' $((lines-lines/10-1)) "$n"
# i=$(gum choose --header.bold "${playlistarray[@]:1:$n}" --cursor.italic --height=$(("$lines"/1)) --header="${playlistarray[0]}" --header.padding="0 2")
pause "${playlistarray[@]}"
i=$(gum choose --header.bold "${playlistarray[@]:1:$n}" --cursor.italic --height=40 --header="${playlistarray[0]}" --header.padding="0 2")
(( n == 0 )) && printf 'Exiting.\n' && exit 1
selection="$i"; i="${i%%\)*}"; i="${i#*\(}"
$(pause "Play $selection, Y/n? ") && \mpc ${REMOTE:-} play "$i"
}
confirm() {
read -p "$1" -n 1 -r;
echo;
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0;
else
return 1;
fi
}
remotefn() {
[[ "$(uname -n)" != "$servername" ]] && REMOTE="-h $server"
}
title(){
artist="$(mpc current -f %artist%)"
album="$(mpc current -f %album%)"
title="$(mpc current -f %title%)"
printf "\n\033[0mArtist: %s\nAlbum : %s\nTitle : %s\n" "$artist" "$album" "$title"
mpc current -f "%file%"|awk -F / -v OFS=/ '{$1="\033[1;33m" $1 "\033[0m"; $NF="\033[1;34m" $NF "\033[0m"; print}'
# mpc current -f "%title% on %album% by %artist%\n%file%"
printf '\n'
mpc queued -f "%file%"|awk -F / -v OFS=/ '{$1="Up next: \033[1;33m" $1 "\033[0m"; $NF="\033[1;34m" $NF "\033[0m"; print}'
printf '\n'
}
reverse_path() {
local path="$1"
echo "$path" | awk -F'/' '{for(i=NF; i>1; i--) printf("%s/", $i); printf("%s\n", $1)}'
}