diff --git a/src/macros.rs b/src/macros.rs index 09ce6070..64f8b8f5 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -21,8 +21,10 @@ macro_rules! weprintln { { indent=$i: expr, $($arg: expr),* } => {{ let i = $i; - crate::output::wrapping::iweprintln(i.as_ref(), i.as_ref(), - format_args!($($arg),*)) + crate::output::wrapping::iwwriteln( + &mut std::io::stderr(), + i.as_ref(), i.as_ref(), + format_args!($($arg),*)) }}; { @@ -32,8 +34,10 @@ macro_rules! weprintln { } => {{ let ii = $ii; let si = $si; - crate::output::wrapping::iweprintln(ii.as_ref(), si.as_ref(), - format_args!($($arg),*)) + crate::output::wrapping::iwwriteln( + &mut std::io::stderr(), + ii.as_ref(), si.as_ref(), + format_args!($($arg),*)) }}; { @@ -42,12 +46,15 @@ macro_rules! weprintln { } => {{ let ii = $ii; let si = format!("{:1$}", "", ii.len()); - crate::output::wrapping::iweprintln(ii.as_ref(), si.as_ref(), - format_args!($($arg),*)) + crate::output::wrapping::iwwriteln( + &mut std::io::stderr(), + ii.as_ref(), si.as_ref(), + format_args!($($arg),*)) }}; { $($arg: expr),* } => { - crate::output::wrapping::weprintln(format_args!($($arg),*)) + crate::output::wrapping::wwriteln( + &mut std::io::stderr(), format_args!($($arg),*)) }; } diff --git a/src/output/wrapping.rs b/src/output/wrapping.rs index 76318d84..a5902d84 100644 --- a/src/output/wrapping.rs +++ b/src/output/wrapping.rs @@ -1,36 +1,51 @@ //! Line wrapping human-readable output. use std::fmt; +use std::io::Write; use std::sync::OnceLock; /// A non-breaking space. pub const NBSP: char = '\u{00A0}'; -/// Prints the given message to stderr. +/// Prints the given message to the specified stream. /// /// Hint: Use `weprintln!(..)` instead of invoking this function /// directly. -pub fn weprintln(msg: fmt::Arguments) { +/// +/// Like [`eprintln!`], panics if writing to the stream fails. +/// +/// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html +pub fn wwriteln(stream: &mut dyn Write, msg: fmt::Arguments) { let m = format!("{}", msg); for l in textwrap::wrap(&m, options()) { - eprintln!("{}", l); + if let Err(err) = writeln!(stream, "{}", l) { + panic!("Writing to output: {}", err); + } } } -/// Prints the given message to stderr, indenting continuations. +/// Prints the given message to the specified stream, indenting +/// continuations. /// /// Hint: Use `weprintln!(indent="...", ..)` or /// `weprintln!(initial_indent="...", subsequent_indent="...", ..)` /// instead of invoking this function directly. -pub fn iweprintln(initial_indent: &str, - subsequent_indent: &str, - msg: fmt::Arguments) { +/// +/// Like [`eprintln!`], panics if writing to the stream fails. +/// +/// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html +pub fn iwwriteln(stream: &mut dyn Write, + initial_indent: &str, + subsequent_indent: &str, + msg: fmt::Arguments) { let m = format!("{}", msg); for l in textwrap::wrap(&m, options() .initial_indent(initial_indent) .subsequent_indent(subsequent_indent)) { - eprintln!("{}", l); + if let Err(err) = writeln!(stream, "{}", l) { + panic!("Writing to output: {}", err); + } } }