feat(render): implement direct rendering
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 38s

This commit is contained in:
2024-11-10 19:08:38 +01:00
parent 35a7f9cc02
commit 67a535db6d
5 changed files with 93 additions and 32 deletions

View File

@@ -6,16 +6,17 @@ const Contents = std.ArrayList(u8);
const Position = terminal.Position;
const Size = terminal.Size;
pub fn Buffered(comptime fullscreen: bool) type {
pub fn Buffered(comptime _: bool) type {
return struct {
refresh: bool = false,
size: terminal.Size = undefined,
screen: Contents = undefined,
next_frame: Contents = undefined,
frame: Contents = undefined,
pub fn init(allocator: std.mem.Allocator) @This() {
return .{
.fullscreen = fullscreen,
.screen = Contents.init(allocator),
.next_frame = Contents.init(allocator),
.frame = Contents.init(allocator),
};
}
@@ -41,7 +42,7 @@ pub fn Buffered(comptime fullscreen: bool) type {
};
}
pub fn Plain(comptime _: bool) type {
pub fn Direct(comptime _: bool) type {
return struct {
pub fn clear(this: @This(), size: Size) !void {
_ = this;
@@ -52,12 +53,52 @@ pub fn Plain(comptime _: bool) type {
}
}
pub fn render(this: @This(), anchor: Position, size: Size, contents: *Contents) !void {
pub fn render(this: @This(), size: Size, contents: []u8) !void {
_ = this;
_ = size;
// FIXME: this should respect the given `size`
try terminal.setCursorPosition(anchor);
_ = try terminal.write(contents.items);
try terminal.setCursorPosition(size.anchor);
var row: u16 = 0;
var idx: usize = 0;
var skip_next_line = false;
for (contents, 0..) |item, i| {
if (item == '\n') { // do not print newlines
if (!skip_next_line) {
_ = try terminal.write(contents[idx..i]); // does not include '\n' at position _i_
row += 1;
if (row >= size.rows) {
return; // we are done
}
try terminal.setCursorPosition(.{
.col = size.anchor.col,
.row = size.anchor.row + row,
});
}
skip_next_line = false;
idx = i + 1; // skip over '\n'
continue;
}
if (i - idx == size.cols) {
// FIXME: this will introduce another additional line which is not accounted for and will cut off these lines in the end from rendering
// -> *current solution*: cut of the line and skip that remaining content (not sure if that should be done)
// - however the widget has actually knowledge about the size and could change the reported contents which fit to the size or simply don't care and leave it to the renderer?
// flush line
if (!skip_next_line) {
_ = try terminal.write(contents[idx..i]);
row += 1;
if (row >= size.rows) {
return; // we are done
}
try terminal.setCursorPosition(.{
.col = size.anchor.col,
.row = size.anchor.row + row,
});
}
skip_next_line = true;
idx = i;
}
}
if (idx < contents.len) {
_ = try terminal.write(contents[idx..]);
}
}
};
}