Skip to content

Instantly share code, notes, and snippets.

@y4my4my4m
Last active May 15, 2023 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y4my4my4m/e561160dc4119f2ad2a53c6897d9568b to your computer and use it in GitHub Desktop.
Save y4my4my4m/e561160dc4119f2ad2a53c6897d9568b to your computer and use it in GitHub Desktop.
ANSI Art Browser
#!/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
@y4my4my4m
Copy link
Author

This uses iconv as dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment