Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
remove header in stdout unless there is a panic (#112)
Browse files Browse the repository at this point in the history
* remove header in stdout unless there is a panic

* added verbose and path to config file

* corrected verbose description and removed custom config file path

* typo in help

---------

Co-authored-by: astrale-sharp <[email protected]>
  • Loading branch information
ayghri and astrale-sharp authored Oct 12, 2023
1 parent ef4d9dd commit a825606
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options:
--stdout Same as `--output -` (Deprecated, here for compatibility).
--check Run in 'check' mode. Exits with 0 if input is
formatted correctly. Exits with 1 if formatting is required.
--verbose increase verbosity for non errors
-v, --version Prints the current version.
-h, --help Prints this help.
--get-global-config-path Prints the path of the global configuration file.
Expand Down Expand Up @@ -77,7 +78,7 @@ enum Output {
}

impl Output {
fn write(&self, input: &Input, formatted: &str) -> Result<(), ()> {
fn write(&self, input: &Input, formatted: &str, verbose: bool) -> Result<(), ()> {
match self {
Output::None => {
// this is not stdout by the check after parsing the arguments that sets the output
Expand All @@ -95,21 +96,31 @@ impl Output {
.unwrap_or_else(|err| panic!("Couldn't open file: {path:?}: {err}"));
file.write_all(formatted.as_bytes())
.unwrap_or_else(|err| panic!("Failed to write to file {path:?}: {err}"));
println!("file: {path:?} overwritten.");
if verbose {
println!("file: {path:?} overwritten.");
};
}
Output::Check => {
if input.content != formatted {
println!("{} needs formatting.", input.name);
if verbose {
println!("{} needs formatting.", input.name);
}
return Err(());
} else {
println!("{} is already formatted.", input.name);
if verbose {
println!("{} is already formatted.", input.name);
}
}
}
Output::Stdout => {
println!("=== {:?} ===", input.name);
if verbose {
println!("=== {:?} ===", input.name);
};
stdout()
.write_all(formatted.as_bytes())
.unwrap_or_else(|err| panic!("Couldn't write to stdout: {err}"));
.unwrap_or_else(|err| {
panic!("Couldn't write to stdout: {}", err);
});
}
Output::File(output) => {
let mut file = File::options()
Expand All @@ -131,6 +142,7 @@ fn main() -> Result<(), lexopt::Error> {
let mut parser = lexopt::Parser::from_env();
let mut inputs = Inputs::Stdin;
let mut output = Output::None;
let mut verbose = false;
while let Some(arg) = parser.next()? {
match arg {
Long("version") | Short('v') => {
Expand Down Expand Up @@ -182,6 +194,9 @@ fn main() -> Result<(), lexopt::Error> {
Output::File(value)
};
}
Long("verbose") => {
verbose = true;
}
Long("check") => {
output = Output::Check;
}
Expand Down Expand Up @@ -231,7 +246,7 @@ fn main() -> Result<(), lexopt::Error> {
for input in inputs.read() {
let formatted = format(&input.content, config);

match output.write(&input, &formatted) {
match output.write(&input, &formatted, verbose) {
Ok(()) => {}
Err(()) => {
exit_status = 1;
Expand Down

0 comments on commit a825606

Please sign in to comment.