testsAndMisc/linux_configuration/scripts/single_use/utils/txt_to_image.sh
Krzysztof kuhy Rudnicki 9dd9aeb50e Fix the 11 shellcheck findings that kept CI red since b83ce5d
"Pre-commit checks" has failed on every push since b83ce5d removed
shellcheck's --severity filter. The findings never appeared locally: CI
installs an older shellcheck than this machine, and 0.11.0 no longer emits
SC2002/SC2015/SC2119 at all. Reproduced by fetching shellcheck 0.9.0, which
matches CI's output exactly, and verified against both versions.

SC2015 x6 -- rewrote `A && B || C` as an explicit if. Each keeps its trailing
`|| true`, so a failing side effect still cannot abort the caller under set -e.
The first draft dropped it in run_phone.sh; a review pass caught that and it
was restored, since that call is the last statement of a function.

SC2119 x3 -- require_imagemagick and adb_select_device take an OPTIONAL first
argument, so the bare calls were already correct. Following shellcheck's
literal advice and passing "$@" would forward the script's own arguments as a
tool preference, which would be a real defect. Passing an explicit "" states
the same intent without changing behaviour, confirmed by running both forms.

SC2002 x2 -- `cat FILE | tr ...` becomes `tr ... < FILE`, keeping 2>/dev/null
so a missing file still yields an empty string.

No suppressions, no --severity filter, no exclusions added. Verified: 0
findings under both shellcheck versions, `pre-commit run shellcheck
--all-files` passes, test_adb_common 13/13, test_boot_repair 31/31, 1193
Python tests at 100% coverage.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ReVsw7qovYkkGbqcU9tn7
2026-08-04 20:55:02 +02:00

163 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Convert a text file to image(s) with specified resolution
# Default resolution: 320x240
# Automatically splits text into multiple images if it doesn't fit
# Usage: txt_to_image.sh <input_text_file> [resolution] [output_prefix]
set -euo pipefail
# Source common library for shared functions
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck source=../../lib/common.sh
source "$SCRIPT_DIR/../../lib/common.sh"
# Default resolution
DEFAULT_RESOLUTION="320x240"
# Function to display usage
usage() {
cat << EOF
Usage: $0 <input_text_file> [resolution] [output_prefix]
Arguments:
input_text_file Path to the input text file (required)
resolution Target resolution in WIDTHxHEIGHT format (default: ${DEFAULT_RESOLUTION})
output_prefix Prefix for output image files (default: <input_filename>)
Examples:
$0 notes.txt
$0 notes.txt 640x480
$0 notes.txt 320x240 output
Note: Requires ImageMagick (magick or convert command)
EOF
exit 1
}
# Check if ImageMagick is installed and determine which command to use
# Empty arg = no preference between magick/convert. Passing "$@" here would
# forward this script's own arguments as a tool preference, which is wrong.
require_imagemagick "" || exit 1
# Parse arguments
if [[ $# -lt 1 ]]; then
echo "Error: Missing required argument <input_text_file>"
usage
fi
INPUT_FILE="$1"
RESOLUTION="${2:-${DEFAULT_RESOLUTION}}"
OUTPUT_PREFIX="${3:-}"
# Validate input file exists
if [[ ! -f ${INPUT_FILE} ]]; then
echo "Error: Input file '${INPUT_FILE}' does not exist."
exit 1
fi
# Validate resolution format (WIDTHxHEIGHT)
if ! validate_resolution "$RESOLUTION"; then
echo "Error: Invalid resolution format '${RESOLUTION}'"
echo "Expected format: WIDTHxHEIGHT (e.g., 320x240, 1920x1080)"
exit 1
fi
# Extract width and height
WIDTH=$(echo "${RESOLUTION}" | cut -d'x' -f1)
HEIGHT=$(echo "${RESOLUTION}" | cut -d'x' -f2)
# Calculate font size based on resolution
FONT_SIZE=$((WIDTH / 30))
if [[ ${FONT_SIZE} -lt 8 ]]; then
FONT_SIZE=8
fi
# Generate output prefix if not provided
if [[ -z ${OUTPUT_PREFIX} ]]; then
BASENAME=$(basename "${INPUT_FILE}")
FILENAME="${BASENAME%.*}"
DIRNAME=$(dirname "${INPUT_FILE}")
OUTPUT_PREFIX="${DIRNAME}/${FILENAME}"
fi
# Calculate lines per image based on resolution and font size
# Rough estimate: height / (font_size * 1.5) for line spacing
LINES_PER_IMAGE=$((HEIGHT / (FONT_SIZE * 3 / 2)))
if [[ ${LINES_PER_IMAGE} -lt 5 ]]; then
LINES_PER_IMAGE=5
fi
echo "Converting text file to image(s)..."
echo "Resolution: ${RESOLUTION}"
echo "Font size: ${FONT_SIZE}"
echo "Estimated lines per image: ${LINES_PER_IMAGE}"
# Read the file and count total lines
mapfile -t LINES < "${INPUT_FILE}"
TOTAL_LINES=${#LINES[@]}
echo "Total lines in file: ${TOTAL_LINES}"
# Calculate number of images needed
NUM_IMAGES=$(((TOTAL_LINES + LINES_PER_IMAGE - 1) / LINES_PER_IMAGE))
echo "Creating ${NUM_IMAGES} image(s)..."
# Create temporary directory for chunks
TEMP_DIR=$(mktemp -d)
trap 'rm -rf ${TEMP_DIR}' EXIT
# Split text into chunks and create images
IMAGE_COUNT=0
for ((i = 0; i < TOTAL_LINES; i += LINES_PER_IMAGE)); do
IMAGE_COUNT=$((IMAGE_COUNT + 1))
# Calculate end line for this chunk
END_LINE=$((i + LINES_PER_IMAGE))
if [[ ${END_LINE} -gt ${TOTAL_LINES} ]]; then
END_LINE=${TOTAL_LINES}
fi
# Create chunk file
CHUNK_FILE="${TEMP_DIR}/chunk_${IMAGE_COUNT}.txt"
for ((j = i; j < END_LINE; j++)); do
echo "${LINES[$j]}" >> "${CHUNK_FILE}"
done
# Determine output filename
if [[ ${NUM_IMAGES} -eq 1 ]]; then
OUTPUT_FILE="${OUTPUT_PREFIX}.png"
else
OUTPUT_FILE="${OUTPUT_PREFIX}_$(printf "%03d" ${IMAGE_COUNT}).png"
fi
echo " Creating image ${IMAGE_COUNT}/${NUM_IMAGES}: ${OUTPUT_FILE}"
# Create image from text
# Using label: instead of caption: for better control
if ${MAGICK_CMD} -size "${WIDTH}x${HEIGHT}" \
-background white \
-fill black \
-font "DejaVu-Sans-Mono" \
-pointsize "${FONT_SIZE}" \
-gravity northwest \
label:@"${CHUNK_FILE}" \
-extent "${WIDTH}x${HEIGHT}" \
"${OUTPUT_FILE}"; then
OUTPUT_SIZE=$(du -h "${OUTPUT_FILE}" | cut -f1)
echo " ✓ Created: ${OUTPUT_FILE} (${OUTPUT_SIZE})"
else
echo " ✗ Failed to create: ${OUTPUT_FILE}"
exit 1
fi
done
echo ""
echo "✓ Successfully created ${IMAGE_COUNT} image(s)"
echo "Output files:"
if [[ ${NUM_IMAGES} -eq 1 ]]; then
echo " ${OUTPUT_PREFIX}.png"
else
echo " ${OUTPUT_PREFIX}_001.png to ${OUTPUT_PREFIX}_$(printf "%03d" ${IMAGE_COUNT}).png"
fi