ansi-utils/ansicols.py

75 lines
1.9 KiB
Python

import os, random, math
# From http://stackoverflow.com/a/566752/1753929
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
return int(cr[1]), int(cr[0])
os.system("clear")
styles = {
"Bold & Bright": "1",
"Dim": "2",
"Underlined": "4",
"Blink": "5",
"Inverted": "7",
# "Hidden": "8", wont be visible :(
"Reset": "0"
}
print("\033[1mStyles:\033[0m")
styleSpace = max(list(map(len, styles)))
print("|", end=' ')
for style in sorted(styles):
text = "\033[" + styles[style] + "m" + style + "\033[0m" + " \\033[" + styles[style] + "m"
print(" " * int(math.floor((styleSpace - len(text)) / 2.0)) + text + " " * int(math.ceil((styleSpace - len(text)) / 2)) + " |", end=' ')
print()
print("\n")
print("".join(["\033[38;5;" + str(random.randrange(0, 255)) + "m" + letter for letter in "Colors:"]))
numsPerLine = getTerminalSize()[0] / 18
for colorType in [38, 48]:
currentNumsPerLine = 0
for color in range(256):
esc = "[" + str(colorType) + ";5;" + str(color) + "m"
print("\033" + esc + "\\033" + esc + "\033[0m\t", end=' ')
currentNumsPerLine += 1
if currentNumsPerLine > numsPerLine:
print()
currentNumsPerLine = 0
print("\033[0m\n")