diff options
Diffstat (limited to 'pyfarbpracht')
-rwxr-xr-x | pyfarbpracht/__init__.py | 4 | ||||
-rwxr-xr-x | pyfarbpracht/pyfarbpracht.py | 73 | ||||
-rwxr-xr-x | pyfarbpracht/test.py | 22 |
3 files changed, 99 insertions, 0 deletions
diff --git a/pyfarbpracht/__init__.py b/pyfarbpracht/__init__.py new file mode 100755 index 0000000..dc03860 --- /dev/null +++ b/pyfarbpracht/__init__.py @@ -0,0 +1,4 @@ +__pkg__name = 'pyfarbpracht' +__version_ = '0.1' + +from .pyfarbpracht import * diff --git a/pyfarbpracht/pyfarbpracht.py b/pyfarbpracht/pyfarbpracht.py new file mode 100755 index 0000000..62ef308 --- /dev/null +++ b/pyfarbpracht/pyfarbpracht.py @@ -0,0 +1,73 @@ +from os import linesep + +# we can also do print(colored('green', msg), end='') for example... +# printc helps if You have a global color_enabled flag in Your program +def printc(color, msg, no_newline=False, enable_color=False): + """like print but with color support (if enabled!)""" + 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 + +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' + ret += msg + # reset color + ret += '\033[0m' + return ret + +# When data contains ANSI color escape codes convert them to BBcode format +# e.g.: Foo bar baz [\033[0;31m + text + [\033[0m +# where 31 can be 31..36 31=red, 32=green, +# 33=yellow, 34=blue, 35=magenta, 36-cyan +# 0 resets back to standard +# we can map those three parts to [color=#abcdef] + text + [/color] +def convert_ansi_to_bbcode_colors(instr): + """convert ansi escape sequences in a string to bbcode tags""" + ansi_red = '\033[0;31m' + ansi_green = '\033[0;32m' + ansi_yellow = '\033[0;33m' + ansi_blue = '\033[0;34m' + ansi_magenta = '\033[0;35m' + ansi_cyan = '\033[0;36m' + ansi_end = '\033[0m' + + bb_red = '[color=#FF0000]' + bb_green = '[color=#00FF00]' + bb_yellow = '[color=#FFFF00]' + bb_blue = '[color=#0000FF]' + bb_magenta = '[color=#FF00FF]' + bb_cyan = '[color=#00FFFF]' + bb_end = '[/color]' + + return instr.replace(ansi_red, bb_red) \ + .replace(ansi_green, bb_green) \ + .replace(ansi_yellow, bb_yellow) \ + .replace(ansi_blue, bb_blue) \ + .replace(ansi_magenta, bb_magenta) \ + .replace(ansi_cyan, bb_cyan) \ + .replace(ansi_end, bb_end) diff --git a/pyfarbpracht/test.py b/pyfarbpracht/test.py new file mode 100755 index 0000000..ac74add --- /dev/null +++ b/pyfarbpracht/test.py @@ -0,0 +1,22 @@ +#!/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) + + print('') + print('') + + print(pyfarbpracht.colored('green', 'print(colored(... test in green')) + print(pyfarbpracht.colored( \ + 'green', 'print(colored(... test in green no newline'), end='') + +if __name__ == '__main__': + test() |