terminal.py
Go to the documentation of this file.
1 import subprocess
2 try:
3  from colorama import Fore, Back, init
4  init()
5 except ImportError: # fallback so that the imported classes always exist
6  class ColorFallback():
7  def __getattr__(self, name):
8  return ''
9  Fore = Back = ColorFallback()
10 
11 rows, columns = map(int, subprocess.check_output(['stty', 'size']).split())
12 
13 
14 def color_diff(diff):
15  for line in diff:
16  if line.startswith('+'):
17  yield Fore.GREEN + line + Fore.RESET
18  elif line.startswith('-'):
19  yield Fore.RED + line + Fore.RESET
20  elif line.startswith('^'):
21  yield Fore.BLUE + line + Fore.RESET
22  else:
23  yield line
24 
25 
26 def color_header(s, fore='WHITE', back='BLUE'):
27  header = ''
28  line = '+' + ('-' * (columns - 2)) + '+'
29  header += getattr(Fore, fore) + getattr(Back, back) + line
30  n = columns - len(s) - 3
31  header += '| ' + s + ' ' * n + '|'
32  header += line + Back.RESET + Fore.RESET
33  return header
34 
35 
36 def color_text(s, fore='YELLOW'):
37  return getattr(Fore, fore) + s + Fore.RESET
38 
39 
40 def query_yes_no(question, default="no"):
41  """Ask a yes/no question via raw_input() and return their answer.
42 
43  Based on http://code.activestate.com/recipes/577058/
44 
45  "question" is a string that is presented to the user.
46  "default" is the presumed answer if the user just hits <Enter>.
47  It must be "yes" (the default), "no" or None (meaning
48  an answer is required of the user).
49 
50  The "answer" return value is True for "yes" or False for "no".
51  """
52  valid = {"yes": True, "y": True, "ye": True,
53  "no": False, "n": False}
54  if default is None:
55  prompt = " [y/n] "
56  elif default == "yes":
57  prompt = " [Y/n] "
58  elif default == "no":
59  prompt = " [y/N] "
60  else:
61  raise ValueError("invalid default answer: '%s'" % default)
62 
63  while True:
64  choice = raw_input(color_text(question + prompt)).lower()
65  if default is not None and choice == '':
66  return valid[default]
67  elif choice in valid:
68  return valid[choice]
69  else:
70  print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
def color_diff(diff)
Definition: terminal.py:14
def color_text(s, fore='YELLOW')
Definition: terminal.py:36
def query_yes_no(question, default="no")
Definition: terminal.py:40
def color_header(s, fore='WHITE', back='BLUE')
Definition: terminal.py:26
def __getattr__(self, name)
Definition: terminal.py:7


roscompile
Author(s):
autogenerated on Wed Jun 19 2019 19:56:53