From d0dc69a0a83da04339ba6c1554cd352cd4f1ca10 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Aug 2022 14:37:10 -0700 Subject: [PATCH] color selection! --- src/Grid.zig | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/src/Grid.zig b/src/Grid.zig index 6626f5dab..d6981de40 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -288,8 +288,7 @@ pub fn updateCells(self: *Grid, term: Terminal) !void { defer y += 1; for (line) |cell, x| { - // The colors for the cell. - const colors: struct { + const BgFg = struct { /// Background is optional because in un-inverted mode /// it may just be equivalent to the default background in /// which case we do nothing to save on GPU render time. @@ -299,17 +298,42 @@ pub fn updateCells(self: *Grid, term: Terminal) !void { /// any fg if the cell is empty or has no attributes like /// underline. fg: terminal.color.RGB, - } = if (cell.attrs.inverse == 0) .{ - // In normal mode, background and fg match the cell. We - // un-optionalize the fg by defaulting to our fg color. - .bg = cell.bg, - .fg = cell.fg orelse self.foreground, - } else .{ - // In inverted mode, the background MUST be set to something - // (is never null) so it is either the fg or default fg. The - // fg is either the bg or default background. - .bg = cell.fg orelse self.foreground, - .fg = cell.bg orelse self.background, + }; + + // The colors for the cell. + const colors: BgFg = colors: { + // If we have a selection, then we need to check if this + // cell is selected. + // TODO(perf): we can check in advance if selection is in + // our viewport at all and not run this on every point. + if (term.selection) |sel| { + const screen_point = (terminal.point.Viewport{ + .x = x, + .y = y, + }).toScreen(&term.screen); + + // If we are selected, we our colors are just inverted fg/bg + if (sel.contains(screen_point)) { + break :colors BgFg{ + .bg = self.foreground, + .fg = self.background, + }; + } + } + + const res: BgFg = if (cell.attrs.inverse == 0) .{ + // In normal mode, background and fg match the cell. We + // un-optionalize the fg by defaulting to our fg color. + .bg = cell.bg, + .fg = cell.fg orelse self.foreground, + } else .{ + // In inverted mode, the background MUST be set to something + // (is never null) so it is either the fg or default fg. The + // fg is either the bg or default background. + .bg = cell.fg orelse self.foreground, + .fg = cell.bg orelse self.background, + }; + break :colors res; }; // If the cell has a background, we always draw it.