Last active
December 21, 2021 22:40
-
-
Save GammaGames/720af145380a2ac608299745b4b4a8fc to your computer and use it in GitHub Desktop.
Create colored text to copy/paste into other applications using python
This file contains 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
import click | |
import webbrowser | |
import os | |
import random | |
import tempfile | |
from time import sleep | |
random.seed() | |
tmp = tempfile.NamedTemporaryFile() | |
@click.command() | |
@click.argument("text") | |
@click.option("--colors", default="crimson,chartreuse") | |
@click.option("--pattern", type=click.Choice(["loop", "random"]), default="loop") | |
@click.option("--split", type=click.Choice(["character", "word"]), default="character") | |
def color(text, colors, pattern, split): | |
filename = f"{tmp.name}.html" | |
with open(filename, "w") as out_file: | |
cols = colors.split(",") | |
chs = text if split == "character" else text.split() | |
for ch in chs: | |
if pattern == "loop": | |
col = cols[0] | |
else: | |
col = random.choice(cols) | |
out_file.write(f"<span style='color: {col}'>{ch}</span>") | |
if split == "word": | |
out_file.write(" ") | |
if ch != " " and pattern == "loop": | |
cols.append(cols.pop(0)) | |
webbrowser.open(f"file://{os.path.realpath(filename)}") | |
if __name__ == "__main__": | |
color() |
Author
GammaGames
commented
Dec 21, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment