config: add cursor-opacity

This commit is contained in:
Mitchell Hashimoto
2023-10-14 14:08:06 -07:00
parent 7167cf9054
commit ff38d3e358
3 changed files with 23 additions and 2 deletions

View File

@ -174,6 +174,13 @@ palette: Palette = .{},
/// The color of the cursor. If this is not set, a default will be chosen. /// The color of the cursor. If this is not set, a default will be chosen.
@"cursor-color": ?Color = null, @"cursor-color": ?Color = null,
/// The opacity level (opposite of transparency) of the cursor.
/// A value of 1 is fully opaque and a value of 0 is fully transparent.
/// A value less than 0 or greater than 1 will be clamped to the nearest
/// valid value. Note that a sufficiently small value such as 0.3 may be
/// effectively invisible and may make it difficult to find the cursor.
@"cursor-opacity": f64 = 1.0,
/// The style of the cursor. This sets the default style. A running /// The style of the cursor. This sets the default style. A running
/// programn can still request an explicit cursor style using escape /// programn can still request an explicit cursor style using escape
/// sequences (such as CSI q). Shell configurations will often request /// sequences (such as CSI q). Shell configurations will often request

View File

@ -104,6 +104,7 @@ pub const DerivedConfig = struct {
font_features: std.ArrayList([]const u8), font_features: std.ArrayList([]const u8),
font_styles: font.Group.StyleStatus, font_styles: font.Group.StyleStatus,
cursor_color: ?terminal.color.RGB, cursor_color: ?terminal.color.RGB,
cursor_opacity: f64,
cursor_text: ?terminal.color.RGB, cursor_text: ?terminal.color.RGB,
background: terminal.color.RGB, background: terminal.color.RGB,
background_opacity: f64, background_opacity: f64,
@ -144,6 +145,8 @@ pub const DerivedConfig = struct {
else else
null, null,
.cursor_opacity = @max(0, @min(1, config.@"cursor-opacity")),
.background = config.background.toTerminalRGB(), .background = config.background.toTerminalRGB(),
.foreground = config.foreground.toTerminalRGB(), .foreground = config.foreground.toTerminalRGB(),
@ -1419,6 +1422,10 @@ fn addCursor(
}; };
const color = self.config.cursor_color orelse self.config.foreground; const color = self.config.cursor_color orelse self.config.foreground;
const alpha: u8 = alpha: {
const alpha = 255 * self.config.cursor_opacity;
break :alpha @intFromFloat(@ceil(alpha));
};
const sprite: font.Sprite = switch (cursor_style) { const sprite: font.Sprite = switch (cursor_style) {
.block => .cursor_rect, .block => .cursor_rect,
@ -1444,7 +1451,7 @@ fn addCursor(
@as(f32, @floatFromInt(screen.cursor.y)), @as(f32, @floatFromInt(screen.cursor.y)),
}, },
.cell_width = if (cell.attrs.wide) 2 else 1, .cell_width = if (cell.attrs.wide) 2 else 1,
.color = .{ color.r, color.g, color.b, 0xFF }, .color = .{ color.r, color.g, color.b, alpha },
.glyph_pos = .{ glyph.atlas_x, glyph.atlas_y }, .glyph_pos = .{ glyph.atlas_x, glyph.atlas_y },
.glyph_size = .{ glyph.width, glyph.height }, .glyph_size = .{ glyph.width, glyph.height },
.glyph_offset = .{ glyph.offset_x, glyph.offset_y }, .glyph_offset = .{ glyph.offset_x, glyph.offset_y },

View File

@ -219,6 +219,7 @@ pub const DerivedConfig = struct {
font_styles: font.Group.StyleStatus, font_styles: font.Group.StyleStatus,
cursor_color: ?terminal.color.RGB, cursor_color: ?terminal.color.RGB,
cursor_text: ?terminal.color.RGB, cursor_text: ?terminal.color.RGB,
cursor_opacity: f64,
background: terminal.color.RGB, background: terminal.color.RGB,
background_opacity: f64, background_opacity: f64,
foreground: terminal.color.RGB, foreground: terminal.color.RGB,
@ -258,6 +259,8 @@ pub const DerivedConfig = struct {
else else
null, null,
.cursor_opacity = @max(0, @min(1, config.@"cursor-opacity")),
.background = config.background.toTerminalRGB(), .background = config.background.toTerminalRGB(),
.foreground = config.foreground.toTerminalRGB(), .foreground = config.foreground.toTerminalRGB(),
@ -870,6 +873,10 @@ fn addCursor(
}; };
const color = self.config.cursor_color orelse self.config.foreground; const color = self.config.cursor_color orelse self.config.foreground;
const alpha: u8 = alpha: {
const alpha = 255 * self.config.cursor_opacity;
break :alpha @intFromFloat(@ceil(alpha));
};
const sprite: font.Sprite = switch (cursor_style) { const sprite: font.Sprite = switch (cursor_style) {
.block => .cursor_rect, .block => .cursor_rect,
@ -896,7 +903,7 @@ fn addCursor(
.fg_r = color.r, .fg_r = color.r,
.fg_g = color.g, .fg_g = color.g,
.fg_b = color.b, .fg_b = color.b,
.fg_a = 255, .fg_a = alpha,
.bg_r = 0, .bg_r = 0,
.bg_g = 0, .bg_g = 0,
.bg_b = 0, .bg_b = 0,