Files
drunkendotfiles/.local/bin/spam-ascii
dissimulo 065686f341
Some checks failed
Close stale issues / stale (push) Has been cancelled
spam-ascii: "*" dumps entire ~/ascii; alias quotes $0 to preserve it
2026-04-22 07:00:52 -07:00

58 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
#
# spam-ascii — cat a file from ~/ascii given a partial/fuzzy name.
#
# Resolution order:
# 0. pattern `*` → cat every file in ~/ascii (sorted)
# 1. exact filename match: ~/ascii/<pattern>
# 2. exact name + common extension: <pattern>.txt, .ans, .asc
# 3. first case-insensitive *substring* match (sorted alphabetically)
#
# If nothing matches, prints a friendly error to stdout and exits non-zero.
# Used by the irssi /spam alias, which pipes our stdout as channel messages.
set -eu
pattern=${1:-}
if [ -z "$pattern" ]; then
echo "usage: spam-ascii <pattern> (pattern `*` dumps everything)"
exit 1
fi
dir="$HOME/ascii"
# 0. `*` → dump the whole collection.
if [ "$pattern" = "*" ]; then
find -L "$dir" -maxdepth 1 -type f -print0 2>/dev/null \
| sort -z \
| xargs -0 cat
exit 0
fi
# 1 + 2. Try exact name, then exact + common ASCII-art extensions.
for candidate in \
"$dir/$pattern" \
"$dir/$pattern.txt" \
"$dir/$pattern.ans" \
"$dir/$pattern.asc"; do
if [ -f "$candidate" ]; then
cat "$candidate"
exit 0
fi
done
# 3. First case-insensitive substring match. -L so find follows a possible
# symlink starting point (e.g. ~/ascii -> ~/.yadr/ascii); without it, find
# treats the symlink itself as the "starting point" and ignores -type f.
match=$(find -L "$dir" -maxdepth 1 -type f -iname "*$pattern*" 2>/dev/null \
| sort \
| head -n1)
if [ -n "$match" ]; then
cat "$match"
exit 0
fi
echo "no match in ~/ascii for: $pattern"
exit 1