From 6ce3ea4234633631eee3a756b782042fc777e438 Mon Sep 17 00:00:00 2001 From: witcher Date: Sun, 13 Feb 2022 20:59:39 +0100 Subject: fix: don't panic when piping Piping stdout to something like `head`, which closes rbw's stdout before it's done writing everything, causes a panic. The panic is circumvented by using `writeln!` instead of `println!` and ignoring the error when it's of kind `BrokenPipe`. Closes #79 --- src/bin/rbw/commands.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs index 0068efd..1e522a7 100644 --- a/src/bin/rbw/commands.rs +++ b/src/bin/rbw/commands.rs @@ -1,4 +1,6 @@ use anyhow::Context as _; +use std::io; +use std::io::prelude::Write; const MISSING_CONFIG_HELP: &str = "Before using rbw, you must configure the email address you would like to \ @@ -530,7 +532,13 @@ pub fn list(fields: &[String]) -> anyhow::Result<()> { ), }) .collect(); - println!("{}", values.join("\t")); + + // write to stdout but don't panic when pipe get's closed + // this happens when piping stdout in a shell + match writeln!(&mut io::stdout(), "{}", values.join("\t")) { + Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => Ok(()), + res => res, + }?; } Ok(()) -- cgit v1.2.3-54-g00ecf