add(ViewPort): wrapper for vaxis.widgets.ScrollView
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 31s

This commit is contained in:
2024-10-08 15:34:48 +02:00
parent 7c6d2aa659
commit 63bef849ec
4 changed files with 146 additions and 40 deletions

View File

@@ -8,14 +8,14 @@ const Event = widget.Event;
allocator: std.mem.Allocator = undefined,
unicode: *const vaxis.Unicode = undefined,
title: ?[]const u8 = undefined,
path: ?[]const u8 = undefined,
view: ?vaxis.widgets.View = undefined,
pub fn init(allocator: std.mem.Allocator, unicode: *const vaxis.Unicode) !@This() {
return .{
.allocator = allocator,
.unicode = unicode,
.title = null,
.path = null,
.view = null,
};
}
@@ -24,8 +24,8 @@ pub fn deinit(this: *@This()) void {
if (this.view) |*view| {
view.*.deinit();
}
if (this.title) |*title| {
this.allocator.free(title.*);
if (this.path) |*path| {
this.allocator.free(path.*);
}
this.* = undefined;
}
@@ -49,18 +49,11 @@ fn fillView(this: *@This()) void {
this.view.?.writeCell(i, 0, cell);
}
if (this.title) |title| {
if (this.path) |path| {
// TODO: this could be a static string on the heap (due to the size of `513`)
var len: usize = 0;
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| {
for (0..path.len, this.view.?.screen.width / 2 - path.len / 2..) |i, col| {
const cell: vaxis.Cell = .{
.char = .{ .grapheme = title[i .. i + 1] },
.char = .{ .grapheme = path[i .. i + 1] },
.style = .{
.ul_style = .single,
},
@@ -69,7 +62,7 @@ fn fillView(this: *@This()) void {
}
// fill rest with default cells
for (this.view.?.screen.width / 2 + len..this.view.?.screen.width) |i| {
for (this.view.?.screen.width / 2 + path.len..this.view.?.screen.width) |i| {
this.view.?.writeCell(i, 0, .{ .default = true });
}
}
@@ -91,14 +84,14 @@ pub fn update(this: *@This(), event: Event) void {
this.fillView();
}
},
.title => |title| {
.path => |path| {
// TODO: try to remove the necessary amount of allocations
if (this.title) |*t| {
this.allocator.free(t.*);
if (this.path) |*p| {
this.allocator.free(p.*);
}
const t = this.allocator.alloc(u8, title.len) catch @panic("OOM");
@memcpy(t, title);
this.title = t;
const p = this.allocator.alloc(u8, path.len) catch @panic("OOM");
@memcpy(p, path);
this.path = p;
this.fillView();
},
else => {},