From 043e29b885d37f01ff383bfcb68085b112c68729 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 May 2022 20:47:30 -0700 Subject: [PATCH] sgr parse bold and 256 fg/bg --- src/terminal/sgr.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/terminal/sgr.zig b/src/terminal/sgr.zig index f5c826471..4c82c8298 100644 --- a/src/terminal/sgr.zig +++ b/src/terminal/sgr.zig @@ -11,12 +11,21 @@ pub const Attribute = union(enum) { /// Unknown attribute, the raw CSI command parameters are here. unknown: []const u16, + /// Bold the text. + bold: void, + /// Set foreground color as RGB values. direct_color_fg: RGB, /// Set background color as RGB values. direct_color_bg: RGB, + /// Set background color as 256-color palette. + @"256_bg": u8, + + /// Set foreground color as 256-color palette. + @"256_fg": u8, + pub const RGB = struct { r: u8, g: u8, @@ -48,6 +57,8 @@ pub const Parser = struct { switch (slice[0]) { 0 => return Attribute{ .unset = {} }, + 1 => return Attribute{ .bold = {} }, + 38 => if (slice.len >= 5 and slice[1] == 2) { self.idx += 4; @@ -63,6 +74,11 @@ pub const Parser = struct { .b = @truncate(u8, rgb[2]), }, }; + } else if (slice.len >= 2 and slice[1] == 5) { + self.idx += 2; + return Attribute{ + .@"256_fg" = @truncate(u8, slice[2]), + }; }, 48 => if (slice.len >= 5 and slice[1] == 2) { @@ -80,6 +96,11 @@ pub const Parser = struct { .b = @truncate(u8, rgb[2]), }, }; + } else if (slice.len >= 2 and slice[1] == 5) { + self.idx += 2; + return Attribute{ + .@"256_bg" = @truncate(u8, slice[2]), + }; }, else => {}, @@ -126,3 +147,14 @@ test "sgr: Parser multiple" { try testing.expect(p.next() == null); try testing.expect(p.next() == null); } + +test "sgr: bold" { + const v = testParse(&[_]u16{1}); + try testing.expect(v == .bold); +} + +test "sgr: 256 color" { + var p: Parser = .{ .params = &[_]u16{ 38, 5, 161, 48, 5, 236 } }; + try testing.expect(p.next().? == .@"256_fg"); + try testing.expect(p.next().? == .@"256_bg"); +}