Skip to content

Commit f9ee701

Browse files
71zenithport19xDerisis13CoolnsXjustchokingaround
committed
feat: add line number to menu
Co-authored-by: port19 <port19@port19.xyz> Co-authored-by: Derisis13 <email.felhasznalasra@gmail.com> Co-authored-by: CoolnsX <ansari.tan20@gmail.com> Co-authored-by: chokerman <44473782+justchokingaround@users.noreply.github.com> Co-authored-by: zenith71 <71zenith@protonmail.com>
1 parent 2307b20 commit f9ee701

1 file changed

Lines changed: 39 additions & 36 deletions

File tree

ani-cli

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ nth() {
1818
stdin=$(cat -)
1919
[ -z "$stdin" ] && return 1
2020
line_count="$(printf "%s\n" "$stdin" | wc -l)"
21-
[ "$line_count" -eq 1 ] && printf "%s" "$stdin" && return 0
21+
[ "$line_count" -eq 1 ] && printf "%s" "$stdin" | cut -f2,3 && return 0
2222
prompt="$1"
2323
multi_flag=""
2424
[ $# -ne 1 ] && shift && multi_flag="$1"
25-
line=$(printf "%s" "$stdin" | sed 's|.*\t||' | launcher "$multi_flag" "$prompt")
26-
[ -n "$line" ] && printf "%s" "$stdin" | grep -w "${line}$" || exit 1
25+
line=$(printf "%s" "$stdin" | cut -f1,3 --output-delimiter " " | launcher "$multi_flag" "$prompt" | cut -d " " -f 2-)
26+
[ -n "$line" ] && printf "%s" "$stdin" | grep -w "${line}$" | cut -f2,3 || exit 1
2727
}
2828

2929
die() {
30-
printf "%s\n" "$*" 1>&2
30+
printf "\33[2K\r\033[1;31m%s\033[0m\n" "$*" >&2
3131
exit 1
3232
}
3333

@@ -141,7 +141,6 @@ generate_link() {
141141
4) provider_init 'usercloud' '/Uv-mp4 :/p' ;; # usercloud(mp4)(single)
142142
*) provider_init 'gogoanime' '/Luf-mp4 :/p' ;; # gogoanime(m3u8)(multi)
143143
esac
144-
# logic yet to implement
145144
[ -n "$provider_id" ] && get_links "$provider_id"
146145
}
147146

@@ -227,6 +226,28 @@ play_episode() {
227226
[ "$use_external_menu" = "1" ] && wait
228227
}
229228

229+
play() {
230+
start=$(printf "%s" "$ep_no" | sed "s/[^0-9]*\([0-9]*\).*/\1/")
231+
end=$(printf "%s" "$ep_no" | sed "s/[^0-9]*[0-9]*[^0-9]*\([0-9]*\).*/\1/")
232+
[ -z "$start" ] || [ -z "$end" ] && unset start end
233+
line_count=$(printf "%s\n" "$ep_no" | wc -l)
234+
if [ "$line_count" != 1 ] || [ -n "$start" ] || [ -n "$end" ]; then
235+
[ -z "$start" ] && start=$(printf "%s\n" "$ep_no" | tail -n1)
236+
[ -z "$end" ] && end=$(printf "%s\n" "$ep_no" | head -n1)
237+
range=$(printf "%s\n" "$ep_list" | sed '1!G;h;$!d' | sed -nE "/^${start}\$/,/^${end}\$/p")
238+
[ -z "$range" ] && die "Invalid range!"
239+
for i in $range; do
240+
tput clear
241+
ep_no=$i
242+
printf "\33[2K\r\033[1;34mPlaying episode %s...\033[0m\n" "$ep_no"
243+
play_episode
244+
done
245+
else
246+
play_episode
247+
fi
248+
}
249+
250+
230251
# MAIN
231252

232253
# setup
@@ -243,9 +264,9 @@ case "$(uname -a)" in
243264
*) player_function="${ANI_CLI_PLAYER:-mpv}" ;; # Linux OS
244265
esac
245266

246-
[ -t 0 ] && use_external_menu="${ANI_CLI_EXTERNAL_MENU:-0}" || use_external_menu="${ANI_CLI_EXTERNAL_MENU:-1}"
247-
[ "$use_external_menu" = "0" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-${multi_selection_flag:-"-m"}}"
248-
[ "$use_external_menu" = "1" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-${multi_selection_flag:-"-multi-select"}}"
267+
use_external_menu="${ANI_CLI_EXTERNAL_MENU:-0}"
268+
[ "$use_external_menu" = "0" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-"-m"}"
269+
[ "$use_external_menu" = "1" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-"-multi-select"}"
249270
cache_dir="${ANI_CLI_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/ani-cli}"
250271
[ ! -d "$cache_dir" ] && mkdir -p "$cache_dir"
251272
hist_dir="${ANI_CLI_HIST_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/ani-cli}"
@@ -286,7 +307,7 @@ while [ $# -gt 0 ]; do
286307
;;
287308
--dub) mode="dub" ;;
288309
-U | --update) update_script ;;
289-
*) query="$(printf "%s" "$query $1" | sed "s/^ //;s/ /%20/g")" ;;
310+
*) query="$(printf "%s" "$query $1" | sed "s|^ ||;s| |%20|g")" ;;
290311
esac
291312
shift
292313
done
@@ -299,7 +320,7 @@ flatpak*)
299320
dep_ch "flatpak"
300321
flatpak info io.mpv.Mpv >/dev/null 2>&1 || die "Program \"mpv (flatpak)\" not found. Please install it."
301322
;;
302-
android*) printf "Dependency checking of players on Android is disabled\n" ;;
323+
android*) printf "Checking of players on Android is disabled\n" ;;
303324
*) dep_ch "$player_function" ;;
304325
esac
305326

@@ -309,7 +330,8 @@ history)
309330
anime_list=$(while read -r ep_no id title; do process_hist_entry & done <"$histfile")
310331
wait
311332
[ -z "$anime_list" ] && die "No unwatched series in histroy!"
312-
result=$(printf "%s" "$anime_list" | nth "Select anime: " | cut -f1)
333+
result=$(printf "%s" "$anime_list" | nl -w 1 | nth "Select anime: " | cut -f1)
334+
[ -z "$result" ] && exit 1
313335
result=$(grep "$result" "$histfile")
314336
read -r ep_no id title <<-EOF
315337
$result
@@ -330,9 +352,7 @@ history)
330352
query=$(printf "%s" "$query" | sed "s| |%20|g")
331353
anime_list=$(search_anime "$query")
332354
[ -z "$anime_list" ] && die "No results found!"
333-
result=$(printf "%s" "$anime_list" | nth "Select anime: ") || exit 1
334-
# moves the cursor up one line and clears that line
335-
tput cuu1 && tput el
355+
result=$(printf "%s" "$anime_list" | nl -w 1 | nth "Select anime: ") || exit 1
336356
title=$(printf "%s" "$result" | cut -f2)
337357
allanime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]' | tr 'A-Z ' 'a-z-')"
338358
id=$(printf "%s" "$result" | cut -f1)
@@ -342,36 +362,19 @@ history)
342362
;;
343363
esac
344364

365+
# moves the cursor up one line and clears that line
366+
tput cuu1 && tput el
367+
345368
# playback & loop
346-
play() {
347-
start=$(printf "%s" "$ep_no" | sed "s/[^0-9]*\([0-9]*\).*/\1/")
348-
end=$(printf "%s" "$ep_no" | sed "s/[^0-9]*[0-9]*[^0-9]*\([0-9]*\).*/\1/")
349-
[ -z "$start" ] || [ -z "$end" ] && unset start end
350-
line_count=$(printf "%s\n" "$ep_no" | wc -l)
351-
if [ "$line_count" != 1 ] || [ -n "$start" ] || [ -n "$end" ]; then
352-
[ -z "$start" ] && start=$(printf "%s\n" "$ep_no" | tail -n1)
353-
[ -z "$end" ] && end=$(printf "%s\n" "$ep_no" | head -n1)
354-
range=$(printf "%s\n" "$ep_list" | sed '1!G;h;$!d' | sed -nE "/^${start}\$/,/^${end}\$/p")
355-
[ -z "$range" ] && die "Invalid range!"
356-
for i in $range; do
357-
ep_no=$i
358-
printf "\33[2K\r\033[1;34mPlaying episode %s...\033[0m\n" "$ep_no"
359-
play_episode
360-
tput clear
361-
done
362-
else
363-
play_episode
364-
fi
365-
}
366369
play
367-
[ "$player_function" = "download" ] && exit 0
370+
[ "$player_function" = "download" ] || [ "$player_function" = "debug" ] && exit 0
368371

369372
while cmd=$(printf "quit\nselect\nprevious\nreplay\nnext" | nth "Playing episode $ep_no of $title... "); do
370373
case "$cmd" in
371374
previous) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}") 2>/dev/null ;;
372375
replay) ;;
373376
next) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{g;1!p;};h") 2>/dev/null ;;
374-
select) ep_no=$(printf "%s" "$ep_list" | nth "Select episode: ") ;;
377+
select) ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$multi_selection_flag") ;;
375378
*) exit 0 ;;
376379
esac
377380
[ -z "$ep_no" ] && die "Out of range"

0 commit comments

Comments
 (0)