From f05b911415ae0778faae92a97707687bbe04623c Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sun, 18 Jul 2021 11:56:01 +0200 Subject: [PATCH] added #3 support drag and drop in windows --- README.md | 6 ++++++ src/main.rs | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f9bbc5..6782992 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ OPTIONS: Reads from stdin if no file is supplied. Outputs to stdout if no output file is specified. +On windows, it writes to a file called `_f.json`, unless the `--stdout` flag is used or a custom output file is provided. This it to enable drag-and-drop in windows explorer. + +## Error handling +`jsonformat` does not report malformed json - it can't even fully know whether the json is actually malformed. Malformed json is just formatted kind of incorrectly, with no data lost and no crashes. If you find one, open an issue, + + ## How? `jsonformat` does not actually parse the json, it just loops through each character and keeps track of some flags. It then copies these characters to the output buffer, adding and removing whitespace. diff --git a/src/main.rs b/src/main.rs index 5749763..501a4c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,10 @@ use std::io::Read; fn main() -> Result<(), io::Error> { let matches = clap_app!(jsonformat => - (version: "1.0") + (version: "1.1") (author: "nilstrieb ") (about: "Formats json from stdin or from a file") + (@arg stdout: -s --stdout "Output the result to stdout instead of the default output file. Windows only.") (@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)") (@arg output: -o --output +takes_value "The output file for the formatted json") (@arg input: "The input file to format") @@ -37,7 +38,21 @@ fn main() -> Result<(), io::Error> { let formatted = format_json(&str, replaced_indent.as_deref()); - match matches.value_of("output") { + let mut output = matches.value_of("output"); + let mut windows_output_default_file: Option = None; + + #[cfg(windows)] + if !matches.is_present("stdout") { + if let Some(file) = matches.value_of("input") { + // on windows, set the default output file if no stdout flag is provided + // this makes it work with drag and drop in windows explorer + windows_output_default_file = Some(file.replace(".json", "_f.json")) + } + } + + output = windows_output_default_file.as_deref().or(output); + + match output { Some(file) => { fs::write(file, formatted)?; }