Merge pull request #901 from Raiden1411/invert-fg-bg

renderer: add config option to invert fg and bg of cell when selected
This commit is contained in:
Mitchell Hashimoto
2023-11-17 21:49:35 -08:00
committed by GitHub
3 changed files with 42 additions and 14 deletions

View File

@ -165,6 +165,14 @@ foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF },
@"selection-foreground": ?Color = null,
@"selection-background": ?Color = null,
/// Swap the foreground and background colors of cells for selection.
/// This option overrides the "selection-foreground" and "selection-background"
/// options.
///
/// If you select across cells with differing foregrounds and backgrounds,
/// the selection color will vary across the selection.
@"selection-invert-fg-bg": bool = false,
/// 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

View File

@ -123,6 +123,7 @@ pub const DerivedConfig = struct {
foreground: terminal.color.RGB,
selection_background: ?terminal.color.RGB,
selection_foreground: ?terminal.color.RGB,
invert_selection_fg_bg: bool,
pub fn init(
alloc_gpa: Allocator,
@ -161,6 +162,7 @@ pub const DerivedConfig = struct {
.background = config.background.toTerminalRGB(),
.foreground = config.foreground.toTerminalRGB(),
.invert_selection_fg_bg = config.@"selection-invert-fg-bg",
.selection_background = if (config.@"selection-background") |bg|
bg.toTerminalRGB()
@ -1304,13 +1306,8 @@ pub fn updateCell(
// The colors for the cell.
const colors: BgFg = colors: {
// If we are selected, we our colors are just inverted fg/bg
const selection_res: ?BgFg = if (selected) .{
.bg = self.config.selection_background orelse self.foreground_color,
.fg = self.config.selection_foreground orelse self.background_color,
} else null;
const res: BgFg = selection_res orelse if (!cell.attrs.inverse) .{
// The normal cell result
const cell_res: BgFg = if (!cell.attrs.inverse) .{
// In normal mode, background and fg match the cell. We
// un-optionalize the fg by defaulting to our fg color.
.bg = if (cell.attrs.has_bg) cell.bg else null,
@ -1323,8 +1320,21 @@ pub fn updateCell(
.fg = if (cell.attrs.has_bg) cell.bg else self.background_color,
};
// If we are selected, we our colors are just inverted fg/bg
const selection_res: ?BgFg = if (selected) .{
.bg = if (self.config.invert_selection_fg_bg)
cell_res.fg
else
self.config.selection_background orelse self.foreground_color,
.fg = if (self.config.invert_selection_fg_bg)
cell_res.bg orelse self.background_color
else
self.config.selection_foreground orelse self.background_color,
} else null;
// If the cell is "invisible" then we just make fg = bg so that
// the cell is transparent but still copy-able.
const res: BgFg = selection_res orelse cell_res;
if (cell.attrs.invisible) {
break :colors BgFg{
.bg = res.bg,

View File

@ -237,6 +237,7 @@ pub const DerivedConfig = struct {
foreground: terminal.color.RGB,
selection_background: ?terminal.color.RGB,
selection_foreground: ?terminal.color.RGB,
invert_selection_fg_bg: bool,
pub fn init(
alloc_gpa: Allocator,
@ -275,6 +276,7 @@ pub const DerivedConfig = struct {
.background = config.background.toTerminalRGB(),
.foreground = config.foreground.toTerminalRGB(),
.invert_selection_fg_bg = config.@"selection-invert-fg-bg",
.selection_background = if (config.@"selection-background") |bg|
bg.toTerminalRGB()
@ -1086,13 +1088,8 @@ pub fn updateCell(
// The colors for the cell.
const colors: BgFg = colors: {
// If we are selected, we our colors are just inverted fg/bg
const selection_res: ?BgFg = if (selected) .{
.bg = self.config.selection_background orelse self.foreground_color,
.fg = self.config.selection_foreground orelse self.background_color,
} else null;
const res: BgFg = selection_res orelse if (!cell.attrs.inverse) .{
// The normal cell result
const cell_res: BgFg = if (!cell.attrs.inverse) .{
// In normal mode, background and fg match the cell. We
// un-optionalize the fg by defaulting to our fg color.
.bg = if (cell.attrs.has_bg) cell.bg else null,
@ -1105,8 +1102,21 @@ pub fn updateCell(
.fg = if (cell.attrs.has_bg) cell.bg else self.background_color,
};
// If we are selected, we our colors are just inverted fg/bg
const selection_res: ?BgFg = if (selected) .{
.bg = if (self.config.invert_selection_fg_bg)
cell_res.fg
else
self.config.selection_background orelse self.foreground_color,
.fg = if (self.config.invert_selection_fg_bg)
cell_res.bg orelse self.background_color
else
self.config.selection_foreground orelse self.background_color,
} else null;
// If the cell is "invisible" then we just make fg = bg so that
// the cell is transparent but still copy-able.
const res: BgFg = selection_res orelse cell_res;
if (cell.attrs.invisible) {
break :colors BgFg{
.bg = res.bg,