diff --git a/examples/demo.zig b/examples/demo.zig index 3148ad1..c200e7f 100644 --- a/examples/demo.zig +++ b/examples/demo.zig @@ -13,10 +13,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - pub fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const y = 2; const x = size.x / 2 -| (text.len / 2); diff --git a/examples/elements/button.zig b/examples/elements/button.zig index a5b3a9d..dfd334f 100644 --- a/examples/elements/button.zig +++ b/examples/elements/button.zig @@ -14,10 +14,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -65,10 +64,9 @@ const Clickable = struct { } } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { const this: *@This() = @ptrCast(@alignCast(ctx)); std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = size.y / 2 -| (text.len / 2); const col = size.x / 2 -| (text.len / 2); diff --git a/examples/elements/input.zig b/examples/elements/input.zig index 3a86832..d7dd2fa 100644 --- a/examples/elements/input.zig +++ b/examples/elements/input.zig @@ -14,10 +14,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -75,10 +74,9 @@ const InputField = struct { } } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { const this: *@This() = @ptrCast(@alignCast(ctx)); std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; if (this.input.items.len == 0) return; diff --git a/examples/elements/scrollable.zig b/examples/elements/scrollable.zig index fe785d9..add8e74 100644 --- a/examples/elements/scrollable.zig +++ b/examples/elements/scrollable.zig @@ -13,10 +13,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -43,10 +42,9 @@ const HelloWorldText = packed struct { }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = size.y / 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/errors.zig b/examples/errors.zig index 67ddaac..7385095 100644 --- a/examples/errors.zig +++ b/examples/errors.zig @@ -12,10 +12,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -39,10 +38,9 @@ const InfoText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -75,10 +73,9 @@ const ErrorNotification = struct { } } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { const this: *@This() = @ptrCast(@alignCast(ctx)); std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; if (this.msg) |msg| { const row = size.y -| 2; diff --git a/examples/layouts/grid.zig b/examples/layouts/grid.zig index c1767a7..35c0608 100644 --- a/examples/layouts/grid.zig +++ b/examples/layouts/grid.zig @@ -15,10 +15,9 @@ const QuitText = struct { }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/layouts/horizontal.zig b/examples/layouts/horizontal.zig index 3ffe40e..74a573d 100644 --- a/examples/layouts/horizontal.zig +++ b/examples/layouts/horizontal.zig @@ -15,10 +15,9 @@ const QuitText = struct { }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/layouts/mixed.zig b/examples/layouts/mixed.zig index 8774dae..240cc4c 100644 --- a/examples/layouts/mixed.zig +++ b/examples/layouts/mixed.zig @@ -15,10 +15,9 @@ const QuitText = struct { }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/layouts/vertical.zig b/examples/layouts/vertical.zig index 46240ba..5e6f697 100644 --- a/examples/layouts/vertical.zig +++ b/examples/layouts/vertical.zig @@ -15,10 +15,9 @@ const QuitText = struct { }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/styles/palette.zig b/examples/styles/palette.zig index 4adaa02..42c585b 100644 --- a/examples/styles/palette.zig +++ b/examples/styles/palette.zig @@ -12,10 +12,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); diff --git a/examples/styles/text.zig b/examples/styles/text.zig index 331894d..c9d979b 100644 --- a/examples/styles/text.zig +++ b/examples/styles/text.zig @@ -12,10 +12,9 @@ const QuitText = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; const row = 2; const col = size.x / 2 -| (text.len / 2); @@ -39,11 +38,10 @@ const TextStyles = struct { return .{ .ptr = this, .vtable = &.{ .content = content } }; } - fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void { + fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void { @setEvalBranchQuota(50000); _ = ctx; std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); - _ = origin; var row: usize = 0; var col: usize = 0; diff --git a/src/container.zig b/src/container.zig index 0638591..1b7a3c4 100644 --- a/src/container.zig +++ b/src/container.zig @@ -38,7 +38,7 @@ pub const Border = packed struct { pub const vertical: @This() = .{ .top = true, .bottom = true }; } = .{}, - pub fn contents(this: @This(), cells: []Cell, size: Point) void { + pub fn content(this: @This(), cells: []Cell, size: Point) void { std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); const frame = switch (this.corners) { @@ -154,7 +154,7 @@ pub const Rectangle = packed struct { fill: Color = .default, // NOTE caller owns `Cells` slice and ensures that `cells.len == size.x * size.y` - pub fn contents(this: @This(), cells: []Cell, size: Point) void { + pub fn content(this: @This(), cells: []Cell, size: Point) void { std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); for (0..size.y) |row| { @@ -341,7 +341,7 @@ pub const Layout = packed struct { } = .line, } = .{}, - pub fn contents(this: @This(), comptime C: type, cells: []Cell, origin: Point, size: Point, children: []const C) void { + pub fn content(this: @This(), comptime C: type, cells: []Cell, origin: Point, size: Point, children: []const C) void { std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); if (this.separator.enabled and children.len > 1) { @@ -878,17 +878,17 @@ pub fn Container(comptime Event: type) type { } } - pub fn contents(this: *const @This()) ![]const Cell { + pub fn content(this: *const @This()) ![]const Cell { if (this.size.x == 0 or this.size.y == 0) return Error.TooSmall; const cells = try this.allocator.alloc(Cell, @as(usize, this.size.x) * @as(usize, this.size.y)); @memset(cells, .{}); errdefer this.allocator.free(cells); - this.properties.layout.contents(@This(), cells, this.origin, this.size, this.elements.items); - this.properties.border.contents(cells, this.size); - this.properties.rectangle.contents(cells, this.size); + this.properties.layout.content(@This(), cells, this.origin, this.size, this.elements.items); + this.properties.border.content(cells, this.size); + this.properties.rectangle.content(cells, this.size); - try this.element.content(cells, this.origin, this.size); + try this.element.content(cells, this.size); // DEBUG render corresponding top left corner of this `Container` *red* // cells[0].style.fg = .red; diff --git a/src/element.zig b/src/element.zig index 0a6eddb..af6fe77 100644 --- a/src/element.zig +++ b/src/element.zig @@ -16,7 +16,7 @@ pub fn Element(Event: type) type { resize: ?*const fn (ctx: *anyopaque, size: Point) void = null, reposition: ?*const fn (ctx: *anyopaque, origin: Point) void = null, handle: ?*const fn (ctx: *anyopaque, event: Event) anyerror!void = null, - content: ?*const fn (ctx: *anyopaque, cells: []Cell, origin: Point, size: Point) anyerror!void = null, + content: ?*const fn (ctx: *anyopaque, cells: []Cell, size: Point) anyerror!void = null, }; /// Resize the corresponding `Element` with the given *size*. @@ -58,9 +58,9 @@ pub fn Element(Event: type) type { /// non-recoverable (i.e. an allocation error, system error, etc.). /// Otherwise user specific errors should be caught using the `handle` /// function before the rendering of the `Container` happens. - pub inline fn content(this: @This(), cells: []Cell, origin: Point, size: Point) !void { + pub inline fn content(this: @This(), cells: []Cell, size: Point) !void { if (this.vtable.content) |content_fn| - try content_fn(this.ptr, cells, origin, size); + try content_fn(this.ptr, cells, size); } }; } @@ -130,13 +130,13 @@ pub fn Scrollable(Event: type) type { } } - fn render_container(container: Container(Event), cells: []Cell, container_origin: Point, container_size: Point) !void { + fn render_container(container: Container(Event), cells: []Cell, container_size: Point) !void { const size = container.size; const origin = container.origin; - const contents = try container.contents(); + const contents = try container.content(); defer container.allocator.free(contents); - const anchor = (@as(usize, origin.y -| container_origin.y) * @as(usize, container_size.x)) + @as(usize, origin.x -| container_origin.x); + const anchor = (@as(usize, origin.y) * @as(usize, container_size.x)) + @as(usize, origin.x); var idx: usize = 0; blk: for (0..size.y) |row| { @@ -148,25 +148,23 @@ pub fn Scrollable(Event: type) type { } } - for (container.elements.items) |child| try render_container(child, cells, origin, size); + for (container.elements.items) |child| try render_container(child, cells, size); } - fn content(ctx: *anyopaque, cells: []Cell, origin: Point, size: Point) !void { + fn content(ctx: *anyopaque, cells: []Cell, size: Point) !void { const this: *@This() = @ptrCast(@alignCast(ctx)); - _ = origin; std.debug.assert(cells.len == @as(usize, this.size.x) * @as(usize, this.size.y)); const container_size = this.container.size; - const container_origin = this.container.origin; const container_cells = try this.container.allocator.alloc(Cell, @as(usize, container_size.x) * @as(usize, container_size.y)); { - const container_cells_const = try this.container.contents(); + const container_cells_const = try this.container.content(); defer this.container.allocator.free(container_cells_const); std.debug.assert(container_cells_const.len == @as(usize, container_size.x) * @as(usize, container_size.y)); @memcpy(container_cells, container_cells_const); } - for (this.container.elements.items) |child| try render_container(child, container_cells, container_origin, container_size); + for (this.container.elements.items) |child| try render_container(child, container_cells, container_size); const anchor = (@as(usize, this.anchor.y) * @as(usize, container_size.x)) + @as(usize, this.anchor.x); // TODO render scrollbar according to configuration! diff --git a/src/render.zig b/src/render.zig index 6c60b34..032c2d3 100644 --- a/src/render.zig +++ b/src/render.zig @@ -64,7 +64,7 @@ pub const Buffered = struct { pub fn render(this: *@This(), comptime T: type, container: *T) !void { const size: Point = container.size; const origin: Point = container.origin; - const cells: []const Cell = try container.contents(); + const cells: []const Cell = try container.content(); if (cells.len == 0) return; diff --git a/src/testing.zig b/src/testing.zig index e20e17f..da5fe11 100644 --- a/src/testing.zig +++ b/src/testing.zig @@ -48,7 +48,7 @@ pub const Renderer = struct { pub fn render(this: *@This(), comptime T: type, container: *const T) !void { const size: Point = container.size; const origin: Point = container.origin; - const cells: []const Cell = try container.contents(); + const cells: []const Cell = try container.content(); if (cells.len == 0) return;