Esempio n. 1
0
 def cotest(self, desc, bgt, bga, fgt, fga, l, r, et):
     bgt = B(bgt)
     fgt = B(fgt)
     bg = urwid.CompositeCanvas(urwid.TextCanvas([bgt], [bga]))
     fg = urwid.CompositeCanvas(urwid.TextCanvas([fgt], [fga]))
     bg.overlay(fg, l, 0)
     result = list(bg.content())
     assert result == et, "%s expected %r, got %r" % (desc, et, result)
Esempio n. 2
0
 def test_encoding_vt100_graphics(self):
     vterm.util._target_encoding = 'ascii'
     self.write('\e)0\e(0\x0fg\x0eg\e)Bn\e)0g\e)B\e(B\x0fn')
     self.expect([[
         (None, '0', B('g')), (None, '0', B('g')),
         (None, None, B('n')), (None, '0', B('g')),
         (None, None, B('n'))
     ]], raw=True)
Esempio n. 3
0
 def parse_osc(self, buf):
     """
     Parse operating system command.
     """
     if buf.startswith(B(';')): # set window title and icon
         self.widget.set_title(buf[1:])
     elif buf.startswith(B('3;')): # set window title
         self.widget.set_title(buf[2:])
 def test(self):
     urwid.set_encoding("euc-jp")
     self.assertRaises(text_layout.CanNotDisplayText,
                       text_layout.default_layout.calculate_text_segments,
                       B('\xA1\xA1'), 1, 'space')
     urwid.set_encoding("utf-8")
     self.assertRaises(text_layout.CanNotDisplayText,
                       text_layout.default_layout.calculate_text_segments,
                       B('\xe9\xa2\x96'), 1, 'space')
 def read(self, raw=False, focus=False):
     self.term.wait_and_feed()
     rendered = self.term.render(self.termsize, focus=focus)
     if raw:
         is_empty = lambda c: c == (None, None, B(' '))
         content = list(rendered.content())
         lines = [list(dropwhile(is_empty, reversed(line)))
                  for line in content]
         return [list(reversed(line)) for line in lines if len(line)]
     else:
         content = rendered.text
         lines = [line.rstrip() for line in content]
         return B('\n').join(lines).rstrip()
Esempio n. 6
0
    def test_ibmpc_mapping(self):
        vterm.util._target_encoding = 'ascii'

        self.write('\e[11m\x18\e[10m\x18')
        self.expect([[(None, 'U', B('\x18'))]], raw=True)

        self.write('\ec\e)U\x0e\x18\x0f\e[3h\x18\e[3l\x18')
        self.expect([[(None, None, B('\x18'))]], raw=True)

        self.write('\ec\e[11m\xdb\x18\e[10m\xdb')
        self.expect([[(None, 'U', B('\xdb')), (None, 'U', B('\x18')),
                      (None, None, B('\xdb'))]],
                    raw=True)
    def test_set_title(self):
        self._the_title = None

        def _change_title(widget, title):
            self._the_title = title

        self.connect_signal('title')
        self.write('\e]666parsed right?\e\\te\e]0;test title\007st1')
        self.expect('test1')
        self.expect_signal(B('test title'))
        self.write('\e]3;stupid title\e\\\e[0G\e[2Ktest2')
        self.expect('test2')
        self.expect_signal(B('stupid title'))
        self.disconnect_signal('title')
Esempio n. 8
0
    def test_collapse_all_from_middle_node(self):
        """
        test listbox collapse all nested nodes when press key `c`.
        """
        data = [
            {
                "key": [
                    "1",
                    "2"
                ]
            },
            {
                "key": [
                    "2"
                ]
            }
        ]
        node = self._node_factory.create_root_node(data)

        # act
        walker = JSONListWalker(start_from=node)
        listbox = JSONListBox(walker)
        size = (18, 13)  # 13 rows after expanded in total

        # move to focus to the second item, thus move down twice, as the view
        # should be expanded at the first level
        listbox.move_focus_to_next_line(size)
        listbox.move_focus_to_next_line(
            size)  # move to the second item in the list
        listbox.toggle_collapse_on_focused_parent(
            size)  # expand the second item
        listbox.move_focus_to_next_line(
            size)  # move to the first child of the object
        listbox.toggle_collapse_on_focused_parent(size)  # expand the object
        listbox.move_focus_to_next_line(
            size)  # move to the first child of the object

        listbox.keypress(size, 'c')

        content = [[t[2] for t in row] for row in
                   listbox.render((18, 2)).content()]

        # verify
        expected = [
            [B("[\xe2\x80\xa6]               ")],
            [B("                  ")]
        ]
        self.assertEqual(expected, content)
class CalcTranslateCantDisplayTest(CalcTranslateTest, unittest.TestCase):
    text = B('Hello\xe9\xa2\x96')
    mode = 'space'
    width = 1
    result_left = [[]]
    result_right = [[]]
    result_center = [[]]
Esempio n. 10
0
    def test_boolean_node(self):
        """ test JSON `boolean` rendering """

        data = True

        # act
        node = PrimitiveNode("", data, display_key=False)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), "enter")
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B("True              ")]]]
        self.assertEqual(expected, texts)
class DummyCommand(object):
    QUITSTRING = B('|||quit|||')

    def __init__(self):
        self.reader, self.writer = os.pipe()

    def __call__(self):
        # reset
        stdout = getattr(sys.stdout, 'buffer', sys.stdout)
        stdout.write(B('\x1bc'))

        while True:
            data = self.read(1024)
            if self.QUITSTRING == data:
                break
            stdout.write(data)
            stdout.flush()

    def read(self, size):
        while True:
            try:
                return os.read(self.reader, size)
            except OSError as e:
                if e.errno != errno.EINTR:
                    raise

    def write(self, data):
        os.write(self.writer, data)

    def quit(self):
        self.write(self.QUITSTRING)
 def cbtest(self, width, exp):
     result = text_layout.default_layout.calculate_text_segments(
         B(self.text), width, self.mode)
     assert len(result) == len(exp), repr((result, exp))
     for l, e in zip(result, exp):
         end = l[-1][-1]
         assert end == e, repr((result, exp))
Esempio n. 13
0
 def _sigwinch_handler(self, signum, frame=None):
     """
     frame -- will always be None when the GLib event loop is being used.
     """
     if not self._resized:
         self._resize_pipe_wr.send(B("R"))
     self._resized = True
     self.screen_buf = None
Esempio n. 14
0
 def cptest(self, desc, ct, ca, l, r, et):
     ct = B(ct)
     c = urwid.CompositeCanvas(
         urwid.TextCanvas([ct], [ca]))
     c.pad_trim_left_right(l, r)
     result = list(c.content())
     assert result == et, "%s expected %r, got %r"%(
         desc, et, result)
Esempio n. 15
0
 def ctest(self, desc, s, exp, expcs):
     exp = B(exp)
     util.set_encoding('ascii')
     c = urwid.Text(s).render((5, ))
     result = c._text[0]
     assert result == exp, "%s got:%r expected:%r" % (desc, result, exp)
     resultcs = c._cs[0]
     assert resultcs == expcs, "%s got:%r expected:%r" % (desc, resultcs,
                                                          expcs)
Esempio n. 16
0
    def test_list_box_with_simple_object(self):
        """
        test listbox rendering simple object with collapse pressing key `enter` and
        moving focus with pressing key `ctrl n`.
        """
        data = {
            "key": "value"
        }
        node = NodeFactory.create_node("", data, parent=None, display_key=False)

        # act
        walker = JSONListWalker(start_from=node)
        listbox = JSONListBox(walker)

        contents_until_moving_to_end = []
        prev_widget, prev_node = None, None
        cur_widget, cur_node = listbox.get_focus()
        while prev_widget != cur_widget:
            if not cur_node.is_expanded():
                listbox.keypress((18, 18), "enter")
                cur_widget, cur_node = listbox.get_focus()
            contents_until_moving_to_end.append(cur_widget.render((18,)).content())
            listbox.keypress((18, 18), "down")
            prev_widget, prev_node = cur_widget, cur_node
            cur_widget, cur_node = listbox.get_focus()

        texts_until_moving_to_end = [[[t[2] for t in row] for row in content]
                                     for content in contents_until_moving_to_end]

        # moving up until the start and collapse all expanded widgets
        contents_until_moving_to_start = []
        prev_widget, prev_node = None, None
        cur_widget, cur_node = listbox.get_focus()
        while prev_widget != cur_widget:
            if cur_node.is_expanded():
                listbox.keypress((18, 18), "enter")
                cur_widget, cur_node = listbox.get_focus()
            listbox.keypress((18, 18), "up")
            prev_widget, prev_node = cur_widget, cur_node
            cur_widget, cur_node = listbox.get_focus()

        contents_until_moving_to_start.append(listbox.render((18, 1)).content())
        texts_until_moving_to_start = [[[t[2] for t in row] for row in content]
                                       for content in contents_until_moving_to_start]

        # verify
        # verify that after moving to the end, all the expandable lines have been expanded
        self.assertEqual(3, len(texts_until_moving_to_end))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B("key"), B(": "), B("value     ")]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts_until_moving_to_end)

        # verify that after moving to the start, all the expanded lines is collapsed
        self.assertEqual(1, len(texts_until_moving_to_start))
        expected = [[[B("{\xe2\x80\xa6}               ")]]]
        self.assertEqual(expected, texts_until_moving_to_start)
 def expect(self, what, desc=None, raw=False, focus=False):
     if not isinstance(what, list):
         what = B(what)
     got = self.read(raw=raw, focus=focus)
     if desc is None:
         desc = ''
     else:
         desc += '\n'
     desc += 'Expected:\n%r\nGot:\n%r' % (what, got)
     self.assertEqual(got, what, desc)
Esempio n. 18
0
 def rtest(self, w, expected_text, expected_cursor):
     expected_text = [B(t) for t in expected_text]
     get_cursor = w.get_cursor_coords((4, ))
     assert get_cursor == expected_cursor, "got: %r expected: %r" % (
         get_cursor, expected_cursor)
     r = w.render((4, ), focus=1)
     text = [t for a, cs, t in [ln[0] for ln in r.content()]]
     assert text == expected_text, "got: %r expected: %r" % (text,
                                                             expected_text)
     assert r.cursor == expected_cursor, "got: %r expected: %r" % (
         r.cursor, expected_cursor)
    def __call__(self):
        # reset
        stdout = getattr(sys.stdout, 'buffer', sys.stdout)
        stdout.write(B('\x1bc'))

        while True:
            data = self.read(1024)
            if self.QUITSTRING == data:
                break
            stdout.write(data)
            stdout.flush()
class CalcTranslateWordTest3(CalcTranslateTest, unittest.TestCase):
    def setUp(self):
        urwid.set_encoding('utf-8')

    text = B('\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba')
    width = 10
    mode = 'space'
    result_left = [[(4, 0, 6), (0, 6)], [(6, 7, 16), (0, 16)]]
    result_right = [[(6, None), (4, 0, 6), (0, 6)],
                    [(4, None), (6, 7, 16), (0, 16)]]
    result_center = [[(3, None), (4, 0, 6), (0, 6)],
                     [(2, None), (6, 7, 16), (0, 16)]]
Esempio n. 21
0
    def test_prev_order(self):
        data = [
            1,
            2
        ]

        node = ArrayNode("", data, display_key=False)
        # start from the end
        widget = node.get_end_node().get_widget()

        contents = []
        while widget is not None:
            contents.append(widget.render((18,)).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content] for content in contents]
        texts.reverse()

        # verify
        self.assertEqual(4, len(texts))
        expected = [
            [[B("[                 ")]],
            [[B("   "), B("1              ")]],
            [[B("   "), B("2              ")]],
            [[B("]                 ")]],
        ]
        self.assertEqual(expected, texts)
Esempio n. 22
0
    def test_linebox_border(self):
        urwid.set_encoding("utf-8")
        t = urwid.Text("")

        l = urwid.LineBox(t).render((3,)).text

        # default
        self.assertEqual(l,
            self.border(B("\xe2\x94\x8c"), B("\xe2\x94\x80"),
                B("\xe2\x94\x90"), B("\xe2\x94\x82"), B("\xe2\x94\x82"),
                B("\xe2\x94\x94"), B("\xe2\x94\x80"), B("\xe2\x94\x98")))

        nums = [B(str(n)) for n in range(8)]
        b = dict(list(zip(["tlcorner", "tline", "trcorner", "lline", "rline",
            "blcorner", "bline", "brcorner"], nums)))
        l = urwid.LineBox(t, **b).render((3,)).text

        self.assertEqual(l, self.border(*nums))
Esempio n. 23
0
    def ltest(self,desc,body,focus,offset_inset_rows,exp_text,exp_cur):
        exp_text = [B(t) for t in exp_text]
        lbox = urwid.ListBox(body)
        lbox.body.set_focus( focus )
        lbox.shift_focus((4,10), offset_inset_rows )
        canvas = lbox.render( (4,5), focus=1 )

        text = [bytes().join([t for at, cs, t in ln]) for ln in canvas.content()]

        cursor = canvas.cursor

        assert text == exp_text, "%s (text) got: %r expected: %r" %(desc,text,exp_text)
        assert cursor == exp_cur, "%s (cursor) got: %r expected: %r" %(desc,cursor,exp_cur)
Esempio n. 24
0
    def set_g01(self, char, mod):
        """
        Set G0 or G1 according to 'char' and modifier 'mod'.
        """
        if self.modes.main_charset != CHARSET_DEFAULT:
            return

        if mod == B('('):
            g = 0
        else:
            g = 1

        if char == B('0'):
            cset = 'vt100'
        elif char == B('U'):
            cset = 'ibmpc'
        elif char == B('K'):
            cset = 'user'
        else:
            cset = 'default'

        self.charset.define(g, cset)
Esempio n. 25
0
    def test_empty_object(self):
        """ test rendering of an empty JSON object"""
        data = {}

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        # restart and scan from the end widget
        node = self._node_factory.create_root_node(data)
        widget = node.get_end_node().get_widget()
        contents_from_end = []
        while widget is not None:
            contents_from_end.append(widget.render((18, )).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        texts_from_end = [[[t[2] for t in row] for row in content]
                          for content in contents_from_end]
        texts_from_end.reverse()

        # verify
        self.assertEqual(2, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)
        self.assertEqual(expected, texts_from_end)
Esempio n. 26
0
    def test_empty_list(self):
        """ test rendering of an empty JSON object"""
        data = []

        # act
        node = ArrayNode("", data, display_key=False)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18,)).content())
            widget = widget.next_inorder()

        # restart and scan from the end
        node = ArrayNode("", data, display_key=False)
        widget = node.get_end_node().get_widget()
        contents_from_end = []
        while widget is not None:
            contents_from_end.append(widget.render((18,)).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content] for content in contents]
        texts_from_end = [[[t[2] for t in row] for row in content] for content in contents_from_end]
        texts_from_end.reverse()

        # verify
        self.assertEqual(2, len(texts))
        expected = [
            [[B("[                 ")]],
            [[B("]                 ")]],
        ]
        self.assertEqual(expected, texts)
        self.assertEqual(expected, texts_from_end)
Esempio n. 27
0
 def test2(self):
     self.ct2(["Hello"], None, 0, 0, 5, 1, None,
              [[(None, None, B("Hello"))]])
     self.ct2(["Hello"], None, 1, 0, 4, 1, None,
              [[(None, None, B("ello"))]])
     self.ct2(["Hello"], None, 0, 0, 4, 1, None,
              [[(None, None, B("Hell"))]])
     self.ct2(["Hi", "There"], None, 1, 0, 3, 2, None,
              [[(None, None, B("i  "))], [(None, None, B("her"))]])
     self.ct2(["Hi", "There"], None, 0, 0, 5, 1, None,
              [[(None, None, B("Hi   "))]])
     self.ct2(["Hi", "There"], None, 0, 1, 5, 1, None,
              [[(None, None, B("There"))]])
Esempio n. 28
0
    def test_numeric_node(self):
        """ test JSON `numeric` rendering """
        data = 1.0

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('1.0'), B('               ')]]]
        self.assertEqual(expected, texts)
Esempio n. 29
0
    def tab(self, tabstop=8):
        """
        Moves cursor to the next 'tabstop' filling everything in between
        with spaces.
        """
        x, y = self.term_cursor

        while x < self.width - 1:
            self.set_char(B(" "))
            x += 1

            if self.is_tabstop(x):
                break

        self.is_rotten_cursor = False
        self.set_term_cursor(x, y)
Esempio n. 30
0
    def test_toggle_expanded_on_start_widget(self):
        """
        test listbox collapse expandable widgets on start widget
        """
        data = [
            "item"
        ]
        node = NodeFactory.create_node("", data, parent=None, display_key=False)

        # act
        walker = JSONListWalker(start_from=node)
        listbox = JSONListBox(walker)

        listbox.keypress((18, 18), 'enter')
        collapse_content = [[t[2] for t in row] for row in listbox.render((18, 1)).content()]

        # verify
        expected_collapse_content = [[B("[\xe2\x80\xa6]               ")]]
        self.assertEqual(expected_collapse_content, collapse_content)