diff options
author | witcher <witcher@wiredspace.de> | 2022-02-13 20:59:39 +0100 |
---|---|---|
committer | witcher <witcher@wiredspace.de> | 2022-02-13 20:59:39 +0100 |
commit | 6ce3ea4234633631eee3a756b782042fc777e438 (patch) | |
tree | d2b59d255049edb95ea0695ab272d986e7981025 /src | |
parent | 98d3a21a84d7ad731cba939c60368fd352e39636 (diff) | |
download | rbw-6ce3ea4234633631eee3a756b782042fc777e438.tar.gz rbw-6ce3ea4234633631eee3a756b782042fc777e438.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/rbw/commands.rs | 10 |
1 files changed, 9 insertions, 1 deletions
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(()) |