Skip to content

Instantly share code, notes, and snippets.

@squizzi
Last active March 24, 2017 18:24
Show Gist options
  • Save squizzi/9b2cdef6a6761298f60f9bd22b56c2c5 to your computer and use it in GitHub Desktop.
Save squizzi/9b2cdef6a6761298f60f9bd22b56c2c5 to your computer and use it in GitHub Desktop.
A cute little message printer that doesn't need any special libraries to run
#!/usr/bin/env python
# Copyright (C) 2017, Kyle Squizzato <ksquizz@gmail.com>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
Terminal colors
"""
class colors:
INFO = '\033[95m' # purple
OKBLUE = '\033[94m' # blue
OKGREEN = '\033[92m' # green
WARNING = '\033[93m' # yellow
ERROR = '\033[91m' # red
ENDC = '\033[0m' # white
BOLD = '\033[1m' # bold
UNDERLINE = '\033[4m' # underline
"""
Construct a message with the following syntax:
[STATE] Message contents
with optional colorization using the above colors class
"""
def print_message(message, state=None, color=None):
# color should be a member of above colors class, ie. colors.OKGREEN
if color == None:
# if no color is supplied use white
color = colors.ENDC
else:
# calls to this function must accept an item from the colors class
# above only
try:
color = color
except AttributeError as e:
raise
# if no state is supplied just build message with provided color
if state == None:
constructed_state = color + ''
else:
# else uppercase it
state = state.upper()
constructed_state = color + ('[{0}]'.format(state))
# construct message from constructed_state
constructed_message = str(constructed_state).ljust(20) + colors.ENDC + message
return constructed_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment