diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-02-09 22:37:45 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-02-09 22:37:45 -0800 |
commit | efdc17248cbf8336f28c377a301752604fb6db74 (patch) | |
tree | c8700b54c39c4015600f7eedd41681fcf2e9d365 | |
parent | 86fee15a36758dde3e118a92469cc71c11ab50b4 (diff) | |
download | fatcat-cli-efdc17248cbf8336f28c377a301752604fb6db74.tar.gz fatcat-cli-efdc17248cbf8336f28c377a301752604fb6db74.zip |
add colored JSON output for some commands
-rw-r--r-- | Cargo.lock | 25 | ||||
-rw-r--r-- | fatcat-cli/Cargo.toml | 1 | ||||
-rw-r--r-- | fatcat-cli/src/entities.rs | 5 | ||||
-rw-r--r-- | fatcat-cli/src/main.rs | 25 |
4 files changed, 44 insertions, 12 deletions
@@ -19,6 +19,15 @@ dependencies = [ ] [[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi 0.3.8", +] + +[[package]] name = "antidote" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -172,7 +181,7 @@ version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" dependencies = [ - "ansi_term", + "ansi_term 0.11.0", "atty", "bitflags", "strsim", @@ -182,6 +191,19 @@ dependencies = [ ] [[package]] +name = "colored_json" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd32eb54d016e203b7c2600e3a7802c75843a92e38ccc4869aefeca21771a64" +dependencies = [ + "ansi_term 0.12.1", + "atty", + "libc", + "serde 1.0.123", + "serde_json 1.0.55", +] + +[[package]] name = "console" version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -278,6 +300,7 @@ dependencies = [ "assert_cmd", "atty", "chrono-humanize", + "colored_json", "data-encoding", "env_logger", "fatcat-openapi", diff --git a/fatcat-cli/Cargo.toml b/fatcat-cli/Cargo.toml index dca1a30..f7885b6 100644 --- a/fatcat-cli/Cargo.toml +++ b/fatcat-cli/Cargo.toml @@ -18,6 +18,7 @@ fatcat-openapi = { version = "*", path = "../fatcat-openapi", default-features = macaroon = { git = "https://github.com/bnewbold/libmacaroon-rs", branch = "bnewbold-broken" } toml = "0.5" termcolor = "1" +colored_json = "*" atty = "0.2" tabwriter = "1.2" #human-panic = "1" diff --git a/fatcat-cli/src/entities.rs b/fatcat-cli/src/entities.rs index d61f6dc..5e4ac06 100644 --- a/fatcat-cli/src/entities.rs +++ b/fatcat-cli/src/entities.rs @@ -124,6 +124,7 @@ pub fn entity_model_from_json_str( pub trait ApiModelSer { fn to_json_string(&self) -> Result<String>; + fn to_json_value(&self) -> Result<serde_json::Value>; fn to_toml_string(&self) -> Result<String>; } @@ -132,6 +133,10 @@ impl<T: serde::Serialize> ApiModelSer for T { Ok(serde_json::to_string(self)?) } + fn to_json_value(&self) -> Result<serde_json::Value> { + Ok(serde_json::to_value(self)?) + } + fn to_toml_string(&self) -> Result<String> { Ok(toml::Value::try_from(self)?.to_string()) } diff --git a/fatcat-cli/src/main.rs b/fatcat-cli/src/main.rs index 3b0d382..3b4a81b 100644 --- a/fatcat-cli/src/main.rs +++ b/fatcat-cli/src/main.rs @@ -1,6 +1,5 @@ use crate::{path_or_stdin, BatchGrouper, BatchOp}; use anyhow::{anyhow, Context, Result}; -use fatcat_cli::ApiModelSer; use fatcat_cli::*; #[allow(unused_imports)] use log::{self, debug, info}; @@ -8,6 +7,7 @@ use std::io::Write; use std::path::PathBuf; use structopt::StructOpt; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; +use colored_json::to_colored_json_auto; #[derive(StructOpt)] #[structopt(rename_all = "kebab-case", about = "CLI interface to Fatcat API")] @@ -285,6 +285,9 @@ fn main() -> Result<()> { debug!("Args parsed, starting up"); + #[cfg(windows)] + colored_json::enable_ansi_support(); + if let Err(err) = run(opt) { // Be graceful about some errors if let Some(io_err) = err.root_cause().downcast_ref::<std::io::Error>() { @@ -323,7 +326,7 @@ fn run(opt: Opt) -> Result<()> { if toml { writeln!(&mut std::io::stdout(), "{}", result.to_toml_string()?)? } else if json || true { - writeln!(&mut std::io::stdout(), "{}", result.to_json_string()?)? + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&result.to_json_value()?)?)? } } Command::Create { @@ -333,7 +336,7 @@ fn run(opt: Opt) -> Result<()> { } => { let json_str = read_entity_file(input_path)?; let ee = api_client.create_entity_from_json(entity_type, &json_str, editgroup_id)?; - println!("{}", serde_json::to_string(&ee)?); + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)? } Command::Update { specifier, @@ -357,7 +360,7 @@ fn run(opt: Opt) -> Result<()> { }; let ee = api_client.update_entity_from_json(exact_specifier, &json_str, editgroup_id)?; - println!("{}", serde_json::to_string(&ee)?); + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)? } Command::Edit { specifier, @@ -373,7 +376,7 @@ fn run(opt: Opt) -> Result<()> { json, editing_command, )?; - println!("{}", serde_json::to_string(&ee)?); + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)? } Command::Changelog { limit, json } => { let resp = api_client @@ -598,25 +601,25 @@ fn run(opt: Opt) -> Result<()> { cmd: EditgroupCommand::Create { description }, } => { let eg = api_client.create_editgroup(Some(description))?; - println!("{}", serde_json::to_string(&eg)?) + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&eg)?)?)? } Command::Editgroup { cmd: EditgroupCommand::Accept { editgroup_id }, } => { let msg = api_client.accept_editgroup(editgroup_id.clone())?; - println!("{}", serde_json::to_string(&msg)?); + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Editgroup { cmd: EditgroupCommand::Submit { editgroup_id }, } => { - let eg = api_client.update_editgroup_submit(editgroup_id, true)?; - println!("{}", eg.to_json_string()?); + let msg = api_client.update_editgroup_submit(editgroup_id, true)?; + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Editgroup { cmd: EditgroupCommand::Unsubmit { editgroup_id }, } => { - let eg = api_client.update_editgroup_submit(editgroup_id, false)?; - println!("{}", eg.to_json_string()?); + let msg = api_client.update_editgroup_submit(editgroup_id, false)?; + writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Status { json } => { let status = ClientStatus::generate(&mut api_client)?; |