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 var bar_height = self.view_size * win.height / self.total;
29 if (bar_height < 0) bar_height = 1;
30 const bar_top = self.top * win.height / self.total;
31 var i: usize = 0;
32 while (i < bar_height) : (i += 1)
33 win.writeCell(0, i + bar_top, .{ .char = self.character, .style = self.style });
34}