ascii-photobooth/txt2printr.sh

39 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
#
# Sends an ascii-art text file to a parallel port needle/dot-matrix printer.
#
# Usage: ./txt2printr.sh [ascii-art.txt]
# Env: PRINTER_DEV - printer device node (default: /dev/lp0)
infile="${1:-ascii-img.txt}"
# Source - https://stackoverflow.com/a/638980
# Posted by John Feminella, modified by community. See post 'Timeline' for change history
# Retrieved 2026-07-14, License - CC BY-SA 4.0
if [ -n "$PRINTER_DEV" ]; then
dev="$PRINTER_DEV"
elif [ -e /dev/lp0 ]; then
dev=/dev/lp0
elif [ -e /dev/usb/lp0 ]; then
dev=/dev/usb/lp0
else
dev=/dev/stdout
fi
if [ ! -f "$infile" ]; then
echo "txt2printr: file not found: $infile" >&2
exit 1
fi
if [ ! -w "$dev" ]; then
echo "txt2printr: cannot write to printer device: $dev" >&2
echo "txt2printr: set PRINTER_DEV to override, or check permissions/parport module" >&2
exit 1
fi
{
printf '\x1b\x40' # ESC @ - reset printer (ESC/P and IBM Proprinter compatible)
sed 's/$/\r/' "$infile" # CRLF line endings, needed for raw writes to avoid staircasing
printf '\x0c' # form feed - eject the page
} > "$dev"