this repo has no description
1const std = @import("std");
2const vaxis = @import("vaxis");
3const Cell = vaxis.Cell;
4
5const log = std.log.scoped(.main);
6pub fn main() !void {
7 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
8 defer {
9 const deinit_status = gpa.deinit();
10 //fail test; can't try in defer as defer is executed after we return
11 if (deinit_status == .leak) {
12 log.err("memory leak", .{});
13 }
14 }
15 const alloc = gpa.allocator();
16
17 var tty = try vaxis.Tty.init();
18 defer tty.deinit();
19
20 var vx = try vaxis.init(alloc, .{});
21 defer vx.deinit(alloc, tty.anyWriter());
22
23 var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx };
24 try loop.init();
25
26 try loop.start();
27 defer loop.stop();
28
29 // Optionally enter the alternate screen
30 try vx.enterAltScreen(tty.anyWriter());
31 try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
32
33 // We'll adjust the color index every keypress
34 var color_idx: u8 = 0;
35 const msg = "Hello, world!";
36
37 var scale: u3 = 1;
38
39 // The main event loop. Vaxis provides a thread safe, blocking, buffered
40 // queue which can serve as the primary event queue for an application
41 while (true) {
42 // nextEvent blocks until an event is in the queue
43 const event = loop.nextEvent();
44 log.debug("event: {}", .{event});
45 // exhaustive switching ftw. Vaxis will send events if your Event
46 // enum has the fields for those events (ie "key_press", "winsize")
47 switch (event) {
48 .key_press => |key| {
49 color_idx = switch (color_idx) {
50 255 => 0,
51 else => color_idx + 1,
52 };
53 if (key.codepoint == 'c' and key.mods.ctrl) {
54 break;
55 }
56 if (key.matches('j', .{})) {
57 if (vx.caps.scaled_text and scale > 1) {
58 scale -= 1;
59 }
60 }
61 if (key.matches('k', .{})) {
62 if (vx.caps.scaled_text and scale < 7) {
63 scale += 1;
64 }
65 }
66 },
67 .winsize => |ws| {
68 try vx.resize(alloc, tty.anyWriter(), ws);
69 },
70 else => {},
71 }
72
73 // vx.window() returns the root window. This window is the size of the
74 // terminal and can spawn child windows as logical areas. Child windows
75 // cannot draw outside of their bounds
76 const win = vx.window();
77 // Clear the entire space because we are drawing in immediate mode.
78 // vaxis double buffers the screen. This new frame will be compared to
79 // the old and only updated cells will be drawn
80 win.clear();
81
82 const msg_len: u16 = @intCast(msg.len);
83 // Create some child window. .expand means the height and width will
84 // fill the remaining space of the parent. Child windows do not store a
85 // reference to their parent: this is true immediate mode. Do not store
86 // windows, always create new windows each render cycle
87 const child = win.child(
88 .{ .x_off = win.width / 2 - msg_len / 2, .y_off = win.height / 2 },
89 );
90 // Loop through the message and print the cells to the screen
91 for (msg, 0..) |_, i| {
92 const cell: Cell = .{
93 // each cell takes a _grapheme_ as opposed to a single
94 // codepoint. This allows Vaxis to handle emoji properly,
95 // particularly with terminals that the Unicode Core extension
96 // (IE Mode 2027)
97 .char = .{ .grapheme = msg[i .. i + 1] },
98 .style = .{
99 .fg = .{ .index = color_idx },
100 },
101 .scale = .{
102 .scale = scale,
103 },
104 };
105 const second_cell: Cell = .{
106 .char = .{ .grapheme = msg[i .. i + 1] },
107 .style = .{
108 .fg = .{ .index = color_idx },
109 },
110 };
111 child.writeCell(@intCast(i * scale), 0, cell);
112 child.writeCell(@intCast(i), scale - 1, second_cell);
113 child.writeCell(@intCast(i), scale, second_cell);
114 }
115 // Render the screen
116 try vx.render(tty.anyWriter());
117 }
118}
119
120// Our Event. This can contain internal events as well as Vaxis events.
121// Internal events can be posted into the same queue as vaxis events to allow
122// for a single event loop with exhaustive switching. Booya
123const Event = union(enum) {
124 key_press: vaxis.Key,
125 winsize: vaxis.Winsize,
126 focus_in,
127 foo: u8,
128};