-
Notifications
You must be signed in to change notification settings - Fork 2
Closed
Description
My code:
const std = @import("std");
const zz = @import("zigzag");
const binds = @import("keybinds.zig");
const Model = struct {
binds: std.ArrayList(binds.Keybind),
pub const Msg = union(enum) {
key: zz.KeyEvent,
};
pub fn init(self: *Model, ctx: *zz.Context) zz.Cmd(Msg) {
var bindings: std.ArrayList(binds.Keybind) = .{};
std.debug.print("{}\n", .{ctx.*.allocator});
std.debug.print("Allocator vtable: {} \n", .{ctx.allocator.vtable.alloc});
var combo = [_][]const u8{ "CTRL + KEY_D", "A" };
bindings.append(ctx.allocator, .{ .combo = &combo, .exec = "echo \"IT WORKS!!\"" }) catch return .none;
self.* = .{
.binds = bindings,
};
return .none;
}
pub fn update(_: *Model, msg: Msg, _: *zz.Context) zz.Cmd(Msg) {
switch (msg) {
.key => |k| switch (k.key) {
.char => |c| if (c == 'q') return .quit,
else => {},
},
}
return .none;
}
pub fn view(self: *const Model, ctx: *const zz.Context) []const u8 {
const style = (zz.Style{}).fg(zz.Color.cyan());
// Use ArrayList to build the text
var buffer = std.ArrayList(u8){};
defer buffer.deinit(ctx.allocator);
for (self.binds.items) |*bind| {
// Build comboText
var comboBuffer = std.ArrayList(u8){};
defer comboBuffer.deinit(ctx.allocator);
for (bind.combo, 0..) |*key, i| {
if (i > 0) comboBuffer.appendSlice(ctx.allocator, " + ") catch {};
comboBuffer.appendSlice(ctx.allocator, key.*) catch {};
}
const comboText = comboBuffer.toOwnedSlice(ctx.allocator) catch "Error";
buffer.appendSlice(ctx.allocator, comboText) catch {};
buffer.appendSlice(ctx.allocator, " | ") catch {};
buffer.appendSlice(ctx.allocator, bind.exec) catch {};
buffer.appendSlice(ctx.allocator, "\n") catch {};
ctx.allocator.free(comboText);
}
const text = buffer.toOwnedSlice(ctx.allocator) catch "Error";
return style.render(ctx.allocator, text) catch text;
}
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var program = try zz.Program(Model).init(gpa.allocator());
defer program.deinit();
try program.run();
}What it outputs:
$ zig build run-editor
.{ .ptr = anyopaque@7ffd1e765c28, .vtable = .{ .alloc = fn (*anyopaque, usize, mem.Alignment, usize) ?[*]u8@10a4400, .resize = fn
(*anyopaque, []u8, mem.Alignment, usize, usize) bool@10a5050, .remap = fn (*anyopaque, []u8, mem.Alignment, usize, usize) ?[*]u8@10a56c0, .
free = fn (*anyopaque, []u8, mem.Alignment, usize) void@10a5780 } }
Allocator vtable: fn (*anyopaque, usize, mem.Alignment, usize) ?[*]u8@10a4400
Segmentation fault at address 0x0
/usr/lib/zig/std/mem/Allocator.zig:129:26: 0x10c6285 in createNode (std.zig)
return a.vtable.alloc(a.ptr, len, alignment, ret_addr);
^
/usr/lib/zig/std/heap/arena_allocator.zig:193:29: 0x10a4575 in alloc (std.zig)
(self.createNode(0, n + ptr_align) orelse return null);
^
/usr/lib/zig/std/mem/Allocator.zig:129:26: 0x10761ae in allocBytesWithAlignment__anon_9309 (std.zig)
return a.vtable.alloc(a.ptr, len, alignment, ret_addr);
^
/usr/lib/zig/std/mem/Allocator.zig:269:40: 0x10fd2d9 in allocWithSizeAndAlignment__anon_19625 (std.zig)
return self.allocBytesWithAlignment(alignment, byte_count, return_address);
^
/usr/lib/zig/std/mem/Allocator.zig:257:89: 0x1197bbd in alignedAlloc__anon_28888 (std.zig)
const ptr: [*]align(a.toByteUnits()) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
^
/usr/lib/zig/std/array_list.zig:1231:56: 0x119179e in ensureTotalCapacityPrecise (std.zig)
const new_memory = try gpa.alignedAlloc(T, alignment, new_capacity);
^
/usr/lib/zig/std/array_list.zig:1207:51: 0x11857e5 in ensureTotalCapacity (std.zig)
return self.ensureTotalCapacityPrecise(gpa, growCapacity(self.capacity, new_capacity));
^
/usr/lib/zig/std/array_list.zig:1261:41: 0x117bcbb in addOne (std.zig)
try self.ensureTotalCapacity(gpa, newlen);
^
/usr/lib/zig/std/array_list.zig:894:49: 0x116df4d in append (std.zig)
const new_item_ptr = try self.addOne(gpa);
^
src/editor.zig:19:24: 0x114dcc1 in init (editor.zig)
bindings.append(ctx.allocator, .{ .combo = &combo, .exec = "echo \"IT WORKS!!\"" }) catch return .none;
^
/home/matwa/.cache/zig/p/zigzag-0.1.0-YXwYSzd2BgAKEIwRD_yf8qR96bnv2XOuYqEqv8OIEPLw/src/core/program.zig:176:45: 0x114628d in start (root.
zig)
const init_cmd = self.model.init(&self.context);
^
/home/matwa/.cache/zig/p/zigzag-0.1.0-YXwYSzd2BgAKEIwRD_yf8qR96bnv2XOuYqEqv8OIEPLw/src/core/program.zig:117:27: 0x1149799 in run (root.
zig)
try self.start();
^
src/editor.zig:76:20: 0x11499b7 in main (editor.zig)
try program.run();
^
/usr/lib/zig/std/start.zig:627:37: 0x114a289 in posixCallMainAndExit (std.zig)
const result = root.main() catch |err| {
^
/usr/lib/zig/std/start.zig:232:5: 0x1141351 in _start (std.zig)
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
run-editor
+- run exe editor failure
error: the following command terminated unexpectedly:
./.zig-cache/o/7e2707fe90a7a8fbbeabe7d811d909f6/editor
Build Summary: 1/3 steps succeeded; 1 failed
run-editor transitive failure
+- run exe editor failure
error: the following build command failed with exit code 1:
.zig-cache/o/43850ca299773fd56213c75d4336c944/build /usr/bin/zig /usr/lib/zig /home/matwa/MyStuff/keyreader .zig-cache /home/matwa/.
cache/zig --seed 0xf9e04775 -Z7c936a0cd6580b21 run-editorAnd it absolutely wrecks my terminal
Thte allocator seems to be valid until i wan't to append with it, i cannot explain why, ty in advance.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels