Merge duplicate ASCII renderers and trim dead options

The braille and block renderers were two near-identical awk programs
sharing a verbatim PBM tokenizer, header check, and blank-line trim,
differing only in the per-cell rendering. One awk with a mode variable
now branches where they actually differ.

The option surface served two in-repo callers, both passing constants:
the duplicate positional loop behind --, the undocumented --block and
--braille aliases, and the --flag=value forms all go. The two magick
probes for the alpha range collapse into a single invocation.

Output is byte-identical across images and flag sets for everything
that remains, including both real callers' exact invocations.

353 lines to 243.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
David Heinemeier Hansson
2026-07-02 22:09:14 -07:00
co-authored by Claude Fable 5
parent cacd22c671
commit b14735aacb
+34 -144
View File
@@ -42,64 +42,30 @@ while (($# > 0)); do
;; ;;
-w | --width) -w | --width)
shift shift
if [[ -z $1 ]]; then [[ -n $1 ]] || { echo "Missing value for --width" >&2; exit 1; }
echo "Missing value for --width" >&2
exit 1
fi
width="$1" width="$1"
;; ;;
--width=*)
width="${1#*=}"
;;
-H | --height) -H | --height)
shift shift
if [[ -z $1 ]]; then [[ -n $1 ]] || { echo "Missing value for --height" >&2; exit 1; }
echo "Missing value for --height" >&2
exit 1
fi
height="$1" height="$1"
;; ;;
--height=*)
height="${1#*=}"
;;
-m | --mode) -m | --mode)
shift shift
if [[ -z $1 ]]; then [[ -n $1 ]] || { echo "Missing value for --mode" >&2; exit 1; }
echo "Missing value for --mode" >&2
exit 1
fi
mode="$1" mode="$1"
;; ;;
--mode=*)
mode="${1#*=}"
;;
--block | --blocks)
mode=block
;;
--braille)
mode=braille
;;
-t | --threshold) -t | --threshold)
shift shift
if [[ -z $1 ]]; then [[ -n $1 ]] || { echo "Missing value for --threshold" >&2; exit 1; }
echo "Missing value for --threshold" >&2
exit 1
fi
threshold="$1" threshold="$1"
;; ;;
--threshold=*)
threshold="${1#*=}"
;;
--invert) --invert)
invert=true invert=true
;; ;;
--no-trim) --no-trim)
trim=false trim=false
;; ;;
--)
shift
break
;;
-*) -*)
echo "Unknown option: $1" >&2 echo "Unknown option: $1" >&2
usage >&2 usage >&2
@@ -120,46 +86,16 @@ while (($# > 0)); do
shift shift
done done
if (($# > 0)); then
while (($# > 0)); do
if [[ -z $image_path ]]; then
image_path="$1"
elif [[ -z $output_path ]]; then
output_path="$1"
else
echo "Unexpected argument: $1" >&2
usage >&2
exit 1
fi
shift
done
fi
if [[ -z $image_path || -z $output_path ]]; then if [[ -z $image_path || -z $output_path ]]; then
usage >&2 usage >&2
exit 1 exit 1
fi fi
if ! [[ $width =~ ^[0-9]+$ ]] || (( width < 1 )); then
echo "Invalid width: $width" >&2
exit 1
fi
if ! [[ $height =~ ^[0-9]+$ ]] || (( height < 1 )); then
echo "Invalid height: $height" >&2
exit 1
fi
if [[ $mode != "braille" && $mode != "block" ]]; then if [[ $mode != "braille" && $mode != "block" ]]; then
echo "Invalid mode: $mode (expected braille or block)" >&2 echo "Invalid mode: $mode (expected braille or block)" >&2
exit 1 exit 1
fi fi
if ! [[ $threshold =~ ^[0-9]+$ ]] || (( threshold < 0 || threshold > 100 )); then
echo "Invalid threshold: $threshold (expected 0-100)" >&2
exit 1
fi
if [[ ! -f $image_path ]]; then if [[ ! -f $image_path ]]; then
echo "Logo file not found: $image_path" >&2 echo "Logo file not found: $image_path" >&2
exit 1 exit 1
@@ -170,25 +106,19 @@ if omarchy-cmd-missing magick; then
exit 1 exit 1
fi fi
case "$mode" in if [[ $mode == "braille" ]]; then
braille)
pixel_width=$((width * 2)) pixel_width=$((width * 2))
pixel_height=$((height * 4)) pixel_height=$((height * 4))
;; else
block)
pixel_width=$width pixel_width=$width
pixel_height=$((height * 2)) pixel_height=$((height * 2))
;; fi
esac
alpha_min=$(magick -background none "$image_path" -alpha extract -format '%[fx:minima]' info: 2>/dev/null) || { alpha_range=$(magick -background none "$image_path" -alpha extract -format '%[fx:minima] %[fx:maxima]' info: 2>/dev/null) || {
echo "Unable to read logo image: $image_path" >&2
exit 1
}
alpha_max=$(magick -background none "$image_path" -alpha extract -format '%[fx:maxima]' info: 2>/dev/null) || {
echo "Unable to read logo image: $image_path" >&2 echo "Unable to read logo image: $image_path" >&2
exit 1 exit 1
} }
read -r alpha_min alpha_max <<<"$alpha_range"
use_alpha=false use_alpha=false
if awk -v min="$alpha_min" -v max="$alpha_max" 'BEGIN { exit !(min < 0.999 && max > min) }'; then if awk -v min="$alpha_min" -v max="$alpha_max" 'BEGIN { exit !(min < 0.999 && max > min) }'; then
@@ -218,8 +148,7 @@ magick_args+=(-resize "${pixel_width}x${pixel_height}" -threshold "${threshold}%
tmp_output=$(mktemp) tmp_output=$(mktemp)
trap 'rm -f "$tmp_output"' EXIT trap 'rm -f "$tmp_output"' EXIT
if [[ $mode == "braille" ]]; then if ! magick "${magick_args[@]}" | awk -v mode="$mode" '
if ! magick "${magick_args[@]}" | awk '
BEGIN { BEGIN {
braille_base = 10240 braille_base = 10240
dot[0,0] = 1 dot[0,0] = 1
@@ -230,68 +159,7 @@ if [[ $mode == "braille" ]]; then
dot[1,2] = 32 dot[1,2] = 32
dot[0,3] = 64 dot[0,3] = 64
dot[1,3] = 128 dot[1,3] = 128
}
{
sub(/#.*/, "")
for (i = 1; i <= NF; i++) {
token[++token_count] = $i
}
}
END {
if (token[1] != "P1") {
exit 1
}
width = token[2]
height = token[3]
pixel_offset = 4
for (y = 0; y < height; y += 4) {
line = ""
for (x = 0; x < width; x += 2) {
code = 0
for (dy = 0; dy < 4; dy++) {
for (dx = 0; dx < 2; dx++) {
if (y + dy < height && x + dx < width) {
pixel = token[pixel_offset + ((y + dy) * width) + x + dx]
if (pixel == 1) {
code += dot[dx,dy]
}
}
}
}
if (code == 0) {
line = line " "
} else {
line = line sprintf("%c", braille_base + code)
}
}
sub(/[ ]+$/, "", line)
lines[++line_count] = line
}
first = 1
while (first <= line_count && lines[first] ~ /^[ ]*$/) {
first++
}
last = line_count
while (last >= first && lines[last] ~ /^[ ]*$/) {
last--
}
for (i = first; i <= last; i++) {
print lines[i]
}
}
' >"$tmp_output"; then
echo "Unable to convert logo image: $image_path" >&2
exit 1
fi
else
if ! magick "${magick_args[@]}" | awk '
BEGIN {
block["11"] = "█" block["11"] = "█"
block["10"] = "▀" block["10"] = "▀"
block["01"] = "▄" block["01"] = "▄"
@@ -311,14 +179,37 @@ else
width = token[2] width = token[2]
height = token[3] height = token[3]
pixel_offset = 4 pixel_offset = 4
row_step = (mode == "braille") ? 4 : 2
for (y = 0; y < height; y += 2) { for (y = 0; y < height; y += row_step) {
line = "" line = ""
if (mode == "braille") {
for (x = 0; x < width; x += 2) {
code = 0
for (dy = 0; dy < 4; dy++) {
for (dx = 0; dx < 2; dx++) {
if (y + dy < height && x + dx < width) {
pixel = token[pixel_offset + ((y + dy) * width) + x + dx]
if (pixel == 1) {
code += dot[dx,dy]
}
}
}
}
if (code == 0) {
line = line " "
} else {
line = line sprintf("%c", braille_base + code)
}
}
} else {
for (x = 0; x < width; x++) { for (x = 0; x < width; x++) {
top = token[pixel_offset + (y * width) + x] top = token[pixel_offset + (y * width) + x]
bottom = (y + 1 < height) ? token[pixel_offset + ((y + 1) * width) + x] : 0 bottom = (y + 1 < height) ? token[pixel_offset + ((y + 1) * width) + x] : 0
line = line block[top bottom] line = line block[top bottom]
} }
}
sub(/[ ]+$/, "", line) sub(/[ ]+$/, "", line)
lines[++line_count] = line lines[++line_count] = line
} }
@@ -341,7 +232,6 @@ else
echo "Unable to convert logo image: $image_path" >&2 echo "Unable to convert logo image: $image_path" >&2
exit 1 exit 1
fi fi
fi
if [[ ! -s $tmp_output ]]; then if [[ ! -s $tmp_output ]]; then
echo "No logo pixels found in: $image_path" >&2 echo "No logo pixels found in: $image_path" >&2