From 50adf32f14dd255f36678a4c23f1d4fa39633a2c Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Sun, 20 Apr 2025 20:54:30 +0200 Subject: [PATCH] add(style): cursor style to indicate a cursor position --- src/render.zig | 12 +++++++++++- src/style.zig | 1 + src/terminal.zig | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/render.zig b/src/render.zig index 032c2d3..279d088 100644 --- a/src/render.zig +++ b/src/render.zig @@ -88,7 +88,9 @@ pub const Buffered = struct { /// Write *virtual screen* to alternate screen (should be called once and last during each render loop iteration in the main loop). pub fn flush(this: *@This()) !void { + try terminal.hideCursor(); // TODO measure timings of rendered frames? + var cursor_position: ?Point = null; const writer = terminal.writer(); const s = this.screen; const vs = this.virtual_screen; @@ -99,12 +101,20 @@ pub const Buffered = struct { const cvs = vs[idx]; if (cs.eql(cvs)) continue; + if (cvs.style.cursor) cursor_position = .{ + .x = @truncate(col), + .y = @truncate(row), + }; // render differences found in virtual screen - try terminal.setCursorPosition(.{ .y = @truncate(row + 1), .x = @truncate(col + 1) }); + try terminal.setCursorPosition(.{ .y = @truncate(row), .x = @truncate(col) }); try cvs.value(writer); // update screen to be the virtual screen for the next frame s[idx] = vs[idx]; } } + if (cursor_position) |point| { + try terminal.showCursor(); + try terminal.setCursorPosition(point); + } } }; diff --git a/src/style.zig b/src/style.zig index 9e5f53b..4c58a37 100644 --- a/src/style.zig +++ b/src/style.zig @@ -37,6 +37,7 @@ pub const Emphasis = enum(u8) { fg: Color = .default, bg: Color = .default, ul: Color = .default, +cursor: bool = false, ul_style: Underline = .off, emphasis: []const Emphasis, diff --git a/src/terminal.zig b/src/terminal.zig index e92be39..b6b6d4b 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -91,7 +91,7 @@ pub fn writer() Writer { pub fn setCursorPosition(pos: Point) !void { var buf: [64]u8 = undefined; - const value = try std.fmt.bufPrint(&buf, "\x1b[{d};{d}H", .{ pos.y, pos.x }); + const value = try std.fmt.bufPrint(&buf, "\x1b[{d};{d}H", .{ pos.y + 1, pos.x + 1 }); _ = try std.posix.write(std.posix.STDIN_FILENO, value); }