Skip to content

Add support for VT100 terminal color ESC sequences #10105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions shared-bindings/terminalio/Terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
//| * ``ESC [ #### D`` - Move the cursor to the left by ####
//| * ``ESC [ 2 J`` - Erase the entire display
//| * ``ESC [ nnnn ; mmmm H`` - Move the cursor to mmmm, nnnn.
//| * ``ESC [ nn m`` - Set the terminal display attributes.
//| * ``ESC [ nn ; nn m`` - Set the terminal display attributes.
//|
//| Supported Display attributes:
//| 0 - Reset all attributes
//| Foreground Colors Background Colors
//| 30 - Black 40 - Black
//| 31 - Red 41 - Red
//| 32 - Green 42 - Green
//| 33 - Yellow 43 - Yellow
//| 34 - Blue 44 - Blue
//| 35 - Magenta 45 - Magenta
//| 36 - Cyan 46 - Cyan
//| 37 - White 47 - White
//| """
//|
//| def __init__(
Expand Down
42 changes: 42 additions & 0 deletions shared-module/terminalio/Terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "shared-module/fontio/BuiltinFont.h"
#include "shared-bindings/displayio/TileGrid.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/terminalio/Terminal.h"

#if CIRCUITPY_STATUS_BAR
Expand Down Expand Up @@ -46,6 +47,22 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
return len;
}

uint32_t _select_color(uint16_t ascii_color) {
uint32_t color_value = 0;
if ((ascii_color & 1) > 0) {
color_value += 0xff0000;
}
if ((ascii_color & 2) > 0) {
color_value += 0x00ff00;
}
if ((ascii_color & 4) > 0) {
color_value += 0x0000ff;
}

return color_value;
}

displayio_palette_t *terminal_palette = self->scroll_area->pixel_shader;
const byte *i = data;
uint16_t start_y = self->cursor_y;
while (i < data + len) {
Expand Down Expand Up @@ -127,6 +144,15 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
common_hal_displayio_tilegrid_set_all_tiles(self->scroll_area, 0);
}
}
if (c == 'm') {
if ((n >= 40 && n <= 47) || (n >= 30 && n <= 37)) {
common_hal_displayio_palette_set_color(terminal_palette, 1 - (n / 40), _select_color(n % 10));
}
if (n == 0) {
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
}
}
if (c == ';') {
uint16_t m = 0;
for (++j; j < 9; j++) {
Expand Down Expand Up @@ -155,6 +181,22 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
self->cursor_y = n;
start_y = self->cursor_y;
}
if (c == 'm') {
if ((n >= 40 && n <= 47) || (n >= 30 && n <= 37)) {
common_hal_displayio_palette_set_color(terminal_palette, 1 - (n / 40), _select_color(n % 10));
}
if (n == 0) {
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
}
if ((m >= 40 && m <= 47) || (m >= 30 && m <= 37)) {
common_hal_displayio_palette_set_color(terminal_palette, 1 - (m / 40), _select_color(m % 10));
}
if (m == 0) {
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
}
}
}
i += j + 1;
continue;
Expand Down