Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window improvements and first kernel terminal tests. #68

Merged
merged 3 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ppytty/kernel/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def on_color(bg_color):

class Window(object):

def __init__(self, left, top, width, height, bg=None):
def __init__(self, left, top, width, height, bg=None, no_cursor=True):

self._left = left
self._top = top
Expand All @@ -117,6 +117,7 @@ def __init__(self, left, top, width, height, bg=None):
self._screen = pyte.Screen(width, height)
self._stream = pyte.ByteStream(self._screen)

self._screen.cursor.hidden = no_cursor

def __repr__(self):

Expand Down Expand Up @@ -187,11 +188,15 @@ def render(self, bt=_SelfBlessingsTerminal(), full=False, encoding='utf8'):
bt_move = bt.move
self_char_format = self._char_format

payload = [bt.hide_cursor]
payload = []
payload_append = payload.append

prev_char_format = ''
line_numbers = range(height) if full else screen_dirty

if line_numbers and not screen_cursor.hidden:
payload_append(bt.hide_cursor)

for line_no in line_numbers:
line_data = screen_buffer[line_no]
payload_append(bt_move(top+line_no, left))
Expand All @@ -206,10 +211,11 @@ def render(self, bt=_SelfBlessingsTerminal(), full=False, encoding='utf8'):
prev_char_format = char_format
payload_append(char_data)

payload_append(bt.normal)
payload_append(bt_move(top+screen_cursor.y, left+screen_cursor.x))
if not screen_cursor.hidden:
payload_append(bt.normal_cursor)
if line_numbers:
payload_append(bt.normal)
payload_append(bt_move(top+screen_cursor.y, left+screen_cursor.x))
if not screen_cursor.hidden:
payload_append(bt.normal_cursor)
screen_dirty.clear()
return ''.join(payload).encode(encoding)

Expand Down
71 changes: 71 additions & 0 deletions tests/kernel/test_terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ----------------------------------------------------------------------------
# ppytty
# ----------------------------------------------------------------------------
# Copyright (c) Tiago Montes.
# See LICENSE for deatils.
# ----------------------------------------------------------------------------

from ppytty.kernel import terminal

from . import io_bypass



class Test(io_bypass.NoOutputTestCase):

def setUp(self):

self.t = terminal.Terminal()
self.os_write_mock = self.output_mocks['ppytty.kernel.hw.os_write']
self.os_write_mock.reset_mock()


def _get_written_bytes(self):

# c[0] is the call args tuple; c[0][1] is the 2nd arg to os.write
return b''.join(c[0][1] for c in self.os_write_mock.call_args_list)


def _assert_full_80x25_spaces(self, output_bytes):

# Strip possibly nested fake <tigetstr>/<tparm> calls and count b' '.
# The test output terminal is 80x25 so we should get 2000.
open_fake, close_fake, space = b'<> '
depth = 0
space_count = 0
for byte_value in output_bytes:
if byte_value == space and depth == 0:
space_count += 1
elif byte_value == open_fake:
depth += 1
elif byte_value == close_fake:
depth -=1
self.assertEqual(space_count, 2000, '# of spaces on terminal render')


def test_render(self):

self.t.render()
written_bytes = self._get_written_bytes()
self._assert_full_80x25_spaces(written_bytes)


def test_render_rerender(self):

self.t.render()
self.os_write_mock.reset_mock()
self.t.render()
written_bytes = self._get_written_bytes()
self.assertEqual(written_bytes, b'', 'no bytes written on re-render')


def test_render_rerender_full(self):

self.t.render()
self.os_write_mock.reset_mock()
self.t.render(full=True)
written_bytes = self._get_written_bytes()
self._assert_full_80x25_spaces(written_bytes)


# ----------------------------------------------------------------------------