parse SGR really poorly

This commit is contained in:
Mitchell Hashimoto
2022-05-09 10:02:23 -07:00
parent 86ab28cf10
commit 6f66a0dbeb
2 changed files with 42 additions and 0 deletions

View File

@ -1,4 +1,5 @@
//! The primary terminal emulation structure. This represents a single //! The primary terminal emulation structure. This represents a single
//!
//! "terminal" containing a grid of characters and exposes various operations //! "terminal" containing a grid of characters and exposes various operations
//! on that grid. This also maintains the scrollback buffer. //! on that grid. This also maintains the scrollback buffer.
const Terminal = @This(); const Terminal = @This();
@ -50,8 +51,12 @@ const Cell = struct {
/// Cursor represents the cursor state. /// Cursor represents the cursor state.
const Cursor = struct { const Cursor = struct {
// x, y where the cursor currently exists (0-indexed).
x: usize, x: usize,
y: usize, y: usize,
// Bold specifies that text written should be bold
bold: bool = false,
}; };
/// Initialize a new terminal. /// 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}), else => log.warn("unimplemented CSI: {}", .{action}),
} }
} }
@ -255,6 +274,16 @@ pub fn bell(self: *Terminal) void {
log.info("bell", .{}); 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 // Set Cursor Position. Move cursor to the position indicated
// by row and column (1-indexed). If column is 0, it is adjusted to 1. // 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 // If column is greater than the right-most column it is adjusted to

View File

@ -16,3 +16,16 @@ pub const C0 = enum(u7) {
/// Carriage return /// Carriage return
CR = 0x0D, 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.
_,
};