diff --git a/README.md b/README.md index 094969b..8267873 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,6 @@ This is a small tool built with neofetch/fastfetch, ffmpeg and chafa. It allows ### Prerequisites Recommended Python version: 3.12 and later - -You need the following tools installed on your system: - - `bc` - Debian/Ubuntu: `sudo apt install bc` @@ -163,11 +160,12 @@ Any video file you give to anifetch will be stored in `~/.local/share/anifetch/a ### Example usage: ```bash -anifetch video.mp4 -r 10 -W 40 -H 20 -c "--symbols wide --fg-only" +anifetch video.mp4 -k -r 10 -W 40 -H 20 -c "--symbols wide --fg-only" ``` ### Optional arguments: +- `-k` / `--key-exit`: exits `anifetch` on any keypress. - `-f` / `--file`: path to the video file (the path can be added without the `-f` argument) - `-s` / `--sound`: optional sound file to play alongside (requires `ffplay`) - `-r` / `--framerate`: frame rate of playback diff --git a/src/anifetch/anifetch-static-resize2.sh b/src/anifetch/anifetch-static-resize2.sh index 3c88fea..41f9091 100644 --- a/src/anifetch/anifetch-static-resize2.sh +++ b/src/anifetch/anifetch-static-resize2.sh @@ -4,8 +4,8 @@ FRAME_DIR="$HOME/.local/share/anifetch/output" STATIC_TEMPLATE_FILE="$HOME/.local/share/anifetch/template.txt" # check for num of args -if [[ $# -ne 6 && $# -ne 7 ]]; then - echo "Usage: $0 [soundname]" +if [[ $# -ne 6 && $# -ne 7 && $# -ne 8 ]]; then + echo "Usage: $0 [soundname] [--key-exit]" exit 1 fi @@ -15,7 +15,16 @@ left=$3 right=$4 bottom=$5 template_actual_width=$6 -soundname=$7 +soundname="" +if [ $# -ge 7 ] && [ "${7}" != "--key-exit" ]; then + soundname=$7 +fi + +# Check if key-exit functionality is enabled +key_exit_enabled=false +if [[ "${!#}" == "--key-exit" ]]; then + key_exit_enabled=true +fi num_lines=$((bottom - top)) sleep_time=$(echo "scale=4; 1 / $framerate" | bc) @@ -29,15 +38,19 @@ last_term_width=0 # Hide cursor tput civis +# Global variable to store the pressed key +pressed_key="" + # exit handler cleanup() { tput cnorm # Show cursor if [ -t 0 ]; then stty echo # Restore echo - stty icanon + stty icanon fi tput sgr0 # Reset terminal attributes - tput cup $(tput lines) 0 # Move cursor to bottom + tput cup $(tput lines) 0 # Move cursor to bottom + exit 0 } trap cleanup SIGINT SIGTERM @@ -217,7 +230,7 @@ trap 'on_resize' SIGWINCH draw_static_template # Start audio if sound is provided -if [ $# -eq 7 ]; then +if [ -n "$soundname" ]; then ffplay -nodisp -autoexit -loop 0 -loglevel quiet "$soundname" & fi @@ -225,6 +238,10 @@ i=1 wanted_epoch=0 start_time=$(date +%s.%N) while true; do + # Check for any key press (non-blocking) - only if key-exit is enabled + if read -t 0 -n 1 pressed_key && [ "$key_exit_enabled" = true ]; then + cleanup + fi for frame in $(ls "$FRAME_DIR" | sort -n); do lock=true @@ -248,8 +265,23 @@ while true; do sleep_duration=$(echo "$wanted_epoch - ($now - $start_time)" | bc -l) # Only sleep if ahead of schedule - if (( $(echo "$sleep_duration > 0" | bc -l) )); then - sleep "$sleep_duration" + if [ "$key_exit_enabled" = true ]; then + if (( $(echo "$sleep_duration > 0" | bc -l) )); then + # Check for key press during sleep + if read -t "$sleep_duration" -n 1 pressed_key; then + cleanup + fi + else + # Check for key press (non-blocking) + if read -t 0 -n 1 pressed_key; then + cleanup + fi + fi + else + # If key-exit is not enabled, just sleep normally + if (( $(echo "$sleep_duration > 0" | bc -l) )); then + sleep "$sleep_duration" + fi fi i=$((i + 1)) diff --git a/src/anifetch/cli.py b/src/anifetch/cli.py index a19d484..971b875 100644 --- a/src/anifetch/cli.py +++ b/src/anifetch/cli.py @@ -105,6 +105,13 @@ def parse_args(): action="version", version="%(prog)s {version}".format(version=get_version_of_anifetch()), ) + parser.add_argument( + "-k", + "--key-exit", + default=False, + action="store_true", + help="Enable key press to exit functionality. When enabled, pressing any key will exit the animation and echo the key to the terminal.", + ) args = parser.parse_args() diff --git a/src/anifetch/core.py b/src/anifetch/core.py index eade650..a3b2809 100644 --- a/src/anifetch/core.py +++ b/src/anifetch/core.py @@ -392,6 +392,8 @@ def run_anifetch(args): ] if args.sound_flag_given: # if user requested for sound to be played script_args.append(str(args.sound_saved_path)) + if args.key_exit: # if user requested key exit functionality + script_args.append("--key-exit") print_verbose(args.verbose, script_args) # raise SystemExit