Skip to content

Commit a7fee4e

Browse files
authored
refactor(cli): deduplicate accent helpers into crate::help (#2363)
## Description The global CLI carried five copies of the same terminal-styling helpers: - `accent` was duplicated in `commands/version.rs` and `commands/env/current.rs`. - `accent_command` was duplicated in `commands/env/on.rs`, `commands/env/off.rs`, and `commands/env/setup.rs`. All copies were byte-identical apart from the backtick wrapping in `accent_command`, and every one of them already depended on `help::should_style_help()`. This PR moves a single `accent` into `crate::help` next to the other styling helpers, reimplements `accent_command` as a one-line wrapper around it, and deletes the five local copies. The now-unused `owo_colors::OwoColorize` imports in the five call-site files are removed as well. Output is byte-for-byte identical, so no PTY snapshots change. ## Changes - Add `help::accent` and `help::accent_command` to `crates/vp_global_cli/src/help.rs`. - Remove local duplicates and switch call sites to the shared helpers in `commands/version.rs`, `commands/env/current.rs`, `commands/env/on.rs`, `commands/env/off.rs`, and `commands/env/setup.rs`.
1 parent 3905929 commit a7fee4e

6 files changed

Lines changed: 14 additions & 46 deletions

File tree

crates/vp_global_cli/src/commands/env/current.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
use std::process::ExitStatus;
66

7-
use owo_colors::OwoColorize;
87
use serde::Serialize;
98
use vp_pm_cli::{
109
PackageManagerResolution, package_manager_bin_path, package_manager_install_dir,
@@ -62,16 +61,12 @@ impl PackageManagerInfo {
6261
}
6362
}
6463

65-
fn accent(text: &str) -> String {
66-
if help::should_style_help() { text.bright_blue().to_string() } else { text.to_string() }
67-
}
68-
6964
fn print_rows(title: &str, rows: &[(&str, String)]) {
7065
println!("{}", help::render_heading(title));
7166
let label_width = rows.iter().map(|(label, _)| label.chars().count()).max().unwrap_or(0);
7267
for (label, value) in rows {
7368
let padding = " ".repeat(label_width.saturating_sub(label.chars().count()));
74-
println!(" {}{} {value}", accent(label), padding);
69+
println!(" {}{} {value}", help::accent(label), padding);
7570
}
7671
}
7772

crates/vp_global_cli/src/commands/env/off.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,9 @@
55
66
use std::process::ExitStatus;
77

8-
use owo_colors::OwoColorize;
9-
108
use super::config::{ShimMode, load_config, save_config};
119
use crate::{error::Error, help};
1210

13-
fn accent_command(command: &str) -> String {
14-
if help::should_style_help() {
15-
format!("`{}`", command.bright_blue())
16-
} else {
17-
format!("`{command}`")
18-
}
19-
}
20-
2111
/// Execute the `vp env off` command.
2212
pub async fn execute() -> Result<ExitStatus, Error> {
2313
let mut config = load_config().await?;
@@ -39,7 +29,7 @@ pub async fn execute() -> Result<ExitStatus, Error> {
3929
"All vp commands and shims will now prefer system Node.js, falling back to managed if not found."
4030
);
4131
println!();
42-
println!("Run {} to always use Vite+ managed Node.js.", accent_command("vp env on"));
32+
println!("Run {} to always use Vite+ managed Node.js.", help::accent_command("vp env on"));
4333

4434
Ok(ExitStatus::default())
4535
}

crates/vp_global_cli/src/commands/env/on.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@
44
55
use std::process::ExitStatus;
66

7-
use owo_colors::OwoColorize;
8-
97
use super::config::{ShimMode, load_config, save_config};
108
use crate::{error::Error, help};
119

12-
fn accent_command(command: &str) -> String {
13-
if help::should_style_help() {
14-
format!("`{}`", command.bright_blue())
15-
} else {
16-
format!("`{command}`")
17-
}
18-
}
19-
2010
/// Execute the `vp env on` command.
2111
pub async fn execute() -> Result<ExitStatus, Error> {
2212
let mut config = load_config().await?;
@@ -34,7 +24,7 @@ pub async fn execute() -> Result<ExitStatus, Error> {
3424
println!();
3525
println!("All vp commands and shims will now always use Vite+ managed Node.js.");
3626
println!();
37-
println!("Run {} to prefer system Node.js instead.", accent_command("vp env off"));
27+
println!("Run {} to prefer system Node.js instead.", help::accent_command("vp env off"));
3828

3929
Ok(ExitStatus::default())
4030
}

crates/vp_global_cli/src/commands/env/setup.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
1818
use std::process::ExitStatus;
1919

20-
use owo_colors::OwoColorize;
21-
2220
use super::config::{get_bin_dir, get_vp_home};
2321
use crate::{error::Error, help};
2422

@@ -46,14 +44,6 @@ impl EnvShell {
4644
/// Tools to create shims for (node, npm, npx, corepack, vpx, vpr)
4745
pub(crate) const SHIM_TOOLS: &[&str] = &["node", "npm", "npx", "corepack", "vpx", "vpr"];
4846

49-
fn accent_command(command: &str) -> String {
50-
if help::should_style_help() {
51-
format!("`{}`", command.bright_blue())
52-
} else {
53-
format!("`{command}`")
54-
}
55-
}
56-
5747
/// Execute the setup command.
5848
pub async fn execute(refresh: bool, env_only: bool) -> Result<ExitStatus, Error> {
5949
let vite_plus_home = get_vp_home()?;
@@ -67,7 +57,7 @@ pub async fn execute(refresh: bool, env_only: bool) -> Result<ExitStatus, Error>
6757
if env_only {
6858
println!("{}", help::render_heading("Setup"));
6959
println!(" Updated shell environment files.");
70-
println!(" Run {} to verify setup.", accent_command("vp env doctor"));
60+
println!(" Run {} to verify setup.", help::accent_command("vp env doctor"));
7161
return Ok(ExitStatus::default());
7262
}
7363

@@ -869,7 +859,7 @@ fn print_path_instructions(bin_dir: &vt_path::AbsolutePath) {
869859
println!();
870860
println!(
871861
" Restart your terminal and IDE, then run {} to verify.",
872-
accent_command("vp env doctor")
862+
help::accent_command("vp env doctor")
873863
);
874864
}
875865

crates/vp_global_cli/src/commands/version.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
process::ExitStatus,
88
};
99

10-
use owo_colors::OwoColorize;
1110
use serde::Deserialize;
1211
use vp_pm_cli::get_package_manager_type_and_version;
1312
use vt_path::AbsolutePathBuf;
@@ -107,16 +106,12 @@ fn resolve_tool_version(local: &LocalVitePlus, tool: ToolSpec) -> Option<String>
107106
Some(pkg.version)
108107
}
109108

110-
fn accent(text: &str) -> String {
111-
if help::should_style_help() { text.bright_blue().to_string() } else { text.to_string() }
112-
}
113-
114109
fn print_rows(title: &str, rows: &[(&str, String)]) {
115110
println!("{}", help::render_heading(title));
116111
let label_width = rows.iter().map(|(label, _)| label.chars().count()).max().unwrap_or(0);
117112
for (label, value) in rows {
118113
let padding = " ".repeat(label_width.saturating_sub(label.chars().count()));
119-
println!(" {}{} {value}", accent(label), padding);
114+
println!(" {}{} {value}", help::accent(label), padding);
120115
}
121116
}
122117

crates/vp_global_cli/src/help.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ fn write_documentation_footer(output: &mut String, documentation_url: &str) {
7979
let _ = writeln!(output, "{} {documentation_url}", render_heading("Documentation"));
8080
}
8181

82+
pub fn accent(text: &str) -> String {
83+
if should_style_help() { text.bright_blue().to_string() } else { text.to_string() }
84+
}
85+
86+
pub fn accent_command(command: &str) -> String {
87+
format!("`{}`", accent(command))
88+
}
89+
8290
pub fn should_style_help() -> bool {
8391
vp_shared::is_stdout_terminal()
8492
&& std::env::var_os("NO_COLOR").is_none()

0 commit comments

Comments
 (0)