Created
July 14, 2023 14:28
-
-
Save bendoh/bf3bb3d3068e3b2f0e7c06e22b53297a to your computer and use it in GitHub Desktop.
Spit out some single-code-point random emoji or all of them to the terminal
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
#!/bin/zsh | |
# Requires bash 4.2 or Zsh 4.3 | |
# | |
# Usage: $0 [count] | |
# | |
# Dump count (or, if not given, all) single-code-point emoji to the terminal given a reference file | |
# in $EMOJI_REF or ~/emoji-latest.txt | |
# Get this reference file from https://www.unicode.org/Public/emoji/latest/emoji-test.txt | |
ref=${EMOJI_REF:-~/emoji-latest.txt} | |
if [[ $1 != "" ]] then | |
nLines=$1 | |
fi | |
IFS=$'\n' emojiCodes=($(cat $ref | | |
grep fully-qualified | # only fully-qualified ones | |
grep -v 'flag' | # these are mostly broken | |
sed -E 's/^(.*) *;.*/\1/g' | # grab just the token | |
sed -e 's/ *$//' | # kill trailing spaces | |
grep -v ' ' # don't bother with multiple code point emoji, they generally don't render in the terminal | |
)) | |
printEmoji() { | |
local line=$1 | |
eval echo -n $(echo \$\'\\U$1\') | |
} | |
if [[ $nLines != "" ]]; then | |
for j in $(seq 1 $nLines); do | |
ss=($RANDOM % $numCodes) | |
printEmoji "${emojiCodes[$RANDOM % ${#emojiCodes[@]} + 1]}" | |
done | |
else | |
while IFS= read -r line; do | |
printEmoji $line | |
done <<< "$emojiCodes" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment