Skip to content

Commit

Permalink
feat: support color (#48)
Browse files Browse the repository at this point in the history
close #37
  • Loading branch information
sigoden authored Sep 29, 2022
1 parent 8efe573 commit c4e3ff7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
35 changes: 33 additions & 2 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::collections::HashMap;

const ENTRYPOINT: &str = "main";

pub fn eval(source: &str, args: &[&str]) -> Result<Either<String, String>> {
pub fn eval(source: &str, args: &[&str]) -> Result<Either<String, clap::Error>> {
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)),
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c4e3ff7

Please sign in to comment.