From a44d2c53cae8f39975a7e2ecb7c254618aec4ae8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 29 Jun 2026 11:07:01 -0300 Subject: scrollbar --- main.zig | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'main.zig') diff --git a/main.zig b/main.zig index 60bc1290..1181daf7 100644 --- a/main.zig +++ b/main.zig @@ -81,6 +81,7 @@ fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, loop: *Loop) !v const nrows = 64; const ncols = 100; +const term_x_padding = 3; pub fn main(init: std.process.Init) !void { const io = init.io; @@ -139,6 +140,15 @@ pub fn main(init: std.process.Init) !void { if (mouse.button == .wheel_up) { term.screens.active.scroll(.{ .delta_row = -1 }); } + + if (mouse.type == .press) { + if (mouse.button == .left and mouse.col <= term_x_padding) { + term.screens.active.scroll(.{ .delta_row = -(mouse.row) }); + } + if (mouse.button == .right and mouse.col <= term_x_padding) { + term.screens.active.scroll(.{ .delta_row = mouse.row }); + } + } }, .key_press => |key| { if (key.matches('c', .{ .ctrl = true })) { @@ -191,12 +201,37 @@ pub fn main(init: std.process.Init) !void { const win_child = win.child(.{ .border = .{ .where = .bottom }, + .x_off = term_x_padding, .height = nrows + 2, .width = ncols + 2, }); + + const win_scroll = win.child(.{ + .border = .{ .where = .none }, + .height = nrows + 2, + .width = term_x_padding - 1, + }); + + const scroll_pos = (@as(usize, win_scroll.height) * term.screens.active.pages.scrollbar().offset) / term.screens.active.pages.scrollbar().total; + const scroll_height = @max(1, (@as(usize, win_scroll.height) * term.screens.active.pages.scrollbar().len) / term.screens.active.pages.scrollbar().total); + + win_scroll.fill(.{ + .char = .{ .grapheme = " " }, + .style = .{ .bg = .{ .index = 4 } }, + }); + + for (0..term_x_padding) |col| { + for (0..scroll_height) |row| { + win_scroll.writeCell(@intCast(col), @intCast(scroll_pos + row), .{ + .char = .{ .grapheme = " " }, + .style = .{ .bg = .{ .index = 0 }, .fg = .{ .index = 6 } }, + }); + } + } + const win_dbg = win.child(.{ .border = .{ .where = .all }, - .x_off = ncols + 2, + .x_off = ncols + 2 + term_x_padding, .height = nrows + 2, .width = ncols + 2, }); @@ -207,9 +242,14 @@ pub fn main(init: std.process.Init) !void { const dbg_str = try std.fmt.bufPrintSentinel( &dbg_print_buf, - "c {} {}\npty pid {}\nwow it's working! debugging things will show here", + "c {} {}\npty pid {}\n wow it's working! debugging things will show here\n scroll {}/{} vis: {}\n", .{ - pty_master.handle, term.screens.active.cursor.x, term.screens.active.cursor.y, + pty_master.handle, + term.screens.active.cursor.x, + term.screens.active.cursor.y, + term.screens.active.pages.scrollbar().offset, + term.screens.active.pages.scrollbar().total, + term.screens.active.pages.scrollbar().len, }, 0, ); -- cgit v1.3