API Reference¶
Command sequences. |
|
SGR attributes. |
|
Mode parameter for |
|
Erase all or part of line/screen (cursor position does not change). |
|
Move the cursor. |
|
Move the cursor to the top left corner of the screen. |
|
Clear screen. |
|
Hide the cursor. |
|
Return if nestable |
|
Save the cursor position. |
|
Use the alternate screen buffer. |
|
Return SGR parameters for foreground color. |
|
Return SGR parameters for background color. |
|
Build a string with SGR control sequences. |
|
Print a string with SGR control sequences. |
|
Return the plain text without the SGR control sequences. |
|
Return the difference between |
|
Set the delimiters for attributes. |
|
Parse the specification string. |
-
class
ansictrls.
CS
¶ The attributes of this class are control sequences. Most of them can take parameters. Their meaning and number is explained in the documentation of the attributes. The default value for
ED
,EL
andSGR
is0
. For the others it is1
.The attributes support the modulo operator (
%
); the result is astr
. The right operand can be a single value or a tuple of values. The attributes can also be used as strings which is the same as using an empty tuple with the%
operator.>>> CS.CUU % 3 '\x1b[3A' >>> CS.CUP % (4, 2) '\x1b[4;2H' >>> CS.CUP % () '\x1b[H' >>> str(CS.CUP) '\x1b[H'
-
ICH
¶ Insert
N
space characters
-
CUU
¶ Move cursor
N
rows up
-
CUD
¶ Move cursor
N
rows down
-
CUF
¶ Move cursor
N
columns foreward
-
CUB
¶ Move cursor
N
columns backward
-
CNL
¶ Move cursor to beginning of line
N
rows down
-
CPL
¶ Move cursor to beginning of line
N
rows up
-
CHA
¶ Move cursor to column
N
in current row
-
CUP
¶ Move cursor to the absolute position
N,M
(rowN
, columnM
)
-
CHT
¶ Move cursor
N
tab stops forward
-
ED
¶ Erase in display (cursor position does not change).
N=0
: From cursor to end of screenN=1
: From cursor to beginning of screenN=2
: Entire screenN=3
: All lines in scrollback buffer
-
EL
¶ Erase in line (cursor position does not change).
N=0
: From cursor to end of lineN=1
: From cursor to beginning of lineN=2
: Entire line
-
IL
¶ Insert
N
rows
-
DL
¶ Delete
N
rows
-
DCH
¶ Delete
N
characters
-
SU
¶ Scroll page
N
rows up (add new lines at bottom)
-
SD
¶ Scroll page
N
rows down (add new lines at top)
-
ECH
¶ Erase
N
characters
-
CBT
¶ Move cursor
N
tab stops backward
-
HPA
¶ Move cursor to column
N
in current row
-
VPA
¶ Move cursor to row
N
in current column
-
SGR
¶ Select graphic rendition. This control sequence can take multiple parameters. It is more convenient to use the attributes of the
SGR
class.
-
SCP
¶ Save cursor position
-
RCP
¶ Restore cursor position
-
SCU
¶ Show cursor
-
HCU
¶ Hide cursor
-
EAS
¶ Enable alternate screen buffer
-
DAS
¶ Disable alternate screen buffer
-
RIS
¶ Reset screen to initial state
-
-
class
ansictrls.
SGR
¶ The attributes of this class are parameters for
CS.SGR
and thesgr_string()
andsgr_print()
functions. They support the~
unary operator. For attributes likeBold
this means that it will be switched off; for colors that bright colors are used.-
Bold
¶ Attribute bold
-
Faint
¶ Attribute faint
-
Italic
¶ Attribute italic
-
Underlined
¶ Attribute underlined
-
Blink
¶ Attribute blink
-
Inverse
¶ Attribute inverse
-
Crossedout
¶ Attribute crossedout
-
Overlined
¶ Attribute overlined
-
Reset
¶ Attribute reset
-
FgBlack
¶ Foreground color black
-
FgRed
¶ Foreground color red
-
FgGreen
¶ Foreground color green
-
FgYellow
¶ Foreground color yellow
-
FgBlue
¶ Foreground color blue
-
FgMagenta
¶ Foreground color magenta
-
FgCyan
¶ Foreground color cyan
-
FgWhite
¶ Foreground color white
-
FgDefault
¶ Foreground color default
-
BgBlack
¶ Background color black
-
BgRed
¶ Background color red
-
BgGreen
¶ Background color green
-
BgYellow
¶ Background color yellow
-
BgBlue
¶ Background color blue
-
BgMagenta
¶ Background color magenta
-
BgCyan
¶ Background color cyan
-
BgWhite
¶ Background color white
-
BgDefault
¶ Background color default
-
-
class
ansictrls.
EraseMode
(value)¶ Mode parameter for
erase()
.-
SCRN_END
= 0¶ From cursor to end of screen
-
SCRN_BEGIN
= 1¶ From cursor to beginning of screen
-
SCRN_ALL
= 2¶ Entire screen
-
SCRN_LINES
= 3¶ All lines in scrollback buffer
-
LINE_END
= 4¶ From cursor to end of line
-
LINE_BEGIN
= 5¶ From cursor to beginning of line
-
LINE_ALL
= 6¶ Entire line
-
-
ansictrls.
erase
(mode)¶ Erase all or part of line/screen (cursor position does not change).
- Parameters
mode (EraseMode) – what to erase
-
ansictrls.
move
(row=0, col=0, rel=True)¶ Move the cursor.
The cursor can be moved relative (
rel=True
) or absolute (rel=False
). Ifrow=0
the cursor will be moved within the current row; ifcol=0
the cursor will be moved in the current column. Absoluterow
andcol
values start at 1 and cannot be negative. For relative movements negative numbers mean to the left or up and positive numbers move the cursor to the right or down. If only one ofrow
orcol
is relative and the other is absolute, therel
parameter has to be a tuple:rel=(True, False)
means relative row and absolute column movement andrel=(False, True)
absolute row and relative column movement.move(col=2) # move cursor two columns to the right # and leave the row unchanged move(1, 1, False) # move cursor to the top left corner move(-5, 1, (True, False) # move cursor to the beginning of the # line five rows up
- Parameters
- Raises
TypeError – if row or col are not of type
int
ValueError – if not at least one of
row
orcol
is not 0, or ifrel=False
androw
orcol
are negative
-
ansictrls.
home
()¶ Move the cursor to the top left corner of the screen.
-
ansictrls.
clear
(reset=False)¶ Clear screen.
Erase entire screen and move the cursor to the top left corner of the screen.
- Parameters
reset (bool) – if set to True the line buffer will be erased too
-
ansictrls.
hide_cursor
()¶ Hide the cursor.
This function is a context manager for use in
with
statements. It hides the cursor when the context is entered and shows it again when the context is exited.
-
ansictrls.
nestable_save_pos_possible
()¶ Return if nestable
save_pos()
context is supported.- Returns
True if supported
- Return type
-
ansictrls.
save_pos
(nestable=False)¶ Save the cursor position.
This function is a context manager for use in
with
statements. It saves the cursor position when the context is entered and restores the cursor to the saved position when the context is exited.If
nestable=False
the ANSI control sequencesESC[s: SCP - Save Cursor Position
andESC[u: RCP - Restore Cursor Position
will be used. By doing so only the saved cursor position of the innermost context will be restored.If
nestable=True
andnestable_save_pos_possible()
returnsTrue
, each time the context is entered the position is determined by callinggetyx()
from the term package. If the terminal does not support the control sequenceESC[6n: DSR – Device Status Report
this is not possible.- Parameters
nestable (bool) – wether a nestable context should be used
- Raises
RuntimeError – if
nestable=True
and nestable context not possible
-
ansictrls.
alternate_screen
()¶ Use the alternate screen buffer.
This function is a context manager for use in
with
statements. It switches to the alternate screen buffer when the context is entered and back to the normal screen buffer when the context is exited.
-
ansictrls.
fg
(*args)¶ Return SGR parameters for foreground color.
- Parameters
args – arguments for color
- Returns
color for use with
CS.SGR
orsgr_print()
-
ansictrls.
bg
(*args)¶ Return SGR parameters for background color.
- Parameters
args – arguments for color
- Returns
color for use with
CS.SGR
orsgr_print()
-
ansictrls.
sgr_string
(*args, reset=True)¶ Build a string with SGR control sequences.
Those arguments that are
SGR
attributes or the result of thefg()
orbg()
functions will be passed as parameters toCS.SGR
, all others will be converted to strings. Than all elements will be concatenated and returned as a string.>>> CS.SGR % (SGR.Bold, SGR.FgRed) + 'ABC' + CS.SGR % SGR.Reset '\x1b[1;31mABC\x1b[0m' >>> sgr_string(SGR.Bold, SGR.FgRed, 'ABC') '\x1b[1;31mABC\x1b[0m'
-
ansictrls.
sgr_print
(*args, reset=True, end='\n', flush=False)¶ Print a string with SGR control sequences.
See function
sgr_string()
.
-
ansictrls.
text
(s)¶ Return the plain text without the SGR control sequences.
-
ansictrls.
len_diff
(s)¶ Return the difference between
len(s)
andlen(text(s))
.Useful for adjusting the text on the screen:
s = sgr_string(SGR.Bold, SGR.FgRed, 'ABC') w = 20 print(repr(s)) print(text(s)) print('1234567890' * 2) # numbering 20 columns print(s.center(w, '.')) print(s.center(w + len_diff(s), '.')) print('%*s' % (w, s)) print('%*s' % (w + len_diff(s), s)) print('{:.^{width}}'.format(s, width=w)) print('{:.^{width}}'.format(s, width=w + len_diff(s)))
'\x1b[1;31mABC\x1b[0m' ABC 12345678901234567890 ...ABC... .........ABC........ ABC ABC ...ABC... ........ABC.........
-
ansictrls.
set_attributes_delimiters
(start='#[', end=']')¶ Set the delimiters for attributes.
See: specification string.
- Parameters
- Raises
TypeError – if one of the delimiters is not a string
ValueError – if one of the delimiters is an empty string
-
ansictrls.
parse
(spec, strict=False)¶ Parse the specification string.
- Parameters
- Returns
string with SGR control sequences
- Return type
- Raises
ValueError – if
strict=True
and an unkown attribute is encountered