Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(render): draw faint text and readable block cursors

Mirro Chou611009944c69
diff --git a/thinkterm-web/src/emit.rs b/thinkterm-web/src/emit.rs
index aecf386d..0f0f4458 100644
--- a/thinkterm-web/src/emit.rs
+++ b/thinkterm-web/src/emit.rs
@@ -244,13 +244,23 @@ pub fn emit_line(
for cluster in &clusters {
let attrs = &cluster.attrs;
let bg_is_default = attrs.background() == ColorAttribute::Default;
- let bg = p.palette.resolve_bg(attrs.background()).to_linear();
- let fg = resolve_fg(attrs, attrs.foreground(), p.palette).to_linear();
+ let bg = p.palette.resolve_bg(attrs.background());
+ let fg = resolve_fg(attrs, attrs.foreground(), p.palette);
let (fg, bg, bg_is_default) = if attrs.reverse() == !p.reverse_video {
(bg, fg, false)
} else {
(fg, bg, bg_is_default)
};
+ // SGR 2 carries no alpha of its own: mix half way to the cell's own
+ // background, as the desktop renderer does, so the same sequence
+ // reads as a dim grey on either side of a light/dark scheme.
+ let fg = if attrs.intensity() == Intensity::Half {
+ fg.interpolate(bg, 0.5)
+ } else {
+ fg
+ };
+ let bg = bg.to_linear();
+ let fg = fg.to_linear();
// The floor lifts dim text off its ground, as the desktop's
// minimum contrast does; the ground of a default cell is the
// palette's.
diff --git a/wezterm-gui/src/termwindow/render/mod.rs b/wezterm-gui/src/termwindow/render/mod.rs
index d54403a0..001fe3f1 100644
--- a/wezterm-gui/src/termwindow/render/mod.rs
+++ b/wezterm-gui/src/termwindow/render/mod.rs
@@ -853,6 +853,40 @@ impl crate::TermWindow {
adjusted
}
+ /// The colour of the text under a block cursor.
+ ///
+ /// A block cursor is expected to be readable on its own background. A
+ /// colour scheme sets `cursor_fg` for the scheme's own cursor colour, but
+ /// an application can change that colour with OSC 12 -- OpenCode asks for
+ /// `#eeeeee` -- and then a white `cursor_fg` disappears into it. Keep the
+ /// configured colour when it reads; otherwise fall back to the cell's own
+ /// background, the inversion a block cursor normally is, and to black or
+ /// white when even that does not clear the bar.
+ fn block_cursor_text_color(
+ configured: LinearRgba,
+ cell_background: LinearRgba,
+ cursor_background: LinearRgba,
+ ) -> LinearRgba {
+ // WCAG AA for body text. A cursor covers one cell, but that cell is
+ // exactly where the user is looking.
+ const MIN_CONTRAST: f32 = 4.5;
+
+ if configured.contrast_ratio(&cursor_background) >= MIN_CONTRAST {
+ return configured;
+ }
+ if cell_background.contrast_ratio(&cursor_background) >= MIN_CONTRAST {
+ return cell_background;
+ }
+
+ let black = LinearRgba::with_components(0., 0., 0., 1.);
+ let white = LinearRgba::with_components(1., 1., 1., 1.);
+ if black.contrast_ratio(&cursor_background) >= white.contrast_ratio(&cursor_background) {
+ black
+ } else {
+ white
+ }
+ }
+
pub fn compute_cell_fg_bg(&self, params: ComputeCellFgBgParams) -> ComputeCellFgBgResult {
let focused_and_active =
self.focused.is_some() && params.is_active_pane && !self.right_sidebar_has_text_focus();
@@ -901,7 +935,14 @@ impl crate::TermWindow {
let (fg_color, bg_color) = if self.use_reverse_video_cursor(&params) {
(params.bg_color, params.fg_color)
} else {
- (params.cursor_fg, params.cursor_bg)
+ (
+ Self::block_cursor_text_color(
+ params.cursor_fg.when_fully_transparent(params.fg_color),
+ params.bg_color,
+ params.cursor_bg,
+ ),
+ params.cursor_bg,
+ )
};
let fg_color = self.ensure_min_contrast(fg_color, bg_color);
@@ -962,7 +1003,11 @@ impl crate::TermWindow {
(params.bg_color, params.fg_color, params.fg_color)
} else {
(
- params.cursor_fg.when_fully_transparent(params.fg_color),
+ Self::block_cursor_text_color(
+ params.cursor_fg.when_fully_transparent(params.fg_color),
+ params.bg_color,
+ params.cursor_bg,
+ ),
params.cursor_bg,
params.cursor_bg,
)
@@ -1412,6 +1457,17 @@ fn resolve_fg_color_attr(
/// -- and the two swapped where the cell's reverse attribute and the
/// screen's reverse video disagree. The flag says the background is the
/// pane's own ground.
+/// The colour SGR 2 (faint) is drawn in: half way from the foreground to
+/// the cell's own background, which is what a 50% overlay looks like on an
+/// opaque cell. Faint carries no alpha of its own, so mixing is the only way
+/// to honour it without changing the glyph pipeline.
+pub(crate) fn faint_color(
+ fg: wezterm_term::color::SrgbaTuple,
+ bg: wezterm_term::color::SrgbaTuple,
+) -> wezterm_term::color::SrgbaTuple {
+ fg.interpolate(bg, 0.5)
+}
+
pub(crate) fn cell_fg_bg(
attrs: &CellAttributes,
palette: &ColorPalette,
@@ -1427,11 +1483,19 @@ pub(crate) fn cell_fg_bg(
let bg = palette.resolve_bg(attrs.background());
let fg = resolve_fg_color_attr_srgb(attrs, attrs.foreground(), palette, config, style);
// Check the line reverse_video flag and flip.
- if attrs.reverse() == !reverse_video {
+ let (fg, bg, bg_is_default) = if attrs.reverse() == !reverse_video {
(bg, fg, false)
} else {
(fg, bg, bg_is_default)
- }
+ };
+ // Faint is resolved after the flip, against the background the glyph is
+ // actually drawn on.
+ let fg = if attrs.intensity() == wezterm_term::Intensity::Half {
+ faint_color(fg, bg)
+ } else {
+ fg
+ };
+ (fg, bg, bg_is_default)
}
fn resolve_fg_color_attr_srgb(
@@ -1592,3 +1656,72 @@ mod line_quad_cache_tests {
);
}
}
+
+#[cfg(test)]
+mod cursor_colour_tests {
+ use super::*;
+ use crate::TermWindow;
+ use wezterm_term::color::SrgbaTuple;
+
+ fn rgb(r: u8, g: u8, b: u8) -> LinearRgba {
+ LinearRgba::with_srgba(r, g, b, 0xff)
+ }
+
+ /// OpenCode sets the cursor to `#eeeeee` with OSC 12. The scheme's white
+ /// `cursor_fg` has nowhere near enough contrast, so the cursor has to
+ /// fall back to the cell's own background -- the inversion a block
+ /// cursor is normally understood to be.
+ #[test]
+ fn a_white_cursor_fg_on_a_near_white_cursor_inverts_the_cell() {
+ let dark = rgb(0x1e, 0x1e, 0x1e);
+ let near_white = rgb(0xee, 0xee, 0xee);
+ let white = rgb(0xff, 0xff, 0xff);
+
+ let picked = TermWindow::block_cursor_text_color(white, dark, near_white);
+ assert_eq!(picked, dark, "the cell background is the readable fallback");
+ }
+
+ /// A cursor that already reads keeps the colour the scheme asked for.
+ #[test]
+ fn a_readable_cursor_fg_is_left_alone() {
+ let white = rgb(0xff, 0xff, 0xff);
+ let near_black = rgb(0x10, 0x10, 0x10);
+
+ let picked = TermWindow::block_cursor_text_color(white, rgb(0x80, 0x80, 0x80), near_black);
+ assert_eq!(picked, white);
+ }
+
+ /// When neither the scheme's foreground nor the cell's background has
+ /// enough contrast, black or white takes over rather than leaving the
+ /// glyph invisible.
+ #[test]
+ fn a_mid_grey_cursor_gets_black_or_white() {
+ let mid = rgb(0x80, 0x80, 0x80);
+ let picked = TermWindow::block_cursor_text_color(
+ rgb(0x90, 0x90, 0x90),
+ rgb(0x70, 0x70, 0x70),
+ mid,
+ );
+ assert!(
+ picked.contrast_ratio(&mid) >= 4.5,
+ "the fallback has to read, got {:?}",
+ picked
+ );
+ }
+
+ /// SGR 2 has no alpha of its own: it is mixed half way to the cell's
+ /// background, so the same sequence reads as a dim grey on either side
+ /// of a light/dark scheme.
+ #[test]
+ fn faint_text_is_mixed_half_way_to_its_background() {
+ let white = SrgbaTuple(1., 1., 1., 1.);
+ let dark = SrgbaTuple(0.1, 0.1, 0.1, 1.);
+ let faint = faint_color(white, dark);
+
+ assert!(faint.3 == 1.0, "dimming must not make the glyph transparent");
+ assert!(
+ faint.to_linear().relative_luminance() < white.to_linear().relative_luminance(),
+ "faint text has to be darker than normal text"
+ );
+ }
+}
diff --git a/wezterm-gui/src/termwindow/render/screen_line.rs b/wezterm-gui/src/termwindow/render/screen_line.rs
index 8e07ffd6..0804428b 100644
--- a/wezterm-gui/src/termwindow/render/screen_line.rs
+++ b/wezterm-gui/src/termwindow/render/screen_line.rs
@@ -1,8 +1,8 @@
use crate::quad::{QuadTrait, TripleLayerQuadAllocator, TripleLayerQuadAllocatorTrait};
use crate::termwindow::render::{
- cell_fg_bg, resolve_fg_color_attr, same_hyperlink, update_next_frame_time, ClusterStyleCache,
- ComputeCellFgBgParams, ComputeCellFgBgResult, LineToElementParams, LineToElementShape,
- RenderScreenLineParams, RenderScreenLineResult,
+ cell_fg_bg, faint_color, resolve_fg_color_attr, same_hyperlink, update_next_frame_time,
+ ClusterStyleCache, ComputeCellFgBgParams, ComputeCellFgBgResult, LineToElementParams,
+ LineToElementShape, RenderScreenLineParams, RenderScreenLineResult,
};
use crate::termwindow::LineToElementShapeItem;
use ::window::DeadKeyStatus;
@@ -16,7 +16,7 @@ use termwiz::color::LinearRgba;
use termwiz::surface::CursorShape;
use wezterm_bidi::Direction;
use wezterm_term::color::ColorAttribute;
-use wezterm_term::CellAttributes;
+use wezterm_term::{CellAttributes, Intensity};
impl crate::TermWindow {
/// "Render" a line of the terminal screen into the vertex buffer.
@@ -341,7 +341,7 @@ impl crate::TermWindow {
.min(grid_cols)
.saturating_sub(cursor_range.start);
if cursor_cells > 0 {
- let (fg_color, bg_color) = if let Some(c) = &cursor_cell {
+ let (fg_color, bg_color, faint) = if let Some(c) = &cursor_cell {
let attrs = c.attrs();
let bg_color = params.palette.resolve_bg(attrs.background()).to_linear();
@@ -354,9 +354,16 @@ impl crate::TermWindow {
&Default::default(),
);
- (fg_color, bg_color)
+ (fg_color, bg_color, attrs.intensity() == Intensity::Half)
} else {
- (params.foreground, params.default_bg)
+ (params.foreground, params.default_bg, false)
+ };
+ // A bar or underline cursor still shows the cell's glyph, so
+ // faint text has to stay faint under it too.
+ let fg_color = if faint {
+ faint_color(fg_color.to_srgb(), bg_color.to_srgb()).to_linear()
+ } else {
+ fg_color
};
let ComputeCellFgBgResult {