add: color cmd

This commit is contained in:
starnakin 2024-09-25 22:25:43 +02:00
parent 02059bff33
commit 484e013ceb
3 changed files with 88 additions and 1 deletions

View File

@ -16,6 +16,7 @@ struct screen {
size_t column; size_t column;
uint8_t color; uint8_t color;
uint16_t buffer[VGA_WIDTH * VGA_HEIGHT]; uint16_t buffer[VGA_WIDTH * VGA_HEIGHT];
uint8_t default_color;
char line[256]; char line[256];
}; };
@ -53,3 +54,7 @@ struct key_event terminal_getkey(void);
void update_cursor(void); void update_cursor(void);
void move_cursor(int direction); void move_cursor(int direction);
void set_color_level(int level); 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);

View File

@ -0,0 +1,36 @@
#include "kprintf.h"
#include "string.h"
#include "terminal.h"
#include <stdint.h>
#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");
}

View File

@ -65,6 +65,21 @@ void terminal_set_color(uint8_t color)
screen->color = 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) void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{ {
const size_t index = y * VGA_WIDTH + x; const size_t index = y * VGA_WIDTH + x;
@ -87,6 +102,35 @@ static void terminal_new_line(void)
terminal_scroll(); 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) void terminal_clear(void)
{ {
for (size_t y = 0; y < VGA_HEIGHT; y++) { 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_BLUE, VGA_COLOR_GREEN,
VGA_COLOR_LIGHT_GREY}; VGA_COLOR_LIGHT_GREY};
if (level == 0) if (level == 0) {
terminal_set_color(screen->default_color); terminal_set_color(screen->default_color);
return;
}
terminal_set_fg_color(color_translation[level]); terminal_set_fg_color(color_translation[level]);
} }