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