def test_color_name(self): color = Color('bright red on white') self.assertEqual(color.get_color_name(), 'bright red on white') color = Color('white on cyan') self.assertEqual('normal white on cyan', color.get_color_name()) color = Color('magenta') self.assertEqual('normal magenta on black', color.get_color_name())
def __init__(self, colors=None, attributes=None, description=None,\ delimiter=' | ', delimiter_colors=None): """ Input: colors: list of kaleidoscope color names to use in styling the object's attributes attributes: list of names of attributes to render you can optionally use a list of 2 or 3-tuples where: the first value is the attribute name the second value is the length of the attribute rendering the third value is the NSID of a formatter in the formatter namespace The AttributeSpec type exists as well to be passed in here description: the description of this object model spec delimiter: string to place between attributes on a single line delimiter_color: color of delimiter string Notes: All parameters are optional, but you probably want to fill in the attributes before using this """ log = LoggerAdapter(logger, {'name_ext': 'ObjectModelSpec.__init__'}) msg = "Entered: colors: {} | attributes: {}".format(colors, attributes) msg += " | description: {} | delimiter: {}".format( description, delimiter) msg += " | delimiter_colors: {}".format(delimiter_colors) log.debug(msg) self.delimiter = str(delimiter) self.description = description if colors: _colors = list() for _color in colors: _colors.append(Color(_color)) self.colors = cycle(_colors) else: self.colors = repeat(None) if delimiter_colors: _delimiter_colors = list() for _dcolor in delimiter_colors: _delimiter_colors.append(Color(_dcolor)) self.delimiter_colors = cycle(_delimiter_colors) else: self.delimiter_colors = copy.copy(self.colors) #- locally store parsed attributes as AttributeSpec objects if attributes: if not isinstance(attributes, Iterable): attributes = [attributes] log.debug("parsing attributes: {}".format(attributes)) self.attributes = self.parse_attributes(attributes) else: log.info("spec has no attributes specified.") self.attributes = None log.debug("initialized: {}".format(self))
def test_list(self): am = AttributeModel(self.simple, 'list_attribute', color=Color('green')) view = am.render_view() expected = '\x1b[32m\x1b[40m\x1b[22m[0, 1, 2, 3, 4, 5]\x1b[0m' self.assertEqual(view.get_render_output(), expected) view.render()
def test_simple_color(self): am = AttributeModel(self.simple, 'simple_attribute', color=Color('green')) view = am.render_view() expected = '\x1b[32m\x1b[40m\x1b[22m_simple_value_\x1b[0m' self.assertEqual(view.get_render_output(), expected) view.render()
def test_init_color(self): color = Color('bright green on black') self.assertEqual(color.foreground, 'green') self.assertEqual(color.style, 'bright') self.assertEqual(color.background, 'black') color = Color('red on black') self.assertEqual(color.foreground, 'red') self.assertEqual(color.style, 'normal') self.assertEqual(color.background, 'black') color = Color('red on yellow') self.assertEqual(color.foreground, 'red') self.assertEqual(color.style, 'normal') self.assertEqual(color.background, 'yellow') color = Color('cyan') self.assertEqual(color.foreground, 'cyan') self.assertEqual(color.style, 'normal') self.assertEqual(color.background, 'black')
def set_colors(self, colors): """Abstract Method Implementation. Sets the colors for the Group""" log = LoggerAdapter(logger, {'name_ext': 'GroupModel.set_colors'}) log.debug("Entering") if colors: #-TODO: is there a nicer way to do these checks? #- want to make sure that colors is a cycle or a repeater if isinstance(colors, Sequence) or\ isinstance(colors, Generator): self.colors = cycle([Color(x) for x in colors]) elif isinstance(colors, itertools.cycle) or\ isinstance(colors, itertools.repeat): self.colors = colors else: self.colors = repeat(None) log.debug("Exiting: self.colors is: {}".format(self.colors)) return
def make_default_colors(self): """ return a list of the default colors to use for an object model """ default_colors = [Color('bright green'), Color('green'), Color('dim green'), Color('green')] return default_colors
def set_color(self, color): """Takes a name or a color object and calls the correct method to set both the color_name and the color""" self.color = Color(color)
def test_printed_color(self): color = Color('bright cyan on black') text = '[bright cyan on black test text blob]' colored_text = color.terminal_codes() + text + color.terminal_reset self.assertEqual(colored_text, '\x1b[36m\x1b[40m\x1b[1m[bright cyan on black test text blob]\x1b[0m') print(colored_text)