From 484e013ceb239c37d762f10aa262b6cbd2ef73c9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 25 Sep 2024 22:25:43 +0200 Subject: [PATCH] add: color cmd --- headers/terminal.h | 5 ++++ src/shell/commands/color_cmd.c | 36 +++++++++++++++++++++++++ src/terminal/put.c | 48 +++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/shell/commands/color_cmd.c diff --git a/headers/terminal.h b/headers/terminal.h index ab9c3f8..6fca460 100644 --- a/headers/terminal.h +++ b/headers/terminal.h @@ -16,6 +16,7 @@ struct screen { size_t column; uint8_t color; uint16_t buffer[VGA_WIDTH * VGA_HEIGHT]; + uint8_t default_color; char line[256]; }; @@ -53,3 +54,7 @@ struct key_event terminal_getkey(void); void update_cursor(void); void move_cursor(int direction); void set_color_level(int level); +void terminal_set_default_fg_color(uint8_t fg_color); +void terminal_set_default_bg_color(uint8_t fg_color); +void terminal_change_default_fg_color(uint8_t color); +uint8_t terminal_get_default_color(void); \ No newline at end of file diff --git a/src/shell/commands/color_cmd.c b/src/shell/commands/color_cmd.c new file mode 100644 index 0000000..759401c --- /dev/null +++ b/src/shell/commands/color_cmd.c @@ -0,0 +1,36 @@ +#include "kprintf.h" +#include "string.h" +#include "terminal.h" +#include + +#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0]) + +void color_cmd(char *arg) +{ + uint8_t tmp_color; + static const char *colors[] = { + "BLACK", "BLUE", "GREEN", "CYAN", + "RED", "MAGENTA", "BROWN", "LIGHT_GREY", + "DARK_GREY", "LIGHT_BLUE", "LIGHT_GREEN", "LIGHT_CYAN", + "LIGHT_RED", "LIGHT_MAGENTA", "LIGHT_YELLOW", "WHITE", + }; + for (size_t i = 0; i < ARRAY_SIZE(colors); i++) { + if (strcmp(colors[i], arg) == 0) { + terminal_change_default_fg_color(i); + return; + } + } + tmp_color = terminal_get_default_color(); + kprintf(KERN_WARNING "Invalid argument\navailable color: "); + for (size_t i = 0; i < ARRAY_SIZE(colors); i++) { + terminal_set_default_fg_color(i); + // Unai the STRING MERGE argument of kprintf cause me a + // cramptissue. I lost all my skibidi aura to fix it but it work + // (; + kprintf(colors[i]); + if (i != ARRAY_SIZE(colors) - 1) + kprintf(", "); + } + terminal_set_default_fg_color(tmp_color); + kprintf("\n"); +} \ No newline at end of file diff --git a/src/terminal/put.c b/src/terminal/put.c index 7dd90cb..bc8faaf 100644 --- a/src/terminal/put.c +++ b/src/terminal/put.c @@ -65,6 +65,21 @@ void terminal_set_color(uint8_t color) screen->color = color; } +void terminal_set_default_fg_color(uint8_t fg_color) +{ + screen->default_color = (screen->default_color & 0xF0) | fg_color; +} + +void terminal_set_default_bg_color(uint8_t bg_color) +{ + screen->default_color = bg_color << 4 | (screen->default_color & 0x0F); +} + +uint8_t terminal_get_default_color(void) +{ + return screen->default_color; +} + void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { const size_t index = y * VGA_WIDTH + x; @@ -87,6 +102,35 @@ static void terminal_new_line(void) terminal_scroll(); } +static uint8_t get_entry_color(uint16_t vga_entry) +{ + return vga_entry >> 8; +} + +void terminal_change_default_color(uint8_t color) +{ + terminal_set_color(color); + for (size_t y = 0; y < VGA_HEIGHT; y++) { + for (size_t x = 0; x < VGA_WIDTH; x++) { + const size_t index = y * VGA_WIDTH + x; + uint8_t entry_color = get_entry_color(TERM_BUF[index]); + TERM_BUF[index] = vga_entry( + TERM_BUF[index], + entry_color == screen->default_color ? color + : entry_color); + } + } + memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); + screen->default_color = color; + screen->color = color; +} + +void terminal_change_default_fg_color(uint8_t fg_color) +{ + terminal_set_fg_color(fg_color); + terminal_change_default_color(screen->color); +} + void terminal_clear(void) { for (size_t y = 0; y < VGA_HEIGHT; y++) { @@ -179,7 +223,9 @@ void set_color_level(int level) VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN, VGA_COLOR_LIGHT_GREY}; - if (level == 0) + if (level == 0) { terminal_set_color(screen->default_color); + return; + } terminal_set_fg_color(color_translation[level]); }