Source code for pyte.screens

# -*- coding: utf-8 -*-
"""
    pyte.screens
    ~~~~~~~~~~~~

    This module provides classes for terminal screens, currently
    there's only one base screen implementation, but who knows what
    the future will bring :).

    .. warning:: from ``xterm/main.c`` «If you think you know what all
                 of this code is doing, you are probably very mistaken.
                 There be serious and nasty dragons here» -- nothing
                 has changed.

    :copyright: (c) 2011 Selectel, see AUTHORS for more details.
    :license: LGPL, see LICENSE for more details.
"""

from __future__ import absolute_import, print_function, unicode_literals

import copy
import math
import operator
from collections import namedtuple, deque
from itertools import islice, repeat

from . import modes as mo, graphics as g, charsets as cs


[docs]def take(n, iterable): """Returns first n items of the iterable as a list.""" return list(islice(iterable, n)) #: A container for screen's scroll margins.
Margins = namedtuple("Margins", "top bottom") #: A container for savepoint, created on :data:`~pyte.escape.DECSC`. Savepoint = namedtuple("Savepoint", [ "cursor", "g0_charset", "g1_charset", "charset", "origin", "wrap" ]) #: A container for a single character, field names are *hopefully* #: self-explanatory. _Char = namedtuple("_Char", [ "data", "fg", "bg", "bold", "italics", "underscore", "strikethrough", "reverse", ])
[docs]class Char(_Char): """A wrapper around :class:`_Char`, providing some useful defaults for most of the attributes. """ def __new__(cls, data, fg="default", bg="default", bold=False, italics=False, underscore=False, reverse=False, strikethrough=False): return _Char.__new__(cls, data, fg, bg, bold, italics, underscore, reverse, strikethrough)
[docs]class Cursor(object): """Screen cursor. :param int x: horizontal cursor position. :param int y: vertical cursor position. :param pyte.screens.Char attrs: cursor attributes (see :meth:`Screen.selectel_graphic_rendition` for details). """ __slots__ = "x y attrs".split() def __init__(self, x, y, attrs=Char(" ")): self.x, self.y, self.attrs = x, y, attrs
[docs]class Screen(list): """ A screen is an in-memory matrix of characters that represents the screen display of the terminal. It can be instantiated on it's own and given explicit commands, or it can be attached to a stream and will respond to events. .. attribute:: cursor Reference to the :class:`~pyte.screens.Cursor` object, holding cursor position and attributes. .. attribute:: margins Top and bottom screen margins, defining the scrolling region; the actual values are top and bottom line. .. note:: According to ``ECMA-48`` standard, **lines and columnns are 1-indexed**, so, for instance ``ESC [ 10;10 f`` really means -- move cursor to position (9, 9) in the display matrix. .. seealso:: `Standard ECMA-48, Section 6.1.1 \ <http://www.ecma-international.org/publications/standards/Ecma-048.htm>`_ For a description of the presentational component, implemented by ``Screen``. """ #: A plain empty character with default foreground and background #: colors. default_char = Char(data=" ", fg="default", bg="default") #: An inifinite sequence of default characters, used for populating #: new lines and columns. default_line = repeat(default_char) def __init__(self, columns, lines): self.savepoints = [] self.lines, self.columns = lines, columns self.reset() @property
[docs] def size(self): """Returns screen size -- ``(lines, columns)`` when :data:`~pyte.modes.DECTCEM` mode is set, otherwise returns ``None``. """ if mo.DECTCEM in self.mode: return self.lines, self.columns
@property
[docs] def display(self): """Returns a :func:`list` of screen lines as unicode strings.""" return ["".join(map(operator.attrgetter("data"), line)) for line in self]
[docs] def reset(self): """Resets the terminal to its initial state. * Scroll margins are reset to screen boundaries. * Cursor is moved to home location -- ``(0, 0)`` and its attributes are set to defaults (see :attr:`default_char`). * Screen is cleared -- each character is reset to :attr:`default_char`. * Tabstops are reset to "every eight columns". .. note:: Neither VT220 nor VT102 manuals mentioned that terminal modes and tabstops should be reset as well, thanks to :manpage:`xterm` -- we now know that. """ self[:] = (take(self.columns, self.default_line) for _ in xrange(self.lines)) self.mode = set([mo.DECAWM, mo.DECTCEM, mo.LNM, mo.DECTCEM]) self.margins = Margins(0, self.lines - 1) # According to VT220 manual and ``linux/drivers/tty/vt.c`` # the default G0 charset is latin-1, but for reasons unknown # latin-1 breaks ascii-graphics; so G0 defaults to cp437. self.g0_charset = self.charset = cs.IBMPC_MAP self.g1_charset = cs.VT100_MAP # From ``man terminfo`` -- "... hardware tabs are initially # set every `n` spaces when the terminal is powered up. Since # we aim to support VT102 / VT220 and linux -- we use n = 8. self.tabstops = set(xrange(7, self.columns, 8)) self.cursor = Cursor(0, 0) self.cursor_position()
[docs] def resize(self, lines=None, columns=None): """Resize the screen to the given dimensions. If the requested screen size has more lines than the existing screen, lines will be added at the bottom. If the requested size has less lines than the existing screen lines will be clipped at the top of the screen. Similarly, if the existing screen has less columns than the requested screen, columns will be added at the right, and if it has more -- columns will be clipped at the right. .. note:: According to `xterm`, we should also reset origin mode and screen margins, see ``xterm/screen.c:1761``. :param int lines: number of lines in the new screen. :param int columns: number of columns in the new screen. """ lines = lines or self.lines columns = columns or self.columns # First resize the lines: diff = self.lines - lines # a) if the current display size is less than the requested # size, add lines to the bottom. if diff < 0: self.extend(take(self.columns, self.default_line) for _ in xrange(diff, 0)) # b) if the current display size is greater than requested # size, take lines off the top. elif diff > 0: self[:diff] = () # Then resize the columns: diff = self.columns - columns # a) if the current display size is less than the requested # size, expand each line to the new size. if diff < 0: for y in xrange(lines): self[y].extend(take(abs(diff), self.default_line)) # b) if the current display size is greater than requested # size, trim each line from the right to the new size. elif diff > 0: self[:] = (line[:columns] for line in self) self.lines, self.columns = lines, columns self.margins = Margins(0, self.lines - 1) self.reset_mode(mo.DECOM)
[docs] def set_margins(self, top=None, bottom=None): """Selects top and bottom margins for the scrolling region. Margins determine which screen lines move during scrolling (see :meth:`index` and :meth:`reverse_index`). Characters added outside the scrolling region do not cause the screen to scroll. :param int top: the smallest line number that is scrolled. :param int bottom: the biggest line number that is scrolled. """ if top is None or bottom is None: return # Arguments are 1-based, while :attr:`margins` are zero based -- # so we have to decrement them by one. We also make sure that # both of them is bounded by [0, lines - 1]. top = max(0, min(top - 1, self.lines - 1)) bottom = max(0, min(bottom - 1, self.lines - 1)) # Even though VT102 and VT220 require DECSTBM to ignore regions # of width less than 2, some programs (like aptitude for example) # rely on it. Practicality beats purity. if bottom - top >= 1: self.margins = Margins(top, bottom) # The cursor moves to the home position when the top and # bottom margins of the scrolling region (DECSTBM) changes. self.cursor_position()
[docs] def set_charset(self, code, mode): """Set active ``G0`` or ``G1`` charset. :param unicode code: character set code, should be a character from ``"B0UK"`` -- otherwise ignored. :param unicode mode: if ``"("`` ``G0`` charset is set, if ``")"`` -- we operate on ``G1``. .. warning:: User-defined charsets are currently not supported. """ if code in cs.MAPS: setattr(self, {"(": "g0_charset", ")": "g1_charset"}[mode], cs.MAPS[code])
[docs] def set_mode(self, *modes, **kwargs): """Sets (enables) a given list of modes. :param list modes: modes to set, where each mode is a constant from :mod:`pyte.modes`. """ # Private mode codes are shifted, to be distingiushed from non # private ones. if kwargs.get("private"): modes = [mode << 5 for mode in modes] self.mode.update(modes) # When DECOLM mode is set, the screen is erased and the cursor # moves to the home position. if mo.DECCOLM in modes: self.resize(columns=132) self.erase_in_display(2) self.cursor_position() # According to `vttest`, DECOM should also home the cursor, see # vttest/main.c:303. if mo.DECOM in modes: self.cursor_position() # Mark all displayed characters as reverse. if mo.DECSCNM in modes: self[:] = ([char._replace(reverse=True) for char in line] for line in self) self.select_graphic_rendition(g._SGR["+reverse"])
[docs] def reset_mode(self, *modes, **kwargs): """Resets (disables) a given list of modes. :param list modes: modes to reset -- hopefully, each mode is a constant from :mod:`pyte.modes`. """ # Private mode codes are shifted, to be distingiushed from non # private ones. if kwargs.get("private"): modes = [mode << 5 for mode in modes] self.mode.difference_update(modes) # Lines below follow the logic in :meth:`set_mode`. if mo.DECCOLM in modes: self.resize(columns=80) self.erase_in_display(2) self.cursor_position() if mo.DECOM in modes: self.cursor_position() if mo.DECSCNM in modes: self[:] = ([char._replace(reverse=False) for char in line] for line in self) self.select_graphic_rendition(g._SGR["-reverse"])
[docs] def shift_in(self): """Activates ``G0`` character set.""" self.charset = self.g0_charset
[docs] def shift_out(self): """Activates ``G1`` character set.""" self.charset = self.g1_charset
[docs] def draw(self, char): """Display a character at the current cursor position and advance the cursor if :data:`~pyte.modes.DECAWM` is set. :param unicode char: a character to display. """ # Translating a given character. char = char.translate(self.charset) # If this was the last column in a line and auto wrap mode is # enabled, move the cursor to the next line. Otherwise replace # characters already displayed with newly entered. if self.cursor.x == self.columns: if mo.DECAWM in self.mode: self.linefeed() else: self.cursor.x -= 1 # If Insert mode is set, new characters move old characters to # the right, otherwise terminal is in Replace mode and new # characters replace old characters at cursor position. if mo.IRM in self.mode: self.insert_characters(1) self[self.cursor.y][self.cursor.x] = self.cursor.attrs._replace(data=char) # .. note:: We can't use :meth:`cursor_forward()`, because that # way, we'll never know when to linefeed. self.cursor.x += 1
[docs] def carriage_return(self): """Move the cursor to the beginning of the current line.""" self.cursor.x = 0
[docs] def index(self): """Move the cursor down one line in the same column. If the cursor is at the last line, create a new line at the bottom. """ top, bottom = self.margins if self.cursor.y == bottom: self.pop(top) self.insert(bottom, take(self.columns, self.default_line)) else: self.cursor_down()
[docs] def reverse_index(self): """Move the cursor up one line in the same column. If the cursor is at the first line, create a new line at the top. """ top, bottom = self.margins if self.cursor.y == top: self.pop(bottom) self.insert(top, take(self.columns, self.default_line)) else: self.cursor_up()
[docs] def linefeed(self): """Performs an index and, if :data:`~pyte.modes.LNM` is set, a carriage return. """ self.index() if mo.LNM in self.mode: self.carriage_return()
[docs] def tab(self): """Move to the next tab space, or the end of the screen if there aren't anymore left. """ for stop in sorted(self.tabstops): if self.cursor.x < stop: column = stop break else: column = self.columns - 1 self.cursor.x = column
[docs] def backspace(self): """Move cursor to the left one or keep it in it's position if it's at the beginning of the line already. """ self.cursor_back()
[docs] def save_cursor(self): """Push the current cursor position onto the stack.""" self.savepoints.append(Savepoint(copy.copy(self.cursor), self.g0_charset, self.g1_charset, self.charset, mo.DECOM in self.mode, mo.DECAWM in self.mode))
[docs] def restore_cursor(self): """Set the current cursor position to whatever cursor is on top of the stack. """ if self.savepoints: savepoint = self.savepoints.pop() self.g0_charset = savepoint.g0_charset self.g1_charset = savepoint.g1_charset self.charset = savepoint.charset if savepoint.origin: self.set_mode(mo.DECOM) if savepoint.wrap: self.set_mode(mo.DECAWM) self.cursor = savepoint.cursor self.ensure_bounds(use_margins=True) else: # If nothing was saved, the cursor moves to home position; # origin mode is reset. :todo: DECAWM? self.reset_mode(mo.DECOM) self.cursor_position()
[docs] def insert_lines(self, count=None): """Inserts the indicated # of lines at line with cursor. Lines displayed **at** and below the cursor move down. Lines moved past the bottom margin are lost. :param count: number of lines to delete. """ count = count or 1 top, bottom = self.margins # If cursor is outside scrolling margins it -- do nothin'. if top <= self.cursor.y <= bottom: # v +1, because xrange() is exclusive. for line in xrange(self.cursor.y, min(bottom + 1, self.cursor.y + count)): self.pop(bottom) self.insert(line, take(self.columns, self.default_line)) self.carriage_return()
[docs] def delete_lines(self, count=None): """Deletes the indicated # of lines, starting at line with cursor. As lines are deleted, lines displayed below cursor move up. Lines added to bottom of screen have spaces with same character attributes as last line moved up. :param int count: number of lines to delete. """ count = count or 1 top, bottom = self.margins # If cursor is outside scrolling margins it -- do nothin'. if top <= self.cursor.y <= bottom: # v -- +1 to include the bottom margin. for _ in xrange(min(bottom - self.cursor.y + 1, count)): self.pop(self.cursor.y) self.insert(bottom, list( repeat(self.cursor.attrs, self.columns))) self.carriage_return()
[docs] def insert_characters(self, count=None): """Inserts the indicated # of blank characters at the cursor position. The cursor does not move and remains at the beginning of the inserted blank characters. Data on the line is shifted forward. :param int count: number of characters to insert. """ count = count or 1 for _ in xrange(min(self.columns - self.cursor.y, count)): self[self.cursor.y].insert(self.cursor.x, self.cursor.attrs) self[self.cursor.y].pop()
[docs] def delete_characters(self, count=None): """Deletes the indicated # of characters, starting with the character at cursor position. When a character is deleted, all characters to the right of cursor move left. Character attributes move with the characters. :param int count: number of characters to delete. """ count = count or 1 for _ in xrange(min(self.columns - self.cursor.x, count)): self[self.cursor.y].pop(self.cursor.x) self[self.cursor.y].append(self.cursor.attrs)
[docs] def erase_characters(self, count=None): """Erases the indicated # of characters, starting with the character at cursor position. Character attributes are set cursor attributes. The cursor remains in the same position. :param int count: number of characters to erase. .. warning:: Even though *ALL* of the VTXXX manuals tsate that character attributes **should be reset to defaults**, ``libvte``, ``xterm`` and ``ROTE`` completely ignore this. Same applies too all ``erase_*()`` and ``delete_*()`` methods. """ count = count or 1 for column in xrange(self.cursor.x, min(self.cursor.x + count, self.columns)): self[self.cursor.y][column] = self.cursor.attrs
[docs] def erase_in_line(self, type_of=0, private=False): """Erases a line in a specific way. :param int type_of: defines the way the line should be erased in: * ``0`` -- Erases from cursor to end of line, including cursor position. * ``1`` -- Erases from beginning of line to cursor, including cursor position. * ``2`` -- Erases complete line. :param bool private: when ``True`` character attributes aren left unchanged **not implemented**. """ interval = ( # a) erase from the cursor to the end of line, including # the cursor, xrange(self.cursor.x, self.columns), # b) erase from the beginning of the line to the cursor, # including it, xrange(0, self.cursor.x + 1), # c) erase the entire line. xrange(0, self.columns) )[type_of] for column in interval: self[self.cursor.y][column] = self.cursor.attrs
[docs] def erase_in_display(self, type_of=0, private=False): """Erases display in a specific way. :param int type_of: defines the way the line should be erased in: * ``0`` -- Erases from cursor to end of screen, including cursor position. * ``1`` -- Erases from beginning of screen to cursor, including cursor position. * ``2`` -- Erases complete display. All lines are erased and changed to single-width. Cursor does not move. :param bool private: when ``True`` character attributes aren left unchanged **not implemented**. """ interval = ( # a) erase from cursor to the end of the display, including # the cursor, xrange(self.cursor.y + 1, self.lines), # b) erase from the beginning of the display to the cursor, # including it, xrange(0, self.cursor.y), # c) erase the whole display. xrange(0, self.lines) )[type_of] for line in interval: self[line][:] = \ (self.cursor.attrs for _ in xrange(self.columns)) # In case of 0 or 1 we have to erase the line with the cursor. if type_of in [0, 1]: self.erase_in_line(type_of)
[docs] def set_tab_stop(self): """Sest a horizontal tab stop at cursor position.""" self.tabstops.add(self.cursor.x)
[docs] def clear_tab_stop(self, type_of=None): """Clears a horizontal tab stop in a specific way, depending on the ``type_of`` value: * ``0`` or nothing -- Clears a horizontal tab stop at cursor position. * ``3`` -- Clears all horizontal tab stops. """ if not type_of: # Clears a horizontal tab stop at cursor position, if it's # present, or silently fails if otherwise. self.tabstops.discard(self.cursor.x) elif type_of == 3: self.tabstops = set() # Clears all horizontal tab stops.
[docs] def ensure_bounds(self, use_margins=None): """Ensure that current cursor position is within screen bounds. :param bool use_margins: when ``True`` or when :data:`~pyte.modes.DECOM` is set, cursor is bounded by top and and bottom margins, instead of ``[0; lines - 1]``. """ if use_margins or mo.DECOM in self.mode: top, bottom = self.margins else: top, bottom = 0, self.lines - 1 self.cursor.x = min(max(0, self.cursor.x), self.columns - 1) self.cursor.y = min(max(top, self.cursor.y), bottom)
[docs] def cursor_up(self, count=None): """Moves cursor up the indicated # of lines in same column. Cursor stops at top margin. :param int count: number of lines to skip. """ self.cursor.y -= count or 1 self.ensure_bounds(use_margins=True)
[docs] def cursor_up1(self, count=None): """Moves cursor up the indicated # of lines to column 1. Cursor stops at bottom margin. :param int count: number of lines to skip. """ self.cursor_up(count) self.carriage_return()
[docs] def cursor_down(self, count=None): """Moves cursor down the indicated # of lines in same column. Cursor stops at bottom margin. :param int count: number of lines to skip. """ self.cursor.y += count or 1 self.ensure_bounds(use_margins=True)
[docs] def cursor_down1(self, count=None): """Moves cursor down the indicated # of lines to column 1. Cursor stops at bottom margin. :param int count: number of lines to skip. """ self.cursor_down(count) self.carriage_return()
[docs] def cursor_back(self, count=None): """Moves cursor left the indicated # of columns. Cursor stops at left margin. :param int count: number of columns to skip. """ self.cursor.x -= count or 1 self.ensure_bounds()
[docs] def cursor_forward(self, count=None): """Moves cursor right the indicated # of columns. Cursor stops at right margin. :param int count: number of columns to skip. """ self.cursor.x += count or 1 self.ensure_bounds()
[docs] def cursor_position(self, line=None, column=None): """Set the cursor to a specific `line` and `column`. Cursor is allowed to move out of the scrolling region only when :data:`~pyte.modes.DECOM` is reset, otherwise -- the position doesn't change. :param int line: line number to move the cursor to. :param int column: column number to move the cursor to. """ column = (column or 1) - 1 line = (line or 1) - 1 # If origin mode (DECOM) is set, line number are relative to # the top scrolling margin. if mo.DECOM in self.mode: line += self.margins.top # Cursor is not allowed to move out of the scrolling region. if not self.margins.top <= line <= self.margins.bottom: return self.cursor.x, self.cursor.y = column, line self.ensure_bounds()
[docs] def cursor_to_column(self, column=None): """Moves cursor to a specific column in the current line. :param int column: column number to move the cursor to. """ self.cursor.x = (column or 1) - 1 self.ensure_bounds()
[docs] def cursor_to_line(self, line=None): """Moves cursor to a specific line in the current column. :param int line: line number to move the cursor to. """ self.cursor.y = (line or 1) - 1 # If origin mode (DECOM) is set, line number are relative to # the top scrolling margin. if mo.DECOM in self.mode: self.cursor.y += self.margins.top # FIXME: should we also restrict the cursor to the scrolling # region? self.ensure_bounds()
[docs] def bell(self, *args): """Bell stub -- the actual implementation should probably be provided by the end-user. """
[docs] def alignment_display(self): """Fills screen with uppercase E's for screen focus and alignment.""" for line in self: for column, char in enumerate(line): line[column] = char._replace(data="E")
[docs] def select_graphic_rendition(self, *attrs): """Set display attributes. :param list attrs: a list of display attributes to set. """ replace = {} for attr in attrs or [0]: if attr in g.FG: replace["fg"] = g.FG[attr] elif attr in g.BG: replace["bg"] = g.BG[attr] elif attr in g.TEXT: attr = g.TEXT[attr] replace[attr[1:]] = attr.startswith("+") elif not attr: replace = self.default_char._asdict() self.cursor.attrs = self.cursor.attrs._replace(**replace)
[docs]class DiffScreen(Screen): """A screen subclass, which maintains a set of dirty lines in its :attr:`dirty` attribute. The end user is responsible for emptying a set, when a diff is applied. .. attribute:: dirty A set of line numbers, which should be re-drawn. >>> screen = DiffScreen(80, 24) >>> screen.dirty.clear() >>> screen.draw(u"!") >>> screen.dirty set([0]) """ def __init__(self, *args): self.dirty = set() super(DiffScreen, self).__init__(*args) def set_mode(self, *modes, **kwargs): if mo.DECSCNM >> 5 in modes and kwargs.get("private"): self.dirty.update(xrange(self.lines)) super(DiffScreen, self).set_mode(*modes, **kwargs) def reset_mode(self, *modes, **kwargs): if mo.DECSCNM >> 5 in modes and kwargs.get("private"): self.dirty.update(xrange(self.lines)) super(DiffScreen, self).reset_mode(*modes, **kwargs) def reset(self): self.dirty.update(xrange(self.lines)) super(DiffScreen, self).reset() def resize(self, *args, **kwargs): self.dirty.update(xrange(self.lines)) super(DiffScreen, self).resize(*args, **kwargs) def draw(self, *args): self.dirty.add(self.cursor.y) super(DiffScreen, self).draw(*args) def index(self): if self.cursor.y == self.margins.bottom: self.dirty.update(xrange(self.lines)) super(DiffScreen, self).index() def reverse_index(self): if self.cursor.y == self.margins.top: self.dirty.update(xrange(self.lines)) super(DiffScreen, self).reverse_index() def insert_lines(self, *args): self.dirty.update(xrange(self.cursor.y, self.lines)) super(DiffScreen, self).insert_lines(*args) def delete_lines(self, *args): self.dirty.update(xrange(self.cursor.y, self.lines)) super(DiffScreen, self).delete_lines(*args) def insert_characters(self, *args): self.dirty.add(self.cursor.y) super(DiffScreen, self).insert_characters(*args) def delete_characters(self, *args): self.dirty.add(self.cursor.y) super(DiffScreen, self).delete_characters(*args) def erase_characters(self, *args): self.dirty.add(self.cursor.y) super(DiffScreen, self).erase_characters(*args) def erase_in_line(self, *args): self.dirty.add(self.cursor.y) super(DiffScreen, self).erase_in_line(*args) def erase_in_display(self, type_of=0): self.dirty.update(( xrange(self.cursor.y + 1, self.lines), xrange(0, self.cursor.y), xrange(0, self.lines) )[type_of]) super(DiffScreen, self).erase_in_display(type_of) def alignment_display(self): self.dirty.update(xrange(self.cursor.y, self.lines)) super(DiffScreen, self).alignment_display()
History = namedtuple("History", "top bottom")
[docs]class HistoryScreen(Screen): """A screen subclass, which keeps track of screen history and allows pagination. This is not linux-specific, but still useful; see page 462 of VT520 User's Manual. :param int pages: total number of pages to keep. .. attribute:: history A pair of history queues for top and bottom margins accordingly. """ def __init__(self, columns, lines, pages=10): super(HistoryScreen, self).__init__(columns, lines) self.page = pages // 2 self.pages = pages self.history = History(deque(maxlen=self.page * self.lines), deque(maxlen=self.page * self.lines))
[docs] def ensure_width(self): """Ensures all lines on a screen have proper width (attr:`columns`). Extra characters are truncated, missing characters are filled with whitespace. """ for idx, line in enumerate(self): if len(line) > self.columns: self[idx] = line[:self.columns] elif len(line) < self.columns: self[idx] = line + take(self.columns - len(line), self.default_line)
[docs] def index(self): """Overloaded, to update top history with the removed lines.""" top, bottom = self.margins if self.cursor.y == bottom: self.history.top.append(self[top]) super(HistoryScreen, self).index()
[docs] def page_up(self): """Moves the screen half-page up. .. note:: If a screen has odd number of lines, the middle point is floored, so for a 5-line screen only the first two lines are saved. """ if self.page > 0: mid = int(math.floor(self.lines / 2.)) self.history.bottom.extendleft(reversed(self[mid:])) self.page -= 1 self[:] = list(reversed([ self.history.top.pop() for _ in xrange(self.lines - mid) ])) + self[:mid] self.ensure_width()
[docs] def page_down(self): """Moves the screen half-page down. .. note:: If a screen has odd number of lines, the middle point is ceiled, so for a 5-line screen only the last two lines are saved. """ if self.page < self.pages: mid = int(math.ceil(self.lines / 2.)) self.history.top.extend(self[:mid]) self.page +=1 self[:] = self[mid:] + [ self.history.bottom.popleft() for _ in xrange(mid) ] self.ensure_width()