Skip to content

Instantly share code, notes, and snippets.

@NearHuscarl
Last active December 7, 2017 08:19
Show Gist options
  • Save NearHuscarl/a43bb36d389de50fe27b40b629997851 to your computer and use it in GitHub Desktop.
Save NearHuscarl/a43bb36d389de50fe27b40b629997851 to your computer and use it in GitHub Desktop.
print string with color using escape code for bash
#!/bin/env python
def cprint(string, color):
""" print string with escape code for bash color """
color_code = {
'black': '\x1b[30m',
'red': '\x1b[31m',
'green': '\x1b[32m',
'yellow': '\x1b[33m',
'blue': '\x1b[34m',
'magenta': '\x1b[35m',
'cyan': '\x1b[36m',
'lightgray': '\x1b[37m',
'darkgray': '\x1b[90m',
'lightred': '\x1b[91m',
'lightgreen': '\x1b[92m',
'lightyellow': '\x1b[93m',
'lightblue': '\x1b[94m',
'lightmagenta': '\x1b[95m',
'lightcyan': '\x1b[96m',
'white': '\x1b[97m',
'reset': '\x1b[0m'
}
print(color_code[color] + string + color_code['reset'])
def main():
""" main function """
cprint('test', 'black')
cprint('test', 'red')
cprint('test', 'green')
cprint('test', 'yellow')
cprint('test', 'blue')
cprint('test', 'magenta')
cprint('test', 'cyan')
cprint('test', 'lightgray')
cprint('test', 'darkgray')
cprint('test', 'lightred')
cprint('test', 'lightgreen')
cprint('test', 'lightyellow')
cprint('test', 'lightblue')
cprint('test', 'lightmagenta')
cprint('test', 'lightcyan')
cprint('test', 'white')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment