This commit is contained in:
2026-04-18 15:23:29 -05:00
parent 1de39a5824
commit e18ba9ab79
10 changed files with 219 additions and 42 deletions
+1
View File
@@ -0,0 +1 @@
pub const Text = @import("text.zig").Text;
+57
View File
@@ -0,0 +1,57 @@
const std = @import("std");
const grome = @import("../root.zig");
const style = grome.view.style;
const Writer = std.Io.Writer;
const Widget = grome.view.Widget;
const WidgetError = grome.view.WidgetError;
const Cell = grome.view.Cell;
const Block = grome.container.Block;
const SubMatrix = grome.container.SubMatrix;
const Style = style.Style;
pub const Text = struct {
// TODO: This should be a matrix instead and we should add the ability
// to turn raw text into a Matrix(Cell)
text: [1024]Cell,
text_width: usize,
widget: Widget,
pub fn init(text: []const u8, s: ?Style) @This() {
// TODO: this is bad, but I want to see it work.
var cells: [1024]Cell = undefined;
for (0..text.len) |x| {
cells[x] = Cell{
.char = [_]u8{text[x]} ** 8,
.width = 1,
.style = s,
};
}
return .{
.text = cells,
.text_width = text.len,
.widget = .{
.geometryFn = @This().geometry,
.renderFn = @This().render,
},
};
}
pub fn geometry(widget: *const Widget) Block {
const self: *const @This() = @fieldParentPtr("widget", widget);
return Block{
.loc = .{ .x = 0, .y = 0 },
.size = .{ .height = 1, .width = self.text_width },
};
}
pub fn render(widget: *Widget, frame: *SubMatrix(Cell)) WidgetError!void {
const self: *@This() = @fieldParentPtr("widget", widget);
var row = frame.rowable.row(0) catch return WidgetError.UnknownError;
const ending = @min(self.text_width, row.len);
@memcpy(row[0..ending], self.text[0..ending]);
}
};