From c4e3ff71f7982dbbdcb48a997c5c049932ec46e7 Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 29 Sep 2022 11:10:43 +0800 Subject: [PATCH] feat: support color (#48) close #37 --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++-- Cargo.toml | 6 +----- src/cli.rs | 4 ++-- src/main.rs | 18 +++++++++++++----- tests/macros.rs | 4 ++-- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91c69774..c653d65a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,17 @@ dependencies = [ "tempfile", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -92,13 +103,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.0.0-rc.2" +version = "4.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04d5891a0b7bbd946e91a79c350b5475764d7d7eda9c71141059a9d2ab0853e5" +checksum = "31c9484ccdc4cb8e7b117cbd0eb150c7c0f04464854e4679aeb50ef03b32d003" dependencies = [ + "atty", "bitflags", "clap_lex", "strsim", + "termcolor", ] [[package]] @@ -217,6 +230,15 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "ignore" version = "0.4.18" @@ -458,6 +480,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "terminal_size" version = "0.1.17" diff --git a/Cargo.toml b/Cargo.toml index bd40e348..3a8ebb12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,16 +13,12 @@ keywords = ["command-line", "shell-script", "argument-parser"] [dependencies] anyhow = "1" +clap = { version = "4.0", features = ["string"]} convert_case = "0.5" indexmap = "1.9" nom = "7.1" either = "1.8" -[dependencies.clap] -version = "4.0.0-rc.2" -default_features = false -features = ["std", "suggestions", "string", "help", "usage", "error-context"] - [dev-dependencies] insta = "1.15" assert_cmd = "2" diff --git a/src/cli.rs b/src/cli.rs index e04981a8..52321352 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -9,12 +9,12 @@ use std::collections::HashMap; const ENTRYPOINT: &str = "main"; -pub fn eval(source: &str, args: &[&str]) -> Result> { +pub fn eval(source: &str, args: &[&str]) -> Result> { let events = parse(source)?; let cmd = Cli::new_from_events(&events)?; match cmd.eval(args)? { Either::Left(values) => Ok(Either::Left(ArgcValue::to_shell(values))), - Either::Right(error) => Ok(Either::Right(error.to_string())), + Either::Right(error) => Ok(Either::Right(error)), } } diff --git a/src/main.rs b/src/main.rs index 3a173410..a6080acc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,12 +73,20 @@ USAGE:{usage}"#, let (source, cmd_args) = parse_script_args(&script_args)?; let cmd_args: Vec<&str> = cmd_args.iter().map(|v| v.as_str()).collect(); match argc::eval(&source, &cmd_args)? { - Either::Left(stdout) => { - println!("{}", stdout) + Either::Left(output) => { + println!("{}", output) } - Either::Right(stderr) => { - eprintln!("{}", stderr); - println!("exit 1"); + Either::Right(error) => { + if env::var_os("NO_COLOR").is_some() { + eprintln!("{}", error); + } else { + eprintln!("{}", error.render().ansi()); + } + if error.use_stderr() { + println!("exit 1"); + } else { + println!("exit 0"); + } } } } diff --git a/tests/macros.rs b/tests/macros.rs index dbad25c1..536ae2f6 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -6,7 +6,7 @@ macro_rules! snapshot { ) => { let (stdout, stderr) = match argc::eval($source, $args).unwrap() { either::Either::Left(stdout) => (stdout, String::new()), - either::Either::Right(stderr) => (String::new(), stderr), + either::Either::Right(stderr) => (String::new(), stderr.to_string()), }; let args = $args.join(" "); @@ -36,7 +36,7 @@ macro_rules! plain { ) => { let result = match argc::eval($source, $args).unwrap() { either::Either::Left(stdout) => (stdout, String::new()), - either::Either::Right(stderr) => (String::new(), stderr), + either::Either::Right(stderr) => (String::new(), stderr.to_string()), }; $({ assert_eq!(result.0.as_str(), $stdout);