From 69365b944cea5fde0cbb92084e115f8e1b0cf8f2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 May 2022 21:29:06 -0700 Subject: [PATCH] implement named 8 colors sgr --- src/terminal/Terminal.zig | 4 ++++ src/terminal/sgr.zig | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 90bd21f22..4a38c27a2 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -185,6 +185,10 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void { }; }, + .@"8_fg" => |n| self.cursor.pen.fg = color.default[@enumToInt(n)], + + .@"8_bg" => |n| self.cursor.pen.bg = color.default[@enumToInt(n)], + .@"256_fg" => |idx| self.cursor.pen.fg = color.default[idx], .@"256_bg" => |idx| self.cursor.pen.bg = color.default[idx], diff --git a/src/terminal/sgr.zig b/src/terminal/sgr.zig index 4c82c8298..46d62a151 100644 --- a/src/terminal/sgr.zig +++ b/src/terminal/sgr.zig @@ -2,6 +2,7 @@ const std = @import("std"); const testing = std.testing; +const color = @import("color.zig"); /// Attribute type for SGR pub const Attribute = union(enum) { @@ -20,6 +21,10 @@ pub const Attribute = union(enum) { /// Set background color as RGB values. direct_color_bg: RGB, + /// Set the background/foreground as a named color attribute. + @"8_bg": color.Name, + @"8_fg": color.Name, + /// Set background color as 256-color palette. @"256_bg": u8, @@ -59,6 +64,10 @@ pub const Parser = struct { 1 => return Attribute{ .bold = {} }, + 30...37 => return Attribute{ + .@"8_fg" = @intToEnum(color.Name, slice[0] - 30), + }, + 38 => if (slice.len >= 5 and slice[1] == 2) { self.idx += 4; @@ -81,6 +90,10 @@ pub const Parser = struct { }; }, + 40...47 => return Attribute{ + .@"8_bg" = @intToEnum(color.Name, slice[0] - 32), + }, + 48 => if (slice.len >= 5 and slice[1] == 2) { self.idx += 4; @@ -153,6 +166,22 @@ test "sgr: bold" { try testing.expect(v == .bold); } +test "sgr: 8 color" { + var p: Parser = .{ .params = &[_]u16{ 31, 43 } }; + + { + const v = p.next().?; + try testing.expect(v == .@"8_fg"); + try testing.expect(v.@"8_fg" == .red); + } + + { + const v = p.next().?; + try testing.expect(v == .@"8_bg"); + try testing.expect(v.@"8_bg" == .bright_yellow); + } +} + test "sgr: 256 color" { var p: Parser = .{ .params = &[_]u16{ 38, 5, 161, 48, 5, 236 } }; try testing.expect(p.next().? == .@"256_fg");