Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
48 changes: 40 additions & 8 deletions src/anifetch/anifetch-static-resize2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <framerate> <top> <left> <right> <bottom> <template_actual_width> [soundname]"
if [[ $# -ne 6 && $# -ne 7 && $# -ne 8 ]]; then
echo "Usage: $0 <framerate> <top> <left> <right> <bottom> <template_actual_width> [soundname] [--key-exit]"
exit 1
fi

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -217,14 +230,18 @@ 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

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
Expand All @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions src/anifetch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions src/anifetch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down