def test_draw_wcwidth(): # Example from https://github.com/selectel/pyte/issues/9 screen = Screen(10, 1) for char in "コンニチハ": screen.draw(char) assert screen.cursor.x == screen.columns
def test_report_device_attributes(): screen = Screen(10, 10) acc = [] screen.write_process_input = acc.append screen.report_device_attributes() assert acc.pop() == ctrl.CSI + "?6c"
def test_erase_character(): screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.erase_characters(2) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == [" m", "is ", "foo"] assert screen[0] == [ screen.default_char, screen.default_char, Char("m", fg="red") ] screen.cursor.y, screen.cursor.x = 2, 2 screen.erase_characters() assert (screen.cursor.y, screen.cursor.x) == (2, 2) assert screen.display == [" m", "is ", "fo "] screen.cursor.y, screen.cursor.x = 1, 1 screen.erase_characters(0) assert (screen.cursor.y, screen.cursor.x) == (1, 1) assert screen.display == [" m", "i ", "fo "] # ! extreme cases. screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.cursor.x = 1 screen.erase_characters(3) assert (screen.cursor.y, screen.cursor.x) == (0, 1) assert screen.display == ["1 5"] assert screen[0] == [ Char("1", fg="red"), screen.default_char, screen.default_char, screen.default_char, Char("5", "red") ] screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.cursor.x = 2 screen.erase_characters(10) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == ["12 "] assert screen[0] == [ Char("1", fg="red"), Char("2", fg="red"), screen.default_char, screen.default_char, screen.default_char ] screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.erase_characters(4) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == [" 5"] assert screen[0] == [ screen.default_char, screen.default_char, screen.default_char, screen.default_char, Char("5", fg="red") ]
def test_remove_non_existant_attribute(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(24) # underline-off. assert screen == [[screen.default_char, screen.default_char]] * 2 assert not screen.cursor.attrs.underscore
def test_insert_characters(): screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) # a) normal case cursor = copy.copy(screen.cursor) screen.insert_characters(2) assert (screen.cursor.y, screen.cursor.x) == (cursor.y, cursor.x) assert screen[0] == [ screen.default_char, screen.default_char, Char("s", fg="red") ] # b) now inserting from the middle of the line screen.cursor.y, screen.cursor.x = 2, 1 screen.insert_characters(1) assert screen[2] == [Char("f"), screen.default_char, Char("o")] # c) inserting more than we have screen.insert_characters(10) assert screen[2] == [Char("f"), screen.default_char, screen.default_char] # d) 0 is 1 screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.cursor_position() screen.insert_characters() assert screen[0] == [screen.default_char, Char("s", fg="red"), Char("a", fg="red")] screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.cursor_position() screen.insert_characters(1) assert screen[0] == [screen.default_char, Char("s", fg="red"), Char("a", fg="red")]
def test_multi_attribs(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) screen.select_graphic_rendition(3) assert screen.cursor.attrs.bold assert screen.cursor.attrs.italics
def test_draw_width0_decawm_off(): screen = Screen(10, 1) screen.reset_mode(mo.DECAWM) screen.draw(" コンニチハ".encode("utf-8")) assert screen.cursor.x == screen.columns # The following should not advance the cursor. screen.draw("\N{ZERO WIDTH SPACE}".encode("utf-8")) screen.draw("\u0007".encode("utf-8")) # DELETE. assert screen.cursor.x == screen.columns
def test_private_report_device_attributes(): # Some console apps (e.g. ADOM) might add ``?`` to the DA request, # even though the VT102/VT220 spec does not allow this. screen = Screen(10, 10) stream = Stream(screen) acc = [] screen.write_process_input = acc.append stream.feed(ctrl.CSI + b"?0c") assert acc.pop() == ctrl.CSI + b"?6c"
def test_colors(): screen = Screen(2, 2) screen.select_graphic_rendition(30) screen.select_graphic_rendition(40) assert screen.cursor.attrs.fg == "black" assert screen.cursor.attrs.bg == "black" screen.select_graphic_rendition(31) assert screen.cursor.attrs.fg == "red" assert screen.cursor.attrs.bg == "black"
def test_colors_aixterm(): # See issue #57 on GitHub. screen = Screen(2, 2) # a) foreground color. screen.select_graphic_rendition(94) assert screen.cursor.attrs.fg == "blue" assert screen.cursor.attrs.bold # b) background color. screen.select_graphic_rendition(104) assert screen.cursor.attrs.bg == "blue" assert screen.cursor.attrs.bold
def test_hide_cursor(): screen = Screen(10, 10) # DECTCEM is set by default. assert mo.DECTCEM in screen.mode assert not screen.cursor.hidden # a) resetting DECTCEM hides the cursor. screen.reset_mode(mo.DECTCEM) assert screen.cursor.hidden # b) ... and it's back! screen.set_mode(mo.DECTCEM) assert not screen.cursor.hidden
def test_attributes(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) # Bold # Still default, since we haven't written anything. assert screen == [[screen.default_char, screen.default_char]] * 2 assert screen.cursor.attrs.bold screen.draw("f") assert screen == [ [Char("f", "default", "default", bold=True), screen.default_char], [screen.default_char, screen.default_char] ]
def test_draw_width0_irm(): screen = Screen(10, 1) screen.set_mode(mo.IRM) # The following should not insert any blanks. screen.draw("\N{ZERO WIDTH SPACE}".encode("utf-8")) screen.draw("\u0007".encode("utf-8")) # DELETE. assert screen.display == [" " * screen.columns]
def test_erase_in_line(): screen = update(Screen(5, 5), ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.cursor_position(1, 3) # a) erase from cursor to the end of line screen.erase_in_line(0) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == ["sa ", "s foo", "but a", "re yo", "u? "] assert screen.buffer[0] == [ Char("s", fg="red"), Char("a", fg="red"), screen.default_char, screen.default_char, screen.default_char ] # b) erase from the beginning of the line to the cursor screen = update(screen, ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.erase_in_line(1) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == [" i", "s foo", "but a", "re yo", "u? "] assert screen.buffer[0] == [ screen.default_char, screen.default_char, screen.default_char, Char(" ", fg="red"), Char("i", fg="red") ] # c) erase the entire line screen = update(screen, ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.erase_in_line(2) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == [" ", "s foo", "but a", "re yo", "u? "] assert screen.buffer[0] == [screen.default_char] * 5
def __init__(self, parent): Screen.__init__(self, 80, 24) QTextEdit.__init__(self, parent=None) QApplication.instance().setCursorFlashTime(0) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setFont(QFontDatabase.systemFont(QFontDatabase.FixedFont)) self.setFontPointSize(12) self._text_width = self.fontMetrics().maxWidth() + 1.5 # This could be cleaner self._text_height = self.fontMetrics().lineSpacing() + 2.3 # This could be cleaner too # self.setStyleSheet('background-color: black; color: white') self._parent = parent self._conn = None self._term_em = None self._timer = QTimer(self) self._angle_delta = 0
def test_draw_cp437(): screen = Screen(5, 1) assert screen.charset == 0 screen.define_charset(b"U", b"(") screen.select_other_charset(b"@") screen.draw("α ± ε".encode("cp437")) assert screen.display == ["α ± ε"]
def test_screen_set_icon_name_title(): screen = Screen(10, 1) screen.select_other_charset(b"@") text = "±" screen.set_icon_name(text.encode("latin-1")) assert screen.icon_name == text screen.set_title(text.encode("latin-1")) assert screen.title == text
def test_colors24bit(): screen = Screen(2, 2) # a) OK-case screen.select_graphic_rendition(38, 2, 0, 0, 0) screen.select_graphic_rendition(48, 2, 255, 255, 255) assert screen.cursor.attrs.fg == "000000" assert screen.cursor.attrs.bg == "ffffff" # b) invalid color. screen.select_graphic_rendition(48, 2, 255)
def test_colors256(): screen = Screen(2, 2) # a) OK-case. screen.select_graphic_rendition(38, 5, 0) screen.select_graphic_rendition(48, 5, 15) assert screen.cursor.attrs.fg == "000000" assert screen.cursor.attrs.bg == "ffffff" # b) invalid color. screen.select_graphic_rendition(48, 5, 100500)
def test_colors_ignore_invalid(): screen = Screen(2, 2) default_attrs = screen.cursor.attrs screen.select_graphic_rendition(100500) assert screen.cursor.attrs == default_attrs screen.select_graphic_rendition(38, 100500) assert screen.cursor.attrs == default_attrs screen.select_graphic_rendition(48, 100500) assert screen.cursor.attrs == default_attrs
def test_unicode(): screen = Screen(4, 2) stream = Stream(screen) try: stream.feed("тест".encode("utf-8")) except UnicodeDecodeError: pytest.fail("Check your code -- we do accept unicode.") assert screen.display == ["тест", " "]
def test_backspace(): screen = Screen(2, 2) assert screen.cursor.x == 0 screen.backspace() assert screen.cursor.x == 0 screen.cursor.x = 1 screen.backspace() assert screen.cursor.x == 0
def test_select_other_charset(): screen = Screen(3, 3) assert screen.use_utf8 # on by default. # a) disable utf-8 screen.select_other_charset(b"@") assert not screen.use_utf8 # b) unknown code -- noop screen.select_other_charset(b"X") assert not screen.use_utf8 # c) enable utf-8 screen.select_other_charset(b"G") assert screen.use_utf8
def test_restore_cursor_with_none_saved(): screen = Screen(10, 10) screen.set_mode(mo.DECOM) screen.cursor.x, screen.cursor.y = 5, 5 screen.restore_cursor() assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert mo.DECOM not in screen.mode
def test_alignment_display(): screen = Screen(5, 5) screen.draw("a") screen.linefeed() screen.linefeed() screen.draw("b") assert screen.display == ["a ", " ", "b ", " ", " "] screen.alignment_display() assert screen.display == ["EEEEE", "EEEEE", "EEEEE", "EEEEE", "EEEEE"]
def test_report_device_attributes(): screen = Screen(10, 10) acc = [] screen.write_process_input = acc.append # a) noop screen.report_device_attributes(42) assert not acc # b) OK case screen.report_device_attributes() assert acc.pop() == ctrl.CSI + b"?6c"
def test_cursor_up(): screen = Screen(10, 10) # Moving the cursor up at the top doesn't do anything screen.cursor_up(1) assert screen.cursor.y == 0 screen.cursor.y = 1 # Moving the cursor past the top moves it to the top screen.cursor_up(10) assert screen.cursor.y == 0 screen.cursor.y = 5 # Can move the cursor more than one up. screen.cursor_up(3) assert screen.cursor.y == 2
def test_linefeed(): screen = update(Screen(2, 2), ["bo", "sh"], [None, None]) screen.set_mode(mo.LNM) # a) LNM on by default (that's what `vttest` forces us to do). assert mo.LNM in screen.mode screen.cursor.x, screen.cursor.y = 1, 0 screen.linefeed() assert (screen.cursor.y, screen.cursor.x) == (1, 0) # b) LNM off. screen.reset_mode(mo.LNM) screen.cursor.x, screen.cursor.y = 1, 0 screen.linefeed() assert (screen.cursor.y, screen.cursor.x) == (1, 1)
def test_cursor_forward(): screen = Screen(10, 10) # Moving the cursor right at the margin doesn't do anything screen.cursor.x = 9 screen.cursor_forward(1) assert screen.cursor.x == 9 # Moving the cursor past the right margin moves it to the right margin screen.cursor.x = 8 screen.cursor_forward(10) assert screen.cursor.x == 9 # Can move the cursor more than one forward. screen.cursor.x = 5 screen.cursor_forward(3) assert screen.cursor.x == 8
def test_set_margins(): screen = Screen(10, 10) assert screen.margins == (0, 9) # a) ok-case screen.set_margins(1, 5) assert screen.margins == (0, 4) # b) one of the margins is out of bounds screen.set_margins(100, 10) assert screen.margins != (99, 9) assert screen.margins == (0, 4) # c) no margins provided screen.set_margins() assert screen.margins == (0, screen.lines - 1)
def test_cursor_down(): screen = Screen(10, 10) # Moving the cursor down at the bottom doesn't do anything screen.cursor.y = 9 screen.cursor_down(1) assert screen.cursor.y == 9 screen.cursor.y = 8 # Moving the cursor past the bottom moves it to the bottom screen.cursor_down(10) assert screen.cursor.y == 9 screen.cursor.y = 5 # Can move the cursor more than one down. screen.cursor_down(3) assert screen.cursor.y == 8
def test_cursor_back(): screen = Screen(10, 10) # Moving the cursor left at the margin doesn't do anything screen.cursor.x = 0 screen.cursor_back(1) assert screen.cursor.x == 0 screen.cursor.x = 3 # Moving the cursor past the left margin moves it to the left margin screen.cursor_back(10) assert screen.cursor.x == 0 screen.cursor.x = 5 # Can move the cursor more than one back. screen.cursor_back(3) assert screen.cursor.x == 2
def test_reset_resets_colors(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(30) screen.select_graphic_rendition(40) assert screen.cursor.attrs.fg == "black" assert screen.cursor.attrs.bg == "black" screen.select_graphic_rendition(0) assert screen.cursor.attrs == screen.default_char
def test_colors(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(30) # black foreground screen.select_graphic_rendition(40) # black background assert screen.cursor.attrs.fg == "black" assert screen.cursor.attrs.bg == "black" screen.select_graphic_rendition(31) # red foreground assert screen.cursor.attrs.fg == "red" assert screen.cursor.attrs.bg == "black"
def test_attributes(): screen = Screen(2, 2) assert screen.buffer == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) # bold. # Still default, since we haven't written anything. assert screen.buffer == [[screen.default_char, screen.default_char]] * 2 assert screen.cursor.attrs.bold screen.draw(b"f") assert screen.buffer == [[ Char("f", "default", "default", bold=True), screen.default_char ], [screen.default_char, screen.default_char]]
def test_report_device_attributes(): screen = Screen(10, 10) acc = [] screen.write_process_input = acc.append # a) noop screen.report_device_attributes(42) assert not acc # b) OK case screen.report_device_attributes() assert acc.pop() == ctrl.CSI + "?6c"
def test_attributes_reset(): screen = Screen(2, 2) screen.set_mode(mo.LNM) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) screen.draw("f") screen.draw("o") screen.draw("o") assert screen == [ [Char("f", bold=True), Char("o", bold=True)], [Char("o", bold=True), screen.default_char ], ] screen.cursor_position() screen.select_graphic_rendition(0) # Reset screen.draw("f") assert screen == [ [Char("f"), Char("o", bold=True)], [Char("o", bold=True), screen.default_char ], ]
def test_restore_cursor_out_of_bounds(): screen = Screen(10, 10) # a) origin mode off. screen.cursor_position(5, 5) screen.save_cursor() screen.resize(3, 3) screen.reset() screen.restore_cursor() assert (screen.cursor.y, screen.cursor.x) == (2, 2) # b) origin mode is on. screen.resize(10, 10) screen.cursor_position(8, 8) screen.save_cursor() screen.resize(5, 5) screen.reset() screen.set_mode(mo.DECOM) screen.set_margins(2, 3) screen.restore_cursor() assert (screen.cursor.y, screen.cursor.x) == (2, 4)