From 63549df4406216cce7e744576b1ee8fcaba9a30a Mon Sep 17 00:00:00 2001 From: NomisIV <47303199+NomisIV@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:47:46 +0200 Subject: [PATCH] Actually read from stdin (#61) ## Description of changes `--stdin` flag read a command line argument, not stdin. Now it reads from stdin. Not sure if my code would compile on windows, but I don't have a windows computer to test it. I'm aware that https://github.com/nushell/nufmt/pull/57 tries to solve this as well, but it seems stagnant and won't pass CI. ## Relevant Issues https://github.com/nushell/nufmt/issues/56 --- src/main.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index fef3540..a3de0f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use log::{error, info, trace}; use nu_formatter::config::Config; use std::{ fs, - io::Write, + io::{self, Write}, path::{Path, PathBuf}, }; @@ -23,13 +23,15 @@ struct Cli { help = "one of more Nushell files you want to format" )] files: Vec, + #[arg( short, long, conflicts_with = "files", help = "a string of Nushell directly given to the formatter" )] - stdin: Option, + stdin: bool, + #[arg(short, long, help = "the configuration file")] config: Option, } @@ -65,7 +67,10 @@ fn main() { }; let exit_code = match cli.files[..] { - [] => format_string(cli.stdin, &cli_config), + [] if cli.stdin => { + let stdin_input = io::stdin().lines().map(|x| x.unwrap()).collect(); + format_string(Some(stdin_input), &cli_config) + } _ => format_files(cli.files, &cli_config), }; @@ -77,7 +82,7 @@ fn main() { /// format a string passed via stdin and output it directly to stdout fn format_string(string: Option, options: &Config) -> ExitCode { let output = nu_formatter::format_string(&string.unwrap(), options); - println!("output: \n{output}"); + println!("{output}"); ExitCode::Success }