From 61688e27cab9ee474329ee9c7144d4c7b646e78c Mon Sep 17 00:00:00 2001 From: Tushar Date: Tue, 14 Apr 2026 09:29:15 +0530 Subject: [PATCH 1/2] perf(display): cache terminal theme detection with 100ms timeout --- crates/forge_display/src/code.rs | 25 ++++++++++++++++----- crates/forge_markdown_stream/src/utils.rs | 27 ++++++++++++++++++----- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/crates/forge_display/src/code.rs b/crates/forge_display/src/code.rs index 66f1dd1743..538f72ad7c 100644 --- a/crates/forge_display/src/code.rs +++ b/crates/forge_display/src/code.rs @@ -1,4 +1,6 @@ use std::sync::Arc; +use std::sync::OnceLock; +use std::time::Duration; use syntect::easy::HighlightLines; use syntect::highlighting::ThemeSet; @@ -7,6 +9,12 @@ use syntect::util::as_24_bit_terminal_escaped; use terminal_colorsaurus::{QueryOptions, ThemeMode, theme_mode}; use two_face::theme::EmbeddedThemeName; +/// Maximum time to wait for a terminal color query response. +const THEME_DETECT_TIMEOUT: Duration = Duration::from_millis(100); + +/// Process-wide cache for whether the terminal uses a dark background. +static IS_DARK_THEME: OnceLock = OnceLock::new(); + /// Loads and caches syntax highlighting resources. #[derive(Clone)] pub struct SyntaxHighlighter { @@ -25,12 +33,19 @@ impl Default for SyntaxHighlighter { } impl SyntaxHighlighter { - /// Detects whether the terminal is using a dark or light background. + /// Detects whether the terminal is using a dark or light background, + /// querying the terminal at most once per process lifetime. Subsequent + /// calls return the cached result. Falls back to dark mode on timeout or + /// if the terminal does not support color queries. fn is_dark_theme() -> bool { - match theme_mode(QueryOptions::default()) { - Ok(ThemeMode::Light) => false, - Ok(ThemeMode::Dark) | Err(_) => true, - } + *IS_DARK_THEME.get_or_init(|| { + let mut opts = QueryOptions::default(); + opts.timeout = THEME_DETECT_TIMEOUT; + match theme_mode(opts) { + Ok(ThemeMode::Light) => false, + Ok(ThemeMode::Dark) | Err(_) => true, + } + }) } /// Syntax-highlights `code` for the given language token (e.g. `"toml"`, diff --git a/crates/forge_markdown_stream/src/utils.rs b/crates/forge_markdown_stream/src/utils.rs index 85412158c2..dd33c46d02 100644 --- a/crates/forge_markdown_stream/src/utils.rs +++ b/crates/forge_markdown_stream/src/utils.rs @@ -1,5 +1,8 @@ //! Utility functions for the markdown renderer. +use std::sync::OnceLock; +use std::time::Duration; + /// Terminal theme mode (dark or light). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ThemeMode { @@ -9,12 +12,24 @@ pub enum ThemeMode { Light, } -/// Detects the terminal theme mode (dark or light). +/// Maximum time to wait for a terminal color query response. +const THEME_DETECT_TIMEOUT: Duration = Duration::from_millis(100); + +/// Process-wide cache for the detected terminal theme mode. +static THEME_MODE: OnceLock = OnceLock::new(); + +/// Detects the terminal theme mode (dark or light), querying the terminal at +/// most once per process lifetime. Subsequent calls return the cached result. +/// Falls back to dark mode if the terminal does not respond within the timeout. pub fn detect_theme_mode() -> ThemeMode { - use terminal_colorsaurus::{QueryOptions, ThemeMode as ColorsaurusThemeMode, theme_mode}; + *THEME_MODE.get_or_init(|| { + use terminal_colorsaurus::{QueryOptions, ThemeMode as ColorsaurusThemeMode, theme_mode}; - match theme_mode(QueryOptions::default()) { - Ok(ColorsaurusThemeMode::Light) => ThemeMode::Light, - Ok(ColorsaurusThemeMode::Dark) | Err(_) => ThemeMode::Dark, - } + let mut opts = QueryOptions::default(); + opts.timeout = THEME_DETECT_TIMEOUT; + match theme_mode(opts) { + Ok(ColorsaurusThemeMode::Light) => ThemeMode::Light, + Ok(ColorsaurusThemeMode::Dark) | Err(_) => ThemeMode::Dark, + } + }) } From 0905768e1a6647761b687262e2a0044eeff8c700 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 14 Apr 2026 04:03:03 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- crates/forge_display/src/code.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/forge_display/src/code.rs b/crates/forge_display/src/code.rs index 538f72ad7c..e4a90c9b83 100644 --- a/crates/forge_display/src/code.rs +++ b/crates/forge_display/src/code.rs @@ -1,5 +1,4 @@ -use std::sync::Arc; -use std::sync::OnceLock; +use std::sync::{Arc, OnceLock}; use std::time::Duration; use syntect::easy::HighlightLines;