spam-ascii: fuzzy-match helper for /spam alias

New .local/bin/spam-ascii POSIX shell script: resolves a pattern to
a file in ~/ascii with this order:

  1. exact filename match
  2. exact name + common ASCII-art extension (.txt/.ans/.asc)
  3. first case-insensitive substring match (sorted alphabetically)

Non-zero exit + stderr-ish error if nothing matches, so /spam
prints 'no match in ~/ascii for: <pattern>' instead of spewing
a missing-file error.

Rewire the irssi /spam alias to call the script instead of
hardcoding 'cat ~/ascii/$0'. Now:

  /spam mario     -> matches ~/ascii/mario.txt or ~/ascii/Mario_art.ans
  /spam MARIO     -> case-insensitive
  /spam nothere   -> 'no match in ~/ascii for: nothere'
This commit is contained in:
2026-04-21 09:48:19 -07:00
parent 7b47e38f53
commit 4813512731
2 changed files with 47 additions and 1 deletions

View File

@@ -158,7 +158,7 @@ aliases = {
SB = "SCROLLBACK";
SBAR = "STATUSBAR";
SIGNOFF = "QUIT";
SPAM = "EXEC -out cat ~/ascii/$0";
SPAM = "EXEC -out ~/.local/bin/spam-ascii $0";
SV = "MSG * Irssi $J ($V) - http://www.irssi.org";
T = "TOPIC";
UB = "UNBAN";

46
.local/bin/spam-ascii Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/sh
#
# spam-ascii — cat a file from ~/ascii given a partial/fuzzy name.
#
# Resolution order:
# 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>"
exit 1
fi
dir="$HOME/ascii"
# 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.
match=$(find "$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