window: add hideCursor and showCursor

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-22 11:18:59 -06:00
parent f1a9e21a98
commit 70e0cafafe
3 changed files with 30 additions and 1 deletions

View file

@ -12,6 +12,10 @@ height: usize = 0,
buf: []Cell = undefined, buf: []Cell = undefined,
cursor_row: usize = 0,
cursor_col: usize = 0,
cursor_vis: bool = false,
/// sets each cell to the default cell /// sets each cell to the default cell
pub fn init(self: *Screen) void { pub fn init(self: *Screen) void {
for (self.buf, 0..) |_, i| { for (self.buf, 0..) |_, i| {

View file

@ -63,7 +63,7 @@ pub fn initChild(
/// writes a cell to the location in the window /// writes a cell to the location in the window
pub fn writeCell(self: Window, col: usize, row: usize, cell: Cell) void { pub fn writeCell(self: Window, col: usize, row: usize, cell: Cell) void {
if (self.height == 0 or self.width == 0) return; 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); 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" { test "Window size set" {
var parent = Window{ var parent = Window{
.x_off = 0, .x_off = 0,

View file

@ -370,6 +370,17 @@ pub fn Vaxis(comptime T: type) type {
} }
_ = try tty.write(cell.char.grapheme); _ = 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);
}
} }
}; };
} }