pyte implements a mix of VT100, VT220 and VT520 specification, and aims to support most of the TERM=linux functionality.
Two classes: Stream, which parses the command stream and dispatches events for commands, and Screen which, when used with a stream maintains a buffer of strings representing the screen of a terminal.
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: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
This module provides three stream implementations with different features; for starters, here’s a quick example of how streams are typically used:
>>> import pyte
>>>
>>> class Dummy(object):
... def __init__(self):
... self.y = 0
...
... def cursor_up(self, count=None):
... self.y += count or 1
...
>>> dummy = Dummy()
>>> stream = pyte.Stream()
>>> stream.attach(dummy)
>>> stream.feed(u"[5A") # Move the cursor up 5 rows.
>>> dummy.y
5
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
A stream is a state machine that parses a stream of characters and dispatches events based on what it sees.
Note
Stream only accepts unicode strings as input, but if, for some reason, you need to feed it with byte strings, consider using ByteStream instead.
See also
Control sequences, which don’t require any arguments.
non-CSI escape sequences.
“sharp” escape sequences – ESC # <N>.
CSI escape sequences – CSI P1;P2;...;Pn <fn>.
Consume a single unicode character and advance the state as necessary.
Parameters: | char (unicode) – a unicode character to consume. |
---|
Consume a unicode string and advance the state as necessary.
Parameters: | chars (unicode) – a unicode string to feed from. |
---|
Adds a given screen to the listeners queue.
Parameters: |
|
---|
Removes a given screen from the listeners queue and failes silently if it’s not attached.
Parameters: | screen (pyte.screens.Screen) – a screen to detach. |
---|
Dispatch an event.
Event handlers are looked up implicitly in the listeners’ __dict__, so, if a listener only wants to handle DRAW events it should define a draw() method or pass only=["draw"] argument to attach().
Warning
If any of the attached listeners throws an exception, the subsequent callbacks are be aborted.
Parameters: |
|
---|
A stream, which takes bytes strings (instead of unicode) as input and tries to decode them using a given list of possible encodings. It uses codecs.IncrementalDecoder internally, so broken bytes is not an issue.
By default, the following decoding strategy is used:
>>> stream = ByteStream()
>>> stream.feed(b"foo".decode("utf-8"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pyte/streams.py", line 323, in feed
"%s requires input in bytes" % self.__class__.__name__)
TypeError: ByteStream requires input in bytes
>>> stream.feed(b"foo")
Parameters: | encodings (list) – a list of (encoding, errors) pairs, where the first element is encoding name, ex: "utf-8" and second defines how decoding errors should be handeld; see str.decode() for possible values. |
---|
Stream, which dumps a subset of the dispatched events to a given file-like object (sys.stdout by default).
>>> stream = DebugStream()
>>> stream.feed("[1;24r[4l[24;1H[0;10m")
SET_MARGINS 1; 24
RESET_MODE 4
CURSOR_POSITION 24; 1
SELECT_GRAPHIC_RENDITION 0; 10
Parameters: |
|
---|
This module provides classes for terminal screens, currently it contains three screens with different features:
Note
It would be nice to split those features into mixin classes, rather than subclasses, but it’s not obvious how to do – feel free to submit a pull request.
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
Screen cursor.
Parameters: |
|
---|
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.
Top and bottom screen margins, defining the scrolling region; the actual values are top and bottom line.
Current charset number; can be either 0 or 1 for G0 and G1 respectively, note that G0 is activated by default.
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.
See also
Standard ECMA-48, Section 6.1.1
For a description of the presentational component, implemented by Screen.
A plain empty character with default foreground and background colors.
An inifinite sequence of default characters, used for populating new lines and columns.
Resets the terminal to its initial state.
Note
Neither VT220 nor VT102 manuals mentioned that terminal modes and tabstops should be reset as well, thanks to xterm – we now know that.
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.
Parameters: |
|
---|
Selects top and bottom margins for the scrolling region.
Margins determine which screen lines move during scrolling (see index() and reverse_index()). Characters added outside the scrolling region do not cause the screen to scroll.
Parameters: |
|
---|
Set active G0 or G1 charset.
Parameters: |
|
---|
Warning
User-defined charsets are currently not supported.
Sets (enables) a given list of modes.
Parameters: | modes (list) – modes to set, where each mode is a constant from pyte.modes. |
---|
Resets (disables) a given list of modes.
Parameters: | modes (list) – modes to reset – hopefully, each mode is a constant from pyte.modes. |
---|
Display a character at the current cursor position and advance the cursor if DECAWM is set.
Parameters: | char (unicode) – a character to display. |
---|
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.
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.
Move cursor to the left one or keep it in it’s position if it’s at the beginning of the line already.
Set the current cursor position to whatever cursor is on top of the stack.
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.
Parameters: | count – number of lines to delete. |
---|
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.
Parameters: | count (int) – number of lines to delete. |
---|
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.
Parameters: | count (int) – number of characters to insert. |
---|
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.
Parameters: | count (int) – number of characters to delete. |
---|
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.
Parameters: | count (int) – number of characters to erase. |
---|
Warning
Even though ALL of the VTXXX manuals state that character attributes should be reset to defaults, libvte, xterm and ROTE completely ignore this. Same applies too all erase_*() and delete_*() methods.
Erases a line in a specific way.
Parameters: |
|
---|
Erases display in a specific way.
Parameters: |
|
---|
Clears a horizontal tab stop in a specific way, depending on the type_of value:
Ensure that current cursor position is within screen bounds.
Parameters: | use_margins (bool) – when True or when DECOM is set, cursor is bounded by top and and bottom margins, instead of [0; lines - 1]. |
---|
Moves cursor up the indicated # of lines in same column. Cursor stops at top margin.
Parameters: | count (int) – number of lines to skip. |
---|
Moves cursor up the indicated # of lines to column 1. Cursor stops at bottom margin.
Parameters: | count (int) – number of lines to skip. |
---|
Moves cursor down the indicated # of lines in same column. Cursor stops at bottom margin.
Parameters: | count (int) – number of lines to skip. |
---|
Moves cursor down the indicated # of lines to column 1. Cursor stops at bottom margin.
Parameters: | count (int) – number of lines to skip. |
---|
Moves cursor left the indicated # of columns. Cursor stops at left margin.
Parameters: | count (int) – number of columns to skip. |
---|
Moves cursor right the indicated # of columns. Cursor stops at right margin.
Parameters: | count (int) – number of columns to skip. |
---|
Set the cursor to a specific line and column.
Cursor is allowed to move out of the scrolling region only when DECOM is reset, otherwise – the position doesn’t change.
Parameters: |
|
---|
Moves cursor to a specific column in the current line.
Parameters: | column (int) – column number to move the cursor to. |
---|
Moves cursor to a specific line in the current column.
Parameters: | line (int) – line number to move the cursor to. |
---|
A screen subclass, which maintains a set of dirty lines in its dirty attribute. The end user is responsible for emptying a set, when a diff is applied.
A set of line numbers, which should be re-drawn.
>>> screen = DiffScreen(80, 24)
>>> screen.dirty.clear()
>>> screen.draw(u"!")
>>> screen.dirty
set([0])
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.
Parameters: |
|
---|
A pair of history queues for top and bottom margins accordingly; here’s the overall screen structure:
[ 1: .......]
[ 2: .......] <- top history
[ 3: .......]
------------
[ 4: .......] s
[ 5: .......] c
[ 6: .......] r
[ 7: .......] e
[ 8: .......] e
[ 9: .......] n
------------
[10: .......]
[11: .......] <- bottom history
[12: .......]
Note
Don’t forget to update Stream class with appropriate escape sequences – you can use any, since pagination protocol is not standardized, for example:
Stream.escape["N"] = "next_page"
Stream.escape["P"] = "prev_page"
Overloaded to reset screen history state: history position is reset to bottom of both queues; queues themselves are emptied.
This module defines terminal mode switches, used by Screen. There’re two types of terminal modes:
The latter are shifted 5 times to the right, to be easily distinguishable from the former ones; for example Origin Mode – DECOM is 192 not 6.
>>> DECOM
192
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
Line Feed/New Line Mode: When enabled, causes a received LF, pyte.control.FF, or VT to move the cursor to the first column of the next line.
Insert/Replace Mode: When enabled, new display characters move old display characters to the right. Characters moved past the right margin are lost. Otherwise, new display characters replace old display characters at the cursor position.
Text Cursor Enable Mode: determines if the text cursor is visible.
Screen Mode: toggles screen-wide reverse-video mode.
Origin Mode: allows cursor addressing relative to a user-defined origin. This mode resets when the terminal is powered up or reset. It does not affect the erase in display (ED) function.
Auto Wrap Mode: selects where received graphic characters appear when the cursor is at the right margin.
Column Mode: selects the number of columns per line (80 or 132) on the screen.
This module defines simple control sequences, recognized by Stream, the set of codes here is for TERM=linux which is a superset of VT102.
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
Space: Not suprisingly – " ".
Null: Does nothing.
Bell: Beeps.
Backspace: Backspace one column, but not past the begining of the line.
Horizontal tab: Move cursor to the next tab stop, or to the end of the line if there is no earlier tab stop.
Linefeed: Give a line feed, and, if pyte.modes.LNM (new line mode) is set also a carriage return.
Carriage return: Move cursor to left margin on current line.
Shift out: Activate G1 character set.
Shift in: Activate G0 character set.
Cancel: Interrupt escape sequence. If received during an escape or control sequence, cancels the sequence and displays substitution character.
Escape: Starts an escape sequence.
Delete: Is ingored.
Control sequence introducer: An equavalent for ESC [.
This module defines bot CSI and non-CSI escape sequences, recognized by Stream and subclasses.
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
Reset.
Index: Move cursor down one line in same column. If the cursor is at the bottom margin, the screen performs a scroll-up.
Next line: Same as pyte.control.LF.
Tabulation set: Set a horizontal tab stop at cursor position.
Reverse index: Move cursor up one line in same column. If the cursor is at the top margin, the screen performs a scroll-down.
Save cursor: Save cursor position, character attribute (graphic rendition), character set, and origin mode selection (see DECRC).
Restore cursor: Restore previously saved cursor position, character attribute (graphic rendition), character set, and origin mode selection. If none were saved, move cursor to home position.
Alignment display: Fill screen with uppercase E’s for testing screen focus and alignment.
Insert character: Insert the indicated # of blank characters.
Cursor up: Move cursor up the indicated # of lines in same column. Cursor stops at top margin.
Cursor down: Move cursor down the indicated # of lines in same column. Cursor stops at bottom margin.
Cursor forward: Move cursor right the indicated # of columns. Cursor stops at right margin.
Cursor back: Move cursor left the indicated # of columns. Cursor stops at left margin.
Cursor next line: Move cursor down the indicated # of lines to column 1.
Cursor previous line: Move cursor up the indicated # of lines to column 1.
Cursor horizontal align: Move cursor to the indicated column in current line.
Cursor position: Move cursor to the indicated line, column (origin at 1, 1).
Erase data (default: from cursor to end of line).
Erase in line (default: from cursor to end of line).
Insert line: Insert the indicated # of blank lines, starting from the current line. Lines displayed below cursor move down. Lines moved past the bottom margin are lost.
Delete line: Delete the indicated # of lines, starting from the current line. 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 move up.
Delete character: Delete the indicated # of characters on the current line. When character is deleted, all characters to the right of cursor move left.
Erase character: Erase the indicated # of characters on the current line.
Vertical position adjust: Move cursor to the indicated line, current column.
Tabulation clear: Clears a horizontal tab stop at cursor position.
Set mode.
Reset mode.
Select graphics rendition: The terminal can display the following character attributes that change the character display without changing the character (see pyte.graphics).
Select top and bottom margins: Selects margins, defining the scrolling region; parameters are top and bottom line. If called without any arguments, whole screen is used.
This module defines graphic-related constants, mostly taken from console_codes(4) and http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html.
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
A mapping of ANSI text style codes to style names, “+” means the: attribute is set, “-” – reset; example:
>>> text[1]
'+bold'
>>> text[9]
'+strikethrough'
A mapping of ANSI foreground color codes to color names, example:
>>> FG[30]
'black'
>>> FG[38]
'default'
A mapping of ANSI background color codes to color names, example:
>>> BG[40]
'black'
>>> BG[48]
'default'
This module defines G0 and G1 charset mappings the same way they are defined for linux terminal, see linux/drivers/tty/consolemap.c @ http://git.kernel.org
Note
VT100_MAP and IBMPC_MAP were taken unchanged from linux kernel source and therefore are licensed under GPL.
copyright: |
|
---|---|
license: | LGPL, see LICENSE for more details. |
Latin1.
VT100 graphic character set.
IBM Codepage 437.
VAX42 character set.