From e325ea1616e166fe91f325a0034aa47cd230c480 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 26 Jun 2022 17:37:08 -0700 Subject: [PATCH] parse more SGR attrs --- src/Grid.zig | 2 ++ src/terminal/Screen.zig | 6 ++++++ src/terminal/Terminal.zig | 5 +++++ src/terminal/sgr.zig | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/Grid.zig b/src/Grid.zig index 628ea2884..7aa84620f 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -280,6 +280,8 @@ pub fn updateCells(self: *Grid, term: Terminal) !void { defer y += 1; for (line) |cell, x| { + // TODO: inverse + // If the cell has a background, we always draw it. if (cell.bg) |rgb| { self.cells.appendAssumeCapacity(.{ diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index ecb7a785b..920ad1335 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -20,6 +20,12 @@ pub const Cell = struct { fg: ?color.RGB = null, bg: ?color.RGB = null, + /// On/off attributes that can be set + /// TODO: pack it + attrs: struct { + inverse: u1 = 0, + } = .{}, + /// True if the cell should be skipped for drawing pub fn empty(self: Cell) bool { return self.char == 0; diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 4596cdb4b..8e20ea1c5 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -147,6 +147,11 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void { .unset => { self.cursor.pen.fg = null; self.cursor.pen.bg = null; + self.cursor.pen.attrs = .{}; + }, + + .inverse => { + self.cursor.pen.attrs.inverse = 1; }, .direct_color_fg => |rgb| { diff --git a/src/terminal/sgr.zig b/src/terminal/sgr.zig index cdd152656..b85c87fce 100644 --- a/src/terminal/sgr.zig +++ b/src/terminal/sgr.zig @@ -15,6 +15,15 @@ pub const Attribute = union(enum) { /// Bold the text. bold: void, + /// Underline the text + underline: void, + + /// Blink the text + blink: void, + + /// Invert fg/bg colors. + inverse: void, + /// Set foreground color as RGB values. direct_color_fg: RGB, @@ -68,6 +77,12 @@ pub const Parser = struct { 1 => return Attribute{ .bold = {} }, + 4 => return Attribute{ .underline = {} }, + + 5 => return Attribute{ .blink = {} }, + + 7 => return Attribute{ .inverse = {} }, + 30...37 => return Attribute{ .@"8_fg" = @intToEnum(color.Name, slice[0] - 30), }, @@ -178,6 +193,11 @@ test "sgr: bold" { try testing.expect(v == .bold); } +test "sgr: inverse" { + const v = testParse(&[_]u16{7}); + try testing.expect(v == .inverse); +} + test "sgr: 8 color" { var p: Parser = .{ .params = &[_]u16{ 31, 43, 103 } };