feat: add functions to set the default terminal background/foreground

And reset to default on cleanup.
This commit is contained in:
CJ van den Berg 2024-08-12 22:17:00 +02:00 committed by Tim Culverhouse
parent a8baf9ce37
commit 7190fde166
2 changed files with 24 additions and 0 deletions

View file

@ -86,6 +86,8 @@ state: struct {
pixel_mouse: bool = false,
color_scheme_updates: bool = false,
in_band_resize: bool = false,
changed_default_fg: bool = false,
changed_default_bg: bool = false,
cursor: struct {
row: usize = 0,
col: usize = 0,
@ -156,6 +158,14 @@ pub fn resetState(self: *Vaxis, tty: AnyWriter) !void {
try tty.writeAll(ctlseqs.in_band_resize_reset);
self.state.in_band_resize = false;
}
if (self.state.changed_default_fg) {
try tty.writeAll(ctlseqs.osc10_reset);
self.state.changed_default_fg = false;
}
if (self.state.changed_default_bg) {
try tty.writeAll(ctlseqs.osc11_reset);
self.state.changed_default_bg = false;
}
}
/// resize allocates a slice of cells equal to the number of cells
@ -905,6 +915,18 @@ pub fn requestSystemClipboard(self: Vaxis, tty: AnyWriter) !void {
);
}
/// Set the default terminal foreground color
pub fn setTerminalForegroundColor(self: *Vaxis, tty: AnyWriter, rgb: [3]u8) !void {
try tty.print(ctlseqs.osc10_set, .{ rgb[0], rgb[0], rgb[1], rgb[1], rgb[2], rgb[2] });
self.state.changed_default_fg = true;
}
/// Set the default terminal background color
pub fn setTerminalBackgroundColor(self: *Vaxis, tty: AnyWriter, rgb: [3]u8) !void {
try tty.print(ctlseqs.osc11_set, .{ rgb[0], rgb[0], rgb[1], rgb[1], rgb[2], rgb[2] });
self.state.changed_default_bg = true;
}
/// Request a color report from the terminal. Note: not all terminals support
/// reporting colors. It is always safe to try, but you may not receive a
/// response.

View file

@ -130,8 +130,10 @@ pub const kitty_graphics_closing = ",C=1\x1b\\";
pub const osc4_query = "\x1b]4;{d};?\x1b\\"; // color index {d}
pub const osc4_reset = "\x1b]104\x1b\\"; // this resets _all_ color indexes
pub const osc10_query = "\x1b]10;?\x1b\\"; // fg
pub const osc10_set = "\x1b]10;rgb:{x:0>2}{x:0>2}/{x:0>2}{x:0>2}/{x:0>2}{x:0>2}\x1b\\"; // set default terminal fg
pub const osc10_reset = "\x1b]110\x1b\\"; // reset fg to terminal default
pub const osc11_query = "\x1b]11;?\x1b\\"; // bg
pub const osc11_set = "\x1b]11;rgb:{x:0>2}{x:0>2}/{x:0>2}{x:0>2}/{x:0>2}{x:0>2}\x1b\\"; // set default terminal bg
pub const osc11_reset = "\x1b]111\x1b\\"; // reset bg to terminal default
pub const osc12_query = "\x1b]12;?\x1b\\"; // cursor color
pub const osc12_reset = "\x1b]112\x1b\\"; // reset cursor to terminal default