this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

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