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 std.log.warn("YYYYY", .{});
13 const io = init.io;
14 const alloc = init.gpa;
15
16 var buffer: [1024]u8 = undefined;
17 std.log.warn("ZZZZ", .{});
18 var tty = try vaxis.Tty.init(io, &buffer);
19 defer tty.deinit();
20 std.log.warn("1111", .{});
21
22 var vx = try vaxis.init(io, alloc, init.environ_map, .{});
23 defer vx.deinit(alloc, tty.writer());
24 std.log.warn("2222", .{});
25
26 var loop: vaxis.Loop(Event) = .init(io, &tty, &vx);
27 std.log.warn("AAAA", .{});
28 try loop.start();
29 defer loop.stop();
30
31 std.log.warn("BBBB", .{});
32 try vx.enterAltScreen(tty.writer());
33 std.log.warn("CCCC", .{});
34 try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s);
35
36 std.log.warn("DDDD", .{});
37 var read_buffer: [1024 * 1024]u8 = undefined; // 1MB buffer
38 var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, io, "examples/zig.png", &read_buffer);
39 std.log.warn("EEEE", .{});
40 defer img1.deinit(alloc);
41
42 std.log.warn("FFFF", .{});
43 const imgs = [_]vaxis.Image{
44 try vx.transmitImage(alloc, tty.writer(), &img1, .rgba),
45 // var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, "examples/zig.png");
46 // try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/zig.png" }),
47 try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/vaxis.png" }),
48 };
49 std.log.warn("GGGG", .{});
50 defer vx.freeImage(tty.writer(), imgs[0].id);
51 std.log.warn("HHHH", .{});
52 defer vx.freeImage(tty.writer(), imgs[1].id);
53 std.log.warn("JJJJ", .{});
54
55 var n: usize = 0;
56
57 var clip_y: u16 = 0;
58
59 while (true) {
60 const event = loop.nextEvent();
61 switch (event) {
62 .key_press => |key| {
63 if (key.matches('c', .{ .ctrl = true })) {
64 return;
65 } else if (key.matches('l', .{ .ctrl = true })) {
66 vx.queueRefresh();
67 } else if (key.matches('j', .{}))
68 clip_y += 1
69 else if (key.matches('k', .{}))
70 clip_y -|= 1;
71 },
72 .winsize => |ws| try vx.resize(alloc, tty.writer(), ws),
73 }
74
75 n = (n + 1) % imgs.len;
76 const win = vx.window();
77 win.clear();
78
79 const img = imgs[n];
80 const dims = try img.cellSize(win);
81 const center = vaxis.widgets.alignment.center(win, dims.cols, dims.rows);
82 try img.draw(center, .{ .scale = .contain, .clip_region = .{
83 .y = clip_y,
84 } });
85
86 try vx.render(tty.writer());
87 }
88}