Skip to content

Commit

Permalink
feat: colored console output
Browse files Browse the repository at this point in the history
Closes #6.
  • Loading branch information
BD103 committed Jul 13, 2024
1 parent 849ca6a commit ce5aa02
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
51 changes: 51 additions & 0 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::cli::ColorChoice;

/// Resets all colors and styles.
const RESET: &str = "\x1b[0m";

/// Bold.
const BOLD: &str = "\x1b[1m";

/// Dim.
const DIM: &str = "\x1b[2m";

/// Info (cyan).
const INFO: &str = "\x1b[36m";

/// Success (green).
const SUCCESS: &str = "\x1b[32m";

/// Error (magenta).
const ERROR: &str = "\x1b[35m";

pub struct Color {
pub reset: &'static str,
pub bold: &'static str,
pub dim: &'static str,
pub info: &'static str,
pub success: &'static str,
pub error: &'static str,
}

impl Color {
pub fn from_color_choice(choice: ColorChoice) -> Self {
match choice {
ColorChoice::Always => Color {
reset: RESET,
bold: BOLD,
dim: DIM,
info: INFO,
success: SUCCESS,
error: ERROR,
},
ColorChoice::Never => Color {
reset: "",
bold: "",
dim: "",
info: "",
success: "",
error: "",
},
}
}
}
22 changes: 21 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{ensure, Context};
use argh::FromArgs;
use argh::{FromArgValue, FromArgs};
use serde::Deserialize;
use std::{
path::PathBuf,
Expand Down Expand Up @@ -29,6 +29,10 @@ pub struct CLI {
/// the total amount of chunks
#[argh(option)]
pub total_chunks: Option<usize>,

/// when to use color in the terminal output, either "always" or "never"
#[argh(option, default = "ColorChoice::Always")]
pub color: ColorChoice,
}

impl CLI {
Expand All @@ -55,6 +59,22 @@ impl CLI {
}
}

#[derive(Clone, Copy, Debug)]
pub enum ColorChoice {
Always,
Never,
}

impl FromArgValue for ColorChoice {
fn from_arg_value(value: &str) -> Result<Self, String> {
match value {
"always" => Ok(Self::Always),
"never" => Ok(Self::Never),
_ => Err("must be `always` or `never`.".to_string()),
}
}
}

/// Represents the output of `cargo-locate-project`.
#[derive(Deserialize, Debug)]
struct ProjectLocation {
Expand Down
26 changes: 20 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod ansi;
mod chunk;
mod cli;
mod combos;
Expand All @@ -6,6 +7,7 @@ mod intern;
mod metadata;
mod runner;

use ansi::*;
use anyhow::{bail, Context};
use chunk::select_chunk;
use cli::CLI;
Expand All @@ -27,6 +29,15 @@ fn main() -> anyhow::Result<()> {
None => Config::default(),
};

let Color {
reset,
bold,
dim,
info,
success,
error,
} = Color::from_color_choice(cli.color);

let metadata = load_metadata(&cli.manifest_path).context("Failed to load Cargo metadata.")?;

let packages =
Expand All @@ -47,8 +58,11 @@ fn main() -> anyhow::Result<()> {
.with_context(|| format!("Total features: {}, Max combo size: {max_k:?}", storage.len()))
.with_context(|| format!("Unable to estimate checks required for all feature combinations of package {name}."))?;

println!("Package {name} with {} features.", storage.len());
println!("Estimated checks: {}", estimated_checks);
println!(
"{bold}Package {info}{name}{reset}{bold} with {info}{}{reset}{bold} features.{reset}",
storage.len()
);
println!("{bold}Estimated checks: {info}{}{reset}", estimated_checks);

for combo in feature_combos(
&storage,
Expand All @@ -64,7 +78,7 @@ fn main() -> anyhow::Result<()> {

features.sort_unstable();

println!("\tChecking: {:?}", features);
println!("\t{dim}Checking:{reset} {info}{:?}{reset}", features);

let status = check_with_features(&name, &cli.manifest_path, &combo, &storage)
.with_context(|| format!("Tried checking package {name}."))?;
Expand All @@ -79,16 +93,16 @@ fn main() -> anyhow::Result<()> {
}

if !failures.is_empty() {
eprintln!("Failure report:");
eprintln!("{error}{bold}Failure report:{reset}");

for CheckFailure { package, features } in failures {
eprintln!("\tFailed checking package {package} with features {features:?}.");
eprintln!("\t{error}Failed checking package {bold}{package}{reset} {error}with features{reset} {features:?}.");
}

bail!("Some packages failed to be checked.");
}

println!("Feature combination checks successful! Congrats :)");
println!("{success}{bold}Feature combination checks successful! Congrats :){reset}");

Ok(())
}
Expand Down

0 comments on commit ce5aa02

Please sign in to comment.