Example #1
0
 def test_simple(self):
     with tmux.TmuxPane(10, 10) as t:
         tmux.send_command(t, 'true 1')
         self.assertEqual(tmux.visible(t), ['$true 1',
                                            '$'])
         tmux.send_command(t, 'true 2')
         termstate = terminal_dsl.TerminalState.from_tmux_pane(t)
         expected = terminal_dsl.TerminalState(
             lines=['$true 1', '$true 2', '$'],
             cursor_line=2, cursor_offset=1, width=10, height=10,
             history_height=0)
     self.assertEqual(expected, termstate, expected.visible_diff(termstate))
Example #2
0
    def test_simple_save_and_restore(self):
        with tmux.TmuxPane(40, 10) as t:
            tmux.send_command(t, 'python rewrite.py', prompt=u'>')
            self.assertEqual(tmux.visible(t), ['$python rewrite.py', '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))
            save()

            tmux.send_command(t, 'hello!', prompt=u'>')
            self.assertEqual(tmux.visible(t),
                             ['$python rewrite.py', '>hello!', '>'])
            restore(t)

            self.assertEqual(tmux.visible_after_prompt(t, '>'),
                             ['$python rewrite.py', '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))
Example #3
0
    def test_resize(self):
        """lines and cursor pos wrap, reported by line

        The front of the current line retains its position!"""
        with tmux.TmuxPane(10, 10) as t:
            self.assertEqual(tmux.cursor_pos(t), (0, 1))
            tmux.send_command(t, 'true 123456789')
            self.assertEqual(tmux.visible(t),
                             ['$true 1234', '56789', '$'])
            self.assertEqual(tmux.cursor_pos(t), (2, 1))
            t.set_width(5)
            self.assertEqual(tmux.cursor_pos(t), (2, 1))
            self.assertEqual(tmux.visible_after_prompt(t),
                             [' 1234', '56789', '$'])
            tmux.stepwise_resize_width(t, 20)
            tmux.stepwise_resize_height(t, 20)
Example #4
0
    def initialize(cls, pane, termstate):

        lines = termstate.lines[:]
        assert lines.pop(0) == '$rw'
        tmux.send_command(pane, 'rw', prompt=u'>')
        save()
        first_line = lines.pop(0)
        assert first_line.startswith('>')
        pane.tmux('send-keys', first_line[1:])
        pane.enter()
        tmux.wait_until_cursor_moves(pane, 1, 1)
        for i, line in enumerate(lines):
            if i == termstate.cursor_line:
                assert len(lines) == i - 1
                pane.tmux('send-keys', line)
            elif line.startswith('>'):
                tmux.send_command(pane, '>', enter=False, prompt=u'>')
                save()
                pane.tmux('send-keys', line[1:])
                if i != len(lines) - 1:
                    pane.enter()
            else:
                if line != '':
                    row, col = tmux.cursor_pos(pane)
                    pane.tmux('send-keys', line)
                    tmux.wait_until_cursor_moves(pane, row, col)
                if i != len(lines) - 1:
                    pane.enter()
        row, col = tmux.cursor_pos(pane)

        additional_required_blank_rows = (
            termstate.history_height - len(tmux.scrollback(pane)) +
            termstate.height - row - 1)
        assert additional_required_blank_rows >= 0
        assert col == len(line) % termstate.width  # TODO allow other columns
        if additional_required_blank_rows == 1:
            pane.tmux('1c'+str(col))
            pane.enter()
        elif additional_required_blank_rows > 1:
            for _ in range(additional_required_blank_rows - 1):
                pane.enter()
            for _ in range(additional_required_blank_rows - 2):
                pane.tmux('send-keys', 'up2')
                pane.enter()
            pane.tmux('send-keys', 'uc'+str(col))
            pane.enter()
Example #5
0
 def test_contents(self):
     with tmux.TmuxPane(20, 5) as t:
         tmux.send_command(t, 'echo 01234567890123456789')
         tmux.send_command(t, 'echo 01234567890123456789')
         self.assertEqual(tmux.all_contents(t),
                          ['$echo 01234567890123',
                           '456789',
                           '01234567890123456789',
                           '$echo 01234567890123',
                           '456789',
                           '01234567890123456789',
                           '$'])
         self.assertEqual(tmux.all_lines(t),
                          ['$echo 01234567890123'
                           '456789',
                           '01234567890123456789',
                           '$echo 01234567890123'
                           '456789',
                           '01234567890123456789',
                           '$'])
Example #6
0
    def test_rewind_to_scrolled_off_prompt(self):
        """Recreating history in visible area because undo goes offscreen

        For this we need to track history, do math to place
        this history in the visible window, and track scrolling
        or cursor position to know that we've run out of space.
        I think we don't need an emulator yet - just cursor querying should do.
        """
        with tmux.TmuxPane(60, 3) as t:
            tmux.send_command(t, 'python rewrite.py', prompt=u'>')
            save()
            self.assertEqual(tmux.visible(t), ['$python rewrite.py', '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))

            tmux.send_command(t, 'hi there!', prompt=u'>')
            save()

            tmux.send_command(t, 'hello!', prompt=u'>')
            tmux.send_command(t, 'hi again!', prompt=u'>')
            tmux.send_command(t, 'hey!', prompt=u'>')
            save()
            self.assertEqual(tmux.scrollback(t),
                             ['$python rewrite.py',
                              '>hi there!',
                              '>hello!'])
            self.assertEqual(tmux.visible(t),
                             ['>hi again!',
                              '>hey!',
                              '>'])
            restore(t)
            self.assertEqual(tmux.scrollback(t),
                             ['$python rewrite.py',
                              '>hi there!',
                              '>hello!',
                              '#<---History contiguity broken by rewind--->'])
            self.assertEqual(tmux.visible_after_prompt(t, '>'),
                             ['>hi there!',
                              '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))
Example #7
0
    def test_undo_simple(self):
        """Test undoing one liner command in ipython."""
        with ActualUndo(80, 30) as t:

            # type some commands
            tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1))
            tmux.send_command(t, 'a = 1', prompt=IPyPrompt.in_formatted(2))
            tmux.send_command(t, 'a', prompt=IPyPrompt.in_formatted(3))
            output = tmux.visible_without_formatting(t)
            lines = [IPyPrompt.in_prompt(1) + ' a = 1',
                     IPyPrompt.in_prompt(2) + ' a',
                     IPyPrompt.out_prompt(2) + ' 1',
                     IPyPrompt.in_prompt(3)]
            self.assertEqual(output[-4:], lines)

            # undo
            tmux.send_command(t, 'undo', prompt=IPyPrompt.in_formatted(2))
            output = tmux.visible_without_formatting(t)
            self.assertEqual(output[-2:],
                             lines[:1] + [IPyPrompt.in_prompt(2)])
Example #8
0
 def test_cursor_query(self):
     with tmux.TmuxPane(40, 10) as t:
         tmux.send_command(t, 'true 1234')
         tmux.send_command(t, 'true 1234')
         program = "import findcursor, sys; print(findcursor.get_cursor_position(sys.stdout, sys.stdin))"
         tmux.send_command(t, 'python -c "%s"' % (program, ))
         lines = tmux.visible(t)
         while lines[-1] == u'$':
             lines.pop()
         line = lines[-1]
         self.assertTrue(len(line) > 1, repr(line))
         row, col = [int(x) for x in re.search(
             r'[(](\d+), (\d+)[)]', line).groups()]
         self.assertEqual(tmux.cursor_pos(t), (row+1, col+1))
Example #9
0
    def test_scroll_off(self):
        """Scroll down causing recorded output to scroll off the top."""
        with tmux.TmuxPane(40, 3) as t:
            tmux.send_command(t, 'python rewrite.py', prompt=u'>')
            save()
            self.assertEqual(tmux.visible(t), ['$python rewrite.py', '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))

            tmux.send_command(t, 'hello!', prompt=u'>')
            save()

            tmux.send_command(t, 'hi again!', prompt=u'>')
            save()
            self.assertEqual(tmux.visible(t),
                             ['>hello!', '>hi again!', '>'])
            restore(t)
            self.assertEqual(tmux.visible_after_prompt(t, '>'),
                             ['>hello!', '>'])
            self.assertEqual(tmux.cursor_pos(t), (1, 1))
Example #10
0
    def test_scroll_down(self):
        with tmux.TmuxPane(40, 8) as t:
            tmux.send_command(t, 'true')
            tmux.send_command(t, 'true')
            tmux.send_command(t, 'true')
            tmux.send_command(t, 'true')
            tmux.send_command(t, 'true')
            tmux.send_command(t, 'python rewrite.py', prompt=u'>')
            self.assertEqual(tmux.visible(t),
                             (['$true']*5 +
                              ['$python rewrite.py',
                               '>']))
            self.assertEqual(tmux.cursor_pos(t), (6, 1))
            save()
            tmux.send_command(t, 'hello!', prompt=u'>')
            save()

            tmux.send_command(t, 'hi again!', prompt=u'>')
            save()
            self.assertEqual(tmux.visible(t),
                             ['$true']*4 + ['$python rewrite.py',
                                            '>hello!',
                                            '>hi again!',
                                            '>'])
            restore(t)

            self.assertEqual(tmux.visible_after_prompt(t, '>'),
                             ['$true']*4 + ['$python rewrite.py',
                                            '>hello!',
                                            '>'])
            self.assertEqual(tmux.cursor_pos(t), (6, 1))
Example #11
0
    def test_undo_multiple_input_lines(self):
        """Test udoing a line of a multiple lines command in ipython."""
        with ActualUndo(80, 30) as t:

            # type some commands
            tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1))
            tmux.send_command(t, 'def foo():',
                              prompt=IPyPrompt.new_l_formatted())
            tmux.send_command(t, 'print "hi"',
                              prompt=IPyPrompt.new_l_formatted())
            tmux.send_command(t, ' ', prompt=IPyPrompt.in_formatted(2))
            tmux.send_command(t, 'foo()', prompt=IPyPrompt.in_formatted(3))
            output = tmux.visible_without_formatting(t)
            lines = [IPyPrompt.in_prompt(1) + ' def foo():',
                     IPyPrompt.new_l_prompt() + '     print "hi"',
                     IPyPrompt.new_l_prompt() + '',
                     IPyPrompt.in_prompt(2) + ' foo()',
                     'hi',
                     IPyPrompt.in_prompt(3)]
            self.assertEqual(output[-6:], lines)

            # undo
            tmux.send_command(t, 'undo', prompt=IPyPrompt.in_formatted(2))
            output = tmux.visible_without_formatting(t)
            self.assertEqual(output[-4:], lines[:3] + [IPyPrompt.in_prompt(2)])

            # undo again
            tmux.send_command(t, 'undo', prompt=IPyPrompt.new_l_formatted())
            output = tmux.visible_without_formatting(t)
            self.assertEqual(output[-3:],
                             lines[:2] + [IPyPrompt.new_l_prompt()])
Example #12
0
 def test_simple(self):
     """Test sending commands and reading formatted output with tmux."""
     with ActualUndo(80, 30) as t:
         tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1))
         tmux.send_command(t, 'a = 1', prompt=IPyPrompt.in_formatted(2))
         tmux.send_command(t, 'a', prompt=IPyPrompt.in_formatted(3))
         tmux.send_command(t, 'def foo():',
                           prompt=IPyPrompt.new_l_formatted())
         tmux.send_command(t, 'print "hi"',
                           prompt=IPyPrompt.new_l_formatted())
         tmux.send_command(t, ' ', prompt=IPyPrompt.in_formatted(4))
         output = tmux.visible(t)
         lines = [IPyPrompt.in_formatted(1) + ' \x1b[39ma = 1',
                  IPyPrompt.in_formatted(2) + ' \x1b[39ma',
                  IPyPrompt.out_formatted(2) + ' \x1b[39m1',
                  IPyPrompt.in_formatted(3) + ' \x1b[39mdef foo():',
                  IPyPrompt.new_l_formatted() + '    print "hi"',
                  IPyPrompt.new_l_formatted() + '',
                  IPyPrompt.in_formatted(4)]
         self.assertEqual(output[-7:], lines)
Example #13
0
 def test_send_command(self):
     with tmux.TmuxPane(20, 20) as t:
         tmux.send_command(t, 'sleep .1; echo hi')
         self.assertEqual(tmux.visible(t), ['$sleep .1; echo hi',
                                            'hi',
                                            '$'])