API Reference

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:

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
class bg

Background Colors.

class fg

Forderground Colors.

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)
class cursor

Cursor Movement.

back()

cursor back (CUB)

move cursor n cells back.

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)

horizontal_absolute()

cursor horizontal absolute (CHA)

moves cursor to column n.

Parameters:

value (string) – column to move to. (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)

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)

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)

up()

cursor up (CUU)

move cursor n cells up.

Parameters:

value (string) – cells to move (default: 1)

device_status_report = '\x1b[6n'

device status report (DSR)

request Cursor Position Report (CPR).

terminal answers / transmitts report: "ESC[n;mR" n = row m = column

classmethod device_status_report_parse(input_string)

Parse Device Status Report. (Cursor Position Report / CPR)

"ESC[n;mR" n = row m = column

Parameters:

input_string (string) – raw report

Return (int row, int column):

cursor position.

device_status_report_regex = re.compile('.\\[(\\d*);(\\d*)R')

Regex to match against the report returned by terminal.

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_down()

scroll down (SD)

scroll hole page down by n lines. (new lines added at top)

Parameters:

value (string) – lines to scroll down

scroll_up()

scroll up (SU)

scroll hole page up by n lines. (new lines added at bottom)

Parameters:

value (string) – lines to scroll up

class ansi_escape_code.ANSIControllsBase

Base Class for ANSI color and control sequences.

classmethod get_flat_list()

Get a flattend list of all control characters in dict.

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.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.filter_ansi_controlls(data)

Remove ANSI controll characters.

Parameters:

data (string) – input data to filter.

Return string:

filtered result.

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_flat_list(obj_dict)

Get a flattend list of all control characters in dict.

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.read_serial_until(*, serial, read_end='R', timeout=0.1)

Read serial until finding ‘read_end’.

ansi_escape_code.run_tests()

Run a bunch of tests.

ansi_escape_code.test_control()

Test for control sequences.

print some test cases.

ansi_escape_code.test_filtering()

Test for filter_ansi_controlls.

print some test cases.

ansi_escape_code.test_get_terminal_size()

Test get_terminal_size.