Simple test

Ensure your device works with this simple test.

examples/ansi_escape_code_simpletest.py
 1#!/usr/bin/env python3
 2
 3# SPDX-FileCopyrightText: Copyright (c) 2022 Stefan Krüger for s-light
 4#
 5# SPDX-License-Identifier: Unlicense
 6
 7import sys
 8import time
 9
10import ansi_escape_code as terminal
11
12##########################################
13# main
14
15
16def test_colors():
17    """Test colors."""
18    test_string = (
19        terminal.ANSIColors.fg.lightblue
20        + "Hello "
21        + terminal.ANSIColors.fg.green
22        + "World "
23        + terminal.ANSIColors.fg.orange
24        + ":-)"
25        + terminal.ANSIColors.reset
26    )
27    print("test_string", test_string)
28
29
30def test_control():
31    """Test control."""
32    test_string = (
33        terminal.ANSIControl.cursor.previous_line(2)
34        + "WOOO"
35        + terminal.ANSIControl.cursor.next_line(1)
36        + terminal.ANSIControl.erase_line()
37        + ":-)"
38    )
39    print(test_string)
40    time.sleep(1)
41    print("this is a line.")
42    print("this is a second line.")
43    time.sleep(1)
44    print(terminal.ANSIControl.cursor.previous_line(3), end="")
45    time.sleep(1)
46    print(terminal.ANSIControl.cursor.horizontal_absolute(20), end="")
47    print("ping", end="")
48    time.sleep(1)
49    print(terminal.ANSIControl.erase_line(), end="")
50
51    print("we erased a line...")
52
53
54##########################################
55# main
56
57
58def main():
59    """Main."""
60    # wait some time untill the computer / terminal is ready
61    for _i in range(10):
62        # print(".", end="")
63        print(".")
64        time.sleep(0.5 / 10)
65    print("")
66    print(42 * "*")
67    print("ansi_escape_code__cursor_position_test.py")
68    print("Python Version: " + sys.version)
69    print(42 * "*")
70    print("run")
71
72    print("test_colors")
73    test_colors()
74    print("test_colors")
75    test_colors()
76    print("test_colors")
77    test_colors()
78    print("test_control")
79    test_control()
80    print("all tests done.")
81    time.sleep(1)
82
83
84##########################################
85if __name__ == "__main__":
86    main()
87
88##########################################

Cursor Position

Shows an example of cursor movements

examples/ansi_escape_code__cursor_position_dev.py
  1#!/usr/bin/env python3
  2
  3# SPDX-FileCopyrightText: Copyright (c) 2022 Stefan Krüger for s-light
  4#
  5# SPDX-License-Identifier: Unlicense
  6
  7"""Test Cursor Movement."""
  8
  9import sys
 10import time
 11
 12import board
 13import usb_cdc
 14
 15import ansi_escape_code as terminal
 16
 17##########################################
 18# globals
 19
 20
 21##########################################
 22# functions
 23def read_serial():
 24    serial = usb_cdc.console
 25    available = serial.in_waiting
 26    buffer = ""
 27    while available:
 28        raw = serial.read(available)
 29        text = raw.decode("utf-8")
 30        buffer += text
 31        available = serial.in_waiting
 32    return buffer
 33
 34
 35def read_serial_until(read_end="R", timeout=0.1):
 36    serial = usb_cdc.console
 37    end_time = time.monotonic() + timeout
 38    buffer = ""
 39    while end_time >= time.monotonic() and not buffer.endswith(read_end):
 40        raw = serial.read(serial.in_waiting)
 41        buffer += raw.decode("utf-8")
 42    return buffer
 43
 44
 45def check_input():
 46    serial = usb_cdc.console
 47    # enable nonblocking read
 48    serial.timeout = 0
 49
 50    buffer = read_serial()
 51    print("buffer:", repr(buffer))
 52
 53    # get cursor
 54    print(
 55        "terminal.ANSIControl.device_status_report",
 56        repr(terminal.ANSIControl.device_status_report),
 57    )
 58
 59    print("set cursor to max & wait & read answer")
 60
 61    print(terminal.ANSIControl.cursor.position("999;999"), end="")
 62    time.sleep(2)
 63    serial.write(bytearray(terminal.ANSIControl.device_status_report))
 64
 65    # serial.timeout = 0.1
 66    # raw = serial.read(7)
 67    # buffer = raw.decode("utf-8")
 68    buffer = read_serial_until(read_end="R", timeout=0.1)
 69
 70    # ESC[n;mR
 71    # available = serial.in_waiting
 72    # while available < 1:
 73    #     print("wait 0.1s")
 74    #     time.sleep(0.1)
 75    #
 76    # buffer = read_serial()
 77    print("answer:", repr(buffer))
 78
 79    row, col = terminal.ANSIControl.device_status_report_parse(buffer)
 80    print(f"row: {row}; col: {col}")
 81
 82    print("wait 2s")
 83    time.sleep(2)
 84
 85
 86##########################################
 87# main
 88
 89
 90def main():
 91    """Main."""
 92    # wait some time untill the computer / terminal is ready
 93    for _i in range(10):
 94        # print(".", end="")
 95        print(".")
 96        time.sleep(0.5 / 10)
 97    print("")
 98    print(42 * "*")
 99    print("ansi_escape_code__cursor_position_test.py")
100    print("Python Version: " + sys.version)
101    print("board: " + board.board_id)
102    print(42 * "*")
103    print("run")
104
105    running = True
106    while running:
107        try:
108            serial = usb_cdc.console
109            if serial.connected:
110                check_input()
111        except KeyboardInterrupt as e:
112            print("KeyboardInterrupt - Stop Program.", e)
113            running = False
114        else:
115            pass
116
117
118##########################################
119if __name__ == "__main__":
120    main()
121
122##########################################

Background Color Example

Illustrate the ability to change the background color

examples/ansi_escape_code_background_test.py
 1# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
 2#
 3# SPDX-License-Identifier: Unlicense
 4
 5import random
 6import time
 7
 8import ansi_escape_code as terminal
 9
10color = [
11    terminal.ANSIColors.fg.black,
12    terminal.ANSIColors.fg.red,
13    terminal.ANSIColors.fg.green,
14    terminal.ANSIColors.fg.orange,
15    terminal.ANSIColors.fg.blue,
16    terminal.ANSIColors.fg.purple,
17    terminal.ANSIColors.fg.cyan,
18    terminal.ANSIColors.fg.lightgrey,
19    terminal.ANSIColors.fg.darkgrey,
20    terminal.ANSIColors.fg.lightred,
21    terminal.ANSIColors.fg.lightgreen,
22    terminal.ANSIColors.fg.yellow,
23    terminal.ANSIColors.fg.lightblue,
24    terminal.ANSIColors.fg.pink,
25    terminal.ANSIColors.fg.lightcyan,
26]
27
28background = [
29    terminal.ANSIColors.bg.black,
30    terminal.ANSIColors.bg.red,
31    terminal.ANSIColors.bg.green,
32    terminal.ANSIColors.bg.orange,
33    terminal.ANSIColors.bg.blue,
34    terminal.ANSIColors.bg.purple,
35    terminal.ANSIColors.bg.cyan,
36    terminal.ANSIColors.bg.lightgrey,
37]
38
39while True:
40    """Test colors."""
41    test_string = (
42        random.choice(background)
43        + random.choice(color)
44        + "Hello "
45        + random.choice(color)
46        + "World "
47        + random.choice(color)
48        + ":-)"
49        + terminal.ANSIColors.reset
50    )
51    print(test_string)
52    time.sleep(2)