From 92e90b8a8e0cf646265c2f8b5c2f5c983c07be9e Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Fri, 22 Sep 2023 22:27:56 +0100 Subject: [PATCH] Make info file output optional, don't include tile properties when not specifying tile size --- src/cli_args.rs | 10 ++++++++-- src/commands/info.rs | 10 +++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/cli_args.rs b/src/cli_args.rs index 1d70dc9..f50c3b3 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -126,10 +126,16 @@ impl Args { Args::Info(info) => { let image_data = load_image(&info.input, None)?; let output = info.run(&image_data)?; - { - let file = std::fs::File::create(&info.output)?; + + if let Some(out_path) = &info.output { + let file = std::fs::File::create(out_path)?; serde_json::to_writer_pretty(file, &output)?; + } else { + let stdout = std::io::stdout(); + let stdout_handle = stdout.lock(); + serde_json::to_writer_pretty(stdout_handle, &output)?; } + Ok(()) } } diff --git a/src/commands/info.rs b/src/commands/info.rs index 140deea..235d06e 100644 --- a/src/commands/info.rs +++ b/src/commands/info.rs @@ -2,16 +2,16 @@ use clap::Parser; use image::{GenericImage, ImageFormat, Pixel}; use serde::{Deserialize, Serialize}; -/// Extract Information About An Image +/// Extract Information About An Image or Spritesheet #[derive(Debug, Clone, Parser, Serialize, Deserialize)] #[clap(author, version = "0.7.0")] pub struct Info { /// The path to the image file #[serde(default)] pub input: String, - /// The path to write the flipped image + /// The output path for a JSON formatted information file. Omit to print to stdout #[serde(default)] - pub output: String, + pub output: Option<String>, /// The size of each tile in the spritesheet. Assumed to be square tiles #[clap(short, long, default_value = None)] #[serde(default)] @@ -23,9 +23,13 @@ pub struct ImageInfo { image_width: u32, image_height: u32, image_format: String, + #[serde(skip_serializing_if = "Option::is_none")] tile_width: Option<u32>, + #[serde(skip_serializing_if = "Option::is_none")] tile_height: Option<u32>, + #[serde(skip_serializing_if = "Option::is_none")] rows: Option<u32>, + #[serde(skip_serializing_if = "Option::is_none")] columns: Option<u32>, } -- GitLab