Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion crates/vite_shared/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ fn is_osc_query_unsupported() -> bool {
if std::env::var_os("TMUX").is_some() || std::env::var_os("STY").is_some() {
return true;
}

false
})
}
Expand All @@ -254,6 +253,9 @@ fn is_osc_query_unsupported() -> bool {
#[cfg(unix)]
const DA1: &str = "\x1b[c";

#[cfg(unix)]
const QUERY_TERMINAL_COLORS_WALL_CLOCK_TIMEOUT_MS: u64 = 250;

/// Reads from a `BufRead` until one of two delimiter bytes is found.
/// Modelled after `terminal-colorsaurus`'s `read_until2`.
#[cfg(unix)]
Expand Down Expand Up @@ -295,6 +297,29 @@ fn read_until_either(
/// ordering/completeness ambiguities of flat-buffer pattern matching.
#[cfg(unix)]
fn query_terminal_colors(palette_indices: &[u8]) -> (Option<Rgb>, Vec<(u8, Rgb)>) {
let indices = palette_indices.to_vec();
let (tx, rx) = std::sync::mpsc::channel();

let Ok(_handle) =
std::thread::Builder::new().name("vp-tty-color-query".into()).spawn(move || {
let _ = tx.send(query_terminal_colors_inner(&indices));
})
else {
return (None, vec![]);
};

rx.recv_timeout(Duration::from_millis(QUERY_TERMINAL_COLORS_WALL_CLOCK_TIMEOUT_MS))
.unwrap_or((None, vec![]))
Comment on lines +311 to +312
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align wall-clock timeout with SSH tty query deadline

query_terminal_colors now returns after a fixed 250ms timeout, but the spawned query_terminal_colors_inner path can keep /dev/tty in raw mode for up to 1000ms when SSH_CONNECTION/SSH_TTY is set. If the outer timeout fires first, the caller continues while the detached thread still owns terminal mode changes, so delegated/interactive commands can start with a raw tty and exhibit input/editing glitches or apparent hangs. This regression is introduced by the new async wrapper and should be fixed by making the outer timeout cover the inner worst-case window (or by preventing raw-mode mutation in a detached worker).

Useful? React with 👍 / 👎.

}

/// Queries terminal colors using the DA1 sandwich technique with
/// stream-based response parsing (modelled after `terminal-colorsaurus`).
///
/// Responses are read sequentially using `BufReader` + `read_until`,
/// which provides exact response boundaries and eliminates the
/// ordering/completeness ambiguities of flat-buffer pattern matching.
#[cfg(unix)]
fn query_terminal_colors_inner(palette_indices: &[u8]) -> (Option<Rgb>, Vec<(u8, Rgb)>) {
use std::{
fs::OpenOptions,
io::{self, BufRead, BufReader},
Expand Down Expand Up @@ -536,6 +561,21 @@ pub fn vite_plus_header() -> String {
render_header_variant(header_colors.blue, &header_colors.suffix_gradient, true, true)
}

#[must_use]
pub fn vite_plus_header_static() -> String {
if !should_colorize() || !supports_true_color() {
return format!("VITE+{HEADER_SUFFIX}");
}

let suffix_gradient = gradient_eased(
HEADER_SUFFIX.chars().count(),
DEFAULT_BLUE,
DEFAULT_MAGENTA,
HEADER_SUFFIX_FADE_GAMMA,
);
render_header_variant(DEFAULT_BLUE, &suffix_gradient, true, true)
}

#[cfg(all(test, unix))]
mod tests {
use std::io::{BufReader, Cursor};
Expand Down
Loading