Skip to content

Instantly share code, notes, and snippets.

@monperrus
Created March 20, 2025 14:27
Show Gist options
  • Save monperrus/12ccb77344c494e9fc54e7fdd567f903 to your computer and use it in GitHub Desktop.
Save monperrus/12ccb77344c494e9fc54e7fdd567f903 to your computer and use it in GitHub Desktop.
a python program that outputs a colored loren ipsum with ASC II color sequences.
import random
def get_random_color_code():
"""Generate a random ANSI color code between 31-36 (red, green,
yellow, blue, magenta, cyan)"""
return random.randint(31, 36)
def colorize_text(text, color_code):
"""Apply ANSI color code to text"""
return f"\033[{color_code}m{text}\033[0m"
def generate_colored_lorem_ipsum():
"""Generate a colored Lorem Ipsum text with ASCII color sequences"""
lorem_ipsum = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod
tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
qui officia
deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium
doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis
et quasi
architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
quia voluptas
sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos
qui ratione
voluptatem sequi nesciunt.
"""
# Split the text into words
words = lorem_ipsum.split()
# Apply random colors to each word
colored_words = [colorize_text(word, get_random_color_code()) for word in words]
# Reconstruct the text with proper spacing and line breaks
colored_text = ""
line_length = 0
max_line_length = 80
for word in colored_words:
# Strip ANSI codes for length calculation
plain_word = word.replace("\033[31m", "").replace("\033[32m",
"").replace("\033[33m", "")
plain_word = plain_word.replace("\033[34m",
"").replace("\033[35m", "").replace("\033[36m", "")
plain_word = plain_word.replace("\033[0m", "")
# Check if adding this word would exceed the line length
if line_length + len(plain_word) + 1 > max_line_length:
colored_text += "\n"
line_length = 0
# Add the word with a space
if line_length > 0:
colored_text += " "
line_length += 1
colored_text += word
line_length += len(plain_word)
# Add paragraph breaks
if plain_word.endswith(".") and random.random() < 0.2:
colored_text += "\n\n"
line_length = 0
return colored_text
if __name__ == "__main__":
print("\nColored Lorem Ipsum Text:\n")
colored_lorem = generate_colored_lorem_ipsum()
print(colored_lorem)
print("\nNote: This will only display colors correctly in terminals that support ANSI color codes.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment