Skip to content

Instantly share code, notes, and snippets.

@andreis
Created November 29, 2021 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreis/01b21318cdbed71d4622497bd6bb4d9b to your computer and use it in GitHub Desktop.
Save andreis/01b21318cdbed71d4622497bd6bb4d9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse, sys, select
from enum import Enum
from random import randint
Mode = Enum("Mode", "white yellow mixed random")
def config():
parser = argparse.ArgumentParser(description="Emojify your butt")
parser.add_argument("-w", "--white", action="store_true", help="Write white text")
parser.add_argument("-y", "--yellow", action="store_true", help="Write yellow text")
parser.add_argument(
"-m",
"--mixed",
action="store_true",
help="Write an even mix of white/yellow text",
)
parser.add_argument(
"-r",
"--random",
action="store_true",
help="Write a random mix of white/yellow text",
)
# TODO: if macOS, use pbcopy/pbpaste
parser.add_argument(
"text", nargs="*", help="Some text to emojify; or just pipe it in"
)
args = parser.parse_args()
mode = Mode.white
if args.white:
mode = Mode.white
elif args.yellow:
mode = Mode.yellow
elif args.mixed:
mode = Mode.mixed
elif args.random:
mode = Mode.random
else:
print("No flag provided, defaulting to white text")
text = " ".join(args.text) if args.text else None
# fmt: off
if select.select([sys.stdin,], [], [], 0.0)[0]:
if not text:
text = "\n".join(sys.stdin.readlines())
# fmt: on
return {"mode": mode, "text": text}
WHITE_FMT = ":alphabet-white-{}:"
YELLOW_FMT = ":alphabet-yellow-{}:"
naive_emoji_map = {
"$": "💲",
"!": "❗",
"?": "❓",
"0": "0️⃣",
"1": "1️⃣",
"2": "2️⃣",
"3": "3️⃣",
"4": "4️⃣",
"5": "5️⃣",
"6": "6️⃣",
"7": "7️⃣",
"8": "8️⃣",
"9": "9️⃣",
"-": "➖",
"+": "➕",
"'": "▫",
}
def gen_mixed():
nxt = 0
while True:
yield nxt
nxt ^= 1
def gen_random():
while True:
yield randint(0, 1)
def gen_n(n):
def fn():
while True:
yield n
return fn
def fmt(mode, text):
fmts = (":alphabet-white-{}:", ":alphabet-yellow-{}:")
mode_map = {
Mode.white: gen_n(0),
Mode.yellow: gen_n(1),
Mode.mixed: gen_mixed,
Mode.random: gen_random,
}
gen = mode_map[mode]
buf = list()
for ch, choice in zip(text, gen()):
if not ch.isalpha():
buf.append(naive_emoji_map.get(ch, ch))
continue
ch = ch.lower()
buf.append(fmts[choice].format(ch))
return "".join(buf)
print(fmt(**config()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment