From 86b412a5fbf3b789db5d2dd7bd08d9ac717d19da Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 7 Sep 2023 11:57:03 -0700 Subject: [PATCH] new: Add CLI colors. (#189) --- CHANGELOG.md | 1 + crates/cli/src/app.rs | 20 ++++++++++++++++++-- crates/core/src/tool.rs | 16 ++++++++++++---- crates/core/src/version.rs | 7 +++++++ crates/pdk-api/src/api.rs | 6 ++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5770c37da..cf65ad402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ #### 🚀 Updates +- Added colors to command line `--help` menus. - WASM API - Added `is_musl` and `get_target_triple` helper functions. diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index 6d93d2a78..658cf8bf8 100644 --- a/crates/cli/src/app.rs +++ b/crates/cli/src/app.rs @@ -3,7 +3,9 @@ use crate::commands::{ InstallGlobalArgs, ListArgs, ListGlobalArgs, ListRemoteArgs, LocalArgs, PluginsArgs, RemovePluginArgs, RunArgs, SetupArgs, UnaliasArgs, UninstallArgs, UninstallGlobalArgs, }; +use clap::builder::styling::{Color, Style, Styles}; use clap::{Parser, Subcommand, ValueEnum}; +use starbase_styles::color::Color as ColorType; use std::fmt::{Display, Error, Formatter}; #[derive(ValueEnum, Clone, Debug, Default)] @@ -36,17 +38,31 @@ impl Display for LogLevel { } } +fn fg(ty: ColorType) -> Style { + Style::new().fg_color(Some(Color::from(ty as u8))) +} + +fn create_styles() -> Styles { + Styles::default() + .error(fg(ColorType::Red)) + .header(Style::new().bold()) + .invalid(fg(ColorType::Yellow)) + .literal(fg(ColorType::Pink)) // args, options, etc + .placeholder(fg(ColorType::GrayLight)) + .usage(fg(ColorType::Purple).bold()) + .valid(fg(ColorType::Green)) +} + #[derive(Debug, Parser)] #[command( name = "proto", version, about, long_about = None, - disable_colored_help = true, disable_help_subcommand = true, propagate_version = true, next_line_help = false, - rename_all = "camelCase" + styles = create_styles() )] pub struct App { #[arg( diff --git a/crates/core/src/tool.rs b/crates/core/src/tool.rs index fafe7bfaa..97ea1a355 100644 --- a/crates/core/src/tool.rs +++ b/crates/core/src/tool.rs @@ -763,7 +763,9 @@ impl Tool { }, )?; - return Ok(result.installed); + if !result.skip_install { + return Ok(result.installed); + } } // Install from a prebuilt archive @@ -847,7 +849,7 @@ impl Tool { }, )?; - if !result.uninstalled { + if !result.skip_uninstall && !result.uninstalled { return Ok(false); } } @@ -907,6 +909,10 @@ impl Tool { /// Find the absolute file path to the tool's binary that will be executed. pub async fn locate_bins(&mut self) -> miette::Result<()> { + if self.bin_path.is_some() { + return Ok(()); + } + let mut options = LocateBinsOutput::default(); let tool_dir = self.get_tool_dir(); @@ -950,7 +956,7 @@ impl Tool { /// Find the directory global packages are installed to. pub async fn locate_globals_dir(&mut self) -> miette::Result<()> { - if !self.plugin.has_func("locate_bins") { + if !self.plugin.has_func("locate_bins") || self.globals_dir.is_some() { return Ok(()); } @@ -1114,7 +1120,9 @@ impl Tool { fn is_installed(&self) -> bool { let dir = self.get_tool_dir(); - dir.exists() && !dir.join(".lock").exists() + self.version.as_ref().is_some_and(|v| !v.is_latest()) + && dir.exists() + && !dir.join(".lock").exists() } /// Return true if the tool has been setup (installed and binaries are located). diff --git a/crates/core/src/version.rs b/crates/core/src/version.rs index 89ac09453..cd3983036 100644 --- a/crates/core/src/version.rs +++ b/crates/core/src/version.rs @@ -142,6 +142,13 @@ impl AliasOrVersion { Ok(Self::from_str(value.as_ref())?) } + pub fn is_latest(&self) -> bool { + match self { + Self::Alias(alias) => alias == "latest", + Self::Version(_) => false, + } + } + pub fn to_implicit_type(&self) -> VersionType { match self { Self::Alias(alias) => VersionType::Alias(alias.to_owned()), diff --git a/crates/pdk-api/src/api.rs b/crates/pdk-api/src/api.rs index e923741c5..8b1f8e407 100644 --- a/crates/pdk-api/src/api.rs +++ b/crates/pdk-api/src/api.rs @@ -151,6 +151,9 @@ json_struct!( pub struct NativeInstallOutput { /// Whether the install was successful. pub installed: bool, + + /// Whether to skip the install process or not. + pub skip_install: bool, } ); @@ -167,6 +170,9 @@ json_struct!( pub struct NativeUninstallOutput { /// Whether the install was successful. pub uninstalled: bool, + + /// Whether to skip the uninstall process or not. + pub skip_uninstall: bool, } );