Path: blob/master/thirdparty/termcolor/termcolor.py
2992 views
# coding: utf-81# Copyright (c) 2008-2011 Volvox Development Team2#3# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to deal5# in the Software without restriction, including without limitation the rights6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7# copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.12#13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19# THE SOFTWARE.20#21# Author: Konstantin Lepa <[email protected]>2223"""ANSII Color formatting for output in terminal."""2425from __future__ import print_function26import os272829__ALL__ = [ 'colored', 'cprint' ]3031VERSION = (1, 1, 0)3233ATTRIBUTES = dict(34list(zip([35'bold',36'dark',37'',38'underline',39'blink',40'',41'reverse',42'concealed'43],44list(range(1, 9))45))46)47del ATTRIBUTES['']484950HIGHLIGHTS = dict(51list(zip([52'on_grey',53'on_red',54'on_green',55'on_yellow',56'on_blue',57'on_magenta',58'on_cyan',59'on_white'60],61list(range(40, 48))62))63)646566COLORS = dict(67list(zip([68'grey',69'red',70'green',71'yellow',72'blue',73'magenta',74'cyan',75'white',76],77list(range(30, 38))78))79)8081COLORS.update(dict(("light%s" % color, COLORS[color] + 60) for color in COLORS))8283# Reference: https://misc.flogisoft.com/bash/tip_colors_and_formatting84COLORS["lightgrey"] = 3785COLORS["darkgrey"] = 908687RESET = '\033[0m'888990def colored(text, color=None, on_color=None, attrs=None):91"""Colorize text.9293Available text colors:94red, green, yellow, blue, magenta, cyan, white.9596Available text highlights:97on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.9899Available attributes:100bold, dark, underline, blink, reverse, concealed.101102Example:103colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])104colored('Hello, World!', 'green')105"""106if os.getenv('ANSI_COLORS_DISABLED') is None:107fmt_str = '\033[%dm%s'108if color is not None:109text = fmt_str % (COLORS[color], text)110111if on_color is not None:112text = fmt_str % (HIGHLIGHTS[on_color], text)113114if attrs is not None:115for attr in attrs:116text = fmt_str % (ATTRIBUTES[attr], text)117118text += RESET119return text120121122def cprint(text, color=None, on_color=None, attrs=None, **kwargs):123"""Print colorize text.124125It accepts arguments of print function.126"""127128print((colored(text, color, on_color, attrs)), **kwargs)129130131if __name__ == '__main__':132print('Current terminal type: %s' % os.getenv('TERM'))133print('Test basic colors:')134cprint('Grey color', 'grey')135cprint('Red color', 'red')136cprint('Green color', 'green')137cprint('Yellow color', 'yellow')138cprint('Blue color', 'blue')139cprint('Magenta color', 'magenta')140cprint('Cyan color', 'cyan')141cprint('White color', 'white')142print(('-' * 78))143144print('Test highlights:')145cprint('On grey color', on_color='on_grey')146cprint('On red color', on_color='on_red')147cprint('On green color', on_color='on_green')148cprint('On yellow color', on_color='on_yellow')149cprint('On blue color', on_color='on_blue')150cprint('On magenta color', on_color='on_magenta')151cprint('On cyan color', on_color='on_cyan')152cprint('On white color', color='grey', on_color='on_white')153print('-' * 78)154155print('Test attributes:')156cprint('Bold grey color', 'grey', attrs=['bold'])157cprint('Dark red color', 'red', attrs=['dark'])158cprint('Underline green color', 'green', attrs=['underline'])159cprint('Blink yellow color', 'yellow', attrs=['blink'])160cprint('Reversed blue color', 'blue', attrs=['reverse'])161cprint('Concealed Magenta color', 'magenta', attrs=['concealed'])162cprint('Bold underline reverse cyan color', 'cyan',163attrs=['bold', 'underline', 'reverse'])164cprint('Dark blink concealed white color', 'white',165attrs=['dark', 'blink', 'concealed'])166print(('-' * 78))167168print('Test mixing:')169cprint('Underline red on grey color', 'red', 'on_grey',170['underline'])171cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])172173174175