Skip to content

Commit

Permalink
Improve resize handling of existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Nov 12, 2020
1 parent ae7795f commit c90b579
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 5 additions & 2 deletions enlighten/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ def _resize_handler(self, *args, **kwarg): # pylint: disable=unused-argument
newHeight = term.height
newWidth = term.width

if newHeight < self.height:
term.move_to(0, max(0, newHeight - self.scroll_offset))
self.stream.write(u'\n' * (2 * max(self.counters.values())))

if newWidth < self.width:
offset = (self.scroll_offset - 1) * (1 + self.width // newWidth)
term.move_to(0, max(0, newHeight - offset))
term.move_to(0, max(0, newHeight - self.scroll_offset))
self.stream.write(term.clear_eos)

self.width = newWidth
Expand Down
15 changes: 8 additions & 7 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ def test_stop_position_1(self):

def test_resize_handler(self):

with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockheight:
mockheight.side_effect = [80, 85, 87, 70, 70]
with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockwidth:
mockwidth.side_effect = [80, 85, 87, 70, 70]

manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter)
counter3 = MockCounter(manager=manager)
Expand All @@ -608,7 +608,7 @@ def test_resize_handler(self):
self.assertEqual(counter3.calls, [])

manager.resize_lock = False
mockheight.side_effect = [80, 85, 87, 70, 70]
mockwidth.side_effect = [80, 85, 87, 70, 70]
with mock.patch('enlighten._manager.Manager._set_scroll_area') as ssa:
manager._resize_handler()
self.assertEqual(ssa.call_count, 1)
Expand All @@ -617,7 +617,7 @@ def test_resize_handler(self):
self.assertFalse(manager.resize_lock)

self.tty.stdout.write(u'X\n')
self.assertEqual(self.tty.stdread.readline(), term.move(19, 0) + term.clear_eos + 'X\n')
self.assertEqual(self.tty.stdread.readline(), term.move(21, 0) + term.clear_eos + 'X\n')

self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])

Expand Down Expand Up @@ -645,7 +645,7 @@ def test_resize_handler_no_change(self):
def test_resize_handler_height_only(self):

with mock.patch('%s.height' % TERMINAL, new_callable=mock.PropertyMock) as mockheight:
mockheight.side_effect = [25, 23, 28, 30, 30]
mockheight.side_effect = [25, 23, 28, 20, 20]

manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter)
counter3 = MockCounter(manager=manager)
Expand All @@ -659,8 +659,9 @@ def test_resize_handler_height_only(self):
# Height is set in _set_scroll_area which is mocked
self.assertEqual(manager.height, 25)

self.tty.stdout.write(u'X\n')
self.assertEqual(self.tty.stdread.readline(), 'X\n')
self.assertEqual(self.tty.stdread.readline(), manager.term.move(16, 0) + '\n')
for _ in range(5):
self.assertEqual(self.tty.stdread.readline(), '\n')

self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])

Expand Down

0 comments on commit c90b579

Please sign in to comment.