Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions crates/vite_global_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ async fn main() -> ExitCode {
}
};

// Set terminal title to the project name from package.json
if let Some(project_name) = vite_shared::read_project_name(cwd.as_path().as_ref()) {
vite_shared::header::set_terminal_title(&project_name);
}

if args.len() == 1 {
match command_picker::pick_top_level_command_if_interactive(&cwd) {
Ok(command_picker::TopLevelCommandPick::Selected(selection)) => {
Expand Down
15 changes: 15 additions & 0 deletions crates/vite_shared/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ fn render_header_variant(
format!("{}{}", bold(&vite_plus, prefix_bold), bold(&suffix, suffix_bold))
}

/// Set the terminal window title using OSC 0 escape sequence.
///
/// Writes `ESC ] 0 ; <title> BEL` to stdout when stdout is a terminal.
/// This is a no-op when stdout is not a terminal or when running in CI.
pub fn set_terminal_title(title: &str) {
use std::io::Write;

if !std::io::stdout().is_terminal() || std::env::var_os("CI").is_some() {
Copy link
Member

Choose a reason for hiding this comment

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

Will there be similar issues to #986? Under what conditions would title not be supported?

return;
}

let _ = write!(std::io::stdout(), "\x1b]0;{title}\x07");
Comment on lines +535 to +539

Choose a reason for hiding this comment

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

P2 Badge Check terminal capability before writing OSC 0

This emits ESC]0;…BEL on any TTY except CI, but that is broader than the terminal-capability checks we already use elsewhere in header.rs. In environments where stdout is a terminal but OSC/VT sequences are not supported—e.g. TERM=dumb, or Windows console hosts that have not enabled ENABLE_VIRTUAL_TERMINAL_PROCESSING—users will see the raw bytes at the top of every vp invocation instead of just getting a title update. Please gate this on real escape-sequence support before writing to stdout.

Useful? React with 👍 / 👎.

let _ = std::io::stdout().flush();
}

/// Render the Vite+ CLI header string with JS-parity coloring behavior.
#[must_use]
pub fn vite_plus_header() -> String {
Expand Down
23 changes: 23 additions & 0 deletions crates/vite_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ pub use path_env::{
prepend_to_path_env,
};
pub use tracing::init_tracing;

/// Read the project name from the nearest `package.json` in the given directory.
///
/// Walks up the directory tree from `start_dir` looking for a `package.json` file
/// with a `name` field. Returns `None` if no such file is found or if it cannot
/// be parsed.
pub fn read_project_name(start_dir: &std::path::Path) -> Option<String> {
let mut dir = Some(start_dir);
while let Some(current) = dir {
let pkg_path = current.join("package.json");
if let Ok(contents) = std::fs::read_to_string(&pkg_path) {
if let Ok(pkg) = serde_json::from_str::<PackageJson>(&contents) {
if let Some(name) = pkg.name {
if !name.is_empty() {
return Some(name.to_string());
}
}
}
}
dir = current.parent();
}
None
}
3 changes: 3 additions & 0 deletions crates/vite_shared/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct Engines {
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PackageJson {
/// The package name
#[serde(default)]
pub name: Option<Str>,
/// The devEngines configuration
#[serde(default)]
pub dev_engines: Option<DevEngines>,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/binding/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ pub async fn main(
options: Option<CliOptions>,
args: Option<Vec<String>>,
) -> Result<ExitStatus, Error> {
// Set terminal title to the project name from package.json
if let Some(project_name) = vite_shared::read_project_name(cwd.as_path().as_ref()) {
vite_shared::header::set_terminal_title(&project_name);
}

let args_vec: Vec<String> = args.unwrap_or_else(|| env::args().skip(1).collect());
let args_vec = normalize_help_args(args_vec);
if should_print_help(&args_vec) {
Expand Down
Loading