1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
//! Terminal - Console output with cursor and scrolling
const ascii = @import("../../common/constants/ascii.zig");
const boot = @import("../../boot/multiboot/multiboot.zig");
const colors = @import("../../common/constants/colors.zig");
const font = @import("../fonts/psf.zig");
const pixel = @import("../../utils/graphics/pixel.zig");
const terminal_limits = @import("../../common/limits/terminal.zig");
const video = @import("../video/video.zig");
const LineType = enum { Hard, Soft };
var fb: ?boot.FramebufferInfo = null;
var cursor_x: u32 = 0;
var cursor_y: u32 = 0;
var char_width: u32 = terminal_limits.DEFAULT_CHAR_WIDTH;
var char_height: u32 = terminal_limits.DEFAULT_CHAR_HEIGHT;
var max_line_width: u32 = 0;
var line_types: [terminal_limits.MAX_LINES]LineType = [_]LineType{.Hard} ** terminal_limits.MAX_LINES;
var current_line: usize = 0;
pub fn init(framebuffer: boot.FramebufferInfo) void {
fb = framebuffer;
cursor_x = 0;
cursor_y = 0;
current_line = 0;
char_width = font.get_width();
char_height = font.get_height();
max_line_width = pixel.line_width(framebuffer);
video.init(framebuffer);
video.clear(colors.BLACK);
reset_line_types();
}
fn reset_line_types() void {
for (&line_types) |*lt| {
lt.* = .Hard;
}
}
pub fn put_char(char: u8) void {
put_char_color(char, colors.WHITE);
}
pub fn put_char_color(char: u8, color: u32) void {
const f = fb orelse return;
switch (char) {
ascii.NEWLINE => handle_newline(f),
ascii.BACKSPACE => handle_backspace(f),
ascii.TAB => handle_tab(f),
else => handle_printable(f, char, color),
}
}
fn handle_newline(f: boot.FramebufferInfo) void {
cursor_x = 0;
cursor_y += char_height;
mark_line(.Hard);
check_scroll(f);
}
fn handle_backspace(f: boot.FramebufferInfo) void {
if (cursor_x >= char_width) {
cursor_x -= char_width;
clear_char_at_cursor(f);
} else if (cursor_x == 0 and cursor_y > 0) {
const line_num = cursor_y / char_height;
if (line_num > 0 and line_num < terminal_limits.MAX_LINES) {
if (line_types[line_num] == .Soft) {
cursor_y -= char_height;
current_line = cursor_y / char_height;
cursor_x = ((max_line_width - char_width) / char_width) * char_width;
clear_char_at_cursor(f);
}
}
}
}
fn handle_tab(f: boot.FramebufferInfo) void {
cursor_x += char_width * terminal_limits.TAB_WIDTH;
if (cursor_x >= max_line_width) {
cursor_x = 0;
cursor_y += char_height;
mark_line(.Soft);
check_scroll(f);
}
}
fn handle_printable(f: boot.FramebufferInfo, char: u8, color: u32) void {
if (char < ascii.PRINTABLE_START or char > ascii.PRINTABLE_END) return;
if (cursor_x + char_width > max_line_width) {
cursor_x = 0;
cursor_y += char_height;
mark_line(.Soft);
check_scroll(f);
}
font.render_char(char, cursor_x, cursor_y, f, color);
cursor_x += char_width;
}
fn mark_line(line_type: LineType) void {
const new_line = cursor_y / char_height;
if (new_line != current_line and new_line < terminal_limits.MAX_LINES) {
line_types[new_line] = line_type;
}
current_line = new_line;
}
fn check_scroll(f: boot.FramebufferInfo) void {
if (cursor_y + char_height >= f.height) {
scroll(f);
}
}
fn clear_char_at_cursor(f: boot.FramebufferInfo) void {
pixel.fill_rect(f, cursor_x, cursor_y, char_width, char_height, colors.BLACK);
}
fn scroll(f: boot.FramebufferInfo) void {
var dst_y: u32 = 0;
var src_y: u32 = char_height;
while (src_y < f.height) : ({
src_y += 1;
dst_y += 1;
}) {
pixel.copy_row(f, dst_y, src_y);
}
while (dst_y < f.height) : (dst_y += 1) {
pixel.clear_row(f, dst_y, colors.BLACK);
}
cursor_y -= char_height;
var i: usize = 1;
while (i < terminal_limits.MAX_LINES) : (i += 1) {
line_types[i - 1] = line_types[i];
}
line_types[terminal_limits.MAX_LINES - 1] = .Hard;
if (current_line > 0) current_line -= 1;
}
pub fn print(text: []const u8) void {
for (text) |char| {
put_char(char);
}
}
pub fn print_color(text: []const u8, color: u32) void {
for (text) |char| {
put_char_color(char, color);
}
}
pub fn clear_screen() void {
const f = fb orelse return;
pixel.fill(f, colors.BLACK);
cursor_x = 0;
cursor_y = 0;
current_line = 0;
reset_line_types();
}
pub fn get_cursor_x() u32 {
return cursor_x;
}
pub fn get_cursor_y() u32 {
return cursor_y;
}
pub fn set_cursor(x: u32, y: u32) void {
cursor_x = x;
cursor_y = y;
}
|