Beispiel #1
0
 def addstr_colored(self, text, y=None, x=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', 'c[0-9]'
     """
     if y is not None and x is not None:
         self.move(y, x)
     next_attr_char = text.find(FORMAT_CHAR)
     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)
         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)
                 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)
Beispiel #2
0
 def addstr_colored(self, text, y=None, x=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', 'c[0-9]'
     """
     if y is not None and x is not None:
         self.move(y, x)
     next_attr_char = text.find(FORMAT_CHAR)
     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)
         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)
                 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)
Beispiel #3
0
def test_read_tuple():
    assert read_tuple('1,-1,u') == ((1, -1), 'u')
    assert read_tuple('1,2') == ((1, 2), None)

    with pytest.raises(IndexError):
        read_tuple('1')

    with pytest.raises(ValueError):
        read_tuple('toto')
Beispiel #4
0
def test_read_tuple():
    assert read_tuple('1,-1,u') == ((1, -1), 'u')
    assert read_tuple('1,2') == ((1, 2), None)

    with pytest.raises(IndexError):
        read_tuple('1')

    with pytest.raises(ValueError):
        read_tuple('toto')