]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: handle ANSI clear/home sequences on VGA console
authorTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 05:56:08 +0000 (02:56 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 05:56:08 +0000 (02:56 -0300)
src/drivers/vga_console.c

index d3c71b5b846fd72e808147996024283cbbae43d6..e8ac448bef025c374becbc83c915a49f80922798 100644 (file)
@@ -109,10 +109,61 @@ static void render_scrollback_view(void) {
     hal_video_set_cursor(VGA_HEIGHT, 0);
 }
 
+static int ansi_state = 0; /* 0=normal, 1=after ESC, 2=after ESC[, 3=after ESC[2 */
+
 static void vga_put_char_unlocked(char c) {
     /* Any new output auto-returns to live view */
     vga_unscroll();
 
+    /* Minimal ANSI support for common terminal clear/home sequences.
+     * Handles:
+     *   ESC [ 2 J  (clear screen)
+     *   ESC [ H    (cursor home)
+     */
+    if (ansi_state != 0) {
+        if (ansi_state == 1) {
+            if (c == '[') {
+                ansi_state = 2;
+                return;
+            }
+            ansi_state = 0;
+        }
+        if (ansi_state == 2) {
+            if (c == 'H') {
+                term_col = 0;
+                term_row = 0;
+                ansi_state = 0;
+                return;
+            }
+            if (c == '2') {
+                ansi_state = 3;
+                return;
+            }
+            ansi_state = 0;
+        }
+        if (ansi_state == 3) {
+            if (c == 'J') {
+                uint16_t blank = (uint16_t)' ' | (uint16_t)term_color << 8;
+                for (int i = 0; i < VGA_CELLS; i++) {
+                    shadow[i] = blank;
+                }
+                dirty_mark(0, VGA_CELLS - 1);
+                term_col = 0;
+                term_row = 0;
+                view_offset = 0;
+                sb_count = 0;
+                sb_head = 0;
+            }
+            ansi_state = 0;
+            return;
+        }
+    }
+
+    if ((unsigned char)c == 0x1B) {
+        ansi_state = 1;
+        return;
+    }
+
     switch (c) {
     case '\n':
         term_col = 0;