aniwrapper/lib/ani-cli/UI
Kyle Yasuda fd954e7ede
Add episode selection param (#18)
* fix quality selection

* add episode selection argument

* fix quality selection menu appearing when flag not set

* fix episode selection appearing when no episodes released yet

* update readme

* update readme

* make selected quality the default if watching/downloading multiple episodes
2022-10-06 20:17:48 -07:00

172 lines
4.5 KiB
Bash

#!/usr/bin/env bash
VERSION="1.0.0"
retry() {
err "$*"
prompt
}
# display error message and exit
die() {
((!SILENT)) && printf "\033[1;31m%s\033[0m\n" "$*" >&2
exit 1
}
# display an error message to stderr (in red)
err() {
((!SILENT)) && printf "\33[2K\r\033[1;31m%s\033[0m\n" "$*" >&2
}
# display a log message if verbose mode is enabled
lg() {
((VERBOSE)) && printf "\033[1;35m%s\033[0m\n" "$*" >&2
}
# display an informational message (first argument in green, second in magenta)
inf() {
printf "\33[2K\r\033[1;35m%s \033[1;35m%s\033[0m\n" "$1" "$2"
}
progress() {
((!SILENT)) && printf "\33[2K\r\033[1;34m%s\033[0m\n" "$1" >&2
}
# prompts the user with message in $1-2 ($1 in blue, $2 in magenta) and saves the input to the variables in $REPLY and $REPLY2
prompt() {
printf "\033[1;35m%s\033[1;35m%s\033[1;34m\033[0m: " "$1" "$2"
}
# displays an even (cyan) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_even() {
printf "\033[1;36m(\033[1;36m%s\033[1;36m) \033[1;36m%s\033[0m\n" "$2" "$1"
}
# displays an odd (yellow) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_odd() {
printf "\033[1;33m(\033[1;33m%s\033[1;33m) \033[1;33m%s\033[0m\n" "$2" "$1"
}
# display alternating menu lines (even and odd)
menu_line_alternate() {
menu_line_parity=${menu_line_parity:-0}
if [ "$menu_line_parity" -eq 0 ]; then
menu_line_odd "$1" "$2"
menu_line_parity=1
else
menu_line_even "$1" "$2"
menu_line_parity=0
fi
}
# displays a warning (red) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_strong() {
printf "\033[1;34m[\033[1;33m%s\033[1;34m] \033[1;33m%s\033[0m\n" "$2" "$1"
}
# Select anime from query results
anime_selection() {
search_results=$*
count=1
while read -r anime_id; do
anime_id=$(printf "%s" "$anime_id" | sed -E 's/\-episode\-.*//')
menu_line_alternate "$anime_id" "$count"
: count=$((count += 1))
done <<< "$search_results"
prompt "Enter choice"
read -r choice
# Check if input is a number
[[ "$choice" -eq "$choice" ]] 2> /dev/null || die "Invalid number entered"
count=1
while read -r anime_id; do
if [[ "$count" -eq "$choice" ]]; then
anime_id=$(printf "%s" "$anime_id" | sed -E 's/\-episode\-.*//')
selection_id=$anime_id
break
fi
count=$((count + 1))
done <<< "$search_results"
[[ -z "$selection_id" ]] && die "Invalid number entered"
insert_history "search" "$selection_id" &
lg "Selection: $selection_id"
progress "(Gogoanime) Searching Episodes.."
episode_list "$selection_id"
return 0
}
# select episode from query results
episode_selection() {
ep_choice_start=1
if [ -n "$LAST_EP_NUMBER" ]; then
[[ "$is_download" -eq 1 ]] &&
inf "Range of episodes can be specified:" "start_number end_number"
inf "Anime:" "$anime_id"
prompt "Choose episode " "[$FIRST_EP_NUMBER-$LAST_EP_NUMBER]"
read -r ep_choice_start ep_choice_end
[[ -z "$ep_choice_end" ]] && ep_choice_end="$ep_choice_start"
fi
if (((ep_choice_start < 0 || ep_choice_start > LAST_EP_NUMBER) || ep_choice_end < ep_choice_start || ep_choice_end > LAST_EP_NUMBER)); then
die "Invalid episode/range entered: ep_start -> $ep_choice_start | ep_end -> $ep_choice_end"
fi
}
continue_watching() {
inf "Continue watching $selection_id?"
prompt "Next episode -> $((episode + 1)) [Y/n]"
read -r choice
[ -z "$choice" ] && choice="y"
[[ "$choice" =~ ^(y|Y|Yes)$ ]] && return 0 || return 1
}
# get the search query from user or from args
get_search_query() {
if [ $# -gt 0 ]; then
query="${*// /-}"
else
prompt "Search Anime"
read -r query
query="${query// /-}"
fi
}
get_dl_dir() {
prompt "Enter download directory"
read -r download_dir
lg "Download dir: $download_dir"
[ -z "$download_dir" ] && download_dir="$HOME/Videos/sauce/"
if [ ! -d "$download_dir" ]; then
mkdir -p "$download_dir" || die "Error creating directory: $download_dir"
fi
}
# sets the video quality
set_video_quality() {
qualities="best|worst"
prompt "Choose quality [$qualities]"
read -r quality
while [[ ! "$quality" =~ ($qualities) ]]; do
lg "$quality not a valid quality"
prompt "Choose quality [$qualities]"
read -r quality
done
[ -z "$quality" ] && die "No quality selected"
NEW_QUALITY=1
}
# gets the video quality from the user
get_quality() {
qualities="best|worst"
prompt "Choose quality " "[$qualities]"
read -r quality
while [[ ! "$quality" =~ ($qualities) ]]; do
lg "$quality not a valid quality -> [$qualities]"
prompt "Choose quality " "[$qualities]"
read -r quality
done
lg "selected quality: $quality"
}
# vim :ft=sh