a modern tui library written in zig
at main 1.7 kB view raw
1const std = @import("std"); 2const vaxis = @import("../main.zig"); 3 4const digits = "0123456789"; 5 6num_lines: usize = std.math.maxInt(usize), 7highlighted_line: usize = 0, 8style: vaxis.Style = .{ .dim = true }, 9highlighted_style: vaxis.Style = .{ .dim = true, .bg = .{ .index = 0 } }, 10 11pub fn extractDigit(v: usize, n: usize) usize { 12 return (v / (std.math.powi(usize, 10, n) catch unreachable)) % 10; 13} 14 15pub fn numDigits(v: usize) u8 { 16 return switch (v) { 17 0...9 => 1, 18 10...99 => 2, 19 100...999 => 3, 20 1000...9999 => 4, 21 10000...99999 => 5, 22 100000...999999 => 6, 23 1000000...9999999 => 7, 24 10000000...99999999 => 8, 25 else => 0, 26 }; 27} 28 29pub fn draw(self: @This(), win: vaxis.Window, y_scroll: usize) void { 30 for (1 + y_scroll..self.num_lines) |line| { 31 if (line - 1 >= y_scroll +| win.height) { 32 break; 33 } 34 const highlighted = line == self.highlighted_line; 35 const num_digits = numDigits(line); 36 for (0..num_digits) |i| { 37 const digit = extractDigit(line, i); 38 win.writeCell(@intCast(win.width -| (i + 2)), @intCast(line -| (y_scroll +| 1)), .{ 39 .char = .{ 40 .width = 1, 41 .grapheme = digits[digit .. digit + 1], 42 }, 43 .style = if (highlighted) self.highlighted_style else self.style, 44 }); 45 } 46 if (highlighted) { 47 for (num_digits + 1..win.width) |i| { 48 win.writeCell(@intCast(i), @intCast(line -| (y_scroll +| 1)), .{ 49 .style = if (highlighted) self.highlighted_style else self.style, 50 }); 51 } 52 } 53 } 54}