Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python coloring for linux, based on this answer http://stackoverflow.com/a/26445590/3191896
def color(text, **user_styles):
styles = {
# styles
'reset': '\033[0m',
'bold': '\033[01m',
'disabled': '\033[02m',
'underline': '\033[04m',
'reverse': '\033[07m',
'strike_through': '\033[09m',
'invisible': '\033[08m',
# text colors
'fg_black': '\033[30m',
'fg_red': '\033[31m',
'fg_green': '\033[32m',
'fg_orange': '\033[33m',
'fg_blue': '\033[34m',
'fg_purple': '\033[35m',
'fg_cyan': '\033[36m',
'fg_light_grey': '\033[37m',
'fg_dark_grey': '\033[90m',
'fg_light_red': '\033[91m',
'fg_light_green': '\033[92m',
'fg_yellow': '\033[93m',
'fg_light_blue': '\033[94m',
'fg_pink': '\033[95m',
'fg_light_cyan': '\033[96m',
# background colors
'bg_black': '\033[40m',
'bg_red': '\033[41m',
'bg_green': '\033[42m',
'bg_orange': '\033[43m',
'bg_blue': '\033[44m',
'bg_purple': '\033[45m',
'bg_cyan': '\033[46m',
'bg_light_grey': '\033[47m'
}
color_text = ''
for style in user_styles:
try:
color_text += styles[style]
except KeyError:
raise KeyError('def color: parameter `{}` does not exist'.format(style))
color_text += text
return '\033[0m{}\033[0m'.format(color_text)
def error(text):
return color(text, bold=True, fg_red=True)
def warning(text):
return color(text, bold=True, fg_orange=True)
def success(text):
return color(text, fg_green=True)
#!/usr/bin/python
import colors
def main():
print colors.error('sorry')
if __name__ == '__main__':
main()
@kxmatejka

This comment has been minimized.

Copy link

@kxmatejka kxmatejka commented May 6, 2015

good idea, but the implementation is furious... what abount

def color(text, *user_styles):

    styles = {
            # styles
            'reset': '\033[0m',
            'bold': '\033[01m',
            'disabled': '\033[02m',
            'underline': '\033[04m',
            'reverse': '\033[07m',
            'strike_through': '\033[09m',
            'invisible': '\033[08m',
            # text colors
            'fg_black': '\033[30m',
            'fg_red': '\033[31m',
            'fg_green': '\033[32m',
            'fg_orange': '\033[33m',
            'fg_blue': '\033[34m',
            'fg_purple': '\033[35m',
            'fg_cyan': '\033[36m',
            'fg_light_grey': '\033[37m',
            'fg_dark_grey': '\033[90m',
            'fg_light_red': '\033[91m',
            'fg_light_green': '\033[92m',
            'fg_yellow': '\033[93m',
            'fg_light_blue': '\033[94m',
            'fg_pink': '\033[95m',
            'fg_light_cyan': '\033[96m',
            # background colors
            'bg_black': '\033[40m',
            'bg_red': '\033[41m',
            'bg_green': '\033[42m',
            'bg_orange': '\033[43m',
            'bg_blue': '\033[44m',
            'bg_purple': '\033[45m',
            'bg_cyan': '\033[46m',
            'bg_light_grey': '\033[47m'
    }

    color_text = ''
    for style in user_styles:
        try:
            color_text += styles[style]
        except KeyError:
            return 'def color: parameter {} does not exist'.format(style)
    color_text += text
    return '\033[0m{}\033[0m'.format(color_text)
@jossef

This comment has been minimized.

Copy link
Owner Author

@jossef jossef commented May 15, 2015

@kxmatejka. i love it. great suggestion 👍

@arulrajnet

This comment has been minimized.

Copy link

@arulrajnet arulrajnet commented Aug 17, 2015

here its my forked version of this https://gist.github.com/arulrajnet/47ff2674ad9da6dcac00

@muammar

This comment has been minimized.

Copy link

@muammar muammar commented May 4, 2016

Thanks for this little module. Works very well. 👍

@lpanebr

This comment has been minimized.

Copy link

@lpanebr lpanebr commented Sep 27, 2016

thanks

@crackjack

This comment has been minimized.

Copy link

@crackjack crackjack commented Jan 11, 2017

awesome +1

@anson2416

This comment has been minimized.

Copy link

@anson2416 anson2416 commented Mar 9, 2017

Any idea how to change the color in windowns using VSCode? Thanks.

@Schnittchen1423

This comment has been minimized.

Copy link

@Schnittchen1423 Schnittchen1423 commented Nov 9, 2020

It didn't help me. Is it possible that your codes aren't working on Mac?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment