this repo has no description
13
fork

Configure Feed

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

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