a modern tui library written in zig
1const std = @import("std");
2const vaxis = @import("vaxis");
3
4fn parseIterations(allocator: std.mem.Allocator) !usize {
5 var args = try std.process.argsWithAllocator(allocator);
6 defer args.deinit();
7 _ = args.next();
8 if (args.next()) |val| {
9 return std.fmt.parseUnsigned(usize, val, 10);
10 }
11 return 200;
12}
13
14fn printResults(writer: anytype, label: []const u8, iterations: usize, elapsed_ns: u64, total_bytes: usize) !void {
15 const ns_per_frame = elapsed_ns / @as(u64, @intCast(iterations));
16 const bytes_per_frame = total_bytes / iterations;
17 try writer.print(
18 "{s}: frames={d} total_ns={d} ns/frame={d} bytes={d} bytes/frame={d}\n",
19 .{ label, iterations, elapsed_ns, ns_per_frame, total_bytes, bytes_per_frame },
20 );
21}
22
23pub fn main() !void {
24 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
25 defer _ = gpa.deinit();
26 const allocator = gpa.allocator();
27
28 const iterations = try parseIterations(allocator);
29
30 var vx = try vaxis.init(allocator, .{});
31 var init_writer = std.io.Writer.Allocating.init(allocator);
32 defer init_writer.deinit();
33 defer vx.deinit(allocator, &init_writer.writer);
34
35 const winsize = vaxis.Winsize{ .rows = 24, .cols = 80, .x_pixel = 0, .y_pixel = 0 };
36 try vx.resize(allocator, &init_writer.writer, winsize);
37
38 const stdout = std.fs.File.stdout().deprecatedWriter();
39
40 var idle_writer = std.io.Writer.Allocating.init(allocator);
41 defer idle_writer.deinit();
42 var timer = try std.time.Timer.start();
43 var i: usize = 0;
44 while (i < iterations) : (i += 1) {
45 try vx.render(&idle_writer.writer);
46 }
47 const idle_ns = timer.read();
48 const idle_bytes: usize = idle_writer.writer.end;
49 try printResults(stdout, "idle", iterations, idle_ns, idle_bytes);
50
51 var dirty_writer = std.io.Writer.Allocating.init(allocator);
52 defer dirty_writer.deinit();
53 timer.reset();
54 i = 0;
55 while (i < iterations) : (i += 1) {
56 vx.queueRefresh();
57 try vx.render(&dirty_writer.writer);
58 }
59 const dirty_ns = timer.read();
60 const dirty_bytes: usize = dirty_writer.writer.end;
61 try printResults(stdout, "dirty", iterations, dirty_ns, dirty_bytes);
62}