Skip to content

Commit

Permalink
new: Add CLI colors. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 10, 2023
1 parent d3a4677 commit 86b412a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### 🚀 Updates

- Added colors to command line `--help` menus.
- WASM API
- Added `is_musl` and `get_target_triple` helper functions.

Expand Down
20 changes: 18 additions & 2 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 12 additions & 4 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,9 @@ impl Tool {
},
)?;

return Ok(result.installed);
if !result.skip_install {
return Ok(result.installed);
}
}

// Install from a prebuilt archive
Expand Down Expand Up @@ -847,7 +849,7 @@ impl Tool {
},
)?;

if !result.uninstalled {
if !result.skip_uninstall && !result.uninstalled {
return Ok(false);
}
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions crates/core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
6 changes: 6 additions & 0 deletions crates/pdk-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
);

Expand All @@ -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,
}
);

Expand Down

0 comments on commit 86b412a

Please sign in to comment.