From 6f66a0dbeb42359dc6cbf9c9c37738e9e5f63c3e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 9 May 2022 10:02:23 -0700 Subject: [PATCH] parse SGR really poorly --- src/terminal/Terminal.zig | 29 +++++++++++++++++++++++++++++ src/terminal/ansi.zig | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index a38b917e8..31b5cca67 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1,4 +1,5 @@ //! The primary terminal emulation structure. This represents a single +//! //! "terminal" containing a grid of characters and exposes various operations //! on that grid. This also maintains the scrollback buffer. const Terminal = @This(); @@ -50,8 +51,12 @@ const Cell = struct { /// Cursor represents the cursor state. const Cursor = struct { + // x, y where the cursor currently exists (0-indexed). x: usize, y: usize, + + // Bold specifies that text written should be bold + bold: bool = false, }; /// Initialize a new terminal. @@ -212,6 +217,20 @@ fn csiDispatch( }, }), + // SGR - Select Graphic Rendition + 'm' => if (action.params.len == 0) { + // No values defaults to code 0 + try self.selectGraphicRendition(.default); + } else { + // Each parameter sets a separate aspect + for (action.params) |param| { + try self.selectGraphicRendition(@intToEnum( + ansi.RenditionAspect, + param, + )); + } + }, + else => log.warn("unimplemented CSI: {}", .{action}), } } @@ -255,6 +274,16 @@ pub fn bell(self: *Terminal) void { log.info("bell", .{}); } +pub fn selectGraphicRendition(self: *Terminal, aspect: ansi.RenditionAspect) !void { + switch (aspect) { + .default => self.cursor.bold = false, + .bold => self.cursor.bold = true, + .default_fg => {}, // TODO + .default_bg => {}, // TODO + else => log.warn("invalid or unimplemented rendition aspect: {}", .{aspect}), + } +} + // Set Cursor Position. Move cursor to the position indicated // by row and column (1-indexed). If column is 0, it is adjusted to 1. // If column is greater than the right-most column it is adjusted to diff --git a/src/terminal/ansi.zig b/src/terminal/ansi.zig index d30f1180a..0665bbb06 100644 --- a/src/terminal/ansi.zig +++ b/src/terminal/ansi.zig @@ -16,3 +16,16 @@ pub const C0 = enum(u7) { /// Carriage return CR = 0x0D, }; + +/// The SGR rendition aspects that can be set, sometimes known as attributes. +/// The value corresponds to the parameter value for the SGR command (ESC [ m). +pub const RenditionAspect = enum(u16) { + default = 0, + bold = 1, + default_fg = 39, + default_bg = 49, + + // Non-exhaustive so that @intToEnum never fails since the inputs are + // user-generated. + _, +};