Is this the wrong way to use writeCell()? #158
Replies: 4 comments 4 replies
-
|
while (x != 0) {
while (y != 0) {
***@***.***(x), @intcast(y), Cell{ .char = Cell.Character{ .grapheme = &[1]u8{'a'}, .width = 1 } });
y -= 1;
}
x -= 1;
}
When you use the syntax `&[1]u8{'a'}`, this creates a reference to an array of
length one and contents 'a'. The lifetime of this reference is tied to the
lifetime of the array, which in this case is scoped to the inner while loop. As
soon as the inner loop moves to the next iteration, the reference is gone - and
you are likely seeing bogus output.
If you have *static* strings, you can do `.grapheme = "a"`. If you have dynamic
strings, you need to do something to make sure their lifetime extends to after
your call to `vx.render` finishes.
|
Beta Was this translation helpful? Give feedback.
-
|
omg finally i have solved it, i've been banging my head against the wall for an hour or more for some reason it didn't work with a while loop, but when i switched to for it worked flawlessly, here's the final version of the outer while loop while (true) {
const event = loop.nextEvent();
switch (event) {
.key_press => |key| {
if (key.matches('c', .{ .ctrl = true })) {
break;
} else if (key.matches('q', .{})) {
break;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
}
},
.winsize => |ws| {
try vx.resize(alloc, tty.anyWriter(), ws);
},
else => {},
}
for (0..vx.screen.width) |i| {
for (0..vx.screen.height) |j| {
const cell: Cell = .{
.char = .{ .grapheme = "a" },
};
vx.screen.writeCell(@intCast(i), @intCast(j), cell);
}
}
try vx.render(tty.anyWriter());
} |
Beta Was this translation helpful? Give feedback.
-
|
Glad it's working! I would take a look at the vx.window() call and associated Window methods. They provided some nice helpers for writing to the screen. |
Beta Was this translation helpful? Give feedback.
-
|
You could try exiting on the second one received. The Loop implementation always sends one as the first event so that you know the initial window size. Subsequent events are from resizes. I wouldn’t call this reliable though.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here's what I do:
and here's the full code
New to Zig, sorry for the noob question!
Beta Was this translation helpful? Give feedback.
All reactions