Skip to content

Commit

Permalink
feat: Add colour to output of --doctor
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Jan 28, 2024
1 parent 83858e9 commit 567f1b2
Showing 1 changed file with 66 additions and 15 deletions.
81 changes: 66 additions & 15 deletions src/doctor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Sanity checking of environment
use anstream::println;
use anstyle::{AnsiColor, Effects, Reset};
use itertools::Itertools;
use std::env::VarError;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::process::Command;
use strum::Display;

use anyhow::{anyhow, Context};

Expand All @@ -14,24 +15,32 @@ use crate::utils::{Chezmoi, ChezmoiVersion, RealChezmoi, CHEZMOI_AUTO_SOURCE_VER
/// Perform environment sanity check
pub(crate) fn doctor() -> anyhow::Result<()> {
let mut worst_issues_found = CheckResult::Ok;
println!("RESULT CHECK MESSAGE");
println!(
"{}RESULT CHECK MESSAGE{}",
Effects::BOLD.render(),
Reset.render()
);
for Check { name, func } in &CHECKS {
match func() {
Ok((result, text)) => {
let text = text.replace('\n', "\n ");
println!("{result: <9} {name: <20} {text}");
println!("{result} {name: <20} {text}");
if result >= worst_issues_found {
worst_issues_found = result;
}
}
Err(err) => {
println!("FATAL {name: <20} {err}");
println!("{} {name: <20} {err}", CheckResult::Fatal);
worst_issues_found = CheckResult::Fatal;
}
}
}
if let Ok(p) = which::which("chezmoi") {
println!("\nOutput of chezmoi doctor:");
println!(
"\n{}Output of chezmoi doctor:{}",
Effects::BOLD.render(),
Reset.render()
);
_ = std::io::stdout().flush();
match Command::new(p).arg("doctor").spawn().as_mut() {
Ok(child) => {
Expand All @@ -46,21 +55,29 @@ pub(crate) fn doctor() -> anyhow::Result<()> {
}

if worst_issues_found >= CheckResult::Error {
println!();
return Err(anyhow!(
"Errors found, you need to rectify these for proper operation"
));
println!(
"\n{}Error{}: Error(s) found, you should rectify these for proper operation",
AnsiColor::Red.render_fg(),
Reset.render()
);
// There isn't a good way to get a non-zero exit code without also
// getting an anyhow error printed from here.
std::process::exit(1);
} else if worst_issues_found >= CheckResult::Warning {
println!();
return Err(anyhow!(
"Warnings found, consider investigating if you have issues"
));
println!(
"\n{}Warning{}: Warning(s) found, consider investigating (especially if you have issues)",
AnsiColor::Yellow.render_fg(),
Reset.render()
);
// There isn't a good way to get a non-zero exit code without also
// getting an anyhow error printed from here.
std::process::exit(1);
}
Ok(())
}

/// Result of a check
#[derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum CheckResult {
Ok,
Info,
Expand All @@ -69,6 +86,40 @@ enum CheckResult {
Fatal,
}

/// Coloured formatting of check result
impl std::fmt::Display for CheckResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CheckResult::Ok => write!(
f,
"{}Ok{} ",
AnsiColor::Green.render_fg(),
Reset.render()
),
CheckResult::Info => {
write!(
f,
"{}Info{} ",
AnsiColor::Green.render_fg(),
Reset.render()
)
}
CheckResult::Warning => write!(
f,
"{}Warning{}",
AnsiColor::Yellow.render_fg(),
Reset.render()
),
CheckResult::Error => {
write!(f, "{}Error{} ", AnsiColor::Red.render_fg(), Reset.render())
}
CheckResult::Fatal => {
write!(f, "{}FATAL{} ", AnsiColor::Red.render_fg(), Reset.render())
}
}
}
}

/// A check with a name
#[derive(Debug)]
struct Check {
Expand Down Expand Up @@ -189,7 +240,7 @@ fn chezmoi_check() -> anyhow::Result<(CheckResult, String)> {
if parsed_version < CHEZMOI_AUTO_SOURCE_VERSION {
Ok((
CheckResult::Warning,
format!("Chezmoi found. Version is old and doesn't support \"source auto\" directive: {version}"),
format!("Chezmoi found. Version: {version}\nWARNING: Version is old and doesn't support \"source auto\" directive!"),
))
} else {
Ok((
Expand Down

0 comments on commit 567f1b2

Please sign in to comment.