this repo has no description
13
fork

Configure Feed

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

examples(xev): duplicate functionality of vaxis example

The xev example is now a complete replication of the vaxis example, but
using xev as the event loop

+80 -14
+79 -13
examples/xev.zig
··· 3 3 const xev = @import("xev"); 4 4 const Cell = vaxis.Cell; 5 5 6 - const Event = union(enum) { 7 - key_press: vaxis.Key, 8 - winsize: vaxis.Winsize, 9 - }; 6 + pub const panic = vaxis.panic_handler; 7 + 8 + const App = struct { 9 + const lower_limit: u8 = 30; 10 + const next_ms: u64 = 8; 11 + 12 + allocator: std.mem.Allocator, 13 + vx: *vaxis.Vaxis, 14 + buffered_writer: std.io.BufferedWriter(4096, std.io.AnyWriter), 15 + color_idx: u8, 16 + dir: enum { 17 + up, 18 + down, 19 + }, 10 20 11 - pub const panic = vaxis.panic_handler; 21 + fn draw(self: *App) !void { 22 + const style: vaxis.Style = .{ .fg = .{ .rgb = [_]u8{ self.color_idx, self.color_idx, self.color_idx } } }; 23 + 24 + const segment: vaxis.Segment = .{ 25 + .text = vaxis.logo, 26 + .style = style, 27 + }; 28 + const win = self.vx.window(); 29 + win.clear(); 30 + const center = vaxis.widgets.alignment.center(win, 28, 4); 31 + _ = try center.printSegment(segment, .{ .wrap = .grapheme }); 32 + switch (self.dir) { 33 + .up => { 34 + self.color_idx += 1; 35 + if (self.color_idx == 255) self.dir = .down; 36 + }, 37 + .down => { 38 + self.color_idx -= 1; 39 + if (self.color_idx == lower_limit) self.dir = .up; 40 + }, 41 + } 42 + try self.vx.render(self.buffered_writer.writer().any()); 43 + try self.buffered_writer.flush(); 44 + } 45 + }; 12 46 13 47 pub fn main() !void { 14 48 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; ··· 19 53 std.log.err("memory leak", .{}); 20 54 } 21 55 } 22 - var alloc = gpa.allocator(); 56 + const alloc = gpa.allocator(); 23 57 24 58 var tty = try vaxis.Tty.init(); 25 59 ··· 29 63 var loop = try xev.Loop.init(.{}); 30 64 defer loop.deinit(); 31 65 32 - var vx_loop: vaxis.xev.TtyWatcher(std.mem.Allocator) = undefined; 33 - try vx_loop.init(&tty, &vx, &loop, &alloc, callback); 66 + var app: App = .{ 67 + .allocator = alloc, 68 + .buffered_writer = tty.bufferedWriter(), 69 + .color_idx = App.lower_limit, 70 + .dir = .up, 71 + .vx = &vx, 72 + }; 73 + 74 + var vx_loop: vaxis.xev.TtyWatcher(App) = undefined; 75 + try vx_loop.init(&tty, &vx, &loop, &app, eventCallback); 34 76 35 77 try vx.enterAltScreen(tty.anyWriter()); 36 78 // send queries asynchronously 37 79 try vx.queryTerminalSend(tty.anyWriter()); 38 80 81 + const timer = try xev.Timer.init(); 82 + var timer_cmp: xev.Completion = .{}; 83 + timer.run(&loop, &timer_cmp, App.next_ms, App, &app, timerCallback); 84 + 39 85 try loop.run(.until_done); 40 86 } 41 87 42 - fn callback( 43 - ud: ?*std.mem.Allocator, 88 + fn eventCallback( 89 + ud: ?*App, 44 90 loop: *xev.Loop, 45 - watcher: *vaxis.xev.TtyWatcher(std.mem.Allocator), 91 + watcher: *vaxis.xev.TtyWatcher(App), 46 92 event: vaxis.xev.Event, 47 93 ) xev.CallbackAction { 94 + const app = ud orelse unreachable; 95 + const writer = app.buffered_writer.writer().any(); 48 96 switch (event) { 49 97 .key_press => |key| { 50 98 if (key.matches('c', .{ .ctrl = true })) { ··· 52 100 return .disarm; 53 101 } 54 102 }, 55 - .winsize => |ws| watcher.vx.resize(ud.?.*, watcher.tty.anyWriter(), ws) catch @panic("TODO"), 103 + .winsize => |ws| watcher.vx.resize(app.allocator, writer, ws) catch @panic("TODO"), 56 104 else => {}, 57 105 } 58 106 const win = watcher.vx.window(); 59 107 win.clear(); 60 - watcher.vx.render(watcher.tty.anyWriter()) catch { 108 + watcher.vx.render(writer) catch { 61 109 std.log.err("couldn't render", .{}); 62 110 return .disarm; 63 111 }; 112 + app.buffered_writer.flush() catch @panic("couldn't flush"); 64 113 return .rearm; 65 114 } 115 + 116 + fn timerCallback( 117 + ud: ?*App, 118 + l: *xev.Loop, 119 + c: *xev.Completion, 120 + r: xev.Timer.RunError!void, 121 + ) xev.CallbackAction { 122 + _ = r catch @panic("timer error"); 123 + 124 + var app = ud orelse return .disarm; 125 + app.draw() catch @panic("couldn't draw"); 126 + 127 + const timer = try xev.Timer.init(); 128 + timer.run(l, c, App.next_ms, App, ud, timerCallback); 129 + 130 + return .disarm; 131 + }
+1 -1
src/xev.zig
··· 146 146 } 147 147 seq_start += n; 148 148 const event_inner = result.event orelse { 149 - log.warn("unknown event: {s}", .{self.read_buf[seq_start - n + 1 .. seq_start]}); 149 + log.debug("unknown event: {s}", .{self.read_buf[seq_start - n + 1 .. seq_start]}); 150 150 continue :parse_loop; 151 151 }; 152 152