this repo has no description
1const std = @import("std");
2const vaxis = @import("vaxis");
3
4const log = std.log.scoped(.main);
5
6const Event = union(enum) {
7 key_press: vaxis.Key,
8 winsize: vaxis.Winsize,
9};
10
11pub fn main(init: std.process.Init) !void {
12 const io = init.io;
13 const alloc = init.gpa;
14
15 var buffer: [1024]u8 = undefined;
16 var tty = try vaxis.Tty.init(io, &buffer);
17 defer tty.deinit();
18
19 var vx = try vaxis.init(io, alloc, init.environ_map, .{});
20 defer vx.deinit(alloc, tty.writer());
21
22 var loop: vaxis.Loop(Event) = .init(io, &tty, &vx);
23 try loop.start();
24 defer loop.stop();
25
26 try vx.enterAltScreen(tty.writer());
27 try vx.queryTerminal(tty.writer(), .fromSeconds(1));
28
29 var read_buffer: [1024 * 1024]u8 = undefined; // 1MB buffer
30 var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, io, "examples/zig.png", &read_buffer);
31 defer img1.deinit(alloc);
32
33 const imgs = [_]vaxis.Image{
34 try vx.transmitImage(alloc, tty.writer(), &img1, .rgba),
35 try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/vaxis.png" }),
36 };
37 defer vx.freeImage(tty.writer(), imgs[0].id);
38 defer vx.freeImage(tty.writer(), imgs[1].id);
39
40 var n: usize = 0;
41
42 var clip_y: u16 = 0;
43
44 while (true) {
45 const event = try loop.nextEvent();
46 switch (event) {
47 .key_press => |key| {
48 if (key.matches('c', .{ .ctrl = true })) {
49 return;
50 } else if (key.matches('l', .{ .ctrl = true })) {
51 vx.queueRefresh();
52 } else if (key.matches('j', .{}))
53 clip_y += 1
54 else if (key.matches('k', .{}))
55 clip_y -|= 1;
56 },
57 .winsize => |ws| try vx.resize(alloc, tty.writer(), ws),
58 }
59
60 n = (n + 1) % imgs.len;
61 const win = vx.window();
62 win.clear();
63
64 const img = imgs[n];
65 const dims = try img.cellSize(win);
66 const center = vaxis.widgets.alignment.center(win, dims.cols, dims.rows);
67 try img.draw(center, .{ .scale = .contain, .clip_region = .{
68 .y = clip_y,
69 } });
70
71 try vx.render(tty.writer());
72 }
73}
74
75// test {
76// std.testing.refAllDecls(@This());
77// }