sgr parse bold and 256 fg/bg

This commit is contained in:
Mitchell Hashimoto
2022-05-19 20:47:30 -07:00
parent fd747ce274
commit 043e29b885

View File

@ -11,12 +11,21 @@ pub const Attribute = union(enum) {
/// Unknown attribute, the raw CSI command parameters are here. /// Unknown attribute, the raw CSI command parameters are here.
unknown: []const u16, unknown: []const u16,
/// Bold the text.
bold: void,
/// Set foreground color as RGB values. /// Set foreground color as RGB values.
direct_color_fg: RGB, direct_color_fg: RGB,
/// Set background color as RGB values. /// Set background color as RGB values.
direct_color_bg: RGB, 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 { pub const RGB = struct {
r: u8, r: u8,
g: u8, g: u8,
@ -48,6 +57,8 @@ pub const Parser = struct {
switch (slice[0]) { switch (slice[0]) {
0 => return Attribute{ .unset = {} }, 0 => return Attribute{ .unset = {} },
1 => return Attribute{ .bold = {} },
38 => if (slice.len >= 5 and slice[1] == 2) { 38 => if (slice.len >= 5 and slice[1] == 2) {
self.idx += 4; self.idx += 4;
@ -63,6 +74,11 @@ pub const Parser = struct {
.b = @truncate(u8, rgb[2]), .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) { 48 => if (slice.len >= 5 and slice[1] == 2) {
@ -80,6 +96,11 @@ pub const Parser = struct {
.b = @truncate(u8, rgb[2]), .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 => {}, else => {},
@ -126,3 +147,14 @@ test "sgr: Parser multiple" {
try testing.expect(p.next() == null); try testing.expect(p.next() == null);
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");
}