Skip to content

Commit

Permalink
Actually read from stdin (#61)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
NomisIV authored Jul 15, 2024
1 parent 06c06fc commit 63549df
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand All @@ -23,13 +23,15 @@ struct Cli {
help = "one of more Nushell files you want to format"
)]
files: Vec<PathBuf>,

#[arg(
short,
long,
conflicts_with = "files",
help = "a string of Nushell directly given to the formatter"
)]
stdin: Option<String>,
stdin: bool,

#[arg(short, long, help = "the configuration file")]
config: Option<PathBuf>,
}
Expand Down Expand Up @@ -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),
};

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

ExitCode::Success
}
Expand Down

0 comments on commit 63549df

Please sign in to comment.