renderer: address issue with inverted cells

This commit is contained in:
Mitchell Hashimoto
2023-11-17 21:47:24 -08:00
parent fd444b6d05
commit 45dfbfe53c
3 changed files with 39 additions and 22 deletions

View File

@ -165,12 +165,13 @@ foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF },
@"selection-foreground": ?Color = null, @"selection-foreground": ?Color = null,
@"selection-background": ?Color = null, @"selection-background": ?Color = null,
/// Inverts the cell color from foreground to background, /// Swap the foreground and background colors of cells for selection.
/// and from background to foreground. /// This option overrides the "selection-foreground" and "selection-background"
/// This option does not take in consideration the color /// options.
/// values defined for "selection-foreground" and "selection-background" ///
/// as it will use the foreground and background of the cell. /// If you select across cells with differing foregrounds and backgrounds,
@"invert-selection-fg-bg": bool = false, /// 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 /// Color palette for the 256 color form that many terminal applications
/// use. The syntax of this configuration is "N=HEXCODE" where "n" /// use. The syntax of this configuration is "N=HEXCODE" where "n"

View File

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

View File

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