From 70e0cafafe76c6249fd4a9a3979719f9920094a3 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 22 Jan 2024 11:18:59 -0600 Subject: [PATCH] window: add hideCursor and showCursor Signed-off-by: Tim Culverhouse --- src/Screen.zig | 4 ++++ src/Window.zig | 16 +++++++++++++++- src/vaxis.zig | 11 +++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Screen.zig b/src/Screen.zig index 61666e0..170451f 100644 --- a/src/Screen.zig +++ b/src/Screen.zig @@ -12,6 +12,10 @@ height: usize = 0, buf: []Cell = undefined, +cursor_row: usize = 0, +cursor_col: usize = 0, +cursor_vis: bool = false, + /// sets each cell to the default cell pub fn init(self: *Screen) void { for (self.buf, 0..) |_, i| { diff --git a/src/Window.zig b/src/Window.zig index 6a8ab0b..05002fa 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -63,7 +63,7 @@ pub fn initChild( /// writes a cell to the location in the window pub fn writeCell(self: Window, col: usize, row: usize, cell: Cell) void { if (self.height == 0 or self.width == 0) return; - if (self.height < row or self.width < col) return; + if (self.height <= row or self.width <= col) return; self.screen.writeCell(col + self.x_off, row + self.y_off, cell); } @@ -83,6 +83,20 @@ pub fn fill(self: Window, cell: Cell) void { } } +/// hide the cursor +pub fn hideCursor(self: Window) void { + self.screen.cursor_vis = false; +} + +/// show the cursor at the given coordinates, 0 indexed +pub fn showCursor(self: Window, col: usize, row: usize) void { + if (self.height == 0 or self.width == 0) return; + if (self.height <= row or self.width <= col) return; + self.screen.cursor_vis = true; + self.screen.cursor_row = row + self.y_off; + self.screen.cursor_col = col + self.x_off; +} + test "Window size set" { var parent = Window{ .x_off = 0, diff --git a/src/vaxis.zig b/src/vaxis.zig index 253f173..865df8f 100644 --- a/src/vaxis.zig +++ b/src/vaxis.zig @@ -370,6 +370,17 @@ pub fn Vaxis(comptime T: type) type { } _ = try tty.write(cell.char.grapheme); } + if (self.screen.cursor_vis) { + try std.fmt.format( + tty.buffered_writer.writer(), + ctlseqs.cup, + .{ + self.screen.cursor_row + 1, + self.screen.cursor_col + 1, + }, + ); + _ = try tty.write(ctlseqs.show_cursor); + } } }; }