mod: each layout and widget now allocates their own instance in memory using the provided allocator (and destroy's themselfes in the end)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 36s

This commit is contained in:
2024-11-16 19:56:36 +01:00
parent f4adf53067
commit ec71e34958
14 changed files with 232 additions and 270 deletions

View File

@@ -27,6 +27,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
require_render: bool = true,
element: Element = undefined,
@@ -49,12 +50,13 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
};
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) @This() {
return .{
.config = config,
.element = element,
.events = Events.init(allocator),
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) *@This() {
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.config = config;
layout.element = element;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -67,6 +69,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -36,11 +36,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
const Events = std.ArrayList(Event);
return struct {
// TODO: current focused `Element`?
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
containers: Containers = undefined,
events: Events = undefined,
pub fn init(allocator: std.mem.Allocator, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .Struct) {
@@ -87,10 +88,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("nested child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.containers = containers,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.containers = containers;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -106,6 +108,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.containers.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -30,11 +30,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
const Events = std.ArrayList(Event);
return struct {
// TODO: current focused `Element`?
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
elements: Elements = undefined,
events: Events = undefined,
pub fn init(allocator: std.mem.Allocator, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .Struct) {
@@ -55,10 +56,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.elements = elements,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.elements = elements;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -74,6 +76,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.elements.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -26,6 +26,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
require_render: bool = false,
element: Element = undefined,
@@ -40,18 +41,19 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
bottom: u8 = 0,
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) @This() {
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) *@This() {
if (config.margin) |margin| {
std.debug.assert(margin <= 50);
} else {
std.debug.assert(config.left + config.right < 100);
std.debug.assert(config.top + config.bottom < 100);
}
return .{
.config = config,
.element = element,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.config = config;
layout.element = element;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -64,6 +66,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -26,6 +26,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
require_render: bool = false,
element: Element = undefined,
@@ -40,12 +41,13 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
bottom: u16 = 0,
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) @This() {
return .{
.config = config,
.element = element,
.events = Events.init(allocator),
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) *@This() {
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.config = config;
layout.element = element;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -58,6 +60,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -36,11 +36,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
const Events = std.ArrayList(Event);
return struct {
// TODO: current focused `Element`?
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
containers: Containers = undefined,
events: Events = undefined,
pub fn init(allocator: std.mem.Allocator, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .Struct) {
@@ -87,10 +88,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("nested child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.containers = containers,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.containers = containers;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -106,6 +108,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.containers.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -30,11 +30,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
const Events = std.ArrayList(Event);
return struct {
// TODO: current focused `Element`?
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
elements: Elements = undefined,
events: Events = undefined,
pub fn init(allocator: std.mem.Allocator, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .Struct) {
@@ -55,10 +56,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.elements = elements,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.elements = elements;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -74,6 +76,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.elements.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -14,13 +14,14 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
const Contents = std.ArrayList(u8);
return struct {
allocator: std.mem.Allocator = undefined,
contents: Contents = undefined,
line_index: std.ArrayList(usize) = undefined,
line: usize = 0,
size: terminal.Size = undefined,
require_render: bool = false,
pub fn init(allocator: std.mem.Allocator, file: std.fs.File) @This() {
pub fn init(allocator: std.mem.Allocator, file: std.fs.File) *@This() {
var contents = Contents.init(allocator);
var line_index = std.ArrayList(usize).init(allocator);
file.reader().readAllArrayList(&contents, std.math.maxInt(usize)) catch {};
@@ -30,15 +31,17 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
line_index.append(i + 1) catch {};
}
}
return .{
.contents = contents,
.line_index = line_index,
};
const widget = allocator.create(@This()) catch @panic("OOM");
widget.allocator = allocator;
widget.contents = contents;
widget.line_index = line_index;
return widget;
}
pub fn deinit(this: *@This()) void {
this.contents.deinit();
this.line_index.deinit();
this.allocator.destroy(this);
this.* = undefined;
}

View File

@@ -11,14 +11,18 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
return struct {
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
size_changed: bool = false,
pub fn init() @This() {
return .{};
pub fn init(allocator: std.mem.Allocator) *@This() {
const widget = allocator.create(@This()) catch @panic("OOM");
widget.allocator = allocator;
return widget;
}
pub fn deinit(this: *@This()) void {
this.allocator.destroy(this);
this.* = undefined;
}

View File

@@ -12,6 +12,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
return struct {
allocator: std.mem.Allocator = undefined,
alignment: Alignment = undefined,
contents: []const Cell = undefined,
size: terminal.Size = undefined,
@@ -26,14 +27,16 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
right,
};
pub fn init(alignment: Alignment, contents: []const Cell) @This() {
return .{
.alignment = alignment,
.contents = contents,
};
pub fn init(allocator: std.mem.Allocator, alignment: Alignment, contents: []const Cell) *@This() {
const widget = allocator.create(@This()) catch @panic("OOM");
widget.allocator = allocator;
widget.alignment = alignment;
widget.contents = contents;
return widget;
}
pub fn deinit(this: *@This()) void {
this.allocator.destroy(this);
this.* = undefined;
}