terminal.py
Go to the documentation of this file.
00001 import subprocess
00002 try:
00003     from colorama import Fore, Back, init
00004     init()
00005 except ImportError:  # fallback so that the imported classes always exist
00006     class ColorFallback():
00007         def __getattr__(self, name):
00008             return ''
00009     Fore = Back = ColorFallback()
00010 
00011 rows, columns = map(int, subprocess.check_output(['stty', 'size']).split())
00012 
00013 
00014 def color_diff(diff):
00015     for line in diff:
00016         if line.startswith('+'):
00017             yield Fore.GREEN + line + Fore.RESET
00018         elif line.startswith('-'):
00019             yield Fore.RED + line + Fore.RESET
00020         elif line.startswith('^'):
00021             yield Fore.BLUE + line + Fore.RESET
00022         else:
00023             yield line
00024 
00025 
00026 def color_header(s, fore='WHITE', back='BLUE'):
00027     header = ''
00028     line = '+' + ('-' * (columns - 2)) + '+'
00029     header += getattr(Fore, fore) + getattr(Back, back) + line
00030     n = columns - len(s) - 3
00031     header += '| ' + s + ' ' * n + '|'
00032     header += line + Back.RESET + Fore.RESET
00033     return header
00034 
00035 
00036 def color_text(s, fore='YELLOW'):
00037     return getattr(Fore, fore) + s + Fore.RESET
00038 
00039 
00040 def query_yes_no(question, default="no"):
00041     """Ask a yes/no question via raw_input() and return their answer.
00042 
00043     Based on http://code.activestate.com/recipes/577058/
00044 
00045     "question" is a string that is presented to the user.
00046     "default" is the presumed answer if the user just hits <Enter>.
00047         It must be "yes" (the default), "no" or None (meaning
00048         an answer is required of the user).
00049 
00050     The "answer" return value is True for "yes" or False for "no".
00051     """
00052     valid = {"yes": True, "y": True, "ye": True,
00053              "no": False, "n": False}
00054     if default is None:
00055         prompt = " [y/n] "
00056     elif default == "yes":
00057         prompt = " [Y/n] "
00058     elif default == "no":
00059         prompt = " [y/N] "
00060     else:
00061         raise ValueError("invalid default answer: '%s'" % default)
00062 
00063     while True:
00064         choice = raw_input(color_text(question + prompt)).lower()
00065         if default is not None and choice == '':
00066             return valid[default]
00067         elif choice in valid:
00068             return valid[choice]
00069         else:
00070             print("Please respond with 'yes' or 'no' (or 'y' or 'n').")


roscompile
Author(s):
autogenerated on Wed Jun 19 2019 19:21:36