clean up code a bit

This commit is contained in:
ksyasuda 2022-02-03 22:18:51 -08:00
parent 7e71788604
commit ebc8587e6f

41
ani-cli
View File

@ -15,7 +15,6 @@ SILENT=0
player_fn="mpv" player_fn="mpv"
playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm" playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm"
prog="ani-cli"
c_red="\033[1;31m" c_red="\033[1;31m"
c_green="\033[1;32m" c_green="\033[1;32m"
c_yellow="\033[1;33m" c_yellow="\033[1;33m"
@ -24,17 +23,6 @@ c_magenta="\033[1;35m"
c_cyan="\033[1;36m" c_cyan="\033[1;36m"
c_reset="\033[0m" c_reset="\033[0m"
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<- EOF
USAGE: $prog <query>
-h show this help text
-d download episode
-H continue where you left off
EOF
}
die() { die() {
((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2 ((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2
exit 1 exit 1
@ -59,8 +47,8 @@ check_input() {
fi fi
} }
# get the download page url
get_dpage_link() { get_dpage_link() {
# get the download page url
anime_id=$1 anime_id=$1
ep_no=$2 ep_no=$2
@ -190,7 +178,7 @@ update_date() {
stmt="" stmt=""
case "$1" in case "$1" in
directory) directory)
lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'" lg "UPDATING FILE_HISTORY for with directory: directory='$2', filename='DIRECTORY', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';" stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
;; ;;
file) file)
@ -211,7 +199,7 @@ update_date() {
lg "Passed in date is older or same than current date... doing nothing" lg "Passed in date is older or same than current date... doing nothing"
return 1 return 1
fi fi
lg "UPDATING watch_history from sync. watch_date -> $temp_dt" lg "UPDATING WATCH_HISTORY from sync. watch_date -> $temp_dt"
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$2' AND episode_number = $3;" stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$2' AND episode_number = $3;"
;; ;;
watch) watch)
@ -370,8 +358,7 @@ get_directory_data() {
find_media() { find_media() {
inp="$1" inp="$1"
[ -z "$inp" ] && die "No directory" [ -z "$inp" ] && die "No directory"
# workaround to allow logging w/o affecting return output lg "BEGIN find_media() on $inp" 1> /dev/stderr
lg "INPUT DIR: $inp" 1> /dev/stderr
# base case hit when a file is found # base case hit when a file is found
if [ -f "$inp" ]; then if [ -f "$inp" ]; then
@ -418,9 +405,7 @@ find_media() {
##################### #####################
get_search_query() { get_search_query() {
# Get search history
stmt="SELECT DISTINCT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;" stmt="SELECT DISTINCT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;"
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for" msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \ query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
@ -435,8 +420,8 @@ get_search_query() {
fi fi
} }
# get anime name along with its id
search_anime() { search_anime() {
# get anime name along with its id
lg "NUM ARGS: $#" lg "NUM ARGS: $#"
if [[ $# -gt 1 ]]; then if [[ $# -gt 1 ]]; then
# if multi-word query, concatenate into one string and replace spaces with '-' # if multi-word query, concatenate into one string and replace spaces with '-'
@ -466,8 +451,8 @@ search_eps() {
' '
} }
# Select anime from query results
anime_selection() { anime_selection() {
# Select anime from query results
search_results=$* search_results=$*
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
menu=() menu=()
@ -514,7 +499,7 @@ anime_selection() {
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n" menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n" menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
count=1 count=1
while read anime_id; do while read -r anime_id; do
# alternating colors for menu # alternating colors for menu
[ $((count % 2)) -eq 0 ] && [ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 || menu_format_string=$menu_format_string_c1 ||
@ -576,7 +561,9 @@ episode_selection() {
lg "Episode watch history -> $watch_history" lg "Episode watch history -> $watch_history"
# get user choice and set the start and end # get user choice and set the start and end
msg=$(printf "%s\n%s" "$(generate_span "Anime Name: $anime_id")" "$(generate_span "Range of episodes can be provided as: START_EPISODE - END_EPISODE")") msg1="Anime Name: $anime_id"
msg2="Range of episodes can be provided as: START_EPISODE - END_EPISODE"
[ "$is_download" -eq 1 ] && msg=$(printf "%s\n%s" "$(generate_span "$msg1")" "$(generate_span "$msg2")") || msg=$(printf "%s\n" "$(generate_span "$msg1")")
choice=$( choice=$(
seq 1 "$last_ep_number" | seq 1 "$last_ep_number" |
rofi -dpi "$DPI" -dmenu -l 12 \ rofi -dpi "$DPI" -dmenu -l 12 \
@ -707,7 +694,6 @@ stream() {
episode_selection episode_selection
} }
# option parsing
parse_args() { parse_args() {
# to clear the colors when exited using SIGINT # to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP trap "printf '$c_reset'" INT HUP
@ -716,12 +702,8 @@ parse_args() {
is_download=0 is_download=0
download_dir="." download_dir="."
half_ep=0 half_ep=0
while getopts 'hd:Hsvq:cf:t:T:CQ:D:S' OPT; do while getopts 'd:Hsvq:cf:t:T:CQ:D:S' OPT; do
case "$OPT" in case "$OPT" in
h)
help_text
exit 0
;;
d) d)
is_download=1 is_download=1
download_dir="$OPTARG" download_dir="$OPTARG"
@ -891,7 +873,6 @@ main() {
check_input check_input
# plays selected episode(s)
for ep in $episodes; do for ep in $episodes; do
open_episode "$selection_id" "$ep" "$download_dir" open_episode "$selection_id" "$ep" "$download_dir"
done done