this repo has no description
1const std = @import("std");
2const vaxis = @import("vaxis");
3const Cell = vaxis.Cell;
4
5const Event = union(enum) {
6 key_press: vaxis.Key,
7 winsize: vaxis.Winsize,
8};
9
10pub const panic = vaxis.panic_handler;
11
12pub fn main() !void {
13 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
14 defer {
15 const deinit_status = gpa.deinit();
16 //fail test; can't try in defer as defer is executed after we return
17 if (deinit_status == .leak) {
18 std.log.err("memory leak", .{});
19 }
20 }
21 const alloc = gpa.allocator();
22
23 var buffer: [1024]u8 = undefined;
24 var tty = try vaxis.Tty.init(&buffer);
25 const writer = tty.writer();
26 var vx = try vaxis.init(alloc, .{});
27 defer vx.deinit(alloc, writer);
28
29 var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx };
30 try loop.init();
31
32 try loop.start();
33 defer loop.stop();
34
35 try vx.enterAltScreen(writer);
36 try vx.queryTerminal(writer, 1 * std.time.ns_per_s);
37 var env = try std.process.getEnvMap(alloc);
38 defer env.deinit();
39
40 const vt_opts: vaxis.widgets.Terminal.Options = .{
41 .winsize = .{
42 .rows = 24,
43 .cols = 100,
44 .x_pixel = 0,
45 .y_pixel = 0,
46 },
47 .scrollback_size = 0,
48 .initial_working_directory = env.get("HOME") orelse @panic("no $HOME"),
49 };
50 const shell = env.get("SHELL") orelse "bash";
51 const argv = [_][]const u8{shell};
52 var write_buf: [4096]u8 = undefined;
53 var vt = try vaxis.widgets.Terminal.init(
54 alloc,
55 &argv,
56 &env,
57 &vx.unicode,
58 vt_opts,
59 &write_buf,
60 );
61 defer vt.deinit();
62 try vt.spawn();
63
64 var redraw: bool = false;
65 while (true) {
66 std.Thread.sleep(8 * std.time.ns_per_ms);
67 // try vt events first
68 while (vt.tryEvent()) |event| {
69 redraw = true;
70 switch (event) {
71 .bell => {},
72 .title_change => {},
73 .exited => return,
74 .redraw => {},
75 .pwd_change => {},
76 }
77 }
78 while (loop.tryEvent()) |event| {
79 redraw = true;
80 switch (event) {
81 .key_press => |key| {
82 if (key.matches('c', .{ .ctrl = true })) return;
83 try vt.update(.{ .key_press = key });
84 },
85 .winsize => |ws| try vx.resize(alloc, writer, ws),
86 }
87 }
88 if (!redraw) continue;
89 redraw = false;
90
91 const win = vx.window();
92 win.hideCursor();
93 win.clear();
94 const child = win.child(.{
95 .x_off = 4,
96 .y_off = 2,
97 .width = 120,
98 .height = 40,
99 .border = .{
100 .where = .all,
101 },
102 });
103
104 try vt.resize(.{
105 .rows = child.height,
106 .cols = child.width,
107 .x_pixel = 0,
108 .y_pixel = 0,
109 });
110 try vt.draw(alloc, child);
111
112 try vx.render(writer);
113 }
114}