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))
def test_lines_wrap(self): """lines and cursor position wrap This is the reason we're using tmux and not vt100 emulator""" with tmux.TmuxPane(10, 10) as t: self.assertEqual(tmux.cursor_pos(t), (0, 1)) t.send_keys('true 678') self.assertEqual(tmux.cursor_pos(t), (1, 1)) t.clear() tmux.wait_for_prompt(t, '$') self.assertEqual(tmux.cursor_pos(t), (0, 1)) t.send_keys('true 6789') self.assertEqual(tmux.cursor_pos(t), (2, 1))
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))
def from_tmux_pane(cls, pane): import tmux lines = tmux.all_lines(pane) history_height = len(tmux.scrollback(pane)) print('tmux.all_contents:', tmux.all_contents(pane)) print('tmux.visible:', tmux.visible(pane)) print('history_height:', history_height) print('tmux.scrollback:', tmux.scrollback(pane)) width, height = tmux.width(pane), tmux.height(pane) cursor_row, cursor_col = tmux.cursor_pos(pane) #TODO deal with cursors not at the bottom termstate = TerminalState( lines=lines, cursor_line=len(lines) - 1, cursor_offset=len(lines[-1]), width=width, height=height, history_height=history_height) print(termstate) #assert termstate.cursor_row == cursor_row #assert termstate.cursor_column == cursor_col return termstate
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)
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()
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))
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))
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))
def test_cursor_position_updated_immediately_after_send_keys(self): """No need to wait for prompt etc. """ with tmux.TmuxPane(10, 10) as t: self.assertEqual(tmux.cursor_pos(t), (0, 1)) t.send_keys('true 678') self.assertEqual(tmux.cursor_pos(t), (1, 1))