cleanup of obsolete comments and dead code
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 35s

This removes the App.Renderer.Buffered, which was not improving on the
direct renderer, which has a simpler implementation and was keept for
this regard.
This commit is contained in:
2024-11-13 19:14:13 +01:00
parent 5908d6d5e3
commit 0cc0ed10d2
6 changed files with 7 additions and 434 deletions

View File

@@ -1,11 +1,8 @@
//! Renderer which holds the screen to compare with the previous screen for efficient rendering.
//! Each renderer should at least implement these functions:
//! - init(allocator: std.mem.Allocator) @This() {}
//! - deinit(this: *@This()) void {}
//! - resize(this: *@This(), size: Size) void {}
//! - clear(this: *@This(), size: Size) !void {}
//! - render(this: *@This(), size: Size, contents: []u8) !void {}
//! - flush(this: @This()) !void {}
//!
//! Each `Renderer` should be able to be used interchangeable without having to
//! change any code of any `Layout` or `Widget`. The only change should be the
@@ -13,117 +10,15 @@
const std = @import("std");
const terminal = @import("terminal.zig");
const Contents = std.ArrayList(u8); // TODO: this may contain more than just a single character! (i.e. styled)
const Cells = []const terminal.Cell;
const Position = terminal.Position;
const Size = terminal.Size;
pub fn Buffered(comptime _: bool) type {
const log = std.log.scoped(.renderer_buffered);
return struct {
size: terminal.Size = undefined,
frame: Contents = undefined,
const EMPTY: u8 = 0;
pub fn init(allocator: std.mem.Allocator) @This() {
_ = log;
return .{
.frame = Contents.init(allocator),
};
}
pub fn deinit(this: *@This()) void {
this.frame.deinit();
this.* = undefined;
}
pub fn resize(this: *@This(), size: Size) void {
std.debug.assert(size.anchor.col == 1 and size.anchor.row == 1);
this.size = size;
this.frame.resize(size.cols * size.rows) catch @panic("OOM");
}
pub fn clear(this: *@This(), size: Size) error{}!void {
for (size.anchor.row..size.anchor.row + size.rows) |row| {
const y = this.size.cols * (row - 1);
for (size.anchor.col..size.anchor.col + size.cols) |col| {
// log.debug("clearing index[{d}]/[{d}] at .{{ .col = {d}, .row = {d} }}", .{ y + col - 1, this.size.cols * this.size.rows, col, row });
std.debug.assert(y + col - 1 < this.frame.items.len);
this.frame.items[y + col - 1] = EMPTY;
}
}
}
pub fn render(this: *@This(), size: Size, contents: []u8) !void {
var c_idx: usize = 0;
row: for (size.anchor.row..size.anchor.row + size.rows) |row| {
var fill_empty = false;
const y = this.size.cols * (row - 1);
for (size.anchor.col..size.anchor.col + size.cols) |col| {
std.debug.assert(y + col - 1 < this.frame.items.len);
const idx = y + col - 1;
const item = contents[c_idx];
if (item == '\n') { // do not print newlines
// reached end of line
// fill rest of col's of this row with EMPTY
fill_empty = true;
}
if (fill_empty) {
this.frame.items[idx] = EMPTY;
} else {
this.frame.items[idx] = item;
c_idx += 1;
}
}
// printed row
// are there still items for this row (even though there is no space left?)
if (!fill_empty) {
// find end of line and update c_idx accordingly
for (c_idx..contents.len) |c| {
if (contents[c] == '\n') {
c_idx = c + 1;
continue :row;
}
}
} else {
c_idx += 1; // skip over '\n'
}
}
}
pub fn flush(this: @This()) !void {
try terminal.clearScreen();
for (this.size.anchor.row..this.size.anchor.row + this.size.rows) |row| {
const y = this.size.cols * (row - 1);
for (1..this.size.cols) |col| {
std.debug.assert(y + col - 1 < this.frame.items.len);
const item = this.frame.items[y + col - 1];
if (item == EMPTY) {
continue;
}
const pos: Position = .{ .col = @truncate(col), .row = @truncate(row) };
try terminal.setCursorPosition(pos);
_ = try terminal.write(&.{item});
}
}
}
};
}
pub fn Direct(comptime _: bool) type {
pub fn Direct(comptime fullscreen: bool) type {
const log = std.log.scoped(.renderer_direct);
_ = log;
_ = fullscreen;
return struct {
pub fn init(allocator: std.mem.Allocator) @This() {
_ = allocator;
_ = log;
return .{};
}
pub fn deinit(this: *@This()) void {
this.* = undefined;
}
pub fn resize(this: *@This(), size: Size) void {
_ = this;
_ = size;
@@ -206,9 +101,5 @@ pub fn Direct(comptime _: bool) type {
}
}
}
pub fn flush(this: @This()) !void {
_ = this;
}
};
}