Last active
January 15, 2026 18:23
-
-
Save wallentx/100de024ec2b06600c0c6cefc539c04f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| usage() { | |
| cat <<'EOF' | |
| Usage: | |
| alphabet-emoji [-w|-y|-m|-a] "text to spell" | |
| Options: | |
| -w white (default) | |
| -y yellow | |
| -m mixed: alternate color every emitted token (letters + supported specials) | |
| -a alternate color every word (word1 white, word2 yellow, ...) | |
| Supported: | |
| - A-Z letters (case-insensitive) | |
| - Special characters: @ # ! ? | |
| Spacing: | |
| - Each space in input becomes TWO spaces in output. | |
| - No other spaces are inserted. | |
| EOF | |
| } | |
| mode="w" | |
| while getopts ":wymah" opt; do | |
| case "$opt" in | |
| w) mode="w" ;; | |
| y) mode="y" ;; | |
| m) mode="m" ;; | |
| a) mode="a" ;; | |
| h) usage; exit 0 ;; | |
| \?) echo "Unknown option: -$OPTARG" >&2; usage; exit 2 ;; | |
| esac | |
| done | |
| shift $((OPTIND - 1)) | |
| if [[ $# -lt 1 ]]; then | |
| echo "Error: missing text string." >&2 | |
| usage | |
| exit 2 | |
| fi | |
| input="$*" | |
| emit_token() { | |
| local color="$1" token="$2" | |
| printf ':alphabet-%s-%s:' "$color" "$token" | |
| } | |
| # Echo token name + return 0 if supported, else return 1. | |
| char_to_token() { | |
| local c="$1" lc="${c,,}" | |
| if [[ "$lc" =~ ^[a-z]$ ]]; then | |
| printf '%s' "$lc" | |
| return 0 | |
| fi | |
| case "$c" in | |
| '@') printf 'at'; return 0 ;; | |
| '#') printf 'hash'; return 0 ;; | |
| '!') printf 'exclamation'; return 0 ;; | |
| '?') printf 'question'; return 0 ;; | |
| esac | |
| return 1 | |
| } | |
| # Alternation state | |
| mix_toggle=0 # for -m: 0 => white, 1 => yellow | |
| word_toggle=0 # for -a: 0 => white, 1 => yellow (first word white) | |
| in_word=0 # for -a: 0 => not in word, 1 => in word | |
| for ((i=0; i<${#input}; i++)); do | |
| c="${input:i:1}" | |
| if [[ "$c" == " " ]]; then | |
| # Interpret a single space as a double space | |
| printf ' ' | |
| in_word=0 | |
| continue | |
| fi | |
| if token="$(char_to_token "$c")"; then | |
| # Decide color (and update alternation state) WITHOUT subshells | |
| case "$mode" in | |
| w) color="white" ;; | |
| y) color="yellow" ;; | |
| m) | |
| if (( mix_toggle == 0 )); then color="white"; else color="yellow"; fi | |
| mix_toggle=$((1 - mix_toggle)) | |
| ;; | |
| a) | |
| if (( in_word == 0 )); then | |
| # new word: flip color except for the very first word | |
| if (( i != 0 )); then | |
| word_toggle=$((1 - word_toggle)) | |
| fi | |
| in_word=1 | |
| fi | |
| if (( word_toggle == 0 )); then color="white"; else color="yellow"; fi | |
| ;; | |
| esac | |
| emit_token "$color" "$token" | |
| else | |
| # Unhandled characters pass through unchanged | |
| printf '%s' "$c" | |
| # Keep punctuation inside the word for -a (so hello-world counts as one word) | |
| if [[ "$mode" == "a" ]]; then | |
| in_word=1 | |
| fi | |
| fi | |
| done | |
| printf '\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment