diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2025-12-17 10:58:36 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2025-12-17 12:36:55 +0100 |
| commit | b239976e1ef2a10e05352c14fdaaee22cea3dca5 (patch) | |
| tree | 4475acdb8a8c1c1d1ce227c905cb7d726ef8664a /src/main.rs | |
| parent | c593918dd9df53d12c4664677f50aacdba6cfbb1 (diff) | |
Allow stdout output
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 833c846..fe0ba04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::fmt; use std::fs::{read_to_string, write}; -use std::io::{Error as IoError, Result as IoResult}; +use std::io::{self, Error as IoError, IsTerminal, Read, Result as IoResult, Write}; use std::path::PathBuf; use std::str::FromStr; use wmap_renderer::{Configuration, StageType, render_to_png, render_to_svg}; @@ -71,14 +71,17 @@ fn stage_from_str(s: &str) -> Option<StageType> { } struct Arguments { - input: PathBuf, + input: Option<PathBuf>, output: PathBuf, + write_to_stdout: bool, stage: StageType, image_type: ImageType, } fn print_help() { - println!("Usage: wmap [-t|--type IMAGE_TYPE] [-s|--stage STAGE] [-o|--output OUTPUT_FILE] INPUT_FILE"); + println!( + "Usage: wmap [-t|--type IMAGE_TYPE] [-s|--stage STAGE] [-o|--output OUTPUT_FILE] INPUT_FILE" + ); } fn parse_arguments() -> Result<Arguments, lexopt::Error> { @@ -86,6 +89,7 @@ fn parse_arguments() -> Result<Arguments, lexopt::Error> { let mut input = None; let mut output: String = String::new(); + let mut write_to_stdout: bool = false; let mut stage = StageType::Activities; let mut image_type = ImageType::Png; @@ -117,9 +121,17 @@ fn parse_arguments() -> Result<Arguments, lexopt::Error> { _ => return Err(argument.unexpected()), } } - let input = PathBuf::from(input.ok_or("missing argument INPUT_FILE")?); + let input = input.map(PathBuf::from); let output = if output.is_empty() { - input.with_extension(image_type.to_string()) + if io::stdout().is_terminal() { + match &input { + Some(path) => path.with_extension(image_type.to_string()), + None => PathBuf::from(format!("./map.{image_type}")), + } + } else { + write_to_stdout = true; + PathBuf::new() + } } else { PathBuf::from(output) }; @@ -127,6 +139,7 @@ fn parse_arguments() -> Result<Arguments, lexopt::Error> { Ok(Arguments { input, output, + write_to_stdout, stage, image_type, }) @@ -135,8 +148,14 @@ fn parse_arguments() -> Result<Arguments, lexopt::Error> { fn run() -> IoResult<()> { let arguments = parse_arguments().map_err(|_| IoError::other("Unable to parse arguments"))?; - arguments.input.try_exists()?; - let source = read_to_string(arguments.input)?; + let source = if let Some(input_path) = arguments.input { + input_path.try_exists()?; + read_to_string(input_path)? + } else { + let mut buffer = String::new(); + io::stdin().read_to_string(&mut buffer)?; + buffer + }; let map = wmap_parser::parse(&source); let configuration = Configuration::default(); @@ -147,7 +166,11 @@ fn run() -> IoResult<()> { .map_err(|_| IoError::other("Unable to render SVG"))? .into_bytes(), }; - write(arguments.output, image_data)?; + if arguments.write_to_stdout { + io::stdout().write_all(&image_data)?; + } else { + write(arguments.output, image_data)?; + } Ok(()) } |