libvaxis/examples/pathological.zig
Jora Troosh 4a463cfa3a
refactor: make code more idiomatic
- Added standard .gitattributes file for Zig projects.
- Reworked build.zig a little, hopefully it's a bit clearer. Also, now zig build will run all steps.
- outer: while in examples was redundant since there's only one loop to break from. switch expressions don't allow breaking from them, so breaking is only for loops, i.e. while and for.
- When returning a struct instance from a function, the compiler infers the return type from function signature, so instead of return MyType{...}; , it's more idiomatic to write return .{...};.
- Logging adds a new line by default, so you don't usually need to write \n like here: log.debug("event: {}\r\n", .{event});.
2024-02-11 12:59:33 -06:00

58 lines
1.6 KiB
Zig

const std = @import("std");
const vaxis = @import("vaxis");
const Cell = vaxis.Cell;
const log = std.log.scoped(.main);
const Event = union(enum) {
winsize: vaxis.Winsize,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
var vx = try vaxis.init(Event, .{});
errdefer vx.deinit(alloc);
try vx.startReadThread();
defer vx.stopReadThread();
try vx.enterAltScreen();
try vx.queryTerminal();
while (true) {
const event = vx.nextEvent();
switch (event) {
.winsize => |ws| {
try vx.resize(alloc, ws);
break;
},
}
}
const timer_start = std.time.microTimestamp();
var iter: usize = 0;
while (iter < 10_000) : (iter += 1) {
const win = vx.window();
const child = win.initChild(0, 0, .{ .limit = 20 }, .{ .limit = 20 });
win.clear();
var row: usize = 0;
while (row < child.height) : (row += 1) {
var col: usize = 0;
while (col < child.width) : (col += 1) {
child.writeCell(col, row, .{
.char = .{
.grapheme = " ",
.width = 1,
},
.style = .{
.bg = .{ .index = @truncate(col + iter) },
},
});
}
}
try vx.render();
}
const took = std.time.microTimestamp() - timer_start;
vx.deinit(alloc);
log.info("took {d}ms", .{@divTrunc(took, std.time.us_per_ms)});
}