From fefe77825d02c6b4c801ee3d69726f52663e368f Mon Sep 17 00:00:00 2001 From: pux Date: Thu, 4 Feb 2021 15:06:10 +0100 Subject: add colored printing for any hex value, and cli color print program --- pyfarbpracht/printc.py | 30 ++++++++++++++++++++++++++++++ pyfarbpracht/pyfarbpracht.py | 39 +++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 16 deletions(-) create mode 100755 pyfarbpracht/printc.py (limited to 'pyfarbpracht') 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' -- cgit v1.2.2-1-g5e49