pyterm is tool designed to easy the use of colors, formatting and positioning of text in a terminal without the use of curses.
website/docs: http://pythonhosted.org/pyterm
source/issues: https://bitbucket.org/schettino72/pyterm
>>> # pyterm - Color manipulation tutorial >>> from pyterm import Term, escape >>> >>> >>> # create a terminal instance >>> term = Term() >>> >>> >>> # you can check the code for a capability in your terminal >>> print(escape(term.codes['BLUE'])) \x1b[34m >>> print(escape(term['BLUE'])) \x1b[34m >>> >>> >>> # and check all the available capabilities >>> print(term.codes.keys()) ['REVERSE', 'NORMAL', 'DEFAULT', 'YELLOW', 'DOWN', 'CLEAR_SCREEN', 'BG_WHITE', 'GREEN', 'BG_YELLOW', 'CYAN', 'RED', 'BG_BLACK', 'BG_RED', 'A_BG_COLOR', 'BLACK', 'UNDERLINE', 'A_COLOR', 'LEFT', 'BOLD', 'BG_MAGENTA', 'WHITE', 'BG_GREEN', 'BLUE', 'RIGHT', 'CLEAR_EOL', 'UP', 'BG_CYAN', 'CLEAR_EOS', 'BOL', 'MAGENTA', 'BG_BLUE'] >>> >>> >>> # to print something call your terminal with the desired string >>> term('a string in normal colors\n') a string in normal colors >>> >>> >>> # to add a color access the capability name as a property >>> term.YELLOW('a yellow line\n') a yellow line >>> >>> >>> # note how the colors are reset after any ouput >>> term('again in normal colors\n') again in normal colors >>> >>> >>> # you can chain codes and output to a stream >>> term.BOLD.BG_MAGENTA.GREEN("bit of green").UNDERLINE.RED(" and red\n") bit of green and red >>> >>> >>> # you can create new named "styles" combining different codes >>> term.set_style('ERROR', ['RED', 'UNDERLINE']) >>> term.ERROR('fail\n') fail >>> >>> >>> # by default every output is flushed, you can disable that with a parameter >>> term.GREEN('part one -> \n', flush=False).GREEN('part two \n') part one -> part two >>> >>> >>> # the default colors can be configured on terminal creation >>> term2 = Term(start_code=('BLUE', 'BOLD')) >>> term2('this is the default font\n') this is the default font >>> term2.NORMAL.RED('a bit of red\n') a bit of red >>> term2('back to the default (blue)\n') back to the default (blue) >>> >>> >>> # by default the stream is sent to stdout (sys.stdout) >>> # If the selected stream is not a tty all codes are disabled >>> with open('/tmp/pyt.txt', 'w') as my_file: ... term3 = Term(stream=my_file) ... term3.RED('ignore color when writting to file') ... >>> with open('/tmp/pyt.txt', 'r') as my_file: ... print(my_file.read()) ... ignore color when writting to file >>> >>> >>> # to force the use of colors use `color=True` >>> with open('/tmp/pyt.txt', 'w') as my_file: ... term3 = Term(stream=my_file, color=True) ... term3.RED('force color when writting to file') ... >>> with open('/tmp/pyt.txt', 'r') as my_file: ... print(my_file.read()) ... force color when writting to file >>> >>> >>> # you can also force disable the use of colors >>> term4 = Term(color=False) >>> term4.RED('dont use colors!\n') dont use colors! >>> >>> >>> # check the demo to see the colors and available capabilities >>> term.demo() ANSI COLORS BLACK bold reverse underline bg_yellow bold+reverse RED bold reverse underline bg_yellow bold+reverse GREEN bold reverse underline bg_yellow bold+reverse YELLOW bold reverse underline bg_yellow bold+reverse BLUE bold reverse underline bg_yellow bold+reverse MAGENTA bold reverse underline bg_yellow bold+reverse CYAN bold reverse underline bg_yellow bold+reverse WHITE bold reverse underline bg_yellow bold+reverse | NAME | CODE | VALUE | | BOL | cr | \r | | UP | cuu1 | \x1b[A | | DOWN | cud1 | \n | | LEFT | cub1 | \x08 | | RIGHT | cuf1 | \x1b[C | | CLEAR_SCREEN | clear | \x1b[H\x1b[2J | | CLEAR_EOL | el | \x1b[K | | CLEAR_EOS | ed | \x1b[J | | BOLD | bold | \x1b[1m | | REVERSE | rev | \x1b[7m | | UNDERLINE | smul | \x1b[4m | | NORMAL | sgr0 | \x1b[m | | A_COLOR | setaf | \x1b[3%p1%dm | | A_BG_COLOR | setab | \x1b[4%p1%dm | --------------------------------------------------