1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Abstraction API above terminal color libraries.
22 from StringIO import StringIO
24 from contextlib import contextmanager
26 from ..cli import color
27 from . import formatting
33 class TopologyStylizer(object):
34 def __init__(self, indentation=0):
35 self._str = StringIO()
36 self._indentation = indentation
38 def write(self, string):
39 self._str.write(' ' * self._indentation)
40 self._str.write(string)
41 self._str.write(os.linesep)
44 def indent(self, indentation=2):
45 self._indentation += indentation
47 self._indentation -= indentation
50 def type_style(value):
51 return Colored.blue(value, bold=True)
54 def node_style(value):
55 return Colored.red(value, bold=True)
58 def property_style(value):
59 return Colored.magenta(value, bold=True)
62 def literal_style(value):
63 return Colored.magenta(formatting.safe_repr(value))
66 def required_style(value):
67 return Colored.white(value)
70 def meta_style(value):
71 return Colored.green(value)
74 return self._str.getvalue()
77 def puts(string='', newline=True, stream=sys.stdout):
78 stream.write(_indent_string)
79 stream.write(formatting.safe_str(string))
81 stream.write(os.linesep)
87 original_indent_string = _indent_string
89 _indent_string += ' ' * size
92 _indent_string = original_indent_string
95 class Colored(object):
97 def black(string, always=False, bold=False):
98 return Colored._color(string, color.Colors.Fore.BLACK, bold)
101 def red(string, always=False, bold=False):
102 return Colored._color(string, color.Colors.Fore.RED, bold)
105 def green(string, always=False, bold=False):
106 return Colored._color(string, color.Colors.Fore.GREEN, bold)
109 def yellow(string, always=False, bold=False):
110 return Colored._color(string, color.Colors.Fore.YELLOW, bold)
113 def blue(string, always=False, bold=False):
114 return Colored._color(string, color.Colors.Fore.BLUE, bold)
117 def magenta(string, always=False, bold=False):
118 return Colored._color(string, color.Colors.Fore.MAGENTA, bold)
121 def cyan(string, always=False, bold=False):
122 return Colored._color(string, color.Colors.Fore.CYAN, bold)
125 def white(string, always=False, bold=False):
126 return Colored._color(string, color.Colors.Fore.WHITE, bold)
129 def _color(string, fore, bold):
130 return color.StringStylizer(string, color.ColorSpec(
132 style=color.Colors.Style.BRIGHT if bold else color.Colors.Style.NORMAL))