add: backspace work with vbe

This commit is contained in:
Starnakin 2024-12-18 23:56:17 +01:00
parent 77928aca4b
commit 4a99b82e15
4 changed files with 53 additions and 26 deletions

View File

@ -3,4 +3,3 @@
#define PROMPT "> " #define PROMPT "> "
void shell_init(void); void shell_init(void);
void auto_complete(void);

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "font.h"
#include "icon.h"
#include "keyboard.h" #include "keyboard.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -19,6 +21,8 @@ struct screen {
uint32_t bg_color; uint32_t bg_color;
uint16_t buffer[VGA_WIDTH * VGA_HEIGHT]; uint16_t buffer[VGA_WIDTH * VGA_HEIGHT];
uint32_t default_color; uint32_t default_color;
struct icon *backgound;
struct font *font;
char line[256]; char line[256];
}; };
@ -62,3 +66,4 @@ void terminal_set_default_bg_color(uint32_t fg_color);
void terminal_change_default_fg_color(uint32_t color); void terminal_change_default_fg_color(uint32_t color);
uint32_t terminal_get_default_color(void); uint32_t terminal_get_default_color(void);
uint8_t terminal_get_char(int column, int row); uint8_t terminal_get_char(int column, int row);
void terminal_remove_last_char(void);

View File

@ -2,7 +2,6 @@
#include "alloc.h" #include "alloc.h"
#include "commands.h" #include "commands.h"
#include "ctype.h"
#include "font.h" #include "font.h"
#include "kprintf.h" #include "kprintf.h"
#include "shell.h" #include "shell.h"
@ -58,7 +57,7 @@ void help_cmd(char *arg)
kprintf("%s\n", BORDER); kprintf("%s\n", BORDER);
} }
void auto_complete(void) static void auto_complete(void)
{ {
const size_t len = strlen(screen->line); const size_t len = strlen(screen->line);
int nb_matches = 0; int nb_matches = 0;
@ -85,38 +84,36 @@ static char *get_line(void)
{ {
size_t i = 0; size_t i = 0;
struct key_event ev; struct key_event ev;
const size_t size = sizeof(screen->line);
while (1) { while (1) {
ev = get_key(); ev = get_key();
if (!ev.scan_code)
continue;
if (ev.c == '\n') if (ev.c == '\n')
break; break;
char *buf = screen->line; char *buf = screen->line;
i = strlen(screen->line); i = strlen(screen->line);
const size_t size = sizeof(screen->line); if (ev.scan_code == KEY_BACKSPACE) {
if (ev.scan_code) { if (!i)
if (ev.scan_code == KEY_BACKSPACE) { continue;
if (!i) terminal_remove_last_char();
continue; buf[--i] = '\0';
buf[--i] = '\0'; } else if (ev.scan_code == KEY_TAB && i) {
move_cursor(LEFT); auto_complete();
/* terminal_refresh(); */ } else if (ev.c && i < size - 1) {
move_cursor(LEFT); kprintf("%c", ev.c);
} else if (ev.scan_code == KEY_TAB && i) { buf[i++] = ev.c;
auto_complete();
} else if (ev.c && i < size - 1) {
kprintf("%c", ev.c);
buf[i++] = ev.c;
}
if (i >= size)
break;
} }
if (i >= size)
break;
} }
kprintf("\n"); kprintf("\n");
screen->line[i] = '\0'; screen->line[i] = '\0';
return screen->line; return screen->line;
} }
void shell_init() void shell_init(void)
{ {
while (1) { while (1) {
kprintf(PROMPT); kprintf(PROMPT);

View File

@ -2,7 +2,6 @@
#include "debug.h" #include "debug.h"
#include "font.h" #include "font.h"
#include "fonts/eating_pasta_regular_13.h" #include "fonts/eating_pasta_regular_13.h"
#include "fonts/minecraft_medium_13.h"
#include "icons/image.h" #include "icons/image.h"
#include "kprintf.h" #include "kprintf.h"
#include "shell.h" #include "shell.h"
@ -17,8 +16,6 @@
static struct screen screens[TERM_COUNT]; static struct screen screens[TERM_COUNT];
struct screen *screen = &screens[0]; struct screen *screen = &screens[0];
struct font *current_font = eating_pasta_regular_13_font;
struct icon *terminal_bg = &image_icon;
static struct font get_font_node(int c); static struct font get_font_node(int c);
static void terminal_scroll(void); static void terminal_scroll(void);
@ -31,6 +28,35 @@ void terminal_initialize(void)
screens[i].column = 0; screens[i].column = 0;
screens[i].default_color = 0xffffff; screens[i].default_color = 0xffffff;
screens[i].fg_color = screens[i].default_color; screens[i].fg_color = screens[i].default_color;
screens[i].backgound = &image_icon;
screens[i].font = eating_pasta_regular_13_font;
}
// draw_icon(0, 0, screen->backgound);
}
void terminal_remove_last_char(void)
{
if (screen->column)
screen->column--;
else if (screen->row) {
screen->column = VGA_WIDTH - 1;
screen->row--;
}
uint32_t pos_x = (screen->column) * FONT_SIZE;
uint32_t pos_y = screen->row * FONT_SIZE;
struct font node = get_font_node(
screen->buffer[screen->row * VGA_WIDTH + screen->column]);
screen->buffer[screen->row * VGA_WIDTH + screen->column] = '\0';
for (uint32_t y = 0; y < node.height; y++) {
for (uint32_t x = 0; x < node.width; x++) {
struct icon *bg = screen->backgound;
uint32_t pixel = 0;
if (bg->width > pos_x + x && bg->height > pos_y + y)
pixel = bg->pixels[(pos_y + y) * bg->width +
(pos_x + x)];
put_pixel(pixel, pos_x + x, pos_y + y);
}
} }
} }
@ -90,7 +116,7 @@ void terminal_putentryat(struct font node, uint32_t fg_color, uint32_t bg_color,
static struct font get_font_node(int c) static struct font get_font_node(int c)
{ {
return current_font[c]; return screen->font[c];
} }
/* void terminal_refresh(void) */ /* void terminal_refresh(void) */