this repo has no description
13
fork

Configure Feed

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

at fa6f3247ee82741d20e5193e532b3fb07a3082bb 123 lines 3.6 kB view raw
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 defer tty.deinit(); 26 27 var vx = try vaxis.init(alloc, .{}); 28 defer vx.deinit(alloc, tty.writer()); 29 30 var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; 31 try loop.init(); 32 33 try loop.start(); 34 defer loop.stop(); 35 36 try vx.enterAltScreen(tty.writer()); 37 try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); 38 39 try vx.queryColor(tty.writer(), .fg); 40 try vx.queryColor(tty.writer(), .bg); 41 var pct: u8 = 0; 42 var dir: enum { 43 up, 44 down, 45 } = .up; 46 47 const fg = [_]u8{ 192, 202, 245 }; 48 const bg = [_]u8{ 26, 27, 38 }; 49 50 // block until we get a resize 51 while (true) { 52 const event = loop.nextEvent(); 53 switch (event) { 54 .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, 55 .winsize => |ws| { 56 try vx.resize(alloc, tty.writer(), ws); 57 break; 58 }, 59 } 60 } 61 62 while (true) { 63 while (loop.tryEvent()) |event| { 64 switch (event) { 65 .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, 66 .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), 67 } 68 } 69 70 const win = vx.window(); 71 win.clear(); 72 73 const color = try blendColors(bg, fg, pct); 74 75 const style: vaxis.Style = .{ .fg = color }; 76 77 const segment: vaxis.Segment = .{ 78 .text = vaxis.logo, 79 .style = style, 80 }; 81 const center = vaxis.widgets.alignment.center(win, 28, 4); 82 _ = center.printSegment(segment, .{ .wrap = .grapheme }); 83 // var bw = tty.bufferedWriter(); 84 // try vx.render(bw.writer().any()); 85 // try bw.flush(); 86 try vx.render(tty.writer()); 87 std.Thread.sleep(16 * std.time.ns_per_ms); 88 switch (dir) { 89 .up => { 90 pct += 1; 91 if (pct == 100) dir = .down; 92 }, 93 .down => { 94 pct -= 1; 95 if (pct == 0) dir = .up; 96 }, 97 } 98 } 99} 100 101/// blend two rgb colors. pct is an integer percentage for te portion of 'b' in 102/// 'a' 103fn blendColors(a: [3]u8, b: [3]u8, pct: u8) !vaxis.Color { 104 // const r_a = (a[0] * (100 -| pct)) / 100; 105 106 const r_a = (@as(u16, a[0]) * @as(u16, (100 -| pct))) / 100; 107 const r_b = (@as(u16, b[0]) * @as(u16, pct)) / 100; 108 109 const g_a = (@as(u16, a[1]) * @as(u16, (100 -| pct))) / 100; 110 const g_b = (@as(u16, b[1]) * @as(u16, pct)) / 100; 111 // const g_a = try std.math.mul(u8, a[1], (100 -| pct) / 100); 112 // const g_b = (b[1] * pct) / 100; 113 114 const b_a = (@as(u16, a[2]) * @as(u16, (100 -| pct))) / 100; 115 const b_b = (@as(u16, b[2]) * @as(u16, pct)) / 100; 116 // const b_a = try std.math.mul(u8, a[2], (100 -| pct) / 100); 117 // const b_b = (b[2] * pct) / 100; 118 return .{ .rgb = [_]u8{ 119 @min(r_a + r_b, 255), 120 @min(g_a + g_b, 255), 121 @min(b_a + b_b, 255), 122 } }; 123}