-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiboeuf.sh
executable file
·126 lines (107 loc) · 2.25 KB
/
tiboeuf.sh
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
#!/usr/bin/env bash
# shellcheck disable=SC2002
# tiboeuf
# Setup
mpv_bin() {
local bin_name
local system_bin_location
bin_name="mpv"
system_bin_location=$(command -v $bin_name)
if test -n "$system_bin_location"; then
mpv_bin="$system_bin_location"
else
echo "Break, $bin_name is not installed."
exit
fi
}
radio_db_test() {
if ! [[ -f "$radio_db_path" ]]; then
echo "Break, $radio_db_path file not exist."
exit
fi
if [[ -f "$radio_db_path" ]] && [[ -z $(< "$radio_db_path") ]] ; then
echo "Break, $radio_db_path file is empty."
exit
fi
}
# Tools
term_size() {
term_width_trunc=$(stty size | awk '{print $2}' | awk '{ print $1 - 10 }')
}
echo_truncate() {
local label
label="$1"
if [[ "${#label}" -gt "$term_width_trunc" ]]; then
echo "$label" | cut -c 1-"$term_width_trunc" | awk '{print $0"..."}'
else
echo "$label"
fi
}
echo_separator() {
tput dim
printf "%*s" "$term_width" "" | tr ' ' "-"; echo
tput sgr0
}
kill() {
stty sane
exit
}
# main
array_radio() {
# Title
mapfile -t lst_radio_title < <( cat "$radio_db_path" | awk -F'|' '{print $1}')
# URL
mapfile -t lst_radio_url < <( cat "$radio_db_path" | awk -F'|' '{print $NF}')
}
print_radio_list() {
term_size
tput bold sitm
echo " < tiboeuf Radio List >"
tput sgr0
cat "$radio_db_path" | column -s $'|' -t | cut -c -"$term_width_trunc" | nl -v 0
}
radio_choice() {
while :; do
read -r -e -p " -> " radio
if [[ "$radio" =~ ^[0-9]+$ ]]; then
# Test result with db
for i in "${!lst_radio_url[@]}"; do
if [[ "$i" = "$radio" ]]; then
valid_number="1"
fi
done
# Result & play
if [[ "$valid_number" = "1" ]]; then
# Title
echo_separator
echo_truncate "Listen ${lst_radio_title[$radio]}: ${lst_radio_url[$radio]}"
# Listen
"$mpv_bin" "${lst_radio_url[$radio]}"
# If quit
echo_separator
print_radio_list
else
echo "This radio number is not in list."
fi
# Reset test
unset valid_number
elif [[ "$radio" = "q" ]]; then
echo "Goodbye Space Cowboy."
exit
else
echo "Enter an integer."
fi
done
}
trap 'kill' SIGINT
export PATH=$PATH:/home/$USER/.local/bin
term_width=$(stty size | awk '{print $2}')
tiboeuf_path="$( cd "$( dirname "$0" )" && pwd )"
radio_db_path="${tiboeuf_path}/radio.db"
# Setup
mpv_bin
radio_db_test
# main
array_radio
print_radio_list
radio_choice