From 2c0fbfccc6907261f77755f13698932a6b21dc2b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 10 May 2022 19:42:00 -0700 Subject: [PATCH] implement CUD, CUU --- src/Window.zig | 8 ++++++++ src/terminal/Terminal.zig | 29 +++++++++++++++++++++++++++-- src/terminal/stream.zig | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Window.zig b/src/Window.zig index 60bacd566..0b21f5090 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -525,6 +525,14 @@ pub fn setCursorRight(self: *Window, amount: u16) !void { self.terminal.cursorRight(amount); } +pub fn setCursorDown(self: *Window, amount: u16) !void { + self.terminal.cursorDown(amount); +} + +pub fn setCursorUp(self: *Window, amount: u16) !void { + self.terminal.cursorUp(amount); +} + pub fn setCursorCol(self: *Window, col: u16) !void { try self.terminal.setCursorPos(self.terminal.cursor.y + 1, col); } diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 4fde7762b..89b6836fb 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -295,11 +295,36 @@ pub fn cursorRight(self: *Terminal, count: usize) void { defer tracy.end(); self.cursor.x += count; - if (self.cursor.x == self.cols) { - self.cursor.x -= 1; + if (self.cursor.x >= self.cols) { + self.cursor.x = self.cols - 1; } } +/// Move the cursor down amount lines. If amount is greater than the maximum +/// move distance then it is internally adjusted to the maximum. This sequence +/// will not scroll the screen or scroll region. If amount is 0, adjust it to 1. +// TODO: test +pub fn cursorDown(self: *Terminal, count: usize) void { + const tracy = trace(@src()); + defer tracy.end(); + + self.cursor.y += count; + if (self.cursor.y >= self.rows) { + self.cursor.y = self.rows - 1; + } +} + +/// Move the cursor up amount lines. If amount is greater than the maximum +/// move distance then it is internally adjusted to the maximum. If amount is +/// 0, adjust it to 1. +// TODO: test +pub fn cursorUp(self: *Terminal, count: usize) void { + const tracy = trace(@src()); + defer tracy.end(); + + self.cursor.y -|= count; +} + /// Backspace moves the cursor back a column (but not less than 0). pub fn backspace(self: *Terminal) void { const tracy = trace(@src()); diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index e2da79c0d..f9f6d7c3d 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -94,6 +94,30 @@ pub fn Stream(comptime Handler: type) type { fn csiDispatch(self: *Self, action: Parser.Action.CSI) !void { switch (action.final) { + // CUU - Cursor Up + 'A' => if (@hasDecl(T, "setCursorUp")) try self.handler.setCursorUp( + switch (action.params.len) { + 0 => 1, + 1 => action.params[0], + else => { + log.warn("invalid cursor up command: {}", .{action}); + return; + }, + }, + ) else log.warn("unimplemented CSI callback: {}", .{action}), + + // CUD - Cursor Down + 'B' => if (@hasDecl(T, "setCursorDown")) try self.handler.setCursorDown( + switch (action.params.len) { + 0 => 1, + 1 => action.params[0], + else => { + log.warn("invalid cursor down command: {}", .{action}); + return; + }, + }, + ) else log.warn("unimplemented CSI callback: {}", .{action}), + // CUF - Cursor Right 'C' => if (@hasDecl(T, "setCursorRight")) try self.handler.setCursorRight( switch (action.params.len) {