1#include "shell.h"
 2
 3// Forward declarations of drawing functions from kmain.c (or better, make a header)
 4extern void draw_string(int x, int y, char* str, unsigned char color);
 5extern void draw_char(int x, int y, char c, unsigned char color);
 6extern void draw_pixel(int x, int y, unsigned char color);
 7
 8#define MAX_BUFFER 64
 9char command_buffer[MAX_BUFFER];
10int buffer_index = 0;
11
12int cursor_x = 10;
13int cursor_y = 10;
14
15void clear_screen_area() {
16	// Clear the shell area (for simplicity, we'll just draw a black rectangle)
17	for (int y = 0; y < 200; y++) {
18		for (int x = 0; x < 320; x++) {
19			draw_pixel(x, y, 0);
20		}
21	}
22	cursor_x = 10;
23	cursor_y = 10;
24}
25
26void print_prompt() {
27	draw_string(cursor_x, cursor_y, "> ", 14); // Yellow prompt
28	cursor_x += 16;
29}
30
31void init_shell() {
32	clear_screen_area();
33	print_prompt();
34}
35
36void execute_command() {
37	command_buffer[buffer_index] = 0;
38	cursor_x = 10;
39	cursor_y += 12;
40
41	if (buffer_index == 0) {
42		// Empty command
43	} else if (command_buffer[0] == 'c' && command_buffer[1] == 'l' && command_buffer[2] == 's') {
44		clear_screen_area();
45	} else if (command_buffer[0] == 'i' && command_buffer[1] == 'n' && command_buffer[2] == 'f' && command_buffer[3] == 'o') {
46		for (int y = 0; y < 200; y++) {
47			for (int x = 0; x < 320; x++) {
48				draw_pixel(x, y, (unsigned char)(x + y));
49			}
50		}
51		draw_string(100, 80, "Hello World!", 15);
52		draw_string(80, 100, "TinyOS VGA Mode 13h", 14);
53		draw_string(110, 120, "256 Colors", 10);
54		cursor_x = 10;
55		cursor_y = 140; // Move cursor below the text
56	} else if (command_buffer[0] == 'h' && command_buffer[1] == 'e' && command_buffer[2] == 'l' && command_buffer[3] == 'p') {
57		draw_string(cursor_x, cursor_y, "Commands: help, cls, info", 11);
58		cursor_y += 12;
59	} else {
60		draw_string(cursor_x, cursor_y, "Unknown command", 12);
61		cursor_y += 12;
62	}
63
64	buffer_index = 0;
65	print_prompt();
66
67	// Simple scrolling check
68	if (cursor_y > 180) {
69		clear_screen_area();
70		print_prompt();
71	}
72}
73
74void shell_handle_char(char c) {
75	if (c == '\n') {
76		execute_command();
77	} else if (c == '\b') {
78		if (buffer_index > 0) {
79			buffer_index--;
80			cursor_x -= 8;
81			// Manually clear the character area with black pixels
82			for (int i = 0; i < 8; i++) {
83				for (int j = 0; j < 8; j++) {
84					draw_pixel(cursor_x + j, cursor_y + i, 0);
85				}
86			}
87		}
88	} else if (buffer_index < MAX_BUFFER - 1) {
89		command_buffer[buffer_index++] = c;
90		draw_char(cursor_x, cursor_y, c, 15); // White text
91		cursor_x += 8;
92	}
93
94	// Wrap check
95	if (cursor_x > 300) {
96		cursor_x = 10;
97		cursor_y += 12;
98	}
99}