diff options
author | pux <pux@pux.dom> | 2021-02-04 15:06:10 +0100 |
---|---|---|
committer | pux <pux@pux.dom> | 2021-02-04 15:06:10 +0100 |
commit | fefe77825d02c6b4c801ee3d69726f52663e368f (patch) | |
tree | bbe3aee604025a163c8eb1a6300ed76ef0382e95 /pyfarbpracht | |
parent | 4bf1d79b27e674f56660200391b7ae878c4de0a9 (diff) | |
download | pyfarbpracht-fefe77825d02c6b4c801ee3d69726f52663e368f.tar.gz pyfarbpracht-fefe77825d02c6b4c801ee3d69726f52663e368f.tar.bz2 pyfarbpracht-fefe77825d02c6b4c801ee3d69726f52663e368f.zip |
add colored printing for any hex value, and cli color print program
Diffstat (limited to 'pyfarbpracht')
-rwxr-xr-x | pyfarbpracht/printc.py | 30 | ||||
-rwxr-xr-x | pyfarbpracht/pyfarbpracht.py | 39 |
2 files changed, 53 insertions, 16 deletions
diff --git a/pyfarbpracht/printc.py b/pyfarbpracht/printc.py new file mode 100755 index 0000000..3ac38a2 --- /dev/null +++ b/pyfarbpracht/printc.py @@ -0,0 +1,30 @@ +#!/bin/env python3 + +import sys +import pyfarbpracht as fp + +def usage(): + print('usage: ' + sys.argv[0] + ' \"#abcdef\" msg') + print('You need to escape the hex color') + sys.exit(1) + +if len(sys.argv) < 2: + usage() + +color = sys.argv[1] +if not color.startswith('#'): + print('Color does not start with "#"') + sys.exit(1) +if len(color) != 7: + print('Invalid color length') + sys.exit(1) + +try: + test = int(color[1:], 16) +except: + print('Color is not a valid hex string') + sys.exit(1) + +msg = sys.argv[2] + +fp.printc(color, msg, enable_color=True) diff --git a/pyfarbpracht/pyfarbpracht.py b/pyfarbpracht/pyfarbpracht.py index 2422699..d41842b 100755 --- a/pyfarbpracht/pyfarbpracht.py +++ b/pyfarbpracht/pyfarbpracht.py @@ -19,22 +19,29 @@ def printc(color, msg, no_newline=False, enable_color=False): def colored(color, msg): """returns msg in ANSI color""" ret = str() - c = 'default' - if color == 'red': - c = '31' - elif color == 'green': - c = '32' - elif color == 'yellow': - c = '33' - elif color == 'blue': - c = '34' - elif color == 'magenta': - c = '35' - elif color == 'cyan': - c = '36' - if c is not 'default': - # set color - ret += '\033[0;' + c + 'm' + if color.startswith("#"): + ret += '\033[38;2;' + ret += str(int(color[1:3], 16)) + ';' \ + + str(int(color[3:5], 16)) + ';' \ + + str(int(color[5:7], 16)) + ret += 'm' + else: + c = 'default' + if color == 'red': + c = '31' + elif color == 'green': + c = '32' + elif color == 'yellow': + c = '33' + elif color == 'blue': + c = '34' + elif color == 'magenta': + c = '35' + elif color == 'cyan': + c = '36' + if c is not 'default': + # set color + ret += '\033[0;' + c + 'm' ret += msg # reset color ret += '\033[0m' |