From 48135127314f6ef1db054e9ee1d02e8c2332b901 Mon Sep 17 00:00:00 2001 From: dissimulo Date: Tue, 21 Apr 2026 09:48:19 -0700 Subject: [PATCH] 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: ' 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' --- .irssi/config | 2 +- .local/bin/spam-ascii | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100755 .local/bin/spam-ascii diff --git a/.irssi/config b/.irssi/config index f5446484..9aa8a168 100644 --- a/.irssi/config +++ b/.irssi/config @@ -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"; diff --git a/.local/bin/spam-ascii b/.local/bin/spam-ascii new file mode 100755 index 00000000..19b3067e --- /dev/null +++ b/.local/bin/spam-ascii @@ -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/ +# 2. exact name + common extension: .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 " + 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