Skip to content

Commit

Permalink
refactor(cli): Extract CLI style definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 11, 2023
1 parent 7541cc7 commit 2d5354a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
anstyle = "1.0.3"
anyhow = "1.0.75"
base64 = "0.21.3"
bytesize = "1.3"
Expand Down Expand Up @@ -121,6 +122,7 @@ name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
anstyle.workspace = true
anyhow.workspace = true
base64.workspace = true
bytesize.workspace = true
Expand Down
18 changes: 9 additions & 9 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::list_commands;
use crate::command_prelude::*;
use crate::util::is_rustup;
use cargo::core::features::HIDDEN;
use cargo::util::style;

pub fn main(config: &mut LazyConfig) -> CliResult {
let args = cli().try_get_matches()?;
Expand Down Expand Up @@ -519,15 +520,14 @@ pub fn cli() -> Command {
};

let styles = {
use clap::builder::styling::*;
Styles::styled()
.header(AnsiColor::Green.on_default() | Effects::BOLD)
.usage(AnsiColor::Green.on_default() | Effects::BOLD)
.literal(AnsiColor::Cyan.on_default() | Effects::BOLD)
.placeholder(AnsiColor::Cyan.on_default())
.error(AnsiColor::Red.on_default() | Effects::BOLD)
.valid(AnsiColor::Cyan.on_default() | Effects::BOLD)
.invalid(AnsiColor::Yellow.on_default() | Effects::BOLD)
clap::builder::styling::Styles::styled()
.header(style::HEADER)
.usage(style::USAGE)
.literal(style::LITERAL)
.placeholder(style::PLACEHOLDER)
.error(style::ERROR)
.valid(style::VALID)
.invalid(style::INVALID)
};

Command::new("cargo")
Expand Down
1 change: 1 addition & 0 deletions src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod queue;
pub mod restricted_names;
pub mod rustc;
mod semver_ext;
pub mod style;
pub mod to_semver;
pub mod toml;
pub mod toml_mut;
Expand Down
9 changes: 9 additions & 0 deletions src/cargo/util/style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use anstyle::*;

pub const HEADER: Style = AnsiColor::Green.on_default().effects(Effects::BOLD);
pub const USAGE: Style = AnsiColor::Green.on_default().effects(Effects::BOLD);
pub const LITERAL: Style = AnsiColor::Cyan.on_default().effects(Effects::BOLD);
pub const PLACEHOLDER: Style = AnsiColor::Cyan.on_default();
pub const ERROR: Style = AnsiColor::Red.on_default().effects(Effects::BOLD);
pub const VALID: Style = AnsiColor::Cyan.on_default().effects(Effects::BOLD);
pub const INVALID: Style = AnsiColor::Yellow.on_default().effects(Effects::BOLD);

0 comments on commit 2d5354a

Please sign in to comment.