ansi_escape_code
¶
simple helper library for common ANSI escape codes
- inspired / based on information from
Author(s): Stefan Krüger
Implementation Notes¶
Hardware:
Software and Dependencies:
- Adafruit CircuitPython firmware
Python3
- terminal with support for escape codes / sequences
(tested with GTKTerm)
- ansi_escape_code.create_seq(control, esc='\x1b[')¶
Control sequences function generator.
- Parameters:
control (string) – control characters.
esc (string) – escape character. Default:
\033[
- Return lambda:
function generator with predefined control sequences.
- ansi_escape_code.create_color(color)¶
Create color sequences.
- Parameters:
color (string) – color number (as string).
- Return string:
ready to use color control character string.
- ansi_escape_code.get_flat_list(obj_dict)¶
Get a flattend list of all control characters in dict.
- class ansi_escape_code.ANSIControllsBase¶
Base Class for ANSI color and control sequences.
- esc = '\x1b['¶
- classmethod get_flat_list()¶
Get a flattend list of all control characters in dict.
- class ansi_escape_code.ANSIColors¶
ANSI Color and Font-Effect control sequences.
usage:
# colors ANSIColors.fg.red ANSIColors.bg.green # font effect ANSIColors.bold # reste ANSIColors.reset
- reset = '\x1b[0m'¶
- bold = '\x1b[01m'¶
- disable = '\x1b[02m'¶
- underline = '\x1b[04m'¶
- reverse = '\x1b[07m'¶
- strikethrough = '\x1b[09m'¶
- invisible = '\x1b[08m'¶
- class fg¶
Forderground Colors.
- black = '\x1b[30m'¶
- red = '\x1b[31m'¶
- green = '\x1b[32m'¶
- orange = '\x1b[33m'¶
- blue = '\x1b[34m'¶
- purple = '\x1b[35m'¶
- cyan = '\x1b[36m'¶
- lightgrey = '\x1b[37m'¶
- darkgrey = '\x1b[90m'¶
- lightred = '\x1b[91m'¶
- lightgreen = '\x1b[92m'¶
- yellow = '\x1b[93m'¶
- lightblue = '\x1b[94m'¶
- pink = '\x1b[95m'¶
- lightcyan = '\x1b[96m'¶
- class ansi_escape_code.ANSIControl¶
ANSI Cursor movement.
please make sure your terminal supports these control sequences. tested with GTKTerm:
usage:
ANSIControl.erease_line() ANSIControl.cursor.up(5)
- erase_display()¶
erase display (ED)
clear part of screen.
- Parameters:
value (string) –
0
(=default): clear from cursor to end of screen.1
: clear from cursor to beginning of screen.2
: clear entire screen. move cursor to 1;1.3
: as 2 and delete all lines in scrollback buffer.
- erase_line()¶
erase line (EL)
erease part of line.
- Parameters:
value (string) –
0
(=default): clear from cursor to end of line.1
: clear from cursor to beginning of line.2
: clear entire line. cursor position does not change.
- scroll_up()¶
scroll up (SU)
scroll hole page up by n lines. (new lines added at bottom)
- Parameters:
value (string) – lines to scroll up
- scroll_down()¶
scroll down (SD)
scroll hole page down by n lines. (new lines added at top)
- Parameters:
value (string) – lines to scroll down
- device_status_report = '\x1b[6n'¶
device status report (DSR)
request Cursor Position Report (CPR).
terminal answers / transmitts report:
"ESC[n;mR"
n
= rowm
= column
- device_status_report_regex = re.compile('.\\[(\\d*);(\\d*)R')¶
Regex to match against the report returned by terminal.
- classmethod device_status_report_parse(input_string)¶
Parse Device Status Report. (Cursor Position Report / CPR)
"ESC[n;mR"
n
= rowm
= column- Parameters:
input_string (string) – raw report
- Return (int row, int column):
cursor position.
- class cursor¶
Cursor Movement.
- up()¶
cursor up (CUU)
move cursor n cells up.
- Parameters:
value (string) – cells to move (default: 1)
- down()¶
cursor down (CUD)
move cursor n cells down.
- Parameters:
value (string) – cells to move (default: 1)
- forward()¶
cursor forward (CUF)
move cursor n cells forward.
- Parameters:
value (string) – cells to move (default: 1)
- back()¶
cursor back (CUB)
move cursor n cells back.
- Parameters:
value (string) – cells to move (default: 1)
- next_line()¶
cursor next line (CNL)
move cursor n lines up and to beginning of the line.
- Parameters:
value (string) – lines to move (default: 1)
- previous_line()¶
cursor previous line (CPL)
move cursor n lines down and to beginning of the line.
- Parameters:
value (string) – lines to move (default: 1)
- horizontal_absolute()¶
cursor horizontal absolute (CHA)
moves cursor to column n.
- Parameters:
value (string) – column to move to. (default: 1)
- position()¶
cursor position (CUP)
set cursor position absolute. Format:
"n;m"
moves cursor to row n, column m. omitted n or m default to 1.- Parameters:
value (string) – position to move to. (default: “1;1” = top left)
- ansi_escape_code.read_serial_until(*, serial, read_end='R', timeout=0.1)¶
Read serial until finding ‘read_end’.
- ansi_escape_code.get_cursor_pos(*, serial)¶
Get Cursor Position by reading from the terminal
this function has side effects on the given serial connection. it resets the buffers and sets the timeout to 0.
- Return (int row, int column):
absolut position in row and column.
- ansi_escape_code.get_terminal_size(*, serial)¶
Get Terminal Size by setting cursor to max and then reading position.
this function has side effects on the given serial connection. it resets the buffers and sets the timeout to 0.
- Return (int rows, int columns):
rows and columns count.
- ansi_escape_code.filter_ansi_controlls(data)¶
Remove ANSI controll characters.
- Parameters:
data (string) – input data to filter.
- Return string:
filtered result.
- ansi_escape_code.test_filtering()¶
Test for filter_ansi_controlls.
print some test cases.
- ansi_escape_code.test_control()¶
Test for control sequences.
print some test cases.
- ansi_escape_code.test_get_terminal_size()¶
Test get_terminal_size.
- ansi_escape_code.run_tests()¶
Run a bunch of tests.