From 87f50ac0e6b4c1b54d17e89352ebae3275c9a5c3 Mon Sep 17 00:00:00 2001 From: aplio Date: Mon, 3 Jun 2024 23:51:06 +0900 Subject: [PATCH] feat: Add detect support for `parity_scale_decoder` Signed-off-by: aplio --- Cargo.lock | 1 + tools/parity_scale_cli/Cargo.toml | 8 +--- tools/parity_scale_cli/README.md | 6 --- tools/parity_scale_cli/src/main.rs | 61 +++++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed34142ae03..3b9fe28debc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4245,6 +4245,7 @@ dependencies = [ "parity-scale-codec", "serde", "serde_json", + "supports-color 2.1.0", "test_samples", ] diff --git a/tools/parity_scale_cli/Cargo.toml b/tools/parity_scale_cli/Cargo.toml index aa8bf8d2c29..6b6b2f7d181 100644 --- a/tools/parity_scale_cli/Cargo.toml +++ b/tools/parity_scale_cli/Cargo.toml @@ -10,11 +10,6 @@ license.workspace = true [lints] workspace = true -[features] -# Disable colour for all program output. -# Useful for Docker-based deployment and terminals without colour support. -no_color = ["colored/no-color"] - [dependencies] iroha_data_model = { workspace = true, features = ["http"] } iroha_primitives = { workspace = true } @@ -24,12 +19,13 @@ iroha_crypto = { workspace = true } iroha_version = { workspace = true } iroha_genesis = { workspace = true } -clap = { workspace = true, features = ["derive", "cargo"] } +clap = { workspace = true, features = ["derive", "cargo", "env", "string"] } eyre = { workspace = true } parity-scale-codec = { workspace = true } colored = "2.1.0" serde_json = { workspace = true, features = ["std"]} serde = { workspace = true } +supports-color = { workspace = true } [build-dependencies] iroha_data_model = { workspace = true } diff --git a/tools/parity_scale_cli/README.md b/tools/parity_scale_cli/README.md index 4ca6aedee2e..dc1345fe24f 100644 --- a/tools/parity_scale_cli/README.md +++ b/tools/parity_scale_cli/README.md @@ -10,12 +10,6 @@ To build the tool, run: cargo build --bin parity_scale_cli ``` -If your terminal does not support colours, run: - -```bash -cargo build --features no_color --bin parity_scale_cli -``` - ## Usage Run Parity Scale Decoder Tool: diff --git a/tools/parity_scale_cli/src/main.rs b/tools/parity_scale_cli/src/main.rs index b47530cb08c..f21912c51e7 100644 --- a/tools/parity_scale_cli/src/main.rs +++ b/tools/parity_scale_cli/src/main.rs @@ -79,7 +79,29 @@ where /// Parity Scale decoder tool for Iroha data types #[derive(Debug, Parser)] #[clap(version, about, author)] -enum Args { +struct Args { + #[clap(subcommand)] + command: Command, + + /// Whether to enable ANSI colored output or not + /// + /// By default, Iroha determines whether the terminal supports colors or not. + /// + /// In order to disable this flag explicitly, pass `--terminal-colors=false`. + #[arg( + long, + env, + default_missing_value("true"), + default_value(default_terminal_colors_str()), + action(clap::ArgAction::Set), + require_equals(true), + num_args(0..=1), + )] + pub terminal_colors: bool, +} + +#[derive(Debug, Parser)] +enum Command { /// Show all available types ListTypes, /// Decode SCALE to Rust debug format from binary file @@ -113,26 +135,34 @@ struct ScaleJsonArgs { type_name: String, } +fn is_colouring_supported() -> bool { + supports_color::on(supports_color::Stream::Stdout).is_some() +} + +fn default_terminal_colors_str() -> clap::builder::OsStr { + is_colouring_supported().to_string().into() +} + fn main() -> Result<()> { let args = Args::parse(); let map = generate_map(); - match args { - Args::ScaleToRust(decode_args) => { + match args.command { + Command::ScaleToRust(decode_args) => { let mut writer = BufWriter::new(io::stdout().lock()); let decoder = ScaleToRustDecoder::new(decode_args, &map); decoder.decode(&mut writer) } - Args::ScaleToJson(args) => { + Command::ScaleToJson(args) => { let decoder = ScaleJsonDecoder::new(args, &map)?; decoder.scale_to_json() } - Args::JsonToScale(args) => { + Command::JsonToScale(args) => { let decoder = ScaleJsonDecoder::new(args, &map)?; decoder.json_to_scale() } - Args::ListTypes => { + Command::ListTypes => { let mut writer = BufWriter::new(io::stdout().lock()); list_types(&map, &mut writer) } @@ -383,4 +413,23 @@ mod tests { .expect("Couldn't convert to SCALE"); assert_eq!(scale_actual, scale_expected); } + + #[test] + fn terminal_colors_works_as_expected() -> eyre::Result<()> { + fn try_with(arg: &str) -> eyre::Result { + // Since arg contains enum Command and we must provide something for it, we use "list-types" + Ok(Args::try_parse_from(["test", arg, "list-types"])?.terminal_colors) + } + + assert_eq!( + Args::try_parse_from(["test", "list-types"])?.terminal_colors, + is_colouring_supported() + ); + assert_eq!(try_with("--terminal-colors")?, true); + assert_eq!(try_with("--terminal-colors=false")?, false); + assert_eq!(try_with("--terminal-colors=true")?, true); + assert!(try_with("--terminal-colors=random").is_err()); + + Ok(()) + } }