Skip to content

Commit 901c4d4

Browse files
committed
feat(tui): follow terminal dark/light switches via DEC mode 2031
1 parent 4d23af0 commit 901c4d4

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎codex-rs/tui/src/render/highlight.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,25 @@ pub(crate) fn set_syntax_theme(theme: Theme) {
285285
TEST_THEME_REVISION.with(|revision| revision.set(revision.get().wrapping_add(1)));
286286
}
287287

288+
/// Re-resolve the adaptive default theme after the terminal background changed.
289+
///
290+
/// A configured theme (`tui.theme` or a `/theme` pick) is left alone; only the
291+
/// dark/light auto-selection follows the terminal. Callers must refresh the
292+
/// cached terminal palette first, since the selection reads `default_bg()`.
293+
pub(crate) fn refresh_adaptive_syntax_theme() {
294+
let name = THEME_OVERRIDE.get().and_then(|name| name.as_deref());
295+
let codex_home = CODEX_HOME
296+
.get()
297+
.and_then(|codex_home| codex_home.as_deref());
298+
if let Some(name) = name
299+
&& (parse_theme_name(name).is_some()
300+
|| codex_home.is_some_and(|home| load_custom_theme(name, home).is_some()))
301+
{
302+
return;
303+
}
304+
set_syntax_theme(resolve_theme_with_override(name, codex_home));
305+
}
306+
288307
/// Return the revision of the active syntax theme for rendered-content caches.
289308
pub(crate) fn syntax_theme_revision() -> u64 {
290309
#[cfg(not(test))]

‎codex-rs/tui/src/tui.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,56 @@ pub fn set_modes() -> Result<()> {
248248
let _ = execute!(stdout(), EnableFocusChange);
249249
#[cfg(windows)]
250250
let _ = execute!(stdout(), DisableFocusChange);
251+
// Ask the terminal to report dark/light switches (DEC private mode 2031) so the palette
252+
// and adaptive syntax theme can follow the OS appearance without a restart. Terminals
253+
// that do not implement the mode ignore the request.
254+
#[cfg(not(windows))]
255+
let _ = execute!(stdout(), EnableColorSchemeReport);
251256
Ok(())
252257
}
253258

259+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
260+
struct EnableColorSchemeReport;
261+
262+
impl Command for EnableColorSchemeReport {
263+
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
264+
write!(f, "\x1b[?2031h")
265+
}
266+
267+
#[cfg(windows)]
268+
fn execute_winapi(&self) -> Result<()> {
269+
Err(std::io::Error::other(
270+
"tried to execute EnableColorSchemeReport using WinAPI; use ANSI instead",
271+
))
272+
}
273+
274+
#[cfg(windows)]
275+
fn is_ansi_code_supported(&self) -> bool {
276+
true
277+
}
278+
}
279+
280+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
281+
struct DisableColorSchemeReport;
282+
283+
impl Command for DisableColorSchemeReport {
284+
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
285+
write!(f, "\x1b[?2031l")
286+
}
287+
288+
#[cfg(windows)]
289+
fn execute_winapi(&self) -> Result<()> {
290+
Err(std::io::Error::other(
291+
"tried to execute DisableColorSchemeReport using WinAPI; use ANSI instead",
292+
))
293+
}
294+
295+
#[cfg(windows)]
296+
fn is_ansi_code_supported(&self) -> bool {
297+
true
298+
}
299+
}
300+
254301
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
255302
struct EnableAlternateScroll;
256303

@@ -320,6 +367,8 @@ fn restore_common(
320367
first_error.get_or_insert(err);
321368
}
322369
let _ = execute!(stdout(), DisableFocusChange);
370+
#[cfg(not(windows))]
371+
let _ = execute!(stdout(), DisableColorSchemeReport);
323372
if matches!(raw_mode_restore, RawModeRestore::Disable)
324373
&& let Err(err) = disable_raw_mode()
325374
{

‎codex-rs/tui/src/tui/event_stream.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,45 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
301301
self.terminal_focused.store(false, Ordering::Relaxed);
302302
Some(TuiEvent::FocusLost)
303303
}
304+
#[cfg(unix)]
305+
Event::ColorSchemeChanged(scheme) => {
306+
// The terminal just switched dark/light (DEC mode 2031). Re-read OSC 10/11 so the
307+
// palette and the adaptive syntax theme follow it. This is the one place a
308+
// mid-session color probe is worth blocking the input loop for: it fires only on
309+
// an appearance change, and the probe is bounded by DEFAULT_TIMEOUT.
310+
self.broker.pause_events();
311+
let started_at = std::time::Instant::now();
312+
match crate::terminal_probe::default_colors(crate::terminal_probe::DEFAULT_TIMEOUT)
313+
{
314+
Ok(Some(colors)) => {
315+
tracing::info!(
316+
duration_ms = %started_at.elapsed().as_millis(),
317+
?scheme,
318+
"terminal color scheme changed; default colors refreshed"
319+
);
320+
crate::terminal_palette::set_default_colors_from_startup_probe(Some(
321+
colors,
322+
));
323+
crate::render::highlight::refresh_adaptive_syntax_theme();
324+
}
325+
Ok(None) => {
326+
tracing::warn!(
327+
duration_ms = %started_at.elapsed().as_millis(),
328+
?scheme,
329+
"terminal color scheme changed but default color probe timed out; keeping cached palette"
330+
);
331+
}
332+
Err(err) => {
333+
tracing::warn!(
334+
duration_ms = %started_at.elapsed().as_millis(),
335+
?scheme,
336+
"terminal color scheme changed but default color probe failed: {err}"
337+
);
338+
}
339+
}
340+
self.broker.resume_events();
341+
Some(TuiEvent::Draw)
342+
}
304343
_ => None,
305344
}
306345
}

0 commit comments

Comments
 (0)