1717# # -q, --query By default trackma will list watching. Use \033[0;36m$adl -q "query"\033[0m to change the list.
1818# # Available queries: watching, completed, rewatching, paused, dropped, plantowatch;
1919# # -u, --update Update this script from github master;
20- # # -c, --cover Show cover/artwork image during anime selction, using ueberzug;
21- # # -g, --debug Enter light debug mode; Use \033[0;36m$set -x adl -g ...\033[0m for full debug mode
20+ # # -c, --cover Show cover/artwork image during anime selction, using ueberzug; use at your own risk;
21+ # # -g, --debug Enter light debug mode; Use \033[0;36m$set -x adl -g ...\033[0m for full debug mode;
2222# # -h, --help Display help and exit;
2323# # -v, --version Display version and exit.
2424
@@ -49,6 +49,14 @@ maxdepth="1"
4949# To use covers with fzf by default
5050# covers="1"
5151
52+ # colors not used in help because it doesn't work there
53+ _cr=" \033[;31m" # red
54+ _cb=" \033[;34m" # blue
55+ _cy=" \033[;33m" # yellow
56+ _cv=" \033[;35m" # violet
57+ _cg=" \033[;36m" # cyan
58+ _cd=" \033[0m" # default
59+
5260# Functions to reset some vars
5361reset_vars () { #{{{
5462 debug_print " reset_vars()"
@@ -139,8 +147,8 @@ check_covers() { #{{{
139147 check_dep " wget"
140148} # }}}
141149trap_ctrlc () { ctrlc=1 ; }
142- color_print () { echo -e " \033[0;36m $* \033[0m " ; } # for normal output
143- color_prompt () { echo -en " \033[0;34m $* \033[0m " ; } # for user input
150+ color_print () { echo -e " $_cg $* $_cd " ; } # for normal output
151+ color_prompt () { echo -en " $_cb $* $_cd " ; } # for user input
144152print_help () { echo -e " $( sed -n " s/##\ //p" " $0 " ) " ; } # more compact than { grep "^##" "$0" | sed -e "s/^...//" ;}
145153print_queries () { print_help | grep ' queries' | sed -e ' s/^[ \t]*//' ; }
146154print_options () { print_help | tail +5 ; }
@@ -150,17 +158,17 @@ print_noconfirm() { color_print "\nDefault option chosen due to option '-y'.\n"
150158watching_prompt () {
151159 debug_print " watching_prompt()"
152160 [[ " $download " == " 0" ]] && var=" watching" || var=" downloading"
153- echo -e " \nNow $var \033[0;34m $1 \033[0m , $2 \033[0;34m $3 \033[0m "
161+ echo -e " \nNow $var $_cb $1 $_cd , $2 $_cb $3 $_cd "
154162}
155163debug_print () {
156164 if [[ " $debug " == " 1" ]]; then
157- echo -e " \033[0;31m --- $* --- \033[0m "
165+ echo -e " $_cr --- $* --- $_cd "
158166 fi
159167}
160168debug_print_vars () {
161169 if [[ " $debug " == " 1" ]]; then
162170 for i in " $@ " ; do
163- echo -e " \033[0;31m -$i = ${! i} \033[0m "
171+ echo -e " $_cr -$i = ${! i} $_cd "
164172 done
165173 fi
166174}
@@ -182,13 +190,13 @@ get_list() { #{{{
182190 # see https://github.com/z411/trackma/issues/79
183191 # LINES and COLUMNS vars for long titles
184192 # see https://github.com/z411/trackma/commit/020c0a25637f7368e6c075bcbe67cd938a51b818
185- tlist=$( echo -e " filter $query \nlist\nexit" | LINES=25 COLUMNS=130 trackma -a " $account " - | \
193+ tlist=$( echo -e " filter $query \nlist\nexit" | LINES=25 COLUMNS=250 trackma -a " $account " - | \
186194 sed -n ' /[[:space:]].1/,${p;/results/q}' | head -n -1)
187195 ;;
188196 " watching" )
189197 # the above works with query=watching, but that method is hacky
190198 # using official way for best compatability
191- tlist=$( LINES=25 COLUMNS=130 trackma -a " $account " list | head -n -2 | tail -n +2)
199+ tlist=$( LINES=25 COLUMNS=250 trackma -a " $account " list | head -n -2 | tail -n +2)
192200 ;;
193201 esac
194202 if [[ " $tlist " == " " ]]; then
@@ -197,13 +205,33 @@ get_list() { #{{{
197205 exit 1
198206 fi
199207} # }}}
200- process_list () {
201- perl -pe ' s/\x1b\[[0-9;]*m//g ; s/^.+?[^\s]\s+(.+?(?=\.\.))\.+\ +(\d+).[^\d]\ (\d+|\?)\s+(\d*[.]?\d+).+$/\1|\2|\3|\4|/g'
202- }
208+
203209# {{{
210+ regex=" ^.+?[^\s]\s+(.+?(?=\.\.))\.+\ +(\d+).[^\d]\ (\d+|\?)\s+(\d*[.]?\d+).+$/\1|\2|\3|\4|"
204211# Entry manipulation explination ...
205212# \x1b is removing color formating and everything untill the first letter it finds
206213# the rest is a regexp; the \n are references: \1=title | \2=current ep | \3=all ep | \4=score |
214+ # note that this will not work if the trackma output gets cutoff; COLUMNS=250 to avoid issues
215+
216+ process_list () {
217+ perl -pe " s/\x1b\[[0-9;]*m//g ; s/${regex} /g"
218+ }
219+ fzf_process_list () {
220+ perl -pe " s/${regex} /g" | \
221+ awk -F' |' ' {printf "%-4s",NR} {printf "%6s / %-5s",$2,$3} {printf " %-6s %s\n",$4,$1}'
222+ # print columns in the format INDEX CURRENT / LAST SCORE TITLE with proper padding
223+ # %s\n has a special space character before it (U+2008) for easier title getting
224+ }
225+ get_entry () {
226+ debug_print " get_entry()"
227+ fzf_selection=" $( echo " $tlist " | fzf_process_list | fzf_select ) "
228+ # now get the line number from selection
229+ line_nr=$( echo " $fzf_selection " | awk ' {print $1}' )
230+ # find the entry from tlist that has that number so we have the expected |||| format
231+ entry=" $( echo " $tlist " | sed -n " $line_nr p" | process_list) "
232+ present=" "
233+ debug_print_vars " fzf_selection" " line_nr" " entry"
234+ }
207235# }}}
208236
209237# Covers functions using ueberzug {{{
@@ -229,7 +257,8 @@ start_ueberzug() {
229257}
230258draw_preview () {
231259 local img
232- img=$( echo " ${@ } " | perl -pe " s/\x1b\[[0-9;]*m//g ; s/^.+?[^\s]\s+(.+?(?=\.\.))\.+\ +(\d+).[^\d]\ (\d+|\?)\s+(\d*[.]?\d+).+$/\1/g" )
260+ # get anime title by replacing special space character
261+ img=$( echo " ${@ } " | sed ' s/^[^ ]* //' )
233262 [ ! -s " /tmp/$img .png" ] && adl_covers " $img "
234263
235264 > " ${UEBERZUG_FIFO} " declare -A -p cmd=( \
@@ -245,22 +274,24 @@ set_fzf_covers() {
245274 debug_print_vars " covers"
246275 if [[ " $covers " == " 1" ]]; then
247276 check_covers
248- _fzf_prompt=" Colored entries are Airing. Type to search. Press enter to select "
249277 # export is needed so it can work in fzf --preview
250278 export -f draw_preview
251279 export -f adl_covers
252280 fzf_select () {
253281 start_ueberzug
254282 # disabled multi-select with +m as the codebase changed. Still works but you get another prompt
255- fzf --ansi --reverse --cycle +m --prompt " $_fzf_prompt " \
283+ fzf --ansi --reverse --cycle +m --prompt " Index Count Score Title: " \
256284 --preview " draw_preview {}" --preview-window " left,17%"
257285 pkill ueberzug
258286 rm " ${UEBERZUG_FIFO} "
259287 }
260288 else
261289 fzf_select () {
262- fzf --ansi --reverse --cycle +m --prompt " $_fzf_prompt " \
263- --preview " echo 'adl has now cover support. Try \$ adl -c.'" --preview-window " down,5%"
290+ fzf --ansi --reverse --cycle +m --prompt " Index Count Score Title: " \
291+ --preview " echo -e '$_cb ''Airing & Caught up $_cr Airing & Behind $_cd Finished airing'" \
292+ --preview-window " down,5%"
293+ # --preview "echo 'adl has now cover support. Try \$adl -c.'"\
294+ # let's not show this as ueberzug has been archived by the author
264295 }
265296 fi
266297}
@@ -286,13 +317,12 @@ select_function() { #{{{
286317 local present entry lines line line_number chosen choice
287318 if [[ -z " $1 " ]]; then
288319 if [[ -z " $use_frece " ]]; then
289- entry= " $( echo " $tlist " | fzf_select | process_list ) "
290- present= " "
320+ get_entry
321+ [[ " $fzf_selection " == " " ]] && print_noselect && return 0
291322 else
292323 debug_print " select_function() > frece"
293324 check_dep " frece"
294325 entry=" $( echo " $tlist " | process_list) "
295- present=" "
296326
297327 echo " $entry " | cut -d' |' -f1 > " $ENTRIES_FILE "
298328 # Create database
@@ -307,6 +337,7 @@ select_function() { #{{{
307337 cp " $DB_FILE " " $FZF_FILE "
308338 # get tlist format
309339 echo " $tlist " > " $ENTRIES_FILE "
340+ # FIXME: this causes issues with anime names that have , in the name
310341 awk ' BEGIN {FS=OFS=","} FNR==NR{a[NR]=$1;next}{$3=","a[FNR]}1' " $ENTRIES_FILE " " $FZF_FILE " | sed ' s/,,/,/g' > " $FZF_FILE .tmp"
311342 mv " $FZF_FILE .tmp" " $FZF_FILE "
312343 # see https://stackoverflow.com/questions/29487337/replace-a-pipe-delimited-column-using-awk
@@ -315,21 +346,18 @@ select_function() { #{{{
315346 # FS=OFS="," because frece has , delimiters
316347 # also sed because the third column will look like ,,| instead of ,|
317348
318- line_number=1
319- choice=" $( frece print " $FZF_FILE " | \
320- while IFS= read -r line ; do
321- [[ $line_number -lt 10 ]] && echo " $line " | sed -e " s/\(.\{9\}\)//; s/^/${line_number} \ \ /; s/^/| /" || \
322- echo " $line " | sed -e " s/\(.\{9\}\)//; s/^/${line_number} \ /; s/^/| /"
323- (( line_number++ )) ; done | fzf_select) "
349+ tlist=" $( frece print " $FZF_FILE " ) "
350+ get_entry
324351 # deal with ctrlc at fzf for frece; otherwise will print "Entry not found in database"
325- [[ " $choice " == " " ]] && print_noselect && return 0
326- echo " $choice " | process_list | \
352+ [[ " $fzf_selection " == " " ]] && print_noselect && return 0
353+
354+ echo " $entry " | \
327355 while IFS= read -r line ; do
328356 # reuse this file
329357 echo " $line " > " $ENTRIES_FILE "
330358 frece increment " $DB_FILE " " $( echo " $line " | cut -d' |' -f1) "
331359 done
332- debug_print_vars " present" " choice "
360+ debug_print_vars " present" " entry " " line "
333361 fi
334362 else
335363 # retrieved custom show from adl input
@@ -678,7 +706,7 @@ watch() { #{{{
678706 debug_print_vars " download" " watched" " animedl_last" " nr" " no_update"
679707 # only ask if anime is in list, only if streaming, only if there is something to even update
680708 if [[ -z " $2 " && " $download " == " 0" && " $watched " -gt " 0" && " $animedl_last " -ge " $nr " && " $no_update " != " 1" ]]; then
681- echo -en " \033[0;34mIncrease nr in anime list by \033[0m $ watched\033[0;34m ? Yes, no, or custom number [Y/n/0-9]: \033[0m "
709+ echo -en " $_cb " " Increase nr in anime list by $_cd$ watched$_cb ? Yes, no, or custom number [Y/n/0-9]: $_cd "
682710 if [[ -z " $yes " ]]; then read ans_update
683711 else ans_update=" " && print_noconfirm
684712 fi
@@ -697,19 +725,24 @@ watch() { #{{{
697725 if [[ " $last " == " ?" ]]; then
698726 trackma -a " $account " update " $title " " $nr "
699727 else
700- [[ $nr -le $last ]] && trackma -a " $account " update " $title " " $nr " || trackma -a " $account " update " $title " " $last "
701- if [[ $score == 0 ]] && [[ $nr -ge $last ]] ; then
702- color_prompt " \nAnime will be set to completed. \
703- \nAlso rate the anime [N]? Type score to assume yes: "
704- read ans_score
705- case " $ans_score " in
706- " " |[nN]* )
707- color_print " Skipping scoring..." ;;
708- * )
709- ans_score=$( nonzero " $ans_score " )
710- trackma -a " $account " score " $title " " $ans_score " ;;
711- esac
712- # ae "$title"
728+ if [[ " $nr " -ge " $last " ]]; then
729+ # code that will run after show is complete
730+ trackma -a " $account " update " $title " " $last "
731+ # ae "$title"
732+ if [[ " $score " == " 0" ]]; then
733+ color_prompt " \nAnime will be set to completed. \
734+ \nAlso rate the anime [N]? Type score to assume yes: "
735+ read ans_score
736+ case " $ans_score " in
737+ " " |[nN]* )
738+ color_print " Skipping scoring..." ;;
739+ * )
740+ ans_score=$( nonzero " $ans_score " )
741+ trackma -a " $account " score " $title " " $ans_score " ;;
742+ esac
743+ fi
744+ else
745+ trackma -a " $account " update " $title " " $nr "
713746 fi
714747 fi
715748 trackma -a " $account " send
@@ -800,12 +833,15 @@ continue_watching() { #{{{
800833 debug_print " continue_watching()"
801834 debug_print_vars " yes" " download" " ctrlc" " ferr" " animedl_last" " nr" " last"
802835
803- if [[ -z " $yes " && " $ctrlc " == " 0" && " $ferr " != " 1" && " $animedl_last " -lt " $last " ]]; then
836+ # deal with last=? error
837+ [[ " $last " == " ?" ]] && _last=" $(( nr + 1 )) " || _last=" $last "
838+ if [[ -z " $yes " && " $ctrlc " == " 0" && " $ferr " != " 1" && " $animedl_last " -lt " $_last " ]]; then
804839 [[ " $download " == " 0" ]] && var=" watch" || var=" download" ;
805840 color_prompt " \nWant to further $var the anime? Enter number for ep. count, [N/0-9]: "
806841 read ans_continue
807842 else ans_continue=" n"
808843 fi
844+ unset -v _last
809845
810846 debug_print_vars " ans_continue"
811847
@@ -847,6 +883,7 @@ continue_watching() { #{{{
847883
848884watch_another () { #{{{
849885 debug_print " watch_another()"
886+ debug_print_vars " ans_loop" " ans_another"
850887
851888 while true ; do
852889 [[ " $ans_another " == " repeat_prompt" ]] && read -p " Not an option. Retype your choice: " ans_another
@@ -875,7 +912,7 @@ updater() { #{{{
875912 local DIR=$( cd " $( dirname " $0 " ) " || exit ; pwd)
876913 local FILE=$( basename " $0 " )
877914 local MESSAGE=" WARNING: $0 will now overwrite itself at the path: $DIR /$FILE . Proceed? [y/N]: "
878- printf " \033[0;31m%s\033[0m " " $MESSAGE " # red warning
915+ printf " $_cr %s $_cd " " $MESSAGE " # red warning
879916 # integration with -y option crossed my mind but it is unwise
880917 local updater_ans
881918 read updater_ans
@@ -973,6 +1010,7 @@ if [[ "$show_title" == "" ]]; then
9731010 break
9741011 ;;
9751012 * )
1013+ ans_another=" $ans_loop "
9761014 watch_another
9771015 reset_all_vars
9781016 ;;
0 commit comments