-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediatree
executable file
·148 lines (129 loc) · 4 KB
/
mediatree
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
#!/bin/bash
export LC_COLLATE="C"
#
# User options
mediadir="/mnt/zed/media"
playercmd="mpv"
playerargs=""
fileregex=".*\(avi\|mkv\|flv\|mp4\|m4v\|mp3\|flac\|mp4\|m4a\|wma\|wav\)"
dirfilter=""
bold="\033[1m"
green=$(tput setaf 2)
blue=$(tput setaf 4)
white=$(tput setaf 7)
normal=$(tput sgr0)
# Running in music mode
if [ "$(basename "$0")" == "musictree" ]; then
mediadir="/mnt/zed/media/music"
playerargs="--no-video"
fileregex=".*\(mp3\|flac\|mp4\|m4a\|wma\|wav\)"
fi
# Running in anime mode
if [ "$(basename "$0")" == "animetree" ]; then
mediadir="/mnt/zed/media/anime"
fi
# Override start dir
if [ "$#" -eq 1 ]; then
mediadir="$1"
fi
cd "$mediadir"
last=0
play_folder() {
mapfile -t files < <(find . -maxdepth 1 -type f -regex "$fileregex" | sed 's|./||' | sort --ignore-case)
if [ ${#files[@]} -ne 0 ]; then
for file in "${files[@]}"; do
printf "${bold}${green}%s\n" "$file"
done
$playercmd $playerargs "${files[@]}"
fi
}
process_selection() {
# numbers only from here on
if ! [[ "$selection" =~ ^[0-9]+$ ]]; then
return
fi
if [ -d "${list_merged[(selection-1)]}" ]; then
cd "${list_merged[(selection-1)]}"
last=0
else
echo "Playing: ${list_merged[(selection-1)]}"
${playercmd} ${playerargs} "${list_merged[(selection-1)]}"
last=$selection
# Post process if available
if type -t postprocess > /dev/null; then
postprocess "${list_merged[(selection-1)]}"
fi
fi
}
while true; do
f=0
mapfile -t list_dirs < <(find . -maxdepth 1 -type d -regex "\./${dirfilter}[^.].*" | sed 's|./||' | sort --ignore-case)
mapfile -t list_files < <(find . -maxdepth 1 -type f -regex "$fileregex" | sed 's|./||' | sort --ignore-case)
# dirfilter is single use
dirfilter=""
digits=$(echo -n ${#list_merged[@]} | wc -m)
# Title
printf "${bold}----(${blue}%s${white})----${normal}\n" "$(pwd | awk -F/ '{print $NF}')"
# Determine dir/media mode and sort lists
if [ ${#list_dirs[@]} -gt 3 ]; then
# More than a few dirs. Show dirs first.
MODE="DIR"
list_merged=("${list_dirs[@]}" "${list_files[@]}")
else
# More files than dirs. Probably a content folder. Show files first.
MODE="MEDIA"
list_merged=("${list_files[@]}" "${list_dirs[@]}")
fi
# Calc padding
digits=$(echo -n ${#list_merged[@]} | wc -m)
# ..
printf "${bold}%${digits}i. ${blue}%s${normal}\n" 0 ".."
# Print entries. Dirs first in DIR mode, else media first.
if [[ $MODE == "DIR" ]]; then
for dir in "${list_dirs[@]}"; do
printf "${bold}%${digits}i. ${blue}%s${normal}\n" $((++f)) "$dir"
done
for file in "${list_files[@]}"; do
printf "${bold}%${digits}i. ${green}%s${normal}\n" $((++f)) "$file"
done
else
for file in "${list_files[@]}"; do
printf "${bold}%${digits}i. ${green}%s${normal}\n" $((++f)) "$file"
done
for dir in "${list_dirs[@]}"; do
printf "${bold}%${digits}i. ${blue}%s${normal}\n" $((++f)) "$dir"
done
fi
echo -ne "> "
read -r selection
# hack. make this global for use in process_selection function
export selection
case $selection in
"0" | "..")
if [ "$(pwd)" != "$mediadir" ]; then
cd ..
fi
;;
".")
play_folder
;;
# special case for next file. use 'N' to filter by dir
"n")
selection=$((last + 1))
process_selection
;;
# Set dir filter to start with lower/upper char
[[:alpha:]])
# Only in DIR mode
if [[ $MODE == "DIR" ]]; then
# bashism: ,,==tolower ^^==toupper
dirfilter="[${selection,,}${selection^^}]"
else
process_selection
fi
;;
*)
process_selection
;;
esac
done