Skip to content

Commit 63549df

Browse files
authored
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 #57 tries to solve this as well, but it seems stagnant and won't pass CI. ## Relevant Issues #56
1 parent 06c06fc commit 63549df

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/main.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use log::{error, info, trace};
55
use nu_formatter::config::Config;
66
use std::{
77
fs,
8-
io::Write,
8+
io::{self, Write},
99
path::{Path, PathBuf},
1010
};
1111

@@ -23,13 +23,15 @@ struct Cli {
2323
help = "one of more Nushell files you want to format"
2424
)]
2525
files: Vec<PathBuf>,
26+
2627
#[arg(
2728
short,
2829
long,
2930
conflicts_with = "files",
3031
help = "a string of Nushell directly given to the formatter"
3132
)]
32-
stdin: Option<String>,
33+
stdin: bool,
34+
3335
#[arg(short, long, help = "the configuration file")]
3436
config: Option<PathBuf>,
3537
}
@@ -65,7 +67,10 @@ fn main() {
6567
};
6668

6769
let exit_code = match cli.files[..] {
68-
[] => format_string(cli.stdin, &cli_config),
70+
[] if cli.stdin => {
71+
let stdin_input = io::stdin().lines().map(|x| x.unwrap()).collect();
72+
format_string(Some(stdin_input), &cli_config)
73+
}
6974
_ => format_files(cli.files, &cli_config),
7075
};
7176

@@ -77,7 +82,7 @@ fn main() {
7782
/// format a string passed via stdin and output it directly to stdout
7883
fn format_string(string: Option<String>, options: &Config) -> ExitCode {
7984
let output = nu_formatter::format_string(&string.unwrap(), options);
80-
println!("output: \n{output}");
85+
println!("{output}");
8186

8287
ExitCode::Success
8388
}

0 commit comments

Comments
 (0)