an experimental irc client
1const std = @import("std");
2const vaxis = @import("vaxis");
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/// The index of the bottom-most item, with 0 being "at the bottom"
13bottom: 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 // (view_size / total) * window height = size of the scroll bar
29 const bar_height = @max(std.math.divCeil(usize, self.view_size * win.height, self.total) catch unreachable, 1);
30
31 // The row of the last cell of the bottom of the bar
32 const bar_bottom = (win.height - 1) -| (std.math.divCeil(usize, self.bottom * win.height, self.total) catch unreachable);
33
34 var i: usize = 0;
35 while (i <= bar_height) : (i += 1)
36 win.writeCell(0, bar_bottom -| i, .{ .char = self.character, .style = self.style });
37}