coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 71565884e30ad6832005d0edf2a83babb276abd9
parent 7019ca4df776e7f42142ececf8ed2f4161334b99
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Sat,  6 Jul 2024 15:11:31 -0700

Add colored output

Diffstat:
MCargo.lock | 17+++++++++++++++++
MCargo.toml | 1+
Msrc/cli.rs | 10+++++++++-
Msrc/core.rs | 14++++++++------
Mtests/basic.rs | 1+
5 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -115,6 +115,7 @@ version = "0.2.0" dependencies = [ "anyhow", "clap", + "colored", "serde", "serde_yaml", "shellexpand", @@ -128,6 +129,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + +[[package]] name = "dirs" version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -216,6 +227,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.86" clap = { version = "4.5.7", features = ["derive"] } +colored = "2.1.0" serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9" shellexpand = "3.0" diff --git a/src/cli.rs b/src/cli.rs @@ -1,5 +1,6 @@ //! The coliru command line interface +use colored::{Colorize, control::set_override}; use clap::{Parser, ColorChoice}; use std::path::Path; use super::core::execute_manifest_file; @@ -39,17 +40,24 @@ struct Args { /// Interpret link commands as copy commands #[arg(long)] pub copy: bool, + + /// Disable color output + #[arg(long)] + pub no_color: bool, } /// Runs the coliru CLI pub fn run() { let args = Args::parse(); let manifest_path = Path::new(&args.manifest); + if args.no_color { + set_override(false); + } match execute_manifest_file(&manifest_path, args.tag_rules, &args.host, args.dry_run, args.copy) { Err(why) => { - eprintln!("Error: {:#}", why); + eprintln!("{} {:#}", "Error:".bold().red(), why); std::process::exit(2); }, Ok(minor_errors) => { diff --git a/src/core.rs b/src/core.rs @@ -1,6 +1,7 @@ //! Manifest execution functions use anyhow::{Context, Result}; +use colored::{Colorize, ColoredString}; use std::env::set_current_dir; use std::path::Path; use super::manifest::{CopyLinkOptions, RunOptions, parse_manifest_file}; @@ -30,7 +31,7 @@ macro_rules! check_dry_run { /// indicating whether an error occurred fn handle_error(result: Result<()>) -> bool { if let Err(why) = result { - eprintln!(" Error: {:#}", why); + eprintln!(" {} {:#}", "Error:".bold().red(), why); return true; } false @@ -54,7 +55,7 @@ pub fn execute_manifest_file(path: &Path, tag_rules: Vec<String>, host: &str, for (i, step) in manifest.steps.iter().enumerate() { if !tags_match(&tag_rules, &step.tags) { continue; } - let step_str = format!("[{}/{}]", i+1, manifest.steps.len()); + let step_str = format!("[{}/{}]", i+1, manifest.steps.len()).bold(); errors |= execute_copies(&step.copy, host, temp_dir.path(), dry_run, &step_str); @@ -76,7 +77,7 @@ pub fn execute_manifest_file(path: &Path, tag_rules: Vec<String>, host: &str, /// Executes a set of copy commands and returns a bool indicating whether any /// error occurred fn execute_copies(copies: &[CopyLinkOptions], host: &str, staging_dir: &Path, - dry_run: bool, step_str: &str) -> bool { + dry_run: bool, step_str: &ColoredString) -> bool { let mut errors = false; @@ -116,8 +117,8 @@ fn execute_copies(copies: &[CopyLinkOptions], host: &str, staging_dir: &Path, /// Executes a set of link commands and returns a bool indicating whether any /// error occurred -fn execute_links(links: &[CopyLinkOptions], dry_run: bool, step_str: &str) - -> bool { +fn execute_links(links: &[CopyLinkOptions], dry_run: bool, + step_str: &ColoredString) -> bool { let mut errors = false; @@ -135,7 +136,8 @@ fn execute_links(links: &[CopyLinkOptions], dry_run: bool, step_str: &str) /// Executes a set of run commands and returns a bool indicating whether any /// error occurred fn execute_runs(runs: &[RunOptions], tag_rules: &[String], host: &str, - staging_dir: &Path, dry_run: bool, step_str: &str) -> bool { + staging_dir: &Path, dry_run: bool, step_str: &ColoredString) -> +bool { let mut errors = false; diff --git a/tests/basic.rs b/tests/basic.rs @@ -22,6 +22,7 @@ Options: -n, --dry-run Do a trial run without any permanent changes --host <HOST> Install dotfiles on another machine over SSH --copy Interpret link commands as copy commands + --no-color Disable color output -h, --help Print help -V, --version Print version