a modern tui library written in zig
1const std = @import("std");
2const vaxis = @import("../main.zig");
3
4const Scrollbar = @This();
5
6/// character to use for the scrollbar
7character: vaxis.Cell.Character = .{ .grapheme = "▐", .width = 1 },
8
9/// style to draw the bar character with
10style: vaxis.Style = .{},
11
12/// index of the top of the visible area
13top: usize = 0,
14
15/// total items in the list
16total: usize,
17
18/// total items that fit within the view area
19view_size: usize,
20
21pub fn draw(self: Scrollbar, win: vaxis.Window) void {
22 // don't draw when 0 items
23 if (self.total < 1) return;
24
25 // don't draw when all items can be shown
26 if (self.view_size >= self.total) return;
27
28 const bar_height = @max(std.math.divCeil(usize, self.view_size * win.height, self.total) catch unreachable, 1);
29 const bar_top = self.top * win.height / self.total;
30 var i: usize = 0;
31 while (i < bar_height) : (i += 1)
32 win.writeCell(0, @intCast(i + bar_top), .{ .char = self.character, .style = self.style });
33}