Skip to content

Commit

Permalink
fix: flush stream even if not stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 10, 2024
1 parent 805c9f5 commit 09504c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap_complete::{generate, Shell};
use clap_complete_nushell::Nushell;

use rip2::{args, util};
use std::io::stdout;
use std::io;
use std::process::ExitCode;

fn main() -> ExitCode {
Expand All @@ -12,19 +12,19 @@ fn main() -> ExitCode {
if let Some(shell) = cli.completions.as_deref() {
if ["nu", "nushell"].contains(&shell) {
let shell = Nushell;
generate(shell, &mut args::Args::command(), "rip", &mut stdout());
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
} else {
let shell = Shell::from_str(shell, true).unwrap_or_else(|_| {
eprintln!("Invalid shell specification: {}", shell);
std::process::exit(1);
});
generate(shell, &mut args::Args::command(), "rip", &mut stdout());
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
}
return ExitCode::SUCCESS;
}

let mode = util::ProductionMode;
let mut stream = stdout();
let mut stream = io::stdout();

if let Err(ref e) = rip2::run(cli, mode, &mut stream) {
println!("Exception: {}", e);
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub fn prompt_yes(
stream: &mut impl Write,
) -> Result<bool, Error> {
write!(stream, "{} (y/N) ", prompt.as_ref())?;
if io::stdout().flush().is_err() {
if stream.flush().is_err() {
// If stdout wasn't flushed properly, fallback to println
println!("{} (y/N)", prompt.as_ref());
writeln!(stream, "{} (y/N)", prompt.as_ref())?;
}

if source.is_test() {
Expand Down

0 comments on commit 09504c8

Please sign in to comment.