add(style): cursor style to indicate a cursor position
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 25s

This commit is contained in:
2025-04-20 20:54:30 +02:00
parent a4293ff243
commit 50adf32f14
3 changed files with 13 additions and 2 deletions

View File

@@ -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);
}
}
};

View File

@@ -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,

View File

@@ -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);
}