Skip to content

Instantly share code, notes, and snippets.

@flyinghyrax
Created December 19, 2014 03:02
Show Gist options
  • Save flyinghyrax/873a4a1e4d17b531348a to your computer and use it in GitHub Desktop.
Save flyinghyrax/873a4a1e4d17b531348a to your computer and use it in GitHub Desktop.
Cleaned up version of random terminal color test script from the interwebz
#!/usr/bin/env python
# Copyright (C) 2006 by Johannes Zellner, <johannes@zellner.org>
# modified by mac@calmar.ws to fit my output needs
# modified by crncosta@carloscosta.org to fit my output needs
# modified by theflyinghyrax@gmail.com (2014)
# - use if __name__ == "__main__" to run script stand-alone
# - organize and define more reusable functions (could be more idiomatic)
import sys
import os
def echo(msg):
os.system('echo -n "' + str(msg) + '"')
def out(n):
os.system("tput setab " + str(n) + "; echo -n " + ("\"% 4d\"" % n))
os.system("tput setab 0")
def print_16():
print_color_range(0, 15, 8)
echo("\n")
def print_231():
print_color_range(16, 231)
echo("\n")
def print_grayscale():
print_color_range(232, 255)
echo("\n")
def print_color_range(lo, hi, line_len=6):
n = lo
while n <= hi:
i = 0
while i < line_len and n <= hi:
out(n)
i += 1
n += 1
echo("\n")
def print_colors():
os.system("tput setaf 16")
print_16()
print_231()
print_grayscale()
os.system("tput setaf 7")
os.system("tput setab 0")
if __name__ == "__main__":
print_colors()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment