Generalize weprintln and iweprintln to write to any stream.

- `weprintln` and `iweprintln` unconditionally write to `stderr`.

  - Change the functions to take an additional parameter, the stream
    to write to.

  - Rename the functions to `wwriteln` and `iwwriteln` to reflect that
    the don't just write to `stderr` anymore, and are now closer to
    `writeln`.

  - This change is in preparation for using these functions to write
    to `stdout`.

  - See #342.
This commit is contained in:
Neal H. Walfield 2024-12-03 12:27:58 +01:00
parent 8902710333
commit 68e3ddf0b9
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
2 changed files with 37 additions and 15 deletions

View File

@ -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),*))
};
}

View File

@ -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);
}
}
}