configurable color palette

This commit is contained in:
Mitchell Hashimoto
2022-11-20 15:24:15 -08:00
parent ea6d1f7e23
commit 473eaa4b2f
3 changed files with 25 additions and 7 deletions

View File

@ -36,6 +36,16 @@ pub const Config = struct {
/// Foreground color for the window. /// Foreground color for the window.
foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF }, foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF },
/// Color palette for the 256 color form that many terminal applications
/// use. The syntax of this configuration is "N=HEXCODE" where "n"
/// is 0 to 255 (for the 256 colors) and HEXCODE is a typical RGB
/// color code such as "#AABBCC". The 0 to 255 correspond to the
/// terminal color table.
///
/// For definitions on all the codes:
/// https://www.ditig.com/256-colors-cheat-sheet
palette: Palette = .{},
/// The command to run, usually a shell. If this is not an absolute path, /// The command to run, usually a shell. If this is not an absolute path,
/// it'll be looked up in the PATH. If this is not set, a default will /// it'll be looked up in the PATH. If this is not set, a default will
/// be looked up from your system. The rules for the default lookup are: /// be looked up from your system. The rules for the default lookup are:

View File

@ -60,6 +60,9 @@ scrolling_region: ScrollingRegion,
/// The charset state /// The charset state
charset: CharsetState = .{}, charset: CharsetState = .{},
/// The color palette to use
color_palette: color.Palette = color.default,
/// The previous printed character. This is used for the repeat previous /// The previous printed character. This is used for the repeat previous
/// char CSI (ESC [ <n> b). /// char CSI (ESC [ <n> b).
previous_char: ?u21 = null, previous_char: ?u21 = null,
@ -429,12 +432,12 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void {
.@"8_fg" => |n| { .@"8_fg" => |n| {
self.screen.cursor.pen.attrs.has_fg = true; self.screen.cursor.pen.attrs.has_fg = true;
self.screen.cursor.pen.fg = color.default[@enumToInt(n)]; self.screen.cursor.pen.fg = self.color_palette[@enumToInt(n)];
}, },
.@"8_bg" => |n| { .@"8_bg" => |n| {
self.screen.cursor.pen.attrs.has_bg = true; self.screen.cursor.pen.attrs.has_bg = true;
self.screen.cursor.pen.bg = color.default[@enumToInt(n)]; self.screen.cursor.pen.bg = self.color_palette[@enumToInt(n)];
}, },
.reset_fg => self.screen.cursor.pen.attrs.has_fg = false, .reset_fg => self.screen.cursor.pen.attrs.has_fg = false,
@ -443,22 +446,22 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void {
.@"8_bright_fg" => |n| { .@"8_bright_fg" => |n| {
self.screen.cursor.pen.attrs.has_fg = true; self.screen.cursor.pen.attrs.has_fg = true;
self.screen.cursor.pen.fg = color.default[@enumToInt(n)]; self.screen.cursor.pen.fg = self.color_palette[@enumToInt(n)];
}, },
.@"8_bright_bg" => |n| { .@"8_bright_bg" => |n| {
self.screen.cursor.pen.attrs.has_bg = true; self.screen.cursor.pen.attrs.has_bg = true;
self.screen.cursor.pen.bg = color.default[@enumToInt(n)]; self.screen.cursor.pen.bg = self.color_palette[@enumToInt(n)];
}, },
.@"256_fg" => |idx| { .@"256_fg" => |idx| {
self.screen.cursor.pen.attrs.has_fg = true; self.screen.cursor.pen.attrs.has_fg = true;
self.screen.cursor.pen.fg = color.default[idx]; self.screen.cursor.pen.fg = self.color_palette[idx];
}, },
.@"256_bg" => |idx| { .@"256_bg" => |idx| {
self.screen.cursor.pen.attrs.has_bg = true; self.screen.cursor.pen.attrs.has_bg = true;
self.screen.cursor.pen.bg = color.default[idx]; self.screen.cursor.pen.bg = self.color_palette[idx];
}, },
.unknown => return error.InvalidAttribute, .unknown => return error.InvalidAttribute,

View File

@ -107,8 +107,13 @@ pub fn init(alloc: Allocator, opts: termio.Options) !Exec {
log.info("started subcommand path={s} pid={?}", .{ path, cmd.pid }); log.info("started subcommand path={s} pid={?}", .{ path, cmd.pid });
// Create our terminal // Create our terminal
var term = try terminal.Terminal.init(alloc, opts.grid_size.columns, opts.grid_size.rows); var term = try terminal.Terminal.init(
alloc,
opts.grid_size.columns,
opts.grid_size.rows,
);
errdefer term.deinit(alloc); errdefer term.deinit(alloc);
term.color_palette = opts.config.palette.value;
return Exec{ return Exec{
.alloc = alloc, .alloc = alloc,