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:
@@ -130,9 +130,8 @@ the primary use-case for myself to create this library in the first place.
|
|||||||
- [x] grid
|
- [x] grid
|
||||||
- [x] mixed (some sort of form)
|
- [x] mixed (some sort of form)
|
||||||
- [ ] Elements
|
- [ ] Elements
|
||||||
- [ ] Button
|
- [x] Button
|
||||||
- [ ] Text Input field
|
- [x] Text Input field
|
||||||
- [ ] User input handling
|
|
||||||
- [ ] Popup-menu
|
- [ ] Popup-menu
|
||||||
- [ ] Scrollable Content (i.e. show long text of an except of something and other smaller `Container`)
|
- [ ] Scrollable Content (i.e. show long text of an except of something and other smaller `Container`)
|
||||||
- [ ] min size
|
- [ ] min size
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ pub const Clickable = struct {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
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 row = size.rows / 2 -| (text.len / 2);
|
||||||
const col: u16 = size.cols / 2 -| @as(u16, @truncate(text.len / 2));
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (text, 0..) |cp, idx| {
|
for (text, 0..) |cp, idx| {
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ pub const InputField = struct {
|
|||||||
|
|
||||||
if (this.input.items.len == 0) return;
|
if (this.input.items.len == 0) return;
|
||||||
|
|
||||||
const row: u16 = 1;
|
const row = 1;
|
||||||
const col: u16 = 1;
|
const col = 1;
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (this.input.items, 0..) |cp, idx| {
|
for (this.input.items, 0..) |cp, idx| {
|
||||||
|
|||||||
@@ -9,48 +9,25 @@ const log = std.log.scoped(.default);
|
|||||||
pub const HelloWorldText = packed struct {
|
pub const HelloWorldText = packed struct {
|
||||||
const text = "Hello World";
|
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 {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
.vtable = &.{
|
.vtable = &.{ .content = content },
|
||||||
.handle = handle,
|
|
||||||
.content = content,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// example function to render contents for a `Container`
|
|
||||||
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
|
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));
|
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
|
// NOTE: error should only be returned here in case an in-recoverable exception has occurred
|
||||||
const row = size.rows / 2;
|
const row = size.rows / 2;
|
||||||
const col = size.cols / 2 -| (text.len / 2);
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
|
const anchor = (row * size.rows) + col;
|
||||||
|
|
||||||
for (0.., text) |idx, char| {
|
for (0.., text) |idx, char| {
|
||||||
cells[(row * size.cols) + col + idx].style.fg = this.text_color;
|
cells[anchor + idx].style.fg = .black;
|
||||||
cells[(row * size.cols) + col + idx].cp = char;
|
cells[anchor + 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 => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
|||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
// no handle function required
|
|
||||||
.vtable = &.{ .content = content },
|
.vtable = &.{ .content = content },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||||
|
|
||||||
const row: u16 = 2;
|
const row = 2;
|
||||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (text, 0..) |cp, idx| {
|
for (text, 0..) |cp, idx| {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
|||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
// no handle function required
|
|
||||||
.vtable = &.{ .content = content },
|
.vtable = &.{ .content = content },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||||
|
|
||||||
const row: u16 = 2;
|
const row = 2;
|
||||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (text, 0..) |cp, idx| {
|
for (text, 0..) |cp, idx| {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
|||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
// no handle function required
|
|
||||||
.vtable = &.{ .content = content },
|
.vtable = &.{ .content = content },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||||
|
|
||||||
const row: u16 = 2;
|
const row = 2;
|
||||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (text, 0..) |cp, idx| {
|
for (text, 0..) |cp, idx| {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ const QuitText = struct {
|
|||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
// no handle function required
|
|
||||||
.vtable = &.{ .content = content },
|
.vtable = &.{ .content = content },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -20,8 +19,8 @@ const QuitText = struct {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||||
|
|
||||||
const row: u16 = 2;
|
const row = 2;
|
||||||
const col: u16 = size.cols / 2 -| (@as(u16, @truncate(text.len)) / 2);
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
const anchor = (row * size.cols) + col;
|
const anchor = (row * size.cols) + col;
|
||||||
|
|
||||||
for (text, 0..) |cp, idx| {
|
for (text, 0..) |cp, idx| {
|
||||||
|
|||||||
Reference in New Issue
Block a user