diff --git a/src/widgets.zig b/src/widgets.zig index e59e00a..5b97e82 100644 --- a/src/widgets.zig +++ b/src/widgets.zig @@ -1,5 +1,6 @@ pub const border = @import("widgets/border.zig"); pub const alignment = @import("widgets/alignment.zig"); -pub const TextInput = @import("widgets/TextInput.zig"); +pub const Scrollbar = @import("widgets/Scrollbar.zig"); pub const Table = @import("widgets/Table.zig"); +pub const TextInput = @import("widgets/TextInput.zig"); pub const nvim = @import("widgets/nvim.zig"); diff --git a/src/widgets/Scrollbar.zig b/src/widgets/Scrollbar.zig new file mode 100644 index 0000000..e67bac4 --- /dev/null +++ b/src/widgets/Scrollbar.zig @@ -0,0 +1,34 @@ +const std = @import("std"); +const vaxis = @import("../main.zig"); + +const Scrollbar = @This(); + +/// character to use for the scrollbar +character: vaxis.Cell.Character = .{ .grapheme = "▐", .width = 1 }, + +/// style to draw the bar character with +style: vaxis.Style = .{}, + +/// index of the top of the visible area +top: usize = 0, + +/// total items in the list +total: usize, + +/// total items that fit within the view area +view_size: usize, + +pub fn draw(self: Scrollbar, win: vaxis.Window) void { + // don't draw when 0 items + if (self.total < 1) return; + + // don't draw when all items can be shown + if (self.view_size >= self.total) return; + + var bar_height = self.view_size * win.height / self.total; + if (bar_height < 0) bar_height = 1; + const bar_top = self.top * win.height / self.total; + var i: usize = 0; + while (i < bar_height) : (i += 1) + win.writeCell(0, i + bar_top, .{ .char = self.character, .style = self.style }); +}