ref(examples): avoid unnecessary casts
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 43s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 43s
This commit is contained in:
@@ -34,8 +34,8 @@ pub const Clickable = struct {
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
const row: u16 = size.rows / 2 -| @as(u16, @truncate(text.len / 2));
|
||||
const col: u16 = size.cols / 2 -| @as(u16, @truncate(text.len / 2));
|
||||
const row = size.rows / 2 -| (text.len / 2);
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (text, 0..) |cp, idx| {
|
||||
|
||||
@@ -54,8 +54,8 @@ pub const InputField = struct {
|
||||
|
||||
if (this.input.items.len == 0) return;
|
||||
|
||||
const row: u16 = 1;
|
||||
const col: u16 = 1;
|
||||
const row = 1;
|
||||
const col = 1;
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (this.input.items, 0..) |cp, idx| {
|
||||
|
||||
@@ -9,48 +9,25 @@ const log = std.log.scoped(.default);
|
||||
pub const HelloWorldText = packed struct {
|
||||
const text = "Hello World";
|
||||
|
||||
text_color: zterm.Color = .black,
|
||||
|
||||
// example function to create the interface instance for this `Element` implementation
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
.vtable = &.{
|
||||
.handle = handle,
|
||||
.content = content,
|
||||
},
|
||||
.vtable = &.{ .content = content },
|
||||
};
|
||||
}
|
||||
|
||||
// example function to render contents for a `Container`
|
||||
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
|
||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
// NOTE: error should only be returned here in case an in-recoverable exception has occurred
|
||||
const row = size.rows / 2;
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.rows) + col;
|
||||
|
||||
for (0.., text) |idx, char| {
|
||||
cells[(row * size.cols) + col + idx].style.fg = this.text_color;
|
||||
cells[(row * size.cols) + col + idx].cp = char;
|
||||
}
|
||||
}
|
||||
|
||||
// example function to handle events for a `Container`
|
||||
fn handle(ctx: *anyopaque, event: App.Event) !void {
|
||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||
switch (event) {
|
||||
.init => log.debug(".init event", .{}),
|
||||
.key => |key| if (key.eql(.{ .cp = input.Space })) {
|
||||
var next_color_idx = @intFromEnum(this.text_color);
|
||||
next_color_idx += 1;
|
||||
next_color_idx %= 17;
|
||||
if (next_color_idx == @intFromEnum(zterm.Color.default)) next_color_idx += 1;
|
||||
this.text_color = @enumFromInt(next_color_idx);
|
||||
log.debug("Next color: {s}", .{@tagName(this.text_color)});
|
||||
},
|
||||
else => {},
|
||||
cells[anchor + idx].style.fg = .black;
|
||||
cells[anchor + idx].cp = char;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
// no handle function required
|
||||
.vtable = &.{ .content = content },
|
||||
};
|
||||
}
|
||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
const row: u16 = 2;
|
||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
||||
const row = 2;
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (text, 0..) |cp, idx| {
|
||||
|
||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
// no handle function required
|
||||
.vtable = &.{ .content = content },
|
||||
};
|
||||
}
|
||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
const row: u16 = 2;
|
||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
||||
const row = 2;
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (text, 0..) |cp, idx| {
|
||||
|
||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
// no handle function required
|
||||
.vtable = &.{ .content = content },
|
||||
};
|
||||
}
|
||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
const row: u16 = 2;
|
||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
||||
const row = 2;
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (text, 0..) |cp, idx| {
|
||||
|
||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
// no handle function required
|
||||
.vtable = &.{ .content = content },
|
||||
};
|
||||
}
|
||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
||||
_ = ctx;
|
||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||
|
||||
const row: u16 = 2;
|
||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
||||
const row = 2;
|
||||
const col = size.cols / 2 -| (text.len / 2);
|
||||
const anchor = (row * size.cols) + col;
|
||||
|
||||
for (text, 0..) |cp, idx| {
|
||||
|
||||
Reference in New Issue
Block a user