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 
12 def color_diff(diff):
13  for line in diff:
14  if line.startswith('+'):
15  yield Fore.GREEN + line + Fore.RESET
16  elif line.startswith('-'):
17  yield Fore.RED + line + Fore.RESET
18  elif line.startswith('^'):
19  yield Fore.BLUE + line + Fore.RESET
20  else:
21  yield line
22 
23 
24 COLUMNS = None
25 
26 
27 def color_header(s, fore='WHITE', back='BLUE'):
28  global COLUMNS
29  if not COLUMNS:
30  COLUMNS = list(map(int, subprocess.check_output(['stty', 'size']).split()))[1]
31  header = ''
32  line = '+' + ('-' * (COLUMNS - 2)) + '+'
33  header += getattr(Fore, fore) + getattr(Back, back) + line
34  n = COLUMNS - len(s) - 3
35  header += '| ' + s + ' ' * n + '|'
36  header += line + Back.RESET + Fore.RESET
37  return header
38 
39 
40 def color_text(s, fore='YELLOW'):
41  return getattr(Fore, fore) + s + Fore.RESET
42 
43 
44 # Python2/3 input function
45 try:
46  my_input = raw_input
47 except NameError:
48  my_input = input
49 
50 
51 def query_yes_no(question, default='no'):
52  """Ask a yes/no question via my_input() and return their answer.
53 
54  Based on http://code.activestate.com/recipes/577058/
55 
56  'question' is a string that is presented to the user.
57  'default' is the presumed answer if the user just hits <Enter>.
58  It must be 'yes' (the default), 'no' or None (meaning
59  an answer is required of the user).
60 
61  The 'answer' return value is True for 'yes' or False for 'no'.
62  """
63  valid = {'yes': True, 'y': True, 'ye': True,
64  'no': False, 'n': False}
65  if default is None:
66  prompt = ' [y/n] '
67  elif default == 'yes':
68  prompt = ' [Y/n] '
69  elif default == 'no':
70  prompt = ' [y/N] '
71  else:
72  raise ValueError("invalid default answer: '%s'" % default)
73 
74  while True:
75  choice = my_input(color_text(question + prompt)).lower()
76  if default is not None and choice == '':
77  return valid[default]
78  elif choice in valid:
79  return valid[choice]
80  else:
81  print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
def color_diff(diff)
Definition: terminal.py:12
def query_yes_no(question, default='no')
Definition: terminal.py:51
def color_text(s, fore='YELLOW')
Definition: terminal.py:40
def color_header(s, fore='WHITE', back='BLUE')
Definition: terminal.py:27
def __getattr__(self, name)
Definition: terminal.py:7


roscompile
Author(s):
autogenerated on Wed Mar 3 2021 03:56:01