mod(memory): do not create items on the stack instead using the provided allocator
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 4m26s

This commit is contained in:
2025-01-06 21:56:04 +01:00
parent 04e1ca087f
commit c2c3f41ff3
21 changed files with 404 additions and 464 deletions

View File

@@ -52,7 +52,6 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
pub fn deinit(this: *LayoutType) void {
this.vtable.deinit(this);
this.* = undefined;
}
pub fn createFrom(object: anytype) LayoutType {

View File

@@ -27,11 +27,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
size: terminal.Size = undefined,
require_render: bool = true,
element: Element = undefined,
events: Events = undefined,
config: Config = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
require_render: bool,
element: Element,
events: Events,
config: Config,
const Config = struct {
style: Style = .{ .fg = .default },
@@ -49,12 +50,14 @@ 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() {
var this = allocator.create(@This()) catch @panic("Framing.zig: Failed to create.");
this.allocator = allocator;
this.require_render = true;
this.config = config;
this.element = element;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -67,6 +70,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
}
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`?
size: terminal.Size = undefined,
containers: Containers = undefined,
events: Events = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
containers: Containers,
events: Events,
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),
};
var this = allocator.create(@This()) catch @panic("HContainer.zig: Failed to create.");
this.allocator = allocator;
this.containers = containers;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -106,6 +108,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.containers.deinit();
this.allocator.destroy(this);
}
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`?
size: terminal.Size = undefined,
elements: Elements = undefined,
events: Events = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
elements: Elements,
events: Events,
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),
};
var this = allocator.create(@This()) catch @panic("HStack.zig: Failed to create.");
this.allocator = allocator;
this.elements = elements;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -74,6 +76,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.elements.deinit();
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -26,11 +26,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
size: terminal.Size = undefined,
require_render: bool = false,
element: Element = undefined,
events: Events = undefined,
config: Config = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
require_render: bool,
element: Element,
events: Events,
config: Config,
const Config = struct {
margin: ?u8 = null,
@@ -40,18 +41,20 @@ 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),
};
var this = allocator.create(@This()) catch @panic("Margin.zig: Failed to create.");
this.allocator = allocator;
this.require_render = true;
this.config = config;
this.element = element;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -64,6 +67,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -26,11 +26,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
size: terminal.Size = undefined,
require_render: bool = false,
element: Element = undefined,
events: Events = undefined,
config: Config = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
require_render: bool,
element: Element,
events: Events,
config: Config,
const Config = struct {
padding: ?u16 = null,
@@ -40,12 +41,14 @@ 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() {
var this = allocator.create(@This()) catch @panic("Padding.zig: Failed to create.");
this.allocator = allocator;
this.require_render = true;
this.config = config;
this.element = element;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -58,6 +61,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -37,12 +37,13 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
};
const Tabs = std.ArrayList(Tab);
return struct {
size: terminal.Size = undefined,
require_render: bool = true,
tabs: Tabs = undefined,
active_tab: usize = 0,
events: Events = undefined,
config: Config = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
require_render: bool,
tabs: Tabs,
active_tab: usize,
events: Events,
config: Config,
const Config = struct {
frame: Frame = .round,
@@ -53,7 +54,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
};
};
pub fn init(allocator: std.mem.Allocator, config: Config, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, config: Config, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .@"struct") {
@@ -104,11 +105,14 @@ 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 .{
.config = config,
.tabs = tabs,
.events = Events.init(allocator),
};
var this = allocator.create(@This()) catch @panic("Tab.zig: Failed to create.");
this.allocator = allocator;
this.active_tab = 0;
this.require_render = true;
this.config = config;
this.tabs = tabs;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -124,6 +128,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.tabs.deinit();
this.allocator.destroy(this);
}
fn resize_active_tab(this: *@This()) !void {

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`?
size: terminal.Size = undefined,
containers: Containers = undefined,
events: Events = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
containers: Containers,
events: Events,
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),
};
var this = allocator.create(@This()) catch @panic("VContainer.zig: Failed to create.");
this.allocator = allocator;
this.containers = containers;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -106,6 +108,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.containers.deinit();
this.allocator.destroy(this);
}
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`?
size: terminal.Size = undefined,
elements: Elements = undefined,
events: Events = undefined,
allocator: std.mem.Allocator,
size: terminal.Size,
elements: Elements,
events: Events,
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),
};
var this = allocator.create(@This()) catch @panic("VStack.zig: Failed to create.");
this.allocator = allocator;
this.elements = elements;
this.events = Events.init(allocator);
return this;
}
pub fn deinit(this: *@This()) void {
@@ -74,6 +76,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.elements.deinit();
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) !*Events {

View File

@@ -58,7 +58,6 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
pub fn deinit(this: *WidgetType) void {
this.vtable.deinit(this);
this.* = undefined;
}
pub fn createFrom(object: anytype) WidgetType {

View File

@@ -13,30 +13,38 @@ 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 {
active: bool = false,
allocator: std.mem.Allocator = undefined,
label: ?[]const u8 = null,
placeholder: ?[]const u8 = null,
size: Size = undefined,
require_render: bool = false,
value: std.ArrayList(u8) = undefined,
value_len: usize = 0, // value content length
cursor_idx: usize = 0, // current cursor position
active: bool,
allocator: std.mem.Allocator,
label: ?[]const u8,
placeholder: ?[]const u8,
size: Size,
require_render: bool,
value: std.ArrayList(u8),
/// value content length
value_len: usize,
/// current cursor position
cursor_idx: usize,
pub fn init(allocator: std.mem.Allocator, label: ?[]const u8, placeholder: ?[]const u8) @This() {
pub fn init(allocator: std.mem.Allocator, label: ?[]const u8, placeholder: ?[]const u8) *@This() {
var value = std.ArrayList(u8).init(allocator);
value.resize(32) catch @panic("Input.zig: out of memory");
return .{
.allocator = allocator,
.value = value,
.label = label,
.placeholder = placeholder,
};
var this = allocator.create(@This()) catch @panic("Input.zig: Failed to create.");
this.allocator = allocator;
this.active = false;
this.require_render = true;
this.label = null;
this.placeholder = null;
this.value_len = 0;
this.cursor_idx = 0;
this.value = value;
this.label = label;
this.placeholder = placeholder;
return this;
}
pub fn deinit(this: *@This()) void {
this.value.deinit();
this.* = undefined;
this.allocator.destroy(this);
}
pub fn getValue(this: *const @This()) []const u8 {

View File

@@ -14,18 +14,19 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
const ListItems = std.ArrayList([]const Cell);
return struct {
idx: usize = 0,
config: ListType = undefined,
contents: ListItems = undefined,
size: terminal.Size = undefined,
require_render: bool = false,
allocator: std.mem.Allocator,
idx: usize,
config: ListType,
contents: ListItems,
size: terminal.Size,
require_render: bool,
const ListType = enum {
unordered,
ordered,
};
pub fn init(allocator: std.mem.Allocator, config: ListType, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, config: ListType, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .@"struct") {
@@ -42,15 +43,18 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
contents.append(child) catch {};
}
return .{
.config = config,
.contents = contents,
};
var this = allocator.create(@This()) catch @panic("List.zig: Failed to create.");
this.allocator = allocator;
this.require_render = true;
this.idx = 0;
this.config = config;
this.contents = contents;
return this;
}
pub fn deinit(this: *@This()) void {
this.contents.deinit();
this.* = undefined;
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) ?Event {

View File

@@ -14,13 +14,14 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
const Contents = std.ArrayList(u8);
return struct {
contents: Contents = undefined,
line_index: std.ArrayList(usize) = undefined,
line: usize = 0,
size: terminal.Size = undefined,
require_render: bool = false,
allocator: std.mem.Allocator,
contents: Contents,
line_index: std.ArrayList(usize),
line: usize,
size: terminal.Size,
require_render: bool,
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,16 +31,19 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
line_index.append(i + 1) catch {};
}
}
return .{
.contents = contents,
.line_index = line_index,
};
var this = allocator.create(@This()) catch @panic("RawText.zig: Failed to create.");
this.allocator = allocator;
this.line = 0;
this.require_render = true;
this.contents = contents;
this.line_index = line_index;
return this;
}
pub fn deinit(this: *@This()) void {
this.contents.deinit();
this.line_index.deinit();
this.* = undefined;
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) ?Event {

View File

@@ -11,15 +11,19 @@ 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 {
size: terminal.Size = undefined,
size_changed: bool = false,
allocator: std.mem.Allocator,
size: terminal.Size,
size_changed: bool,
pub fn init() @This() {
return .{};
pub fn init(allocator: std.mem.Allocator) *@This() {
var this = allocator.create(@This()) catch @panic("Space.zig: Failed to create.");
this.allocator = allocator;
this.size_changed = true;
return this;
}
pub fn deinit(this: *@This()) void {
this.* = undefined;
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) ?Event {

View File

@@ -12,10 +12,11 @@ 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 {
alignment: Alignment = undefined,
contents: []const Cell = undefined,
size: terminal.Size = undefined,
require_render: bool = false,
allocator: std.mem.Allocator,
alignment: Alignment,
contents: []const Cell,
size: terminal.Size,
require_render: bool,
const Alignment = enum {
default,
@@ -26,15 +27,17 @@ 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() {
var this = allocator.create(@This()) catch @panic("Text.zig: Failed to create");
this.allocator = allocator;
this.require_render = true;
this.alignment = alignment;
this.contents = contents;
return this;
}
pub fn deinit(this: *@This()) void {
this.* = undefined;
this.allocator.destroy(this);
}
pub fn handle(this: *@This(), event: Event) ?Event {