libvaxis/examples/pathological.zig
2024-05-22 13:51:19 -05:00

61 lines
1.7 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(alloc, .{});
defer vx.deinit(alloc);
var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
try loop.run(alloc);
defer loop.stop();
try vx.enterAltScreen();
try vx.queryTerminal();
while (true) {
const event = loop.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();
}
try vx.exitAltScreen();
const took = std.time.microTimestamp() - timer_start;
const stdout = std.io.getStdOut().writer();
try stdout.print("\r\ntook {d}ms to render 10,000 times\r\n", .{@divTrunc(took, std.time.us_per_ms)});
}