replace regex with variable expansion

This commit is contained in:
ksyasuda 2022-01-11 11:53:21 -08:00
parent 20bcea5d09
commit e8944dcc69

44
ani-cli
View File

@ -16,6 +16,7 @@ THEMES="aniwrapper (default)|dracula|fancy|flamingo|material|nord|onedark"
# video_player ( needs to be able to play urls ) # video_player ( needs to be able to play urls )
player_fn="mpv" player_fn="mpv"
playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm" playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm"
playable_list="mp4,mkv,ts,mp3,webm"
prog="ani-cli" prog="ani-cli"
c_red="\033[1;31m" c_red="\033[1;31m"
@ -246,7 +247,7 @@ insert_history() {
res="$?" res="$?"
if [[ $res -gt 0 ]]; then if [[ $res -gt 0 ]]; then
logger "Match found... Updating row in history db..." logger "Match found... Updating row in history db..."
wait # in case there's another insert/update still running in background wait # in case there's another insert/update still running in background?
update_date "$@" update_date "$@"
res=$? res=$?
else else
@ -327,8 +328,8 @@ sync_watch_history() {
play_file() { play_file() {
logger "Checking if file is playable" logger "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then if [[ "$1" =~ ($playable)$ ]]; then
filename=$(grep -oE '[^/]*$' <<< "$1") filename="${1##*/}"
directory=$(sed -E "s/(\/[^\/]+$)//" <<< "$1") directory="${1%/*}"
insert_history "file" "$directory" "$filename" & insert_history "file" "$directory" "$filename" &
if [[ "$1" =~ .mp3 ]]; then if [[ "$1" =~ .mp3 ]]; then
logger ".mp3 file found... playing without video" logger ".mp3 file found... playing without video"
@ -349,25 +350,29 @@ get_directory_data() {
inputlist="" inputlist=""
watched="" watched=""
cnt=1 cnt=1
while read -r directory; do [ "$search_dir" = "/" ] && cnt=0 # account for no ../ on /
# sometimes direetory is empty string due to find parsing for directory in "$1"/*; do
[ -z "${directory// /}" ] && continue directory="${directory##*/}"
[ "$inputlist" = "" ] && inputlist="$directory" || inputlist="$inputlist|$directory" [ -z "$inputlist" ] && inputlist="$directory" || inputlist="$inputlist|$directory"
if ! check_db "directory" "$search_dir/$directory"; then if ! check_db "directory" "$search_dir/$directory"; then
logger "$search_dir/$directory opened before... adding $cnt to list" 1> /dev/stderr logger "$search_dir/$directory opened before... adding $cnt to list" 1> /dev/stderr
[ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt" [ -z "$watched" ] && watched="$cnt" || watched="$watched, $cnt"
fi fi
((++cnt)) ((++cnt))
done <<< "$(find "$search_dir" -maxdepth 1 -type d | sed -E "s|$search_dir/||" | tail -n +2 | sort -V)" done
while read -r filename; do # while read -r filename; do
[ -z "${filename// /}" ] && continue shopt -s nullglob # set nullglob to avoid printing output if no files with extension exist
shopt -s nocaseglob # case insensitive globbing
for filename in "$1"/*."{$playable_list}"; do
[ -z "$inputlist" ] && inputlist="$filename" || inputlist="$inputlist|$filename"
if ! check_db "file" "$search_dir" "$filename"; then if ! check_db "file" "$search_dir" "$filename"; then
logger "$filename watched before... adding $cnt to list" 1> /dev/stderr logger "$filename watched before... adding $cnt to list" 1> /dev/stderr
[ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt" [ -z "$watched" ] && watched="$cnt" || watched="$watched, $cnt"
fi fi
[ "$inputlist" = "" ] && inputlist="$filename" || inputlist="$inputlist|$filename"
((++cnt)) ((++cnt))
done <<< "$(find "$search_dir" -maxdepth 1 -type f | sed "s|$search_dir/||" | grep -E "$playable$" | sort -V)" done
shopt -u nullglob
shopt -u nocaseglob
[ "$search_dir" != "/" ] && inputlist="../|$inputlist|Back|Quit" || inputlist="$inputlist|Back|Quit" [ "$search_dir" != "/" ] && inputlist="../|$inputlist|Back|Quit" || inputlist="$inputlist|Back|Quit"
logger "INPUT LIST: $inputlist" 1> /dev/stderr logger "INPUT LIST: $inputlist" 1> /dev/stderr
logger "WATCHED LIST: $watched" 1> /dev/stderr logger "WATCHED LIST: $watched" 1> /dev/stderr
@ -395,13 +400,8 @@ find_media() {
case "$selection" in case "$selection" in
Back | ../) Back | ../)
# go up one directory dotdotslash="${inp%/*}"
# dotdotslash=$(sed -E "s/(\/[^\/]*$)//" <<< "$inp") [ -z "$dotdotslash" ] && find_media "/" || find_media "$dotdotslash"
if [ -z "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")" ]; then
find_media "/"
else
find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")"
fi
;; ;;
Quit) Quit)
return 1 return 1
@ -410,7 +410,7 @@ find_media() {
if [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then if [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
insert_history "directory" "$inp/$selection" & insert_history "directory" "$inp/$selection" &
if [ "$inp" = "/" ]; then if [ "$inp" = "/" ]; then
find_media "$selection" find_media "/$selection"
else else
find_media "$inp/$selection" find_media "$inp/$selection"
fi fi