color.py
Go to the documentation of this file.
1 # Copyright (c) 2015, Bielefeld University
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of Bielefeld University
13 # nor the names of its contributors may be used to endorse or promote
14 # products derived from this software without specific prior
15 # written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 
29 from __future__ import print_function
30 import sys
31 
32 # bold colors
33 _ansi = {'red': 91, 'yellow': 93}
34 
35 
36 def is_tty(stream): # taken from catkin_tools/common.py
37  """Returns True if the given stream is a tty, else False"""
38  return hasattr(stream, 'isatty') and stream.isatty()
39 
40 
41 def colorize(msg, color, file=sys.stderr, alt_text=None):
42  try:
43  color = _ansi[color]
44  except KeyError:
45  pass
46 
47  if color and is_tty(file):
48  return '\033[%dm%s\033[0m' % (color, msg)
49  elif alt_text:
50  return '%s%s' % (alt_text, msg)
51  else:
52  return msg
53 
54 
55 def message(msg, *args, **kwargs):
56  file = kwargs.get('file', sys.stderr)
57  alt_text = kwargs.get('alt_text', None)
58  color = kwargs.get('color', None)
59  print(colorize(msg, color, file, alt_text), *args, file=file)
60 
61 
62 def warning(*args, **kwargs):
63  defaults = dict(file=sys.stderr, alt_text='warning: ', color='yellow')
64  defaults.update(kwargs)
65  message(*args, **defaults)
66 
67 
68 def error(*args, **kwargs):
69  defaults = dict(file=sys.stderr, alt_text='error: ', color='red')
70  defaults.update(kwargs)
71  message(*args, **defaults)
def is_tty(stream)
Definition: color.py:36
def error(args, kwargs)
Definition: color.py:68
def message(msg, args, kwargs)
Definition: color.py:55
def warning(args, kwargs)
Definition: color.py:62
def colorize(msg, color, file=sys.stderr, alt_text=None)
Definition: color.py:41


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Thu May 25 2023 02:45:08