Last active
May 15, 2023 08:16
-
-
Save y4my4my4m/e561160dc4119f2ad2a53c6897d9568b to your computer and use it in GitHub Desktop.
ANSI Art Browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ANSi art function | |
function ANSi() { | |
tr '\0' ' ' < "$1" | iconv -f CP437 -t UTF8 | |
} | |
# Check if a directory was provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 directory" | |
exit 1 | |
fi | |
# Directory containing the files | |
dir_path="$1" | |
# Get a sorted list of all files in the directory | |
files=("$dir_path"/*) | |
index=0 | |
while true; do | |
# Apply the ANSi function to the file | |
ANSi "${files[$index]}" | |
# Reset terminal colors | |
echo -e "\033[0m" | |
# Print the file name | |
echo -e "\n\nDisplaying: ${files[$index]}" | |
# Read the next key pressed by the user | |
read -rsn1 input | |
# Check the key and update the index accordingly | |
case $input in | |
$'\x1b') # If the key is ESC | |
read -rsn2 input # read two more chars | |
if [[ $input == "[A" ]] || [[ $input == "[D" ]]; then | |
# If the key is Up arrow or Left arrow | |
clear; | |
((index--)) | |
elif [[ $input == "[B" ]] || [[ $input == "[C" ]]; then | |
# If the key is Down arrow | |
clear; | |
((index++)) | |
fi | |
;; | |
q) # Quit on 'q' or 'Q' | |
exit | |
;; | |
esac | |
# Ensure the index stays within the array bounds | |
index=$(( (index+${#files[@]}) % ${#files[@]} )) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses
iconv
as dependency.