Add new macro wwrintln.

- Add a new macro `wwriteln`, which is like `writeln`, but wraps the
    output.

  - Rewrite `weprintln` to use it.

  - See #342.
This commit is contained in:
Neal H. Walfield 2024-12-03 14:27:20 +01:00
parent 68e3ddf0b9
commit 9b46f2e239
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3

View File

@ -13,48 +13,123 @@ macro_rules! platform {
} }
} }
/// Like eprintln, but nicely wraps lines. /// Like `writeln!`, but nicely wraps lines.
macro_rules! weprintln { ///
{} => { /// Unlike `writeln` and like `eprintln` panics if it can't write to
eprintln!(); /// the specified stream.
}; macro_rules! wwriteln {
{
stream=$stream: expr
} => {{
let stream: &mut dyn std::io::Write = $stream;
if let Err(err) = writeln!(stream) {
panic!("Error writing to output stream: {}", err);
}
}};
{ indent=$i: expr, $($arg: expr),* } => {{ {
stream=$stream: expr,
indent=$i: expr,
$($arg: expr),*
} => {{
let stream: &mut dyn std::io::Write = $stream;
let i = $i; let i = $i;
crate::output::wrapping::iwwriteln( crate::output::wrapping::iwwriteln(
&mut std::io::stderr(), stream,
i.as_ref(), i.as_ref(), i.as_ref(), i.as_ref(),
format_args!($($arg),*)) format_args!($($arg),*))
}}; }};
{ {
stream=$stream: expr,
initial_indent=$ii: expr, initial_indent=$ii: expr,
subsequent_indent=$si: expr, subsequent_indent=$si: expr,
$($arg: expr),* $($arg: expr),*
} => {{ } => {{
let stream: &mut dyn std::io::Write = $stream;
let ii = $ii; let ii = $ii;
let si = $si; let si = $si;
crate::output::wrapping::iwwriteln( crate::output::wrapping::iwwriteln(
&mut std::io::stderr(), stream,
ii.as_ref(), si.as_ref(), ii.as_ref(), si.as_ref(),
format_args!($($arg),*)) format_args!($($arg),*))
}}; }};
{ {
stream=$stream: expr,
initial_indent=$ii: expr, initial_indent=$ii: expr,
$($arg: expr),* $($arg: expr),*
} => {{ } => {{
let stream: &mut dyn std::io::Write = $stream;
let ii = $ii; let ii = $ii;
let si = format!("{:1$}", "", ii.len()); let si = format!("{:1$}", "", ii.len());
crate::output::wrapping::iwwriteln( crate::output::wrapping::iwwriteln(
&mut std::io::stderr(), stream, ii.as_ref(), si.as_ref(),
ii.as_ref(), si.as_ref(),
format_args!($($arg),*)) format_args!($($arg),*))
}}; }};
{ $($arg: expr),* } => { {
stream=$stream: expr,
$($arg: expr),*
} => {{
let stream: &mut dyn std::io::Write = $stream;
crate::output::wrapping::wwriteln( crate::output::wrapping::wwriteln(
&mut std::io::stderr(), format_args!($($arg),*)) stream, format_args!($($arg),*))
}};
{
$stream: expr,
$($arg: expr),*
} => {
wwriteln!(stream=$stream, $($arg),*)
};
{
$stream: expr
} => {
wwriteln!(stream=$stream)
};
}
/// Like eprintln, but nicely wraps lines.
macro_rules! weprintln {
{ } => {
wwriteln!(stream=&mut std::io::stderr())
};
{
indent=$i: expr,
$($arg: expr),*
} => {
wwriteln!(stream=&mut std::io::stderr(),
indent=$i,
$($arg),*)
};
{
initial_indent=$ii: expr,
subsequent_indent=$si: expr,
$($arg: expr),*
} => {
wwriteln!(stream=&mut std::io::stderr(),
initial_indent=$ii,
subsequent_indent=$si,
$($arg),*)
};
{
initial_indent=$ii: expr,
$($arg: expr),*
} => {
wwriteln!(stream=&mut std::io::stderr(),
initial_indent=$ii,
$($arg),*)
};
{
$($arg: expr),*
} => {
wwriteln!(stream=&mut std::io::stderr(), $($arg),*)
}; };
} }