dotfiles-old/.local/bin/mangaterm

98 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
#using PID for dirname in case multiple instances are opened they do not interfere
CACHEDIR="$HOME/.cache/mangaterm"
TMPDIR="$CACHEDIR/$$"
help() {
echo "Read manga/comics in the terminal"
echo " mangaterm.sh DIRECTORY"
echo " mangaterm.sh FILE"
echo ""
echo "keybindings:"
echo " j -> next page"
echo " k -> previous page"
echo " q -> quit"
echo ""
}
disp() {
FILE="$DIR"/"$(echo "$FILES" | head -n "$1" | tail -n 1)"
clear
[ -f "$FILE" ] && chafa -C on "$FILE"
echo -n "$FILE"
}
close() {
rm -rf "$TMPDIR"
clear
exit
}
readDir() {
DIR="$(echo "$1" | xargs)"
#if opening an archive that created a subdirectory, open subdirectories until there are multiple files
SDIR="$(ls -A "$DIR")"
if [ "$(echo "$SDIR" | wc -l)" = "1" ]; then
echo "==============="
ls "$SDIR"
echo "XXXXXXXXXXXXXX"
if [ -d "$DIR/$SDIR" ]; then
readDir "$DIR"/"$SDIR"
fi
fi
echo "before FILES"
FILES="$(ls "$DIR")"
LEN="$(echo "$FILES" | wc -l)"
disp "1"
I=1
while read -n 1 char; do
if [ "$char" = "j" ]; then
I=$((I + 1))
elif [ "$char" = "k" ]; then
I=$((I - 1))
elif [ "$char" = "q" ]; then
close
fi
if [ "$I" -ge "$LEN" ] || [ "$I" -le "0" ]; then
if [ "$I" -ge "$LEN" ]; then
I="$LEN"
elif [ "$I" -le "0" ]; then
I=0
fi
fi
disp "$I"
done
close
}
handleFile() {
case $(file --mime-type "$1" -b) in
inode/directory) readDir "$1" ;;
application/zip) unzip "$1" -d "$TMPDIR" && readDir "$TMPDIR" ;;
application/*rar) 7z x "$1" -o"$TMPDIR" && readDir "$TMPDIR" ;;
application/gzip) tar xzvf "$1" --directory="$TMPDIR" && readDir "$TMPDIR" ;;
application/pdf) pdftoppm -jpeg "$1" "$TMPDIR"/page && readDir "$TMPDIR" ;;
*) help ;;
esac
}
mkdir -p "$TMPDIR"
ARGS="$1"
if [ "$(echo "$1" | cut -c1-1)" = "-" ]; then
foot -e mangaterm "$2" 2>/dev/null > /dev/null
fi
handleFile "$ARGS"