From 278e5bad7fd4230e923f59290f92fd0bb1755a2e Mon Sep 17 00:00:00 2001
From: hh3-rasheed <Haish2.Rasheed@live.uwe.ac.uk>
Date: Wed, 23 Oct 2024 22:52:46 +0500
Subject: [PATCH] Added newline and scrolling support and color output

---
 kernel.c | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/kernel.c b/kernel.c
index e41db16..145511a 100644
--- a/kernel.c
+++ b/kernel.c
@@ -84,12 +84,31 @@ void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
   terminal_buffer[index] = make_vgaentry(c, color);
 }
 
+void terminal_scroll() {
+  for (size_t y = 1; y < VGA_HEIGHT; y++) {
+    for (size_t x = 0; x < VGA_WIDTH; x++) {
+      terminal_buffer[(y - 1) * VGA_WIDTH + x] = terminal_buffer[y * VGA_WIDTH + x];
+    }
+  }
+  for (size_t x = 0; x < VGA_WIDTH; x++) {
+    terminal_buffer[(VGA_HEIGHT - 1) * VGA_WIDTH + x] = make_vgaentry(' ', terminal_color);
+  }
+  terminal_row--;
+}
+
 void terminal_putchar(char c) {
-  terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
-  if (++terminal_column == VGA_WIDTH) {
+  if (c == '\n') {
     terminal_column = 0;
     if (++terminal_row == VGA_HEIGHT) {
-      terminal_row = 0;
+      terminal_scroll();
+    }
+  } else {
+    terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
+    if (++terminal_column == VGA_WIDTH) {
+      terminal_column = 0;
+      if (++terminal_row == VGA_HEIGHT) {
+        terminal_scroll();
+      }
     }
   }
 }
@@ -111,5 +130,22 @@ void kernel_main() {
    * yet, '\n' will produce some VGA specific character instead.
    * This is normal.
    */
+
+  terminal_setcolor(make_color(COLOR_BLUE, COLOR_BLACK));
+  terminal_writestring("Hello, kernel World!\n");
+  terminal_setcolor(make_color(COLOR_RED, COLOR_BLACK));
   terminal_writestring("Hello, kernel World!\n");
+  terminal_setcolor(make_color(COLOR_WHITE, COLOR_BLACK));
+  for (int i = 0; i < 6; i++) {
+    terminal_writestring("Hello, kernel World!\n");
+  }
+  terminal_setcolor(make_color(COLOR_BLUE, COLOR_BLACK));
+  for (int i = 0; i < 18; i++) {
+    terminal_writestring("Hello, kernel World!\n");  
+  }
+  terminal_setcolor(make_color(COLOR_GREEN, COLOR_BLACK));
+  for (int i = 0; i < 5; i++) {
+    terminal_writestring("Hello, kernel World!\n");  
+  }
+
 }
-- 
GitLab