Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually read from stdin #61

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading