clean up code a bit

This commit is contained in:
ksyasuda 2022-03-31 20:04:07 -07:00
parent cee96ec9cc
commit 6af4a46b86

View File

@ -30,9 +30,6 @@ quit="7. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|6. Choose Theme|$quit" options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|6. Choose Theme|$quit"
theme=default theme=default
#############
# Functions #
#############
help_text() { help_text() {
while IFS= read -r line; do while IFS= read -r line; do
printf "%s\n" "$line" printf "%s\n" "$line"
@ -76,7 +73,7 @@ prompt() {
printf "\033[1;34m%s\033[1;35m%s\033[1;34m: \033[0m" "$1" "$2" printf "\033[1;34m%s\033[1;35m%s\033[1;34m: \033[0m" "$1" "$2"
} }
seppuku() { die() {
printf "%s\n" "$*" printf "%s\n" "$*"
exit 1 exit 1
} }
@ -171,45 +168,40 @@ set_theme() {
} }
parse_args() { parse_args() {
while getopts 'vhqcdf:t:T:CQ:D:Spr' OPT; do while getopts 'cCdD:f:hpqQ:rSt:T:v' OPT; do
case "$OPT" in case "$OPT" in
h)
help_text
exit 0
;;
r)
IS_RESUME=1
lg "Selecting previously watched anime"
;;
v)
VERBOSE=1
;;
q)
GET_QUALITY=1
lg "Quality prompt enabled"
;;
c) c)
IS_ROFI=0 IS_ROFI=0
lg "Command-line (ani-cli) mode set" lg "Command-line (ani-cli) mode set"
;; ;;
C)
lg "Connecting to history database -> $CFG_DIR/history.sqlite3"
sqlite3 "$CFG_DIR/history.sqlite3"
exit $?
;;
d) d)
IS_DOWNLOAD=1 IS_DOWNLOAD=1
lg "Download flag set..." lg "Download flag set..."
;; ;;
D)
DPI="$OPTARG"
;;
f) f)
IS_PLAY_FROM_FILE=1 IS_PLAY_FROM_FILE=1
play_path="$OPTARG" play_path="$OPTARG"
lg "Play from file flag set... skipping main menu" lg "Play from file flag set... skipping main menu"
lg "PLAY_PATH: $play_path" lg "PLAY_PATH: $play_path"
;; ;;
t) h)
theme="$OPTARG" help_text
set_theme "$theme" exit 0
;; ;;
C) p)
lg "Connecting to history database -> $CFG_DIR/history.sqlite3" IS_ALTERNATE_PLAYER=1
sqlite3 "$CFG_DIR/history.sqlite3" ;;
exit $? q)
GET_QUALITY=1
lg "Quality prompt enabled"
;; ;;
Q) Q)
query="$OPTARG" query="$OPTARG"
@ -217,19 +209,24 @@ parse_args() {
sqlite3 -line "$CFG_DIR/history.sqlite3" "$query" sqlite3 -line "$CFG_DIR/history.sqlite3" "$query"
exit $? exit $?
;; ;;
T) r)
CFG_FILE="$OPTARG" IS_RESUME=1
[ ! -f "$CFG_FILE" ] && seppuku "config file $CFG_FILE does not exist" lg "Selecting previously watched anime"
IS_CUSTOM_THEME=1
;;
D)
DPI="$OPTARG"
;; ;;
S) S)
SILENT=1 SILENT=1
;; ;;
p) t)
IS_ALTERNATE_PLAYER=1 theme="$OPTARG"
set_theme "$theme"
;;
T)
CFG_FILE="$OPTARG"
[ ! -f "$CFG_FILE" ] && die "config file $CFG_FILE does not exist"
IS_CUSTOM_THEME=1
;;
v)
VERBOSE=1
;; ;;
*) *)
help_text help_text
@ -253,10 +250,6 @@ get_player() {
else else
printf "%s\n%s\n" "$msg1" "$msg2" printf "%s\n%s\n" "$msg1" "$msg2"
printf "%s\n" "SUPPORTED PLAYERS:" printf "%s\n" "SUPPORTED PLAYERS:"
# while read -r player; do
# printf "%s\n" "$player"
# done <<< "$(printf "%s\n" "$SUPPORTED_PLAYERS" | sed 's/|/\n/g')"
# bash version
while IFS='|' read -ra players; do while IFS='|' read -ra players; do
printf "%s\n" "${players[@]}" printf "%s\n" "${players[@]}"
done <<< "$SUPPORTED_PLAYERS" done <<< "$SUPPORTED_PLAYERS"
@ -265,14 +258,14 @@ get_player() {
fi fi
[ -z "$PLAYER_FN" ] && PLAYER_FN=mpv [ -z "$PLAYER_FN" ] && PLAYER_FN=mpv
if ! command -v "$PLAYER_FN" > /dev/null; then if ! command -v "$PLAYER_FN" > /dev/null; then
seppuku "ERROR: $PLAYER_FN does not exist" die "ERROR: $PLAYER_FN does not exist"
fi fi
} }
check_flags() { check_flags() {
# Check if command-line flag is set # Check if command-line flag is set
if ((VERBOSE == 1 && SILENT == 1)); then if ((VERBOSE == 1 && SILENT == 1)); then
seppuku "verbose and silent options cannot be used together" die "verbose and silent options cannot be used together"
fi fi
if ((IS_DOWNLOAD == 0 && IS_ALTERNATE_PLAYER == 1)); then if ((IS_DOWNLOAD == 0 && IS_ALTERNATE_PLAYER == 1)); then
get_player get_player
@ -290,7 +283,7 @@ check_flags() {
read -r dl_dir read -r dl_dir
lg "Download dir: $dl_dir" lg "Download dir: $dl_dir"
if [ ! -d "$dl_dir" ]; then if [ ! -d "$dl_dir" ]; then
mkdir -p "$dl_dir" || seppuku "Error creating directory: $dl_dir" mkdir -p "$dl_dir" || die "Error creating directory: $dl_dir"
fi fi
if ((IS_RESUME == 1)); then if ((IS_RESUME == 1)); then
run "-crd" "$dl_dir" "$@" run "-crd" "$dl_dir" "$@"
@ -320,13 +313,10 @@ get_dl_dir() {
# if dl_dir is none set to current directory # if dl_dir is none set to current directory
[ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD" [ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD"
if [ ! -d "$dl_dir" ]; then if [ ! -d "$dl_dir" ]; then
mkdir -p "$dl_dir" || seppuku "Error creating directory: $dl_dir" mkdir -p "$dl_dir" || die "Error creating directory: $dl_dir"
fi fi
} }
########
# Main #
########
main() { main() {
((SILENT != 1)) && lg "CONFIG DIR: $CFG_DIR" ((SILENT != 1)) && lg "CONFIG DIR: $CFG_DIR"
((SILENT != 1)) && lg "ROFI CFG: $CFG_FILE" ((SILENT != 1)) && lg "ROFI CFG: $CFG_FILE"
@ -340,31 +330,18 @@ main() {
case "$selection" in case "$selection" in
1.) 1.)
# ---------------------------------------------------------------------------
# streaming
# ---------------------------------------------------------------------------
lg "Streaming mode" lg "Streaming mode"
run run
;; ;;
2.) 2.)
# ---------------------------------------------------------------------------
# download
# ---------------------------------------------------------------------------
lg "Download anime" lg "Download anime"
get_dl_dir && run -d "$dl_dir" get_dl_dir && run -d "$dl_dir"
;; ;;
3.) 3.)
# ---------------------------------------------------------------------------
# continue
# ---------------------------------------------------------------------------
lg "Continue watching" lg "Continue watching"
run -H run -H
;; ;;
4.) 4.)
# ---------------------------------------------------------------------------
# play
# ---------------------------------------------------------------------------
lg "Play from file selected" lg "Play from file selected"
IS_PLAY_FROM_FILE=1 IS_PLAY_FROM_FILE=1
span=$(printf '%s\n%s\n' "$(generate_span "Provide a path to a valid directory, or choose from the list below")" "$(generate_span "The program will begin searching for media files from the supplied directory")") span=$(printf '%s\n%s\n' "$(generate_span "Provide a path to a valid directory, or choose from the list below")" "$(generate_span "The program will begin searching for media files from the supplied directory")")
@ -379,7 +356,7 @@ main() {
play_dir="${play_dir%% }" play_dir="${play_dir%% }"
lg "Play dir: $play_dir" lg "Play dir: $play_dir"
if [ -z "$play_dir" ]; then if [ -z "$play_dir" ]; then
mkdir -p "$DEFAULT_DOWNLOAD" || seppuku "error creating default download directory" mkdir -p "$DEFAULT_DOWNLOAD" || die "error creating default download directory"
run -f"$DEFAULT_DOWNLOAD" run -f"$DEFAULT_DOWNLOAD"
else else
run -f"$play_dir" run -f"$play_dir"
@ -391,9 +368,9 @@ main() {
IS_SYNC=1 IS_SYNC=1
roficmd="rofi -dpi $DPI -dmenu -config $CFG_FILE -l 0 -p" roficmd="rofi -dpi $DPI -dmenu -config $CFG_FILE -l 0 -p"
username=$($roficmd "Enter the username of the remote user:" -theme-str 'window {width: 45%;}') username=$($roficmd "Enter the username of the remote user:" -theme-str 'window {width: 45%;}')
[ -z "$username" ] && seppuku "No username provided... exiting" [ -z "$username" ] && die "No username provided... exiting"
host=$($roficmd "Enter the host for the remote machine (eg 192.168.1.99):" -theme-str 'window {width: 45%;}') host=$($roficmd "Enter the host for the remote machine (eg 192.168.1.99):" -theme-str 'window {width: 45%;}')
[ -z "$host" ] && seppuku "No host provided... exiting" [ -z "$host" ] && die "No host provided... exiting"
port=$($roficmd "Enter in the ssh port for remote machine or leave blank for default [22]:" -theme-str 'window {width: 45%;}') port=$($roficmd "Enter in the ssh port for remote machine or leave blank for default [22]:" -theme-str 'window {width: 45%;}')
[ -z "$port" ] && port=22 [ -z "$port" ] && port=22
keypath=$($roficmd "Enter path to private key (leave blank if not needed or if unsure):" -theme-str 'window {width: 45%;}') keypath=$($roficmd "Enter path to private key (leave blank if not needed or if unsure):" -theme-str 'window {width: 45%;}')
@ -406,10 +383,7 @@ main() {
fi fi
;; ;;
6.) 6.)
# ----------------------------------------------------------------- [ -z "$THEMES" ] && die "No themes provided... exiting"
# choose theme
# -----------------------------------------------------------------
[ -z "$THEMES" ] && seppuku "No themes provided... exiting"
theme_idx="$(get_theme_idx)" theme_idx="$(get_theme_idx)"
lg "Theme index: $theme_idx" lg "Theme index: $theme_idx"
lg "NUM THEMES: $NUM_THEMES" lg "NUM THEMES: $NUM_THEMES"
@ -422,9 +396,6 @@ main() {
main main
;; ;;
7.) 7.)
# -----------------------------------------------------------------
# get out
# -----------------------------------------------------------------
quit quit
;; ;;
*) *)
@ -437,9 +408,9 @@ parse_args "$@"
shift $((OPTIND - 1)) shift $((OPTIND - 1))
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
check_flags "$*" check_flags "$*"
if [ "$IS_DOWNLOAD" -eq 0 ]; then if ((IS_DOWNLOAD == 0)); then
run "$*" run "$*"
elif [ "$IS_DOWNLOAD" -eq 1 ]; then elif ((IS_DOWNLOAD == 1)); then
get_dl_dir && run -d "$dl_dir" "$*" get_dl_dir && run -d "$dl_dir" "$*"
fi fi
else else