Simple test¶
Ensure your device works with this simple test.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# SPDX-FileCopyrightText: Copyright (c) 2022 Stefan Krüger for s-light
5#
6# SPDX-License-Identifier: Unlicense
7
8import sys
9import time
10import ansi_escape_code as terminal
11
12
13##########################################
14# main
15
16
17def test_colors():
18 """Test colors."""
19 test_string = (
20 terminal.ANSIColors.fg.lightblue
21 + "Hello "
22 + terminal.ANSIColors.fg.green
23 + "World "
24 + terminal.ANSIColors.fg.orange
25 + ":-)"
26 + terminal.ANSIColors.reset
27 )
28 print("test_string", test_string)
29
30
31def test_control():
32 """Test control."""
33 test_string = (
34 terminal.ANSIControl.cursor.previous_line(2)
35 + "WOOO"
36 + terminal.ANSIControl.cursor.next_line(1)
37 + terminal.ANSIControl.erase_line()
38 + ":-)"
39 )
40 print(test_string)
41 time.sleep(1)
42 print("this is a line.")
43 print("this is a second line.")
44 time.sleep(1)
45 print(terminal.ANSIControl.cursor.previous_line(3), end="")
46 time.sleep(1)
47 print(terminal.ANSIControl.cursor.horizontal_absolute(20), end="")
48 print("ping", end="")
49 time.sleep(1)
50 print(terminal.ANSIControl.erase_line(), end="")
51
52 print("we erased a line...")
53
54
55##########################################
56# main
57
58
59def main():
60 """Main."""
61 # wait some time untill the computer / terminal is ready
62 for _i in range(10):
63 # print(".", end="")
64 print(".")
65 time.sleep(0.5 / 10)
66 print("")
67 print(42 * "*")
68 print("ansi_escape_code__cursor_position_test.py")
69 print("Python Version: " + sys.version)
70 print(42 * "*")
71 print("run")
72
73 print("test_colors")
74 test_colors()
75 print("test_colors")
76 test_colors()
77 print("test_colors")
78 test_colors()
79 print("test_control")
80 test_control()
81 print("all tests done.")
82 time.sleep(1)
83
84
85##########################################
86if __name__ == "__main__":
87 main()
88
89##########################################
Cursor Position¶
Shows an example of cursor movements
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# SPDX-FileCopyrightText: Copyright (c) 2022 Stefan Krüger for s-light
5#
6# SPDX-License-Identifier: Unlicense
7
8"""Test Cursor Movement."""
9
10import time
11import sys
12import board
13import usb_cdc
14import ansi_escape_code as terminal
15
16##########################################
17# globals
18
19
20##########################################
21# functions
22def read_serial():
23 serial = usb_cdc.console
24 available = serial.in_waiting
25 buffer = ""
26 while available:
27 raw = serial.read(available)
28 text = raw.decode("utf-8")
29 buffer += text
30 available = serial.in_waiting
31 return buffer
32
33
34def read_serial_until(read_end="R", timeout=0.1):
35 serial = usb_cdc.console
36 end_time = time.monotonic() + timeout
37 buffer = ""
38 while end_time >= time.monotonic() and not buffer.endswith(read_end):
39 raw = serial.read(serial.in_waiting)
40 buffer += raw.decode("utf-8")
41 return buffer
42
43
44def check_input():
45 serial = usb_cdc.console
46 # enable nonblocking read
47 serial.timeout = 0
48
49 buffer = read_serial()
50 print("buffer:", repr(buffer))
51
52 # get cursor
53 print(
54 "terminal.ANSIControl.device_status_report",
55 repr(terminal.ANSIControl.device_status_report),
56 )
57
58 print("set cursor to max & wait & read answer")
59
60 print(terminal.ANSIControl.cursor.position("999;999"), end="")
61 time.sleep(2)
62 serial.write(bytearray(terminal.ANSIControl.device_status_report))
63
64 # serial.timeout = 0.1
65 # raw = serial.read(7)
66 # buffer = raw.decode("utf-8")
67 buffer = read_serial_until(read_end="R", timeout=0.1)
68
69 # ESC[n;mR
70 # available = serial.in_waiting
71 # while available < 1:
72 # print("wait 0.1s")
73 # time.sleep(0.1)
74 #
75 # buffer = read_serial()
76 print("answer:", repr(buffer))
77
78 row, col = terminal.ANSIControl.device_status_report_parse(buffer)
79 print("row: {}; col: {}".format(row, col))
80
81 print("wait 2s")
82 time.sleep(2)
83
84
85##########################################
86# main
87
88
89def main():
90 """Main."""
91 # wait some time untill the computer / terminal is ready
92 for _i in range(10):
93 # print(".", end="")
94 print(".")
95 time.sleep(0.5 / 10)
96 print("")
97 print(42 * "*")
98 print("ansi_escape_code__cursor_position_test.py")
99 print("Python Version: " + sys.version)
100 print("board: " + board.board_id)
101 print(42 * "*")
102 print("run")
103
104 running = True
105 while running:
106 try:
107 serial = usb_cdc.console
108 if serial.connected:
109 check_input()
110 except KeyboardInterrupt as e:
111 print("KeyboardInterrupt - Stop Program.", e)
112 running = False
113 else:
114 pass
115
116
117##########################################
118if __name__ == "__main__":
119 main()
120
121##########################################
Background Color Example¶
Illustrate the ability to change the background color
1# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2#
3# SPDX-License-Identifier: Unlicense
4
5import random
6import time
7import ansi_escape_code as terminal
8
9color = [
10 terminal.ANSIColors.fg.black,
11 terminal.ANSIColors.fg.red,
12 terminal.ANSIColors.fg.green,
13 terminal.ANSIColors.fg.orange,
14 terminal.ANSIColors.fg.blue,
15 terminal.ANSIColors.fg.purple,
16 terminal.ANSIColors.fg.cyan,
17 terminal.ANSIColors.fg.lightgrey,
18 terminal.ANSIColors.fg.darkgrey,
19 terminal.ANSIColors.fg.lightred,
20 terminal.ANSIColors.fg.lightgreen,
21 terminal.ANSIColors.fg.yellow,
22 terminal.ANSIColors.fg.lightblue,
23 terminal.ANSIColors.fg.pink,
24 terminal.ANSIColors.fg.lightcyan,
25]
26
27background = [
28 terminal.ANSIColors.bg.black,
29 terminal.ANSIColors.bg.red,
30 terminal.ANSIColors.bg.green,
31 terminal.ANSIColors.bg.orange,
32 terminal.ANSIColors.bg.blue,
33 terminal.ANSIColors.bg.purple,
34 terminal.ANSIColors.bg.cyan,
35 terminal.ANSIColors.bg.lightgrey,
36]
37
38while True:
39 """Test colors."""
40 test_string = (
41 random.choice(background)
42 + random.choice(color)
43 + "Hello "
44 + random.choice(color)
45 + "World "
46 + random.choice(color)
47 + ":-)"
48 + terminal.ANSIColors.reset
49 )
50 print(test_string)
51 time.sleep(2)