implement named 8 colors sgr

This commit is contained in:
Mitchell Hashimoto
2022-05-19 21:29:06 -07:00
parent 2309e7ffda
commit 69365b944c
2 changed files with 33 additions and 0 deletions

View File

@ -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_fg" => |idx| self.cursor.pen.fg = color.default[idx],
.@"256_bg" => |idx| self.cursor.pen.bg = color.default[idx], .@"256_bg" => |idx| self.cursor.pen.bg = color.default[idx],

View File

@ -2,6 +2,7 @@
const std = @import("std"); const std = @import("std");
const testing = std.testing; const testing = std.testing;
const color = @import("color.zig");
/// Attribute type for SGR /// Attribute type for SGR
pub const Attribute = union(enum) { pub const Attribute = union(enum) {
@ -20,6 +21,10 @@ pub const Attribute = union(enum) {
/// Set background color as RGB values. /// Set background color as RGB values.
direct_color_bg: RGB, 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. /// Set background color as 256-color palette.
@"256_bg": u8, @"256_bg": u8,
@ -59,6 +64,10 @@ pub const Parser = struct {
1 => return Attribute{ .bold = {} }, 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) { 38 => if (slice.len >= 5 and slice[1] == 2) {
self.idx += 4; 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) { 48 => if (slice.len >= 5 and slice[1] == 2) {
self.idx += 4; self.idx += 4;
@ -153,6 +166,22 @@ test "sgr: bold" {
try testing.expect(v == .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" { test "sgr: 256 color" {
var p: Parser = .{ .params = &[_]u16{ 38, 5, 161, 48, 5, 236 } }; var p: Parser = .{ .params = &[_]u16{ 38, 5, 161, 48, 5, 236 } };
try testing.expect(p.next().? == .@"256_fg"); try testing.expect(p.next().? == .@"256_fg");