this repo has no description
13
fork

Configure Feed

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

widgets: add LineNumbers widget

The LineNumbers widget draws vertical list of numbers.
This can be used as a line number bar for a text editor for example.

authored by

Jari Vetoniemi and committed by
Tim Culverhouse
b215aec7 f25b8ab4

+55
+1
src/widgets.zig
··· 7 7 pub const TextInput = @import("widgets/TextInput.zig"); 8 8 pub const nvim = @import("widgets/nvim.zig"); 9 9 pub const ScrollView = @import("widgets/ScrollView.zig"); 10 + pub const LineNumbers = @import("widgets/LineNumbers.zig");
+54
src/widgets/LineNumbers.zig
··· 1 + const std = @import("std"); 2 + const vaxis = @import("../main.zig"); 3 + 4 + const digits = "0123456789"; 5 + 6 + num_lines: usize = std.math.maxInt(usize), 7 + highlighted_line: usize = 0, 8 + style: vaxis.Style = .{ .dim = true }, 9 + highlighted_style: vaxis.Style = .{ .dim = true, .bg = .{ .index = 0 } }, 10 + 11 + pub fn extractDigit(v: usize, n: usize) usize { 12 + return (v / (std.math.powi(usize, 10, n) catch unreachable)) % 10; 13 + } 14 + 15 + pub fn numDigits(v: usize) usize { 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 + 29 + pub 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(win.width -| (i + 2), 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(i, line -| (y_scroll +| 1), .{ 49 + .style = if (highlighted) self.highlighted_style else self.style, 50 + }); 51 + } 52 + } 53 + } 54 + }