def addstr_colored(self, text: str, y: Optional[int] = None, x: Optional[int] = None) -> None: """ Write a string on the window, setting the attributes as they are in the string. For example: \x19bhello → hello in bold \x191}Bonj\x192}our → 'Bonj' in red and 'our' in green next_attr_char is the \x19 delimiter attr_char is the char following it, it can be one of 'u', 'b', 'i', 'c[0-9]' """ if y is not None and x is not None: self.move(y, x) next_attr_char = text.find(FORMAT_CHAR) attr_italic = curses.A_ITALIC if hasattr( curses, 'A_ITALIC') else curses.A_REVERSE while next_attr_char != -1 and text: if next_attr_char + 1 < len(text): attr_char = text[next_attr_char + 1].lower() else: attr_char = str() if next_attr_char != 0: self.addstr(text[:next_attr_char]) if attr_char == 'o': self._win.attrset(0) elif attr_char == 'u': self._win.attron(curses.A_UNDERLINE) elif attr_char == 'b': self._win.attron(curses.A_BOLD) elif attr_char == 'i': self._win.attron(attr_italic) if (attr_char in string.digits or attr_char == '-') and attr_char != '': color_str = text[next_attr_char + 1:text.find('}', next_attr_char)] if ',' in color_str: tup, char = read_tuple(color_str) self._win.attron(to_curses_attr(tup)) if char: if char == 'o': self._win.attrset(0) elif char == 'u': self._win.attron(curses.A_UNDERLINE) elif char == 'b': self._win.attron(curses.A_BOLD) elif char == 'i': self._win.attron(attr_italic) else: # this will reset previous bold/uderline sequences if any was used self._win.attroff(curses.A_UNDERLINE) self._win.attroff(curses.A_BOLD) elif color_str: self._win.attron(to_curses_attr((int(color_str), -1))) text = text[next_attr_char + len(color_str) + 2:] else: text = text[next_attr_char + 2:] next_attr_char = text.find(FORMAT_CHAR) self.addstr(text)
def test_read_tuple(): assert read_tuple('1,-1,u') == ((1, -1), 'u') assert read_tuple('1,2') == ((1, 2), '\0') with pytest.raises(IndexError): read_tuple('1') with pytest.raises(ValueError): read_tuple('toto')