From cd630891e73a3edae7926612ecd4192b655936ce Mon Sep 17 00:00:00 2001 From: pux Date: Tue, 31 Mar 2020 12:22:42 +0200 Subject: moved color print stuff from other projects to here --- .gitignore | 2 ++ __init__.py | 4 ++++ pyfarbpracht.py | 39 +++++++++++++++++++++++++++++++++++++++ test.py | 15 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100755 __init__.py create mode 100755 pyfarbpracht.py create mode 100755 test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58af8a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.py[cod] diff --git a/__init__.py b/__init__.py new file mode 100755 index 0000000..f5afe4c --- /dev/null +++ b/__init__.py @@ -0,0 +1,4 @@ +__pkg__name = 'pyfarbpracht' +__version_ = 1 + +import pyfarbpracht diff --git a/pyfarbpracht.py b/pyfarbpracht.py new file mode 100755 index 0000000..bb5e041 --- /dev/null +++ b/pyfarbpracht.py @@ -0,0 +1,39 @@ +from os import linesep + +# print with color support +def printc(color, msg, no_newline=False, enable_color=False): + ret = str() + if enable_color: + ret += colored(color, msg) + else: + ret += msg + if no_newline: + end = '' + else: + end = linesep + print(ret, end=end) + return ret + +# print with color support +def colored(color, msg): + 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' + ret += msg + # reset color + ret += '\033[0m' + return ret diff --git a/test.py b/test.py new file mode 100755 index 0000000..3693134 --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +#!/bin/env python + +import pyfarbpracht + +def test(): + ret = pyfarbpracht.printc('green', 'green test with newline', enable_color=True) + print(ret) + ret = pyfarbpracht.printc('cyan', 'cyan but enable_color=False', enable_color=False) + print(ret) + ret = pyfarbpracht.printc('magenta', 'magenta no newline', enable_color=True, no_newline=True) + print(ret) + print(ret) + +if __name__ == '__main__': + test() -- cgit v1.2.2-1-g5e49