-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpd.xy
executable file
·662 lines (563 loc) · 23.7 KB
/
mpd.xy
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#!/bin/bash
# nb: the swp file that editscript relies on is provided by nano
MPD_SOCK="/var/run/mpd/socket"
LINES_TO_CLEAR=$(tput lines)
# Define tput variables for formatting
bold=$(tput bold)
tput0=$(tput sgr0)
red=$(tput setaf 1)
blue1=$(tput setaf 246)
green=$(tput setaf 2)
green=$(tput setaf 82)
blue0=$(tput setaf 4)
blue1=$(tput setaf 12)
blue2=$(tput setaf 39)
blue=$(tput setaf 33)
red=$(tput setaf 196)
yellow=$(tput setaf 3)
black=$(tput setaf 0)
white=$(tput setaf 7)
bg_blue=$(tput setab 4)
bg_white=$(tput setab 7)
bg_black=$(tput setab 0)
normal="$tput0"
searchfn(){
local revarray playedarr playlistarray i k n path line ext action timestamp basename filepath mpccommand="playlist"
[[ "$1" = listall ]] && mpccommand="listall"
[[ "$1" = current ]] && mpccommand="current" && n=24
# 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
#Jun 28 13:23 : player: played "Buena Vista Social Club/Buena Vista Social Club -- Buena Vista Social Club Live at Carnegie Hall (1998)/Buena Vista Social Club -- 01-05 - Buena Vista Social Club.flac
n=0
while read -r line
do
timestamp="${line%% : *}"
i="${line##* : player: }"
action="${i%% *}"
filepath="${i#*\"}"
filepath="${filepath%\"*}"
ext="${filepath##*.}"
filepath="${filepath%.*}"
basename="${filepath##*\/}"
path="${filepath%\/*}"
k="$timestamp~$basename~$path~$action~"
playedarr["$n"]="$k"
((n++))
done < <(tail -n 2000 /var/log/mpd/mpd.log|\grep player|tail -n 20)
for ((n-1; n>=0; n--)); do
revarray+=( "$( printf '%s ' ${playedarr[$n]} )" )
done
# 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
printf '(-POS-)~title~albumartist~album~(dd-tt)~(mm:ss)~file\n'
printf %s "${bg_blue}"
printf %s\\n "${revarray[@]:0:20}"
# \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" -v bg_blue="$bg_blue" '
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) {
$1 = bg_blue bold blue $1 tput0
$2 = bg_blue bold green (length($2) > 40 ? substr($2, 1, 38) "…" : $2) tput0
$3 = bg_blue bold cyan (length($3) > 30 ? substr($3, 1, 28) "…" : $3) tput0
$4 = bg_blue bold magenta (length($4) > 30 ? substr($4, 1, 28) "…" : $4) tput0
$5 = bg_blue bold red $5 tput0
$6 = bg_blue bold yellow $6 tput0
$7 = bg_blue 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)
}
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 = bg_blue yellow $1 tput0
# $2 = green $2 tput0
$2 = bg_blue white (length($2) > 40 ? substr($2, 1, 38) "…" : $2) tput0
$3 = bg_blue 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 = bg_blue magenta (length($4) > 30 ? substr($4, 1, 28) "…" : $4) tput0
# $5 = red $5 tput0
$5 = bg_blue red disc "-" track tput0
$6 = bg_blue yellow $6 tput0
$7 = bg_blue white $7 tput0
# $7 = white reverse_path($7) tput0
print $1 "~" $2 "~" $3 "~" $4 "~" $5 "~" $6 "~" $7
}
}' |
column -t -s "~" -o "$bg_blue | "
)
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")
printf %s\\n "${playlistarray[@]}"
}
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() <--##################################################################
editscript(){
local scriptpath script path swp; scriptpath=$(realpath "$0" 2>/dev/null); script="${scriptpath##*/}"; path="${scriptpath%/*}"; swp="$path/.$script.swp"
[[ ! -e "$swp" ]] && printf "\n\n%s\n\n" "$swp" && (/usr/bin/nano "$scriptpath") && exit
printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptpath" "$swp"; exit ;}
pause(){ read -rp "$*" </dev/tty ; }
generate_random_number() {
local lower_bound=$1
local upper_bound=$2
local range=$((upper_bound - lower_bound + 1))
local rand=$((1 + $RANDOM % 98000))
while (( rand < lower_bound )); do rand=$(( 3 * $RANDOM )); done
random_number=$(( lower_bound + (rand % (upper_bound - lower_bound + 1)) ))
}
format_path() {
local path="$1"
local term_width=$(tput cols)
local length=11 # Initial length set to the length of the initial indentation
# local indent=' ' # 11 spaces
local indent="$(printf %*s $length '')"
local formatted_path="$indent" # Add initial indentation here
local max_length=$(( term_width - 2 )) # Account for two spaces from the right border
# Split the path into an array based on the '/' character
IFS='/' read -ra parts <<< "$path"
for ((i = 0; i < ${#parts[@]}; i++)); do
part="${parts[i]}"
# Check if adding the next part will exceed the maximum length
if (( length + ${#part} + 2 > max_length )); then
formatted_path+="/\n$indent" # Add newline and indentation after slash
length=${#indent} # Reset length to initial indentation length
fi
# Add a slash if the current length is greater than the initial indentation length
if (( length > ${#indent} )); then
formatted_path+="/"
(( length++ ))
fi
formatted_path+="$part"
(( length += ${#part} ))
done
# Add a slash at the end if it's a directory
if [[ -d "$path" || -z "${path##*/}" ]]; then
formatted_path+="/"
fi
echo -e "$formatted_path"
}
mono_mpd_response() {
local file="" title="" album="" artist="" next_file="" next_title="" next_album="" next_artist="" state=""
local song_pos="" song_total="" elapsed="" duration="" volume="" repeat="" random="" single="" consume=""
local nextsong_id="" current="$(mpc current -f %position%)"
while read -r line; do
case "$line" in
file:*)
file="${line#file: }";;
Album:*)
album="${line#Album: }";;
Artist:*)
artist="${line#Artist: }";;
Title:*)
title="${line#Title: }";;
nextsong:*)
nextsong_id="${line#nextsong: }";;
state:*)
state="${line#state: }";;
song:*)
song_pos="${line#song: }";;
playlistlength:*)
song_total="${line#playlistlength: }";;
elapsed:*)
elapsed="${line#elapsed: }";;
duration:*)
duration="${line#duration: }";;
volume:*)
volume="${line#volume: }";;
repeat:*)
repeat="${line#repeat: }";;
random:*)
random="${line#random: }";;
single:*)
single="${line#single: }";;
consume:*)
consume="${line#consume: }";;
esac
done
# If nextsong_id is not empty, get the next song's info
if [ -n "$nextsong_id" ]; then
nextsong_info=$(mpdc "playlistinfo $nextsong_id")
while read -r line; do
case "$line" in
file:*)
next_file="${line#file: }";;
Album:*)
next_album="${line#Album: }";;
Artist:*)
next_artist="${line#Artist: }";;
Title:*)
next_title="${line#Title: }";;
esac
done <<< "$nextsong_info"
fi
# Calculate the percentage of the song that has played
percentage=$(awk -v e="$elapsed" -v d="$duration" 'BEGIN { printf "%.0f", (e / d) * 100 }')
# Format file paths to wrap at slashes
file_wrapped=$(echo "$file" | sed 's/\//\n\//g')
next_file_wrapped=$(echo "$next_file" | sed 's/\//\n\//g')
# Clear the top LINES_TO_CLEAR lines
tput cup 0 0
for ((i = 0; i < LINES_TO_CLEAR; i++)); do
tput el
tput cud1
done
tput cup 0 0
# Format and print the output
echo "${tput0}${red}${bold}"
echo "[${state}] #${song_pos}/${song_total} ${elapsed}/${duration} (${percentage}%)"
echo "volume: ${volume}% repeat: ${repeat} random: ${random} single: ${single} consume: ${consume}${tput0}"
echo
echo "${bold}Playing: ${blue}${title}${tput0} by ${bold}${red}${artist}${normal}"
echo "${bold} ${yellow}${album}${normal}"
echo "${bold}${blue1}$(format_path "${file}")${normal}"
echo "${bold}Up next:${normal} ${blue}${next_title}${normal} by ${red}${next_artist}${tput0}"
echo "${bold} ${yellow}${next_album}${tput0}"
echo "${blue1}$(format_path "${next_file}") ${tput0}"
echo
#this will print out a useless playlist snippet...
#for ((i=1; i<=6; i++)); do
# while read -r line; do
# case "$line" in
# file:*)
# next_file="${line#file: }";;
# Album:*)
# next_album="${line#Album: }";;
# Artist:*)
# next_artist="${line#Artist: }";;
# Title:*)
# next_title="${line#Title: }";;
# esac
# done < <(mpdc playlistinfo\ $(( current + i )))
# echo "$next_file"
#done
}
parse_mpd_response() {
local file="" title="" album="" artist="" next_file="" next_title="" next_album="" next_artist="" state=""
local song_pos="" song_total="" elapsed="" duration="" volume="" repeat="" random="" single="" consume=""
local nextsong_id="" current="$(mpc current -f %position%)"
while read -r line; do
case "$line" in
file:*)
file="${line#file: }";;
Album:*)
album="${line#Album: }";;
Artist:*)
artist="${line#Artist: }";;
Title:*)
title="${line#Title: }";;
nextsong:*)
nextsong_id="${line#nextsong: }";;
state:*)
state="${line#state: }";;
song:*)
song_pos="${line#song: }";;
playlistlength:*)
song_total="${line#playlistlength: }";;
elapsed:*)
elapsed="${line#elapsed: }";;
duration:*)
duration="${line#duration: }";;
volume:*)
volume="${line#volume: }";;
repeat:*)
repeat="${line#repeat: }";;
random:*)
random="${line#random: }";;
single:*)
single="${line#single: }";;
consume:*)
consume="${line#consume: }";;
esac
done
# If nextsong_id is not empty, get the next song's info
if [ -n "$nextsong_id" ]; then
nextsong_info=$(mpdc "playlistinfo $nextsong_id")
while read -r line; do
case "$line" in
file:*)
next_file="${line#file: }";;
Album:*)
next_album="${line#Album: }";;
Artist:*)
next_artist="${line#Artist: }";;
Title:*)
next_title="${line#Title: }";;
esac
done <<< "$nextsong_info"
fi
# Calculate the percentage of the song that has played
percentage=$(awk -v e="$elapsed" -v d="$duration" 'BEGIN { printf "%.0f", (e / d) * 100 }')
# Format file paths to wrap at slashes
file_wrapped=$(echo "$file" | sed 's/\//\n\//g')
next_file_wrapped=$(echo "$next_file" | sed 's/\//\n\//g')
# Clear the top LINES_TO_CLEAR lines
bg_blue=$(tput setab 4)
tput cup 0 0
for ((i = 0; i < LINES_TO_CLEAR; i++)); do
tput el
tput cud1
echo "$bg_blue"
done
tput cup 0 0
# # Format and print the output
# echo "${tput0}${red}${bold}"
# echo "[${state}] #${song_pos}/${song_total} ${elapsed}/${duration} (${percentage}%)"
# echo "volume: ${volume}% repeat: ${repeat} random: ${random} single: ${single} consume: ${consume}${tput0}"
# echo
# echo "${bold}Playing: ${blue}${title}${tput0} by ${bold}${red}${artist}${normal}"
# echo "${bold} ${yellow}${album}${normal}"
# echo "${bold}${blue1}$(format_path "${file}")${normal}"
# echo "${bold}Up next:${normal} ${blue}${next_title}${normal} by ${red}${next_artist}${tput0}"
# echo "${bold} ${yellow}${next_album}${tput0}"
# echo "${blue1}$(format_path "${next_file}") ${tput0}"
# echo
# Print border top
term_width=$(tput cols)
printf "${bg_blue}${bold}"
# printf '%*s\n' "$term_width" '─' |tr ' ' ' '
# printf '┌%*s┐\n' "$(( term_width - 2 ))" '─' #| tr ' ' '─'
printf '┌%*s┐' "$(( term_width - 2 ))" '' | sed 's/ /─/g'
# Format and print the output
echo "${tput0}${bg_blue}${bold}${black}"
echo "${bg_blue} [${state}] #${song_pos}/${song_total} ${elapsed}/${duration} (${percentage}%)${tput0}"
#printf "%s [%s] #%s/%s %s/%s (%s%%)\n" "${bg_blue}" "${state}" "${song_pos}" "${song_total}" "${elapsed}" "${duration}" "${percentage}"
echo "${bg_blue} volume: ${volume}% repeat: ${repeat} random: ${random} single: ${single} consume: ${consume}${tput0}"
echo
echo "${bg_blue}${bold}${black} Playing: ${white}${title} - ${artist}${tput0}"
echo "${bg_blue}${bold} ${yellow}${album}${normal}"
echo "${bg_blue}${bold}${blue1}$(format_path "${file}")${normal}"
echo "${bg_blue}${bold}${black} Up next: ${white}${next_title} - ${next_artist}${tput0}"
echo "${bg_blue}${bold} ${yellow}${next_album}${tput0}"
echo "${bg_blue}${blue1}$(format_path "${next_file}") ${tput0}"
# Print border bottom
printf "${bg_blue}${bold}"
# printf '%*s\n' "$term_width" '' | tr ' ' ' '
printf '└%*s┘' "$(( term_width - 2 ))" '' | sed 's/ /─/g'
#[[ "$bound1" ]] && printf %s%s\\n "$bg_blue" "$(tail /var/log/mpd/mpd.log)"
printf "${tput0}"
#this will print out a useless playlist snippet...
#for ((i=1; i<=6; i++)); do
# while read -r line; do
# case "$line" in
# file:*)
# next_file="${line#file: }";;
# Album:*)
# next_album="${line#Album: }";;
# Artist:*)
# next_artist="${line#Artist: }";;
# Title:*)
# next_title="${line#Title: }";;
# esac
# done < <(mpdc playlistinfo\ $(( current + i )))
# echo "$next_file"
#done
}
mpdc() {
local command=$1
echo -ne "$command\n" | socat - UNIX-CONNECT:"$MPD_SOCK"
}
[[ "$1" == @(edit|e|-e) ]] && editscript
[[ "$1" =~ [[:digit:]]+ ]] && bound1="$1" && shift
[[ "$1" =~ [[:digit:]]+ ]] && bound2="$1" && shift
mpdc 'currentsong\nstatus'|parse_mpd_response
pllength=$(mpc status %length%)
plpos=$(mpc status %songpos%)
tput cup 15 0
[[ -z "$bound1" ]] && read -rp "${bg_blue} ${tput0}Enter track number bound or enter for current position: " bound1
[[ -z "$bound1" ]] && bound1="$plpos"
[[ -z "$bound2" ]] && read -rp "${bg_blue} ${tput0}Enter track number bound or enter for end of playlist: " bound2
[[ -z "$bound2" ]] && bound2="$pllength"
! (( bound2 > bound1 )) &&
bound0="$bound1" &&
bound1="$bound2" &&
bound2="$bound0"
(( bound2 > pllength )) && bound2="$pllength"
generate_random_number "$bound1" "$bound2"
### mpc move "$random_number" "$bound1"
((bound2--))
mpdc random 0 >/dev/null
mpdc consume 1 >/dev/null
(( bound1-1 <= $(mpc status %songpos%) && $(mpc status %songpos%) <= bound1+1 )) && true ||
mpdcout+=( "$(mpdc play\ "$bound1")" )
echo "$bound1"
echo "$bound2"
for ((i=bound1; i<=bound2; i++)); do
pllength=$(mpc status %length%)
current="$(mpc current)"
echo "${bg_blue}subqueue position: $i"
(( bound2 > pllength )) && bound2="$pllength"
generate_random_number "$bound1" "$bound2"
mpc move "$random_number" "$(( bound1 + 1 ))"
queued="$(mpc queued -f %file%)."
mpdc 'currentsong\nstatus'|parse_mpd_response
((bound2--))
tput cup 15 11
echo "${bg_blue}Songs remaining in subqueue: $(( bound2 - bound1 + 1 ))"
echo
# [[ "$bound1" ]] && printf %s%s\\n "$bg_blue" "$(tail /var/log/mpd/mpd.log)"
searchfn current
while [[ "$current" = "$(mpc current)" ]]; do
mpc idle player playlist update
mpdc 'currentsong\nstatus'|parse_mpd_response
[[ "$reset_bound1" != true ]] &&
(( bound1 != $(mpc status %songpos%) )) &&
reset_bound1=true
done
if [[ "$reset_bound1" = true ]]; then
mpdc_out+=( $(mpdc pause) )
mpc move "$(mpc status %songpos%)" "$bound1"
mpc play "$bound1"
unset reset_bound1
fi
done
exit 0
#!/bin/bash
. /$HOME/.config/mpd-local.conf
MPDPASS="$mpdpass" # "$(cat ~/.config/mpdpass)"
#servername= # [—redacted—]
server="$mpdhost" # "192.168.1.2"
#mpdport= # [—redacted—]
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 i n 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
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)}'
}