fix(header): render title in the aligned horizontal
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 32s

This commit is contained in:
2024-10-08 14:22:30 +02:00
parent fd77a99150
commit 7c6d2aa659
2 changed files with 20 additions and 7 deletions

View File

@@ -98,13 +98,14 @@ pub fn main() !void {
var root_window = vx.window(); var root_window = vx.window();
root_window.clear(); root_window.clear();
// FIXME: this should not be necessary to clear the contents
try vx.render(tty.anyWriter()); // re-draw after clear! try vx.render(tty.anyWriter()); // re-draw after clear!
header.draw(root_window.child(.{ header.draw(root_window.child(.{
.x_off = 0, .x_off = 0,
.y_off = 0, .y_off = 0,
.height = .{ .limit = 2 }, .height = .{ .limit = 3 },
.border = .{ .where = .bottom }, .border = .{ .where = .all },
})); }));
// Create a style // Create a style

View File

@@ -51,8 +51,14 @@ fn fillView(this: *@This()) void {
if (this.title) |title| { if (this.title) |title| {
// TODO: this could be a static string on the heap (due to the size of `513`) // TODO: this could be a static string on the heap (due to the size of `513`)
std.log.debug("this.title := {s}({d}) - width: {d}", .{ title, title.len, this.view.?.screen.width }); var len: usize = 0;
for (title, 0.., this.view.?.screen.width / 2..) |_, i, col| { for (title) |c| {
if (c == 0xaa or c == 0)
break;
len += 1;
}
for (title, 0..len, this.view.?.screen.width / 2 - len / 2..) |_, i, col| {
const cell: vaxis.Cell = .{ const cell: vaxis.Cell = .{
.char = .{ .grapheme = title[i .. i + 1] }, .char = .{ .grapheme = title[i .. i + 1] },
.style = .{ .style = .{
@@ -61,6 +67,11 @@ fn fillView(this: *@This()) void {
}; };
this.view.?.writeCell(col, 0, cell); this.view.?.writeCell(col, 0, cell);
} }
// fill rest with default cells
for (this.view.?.screen.width / 2 + len..this.view.?.screen.width) |i| {
this.view.?.writeCell(i, 0, .{ .default = true });
}
} }
} }
@@ -70,17 +81,18 @@ pub fn update(this: *@This(), event: Event) void {
switch (event) { switch (event) {
.winsize => |ws| { .winsize => |ws| {
if (this.view) |*view| { if (this.view) |*view| {
if (ws.rows != view.screen.width) { if (ws.cols != view.screen.width) {
view.*.deinit(); view.*.deinit();
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.rows, .height = ws.cols }) catch @panic("OOM"); this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.cols, .height = ws.rows }) catch @panic("OOM");
this.fillView(); this.fillView();
} }
} else { } else {
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.rows, .height = ws.cols }) catch @panic("OOM"); this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.cols, .height = ws.rows }) catch @panic("OOM");
this.fillView(); this.fillView();
} }
}, },
.title => |title| { .title => |title| {
// TODO: try to remove the necessary amount of allocations
if (this.title) |*t| { if (this.title) |*t| {
this.allocator.free(t.*); this.allocator.free(t.*);
} }