| 1 |
//! Line wrapping human-readable output. |
| 2 |
|
| 3 |
use std::fmt; |
| 4 |
use std::io::Write; |
| 5 |
use std::sync::OnceLock; |
| 6 |
|
| 7 |
/// A non-breaking space. |
| 8 |
pub const NBSP: char = '\u{00A0}'; |
| 9 |
|
| 10 |
/// Prints the given message to the specified stream. |
| 11 |
/// |
| 12 |
/// Hint: Use `weprintln!(..)` instead of invoking this function |
| 13 |
/// directly. |
| 14 |
/// |
| 15 |
/// Like [`eprintln!`], panics if writing to the stream fails. |
| 16 |
/// |
| 17 |
/// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html |
| 18 |
pub fn wwriteln(stream: &mut dyn Write, msg: fmt::Arguments) { |
| 19 |
let m = format!("{}", msg); |
| 20 |
for l in textwrap::wrap(&m, options()) { |
| 21 |
if let Err(err) = writeln!(stream, "{}", l) { |
| 22 |
panic!("Writing to output: {}", err); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/// Prints the given message to the specified stream, indenting |
| 28 |
/// continuations. |
| 29 |
/// |
| 30 |
/// Hint: Use `weprintln!(indent="...", ..)` or |
| 31 |
/// `weprintln!(initial_indent="...", subsequent_indent="...", ..)` |
| 32 |
/// instead of invoking this function directly. |
| 33 |
/// |
| 34 |
/// Like [`eprintln!`], panics if writing to the stream fails. |
| 35 |
/// |
| 36 |
/// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html |
| 37 |
pub fn iwwriteln(stream: &mut dyn Write, |
| 38 |
initial_indent: &str, |
| 39 |
subsequent_indent: &str, |
| 40 |
msg: fmt::Arguments) { |
| 41 |
let m = format!("{}", msg); |
| 42 |
for l in textwrap::wrap(&m, |
| 43 |
options() |
| 44 |
.initial_indent(initial_indent) |
| 45 |
.subsequent_indent(subsequent_indent)) { |
| 46 |
if let Err(err) = writeln!(stream, "{}", l) { |
| 47 |
panic!("Writing to output: {}", err); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// Returns options for text-wrapping. |
| 53 |
fn options() -> textwrap::Options<'static> { |
| 54 |
static OPTIONS: OnceLock<textwrap::Options> = OnceLock::new(); |
| 55 |
OPTIONS.get_or_init(|| { |
| 56 |
// It is better to use terminal_size instead of letting |
| 57 |
// textwrap do it, because textwrap uses an older version, |
| 58 |
// leading to duplicate crates. |
| 59 |
textwrap::Options::new(stderr_terminal_width()) |
| 60 |
// Using a more naive separation algorithm keeps URLs |
| 61 |
// together, whereas the unicode line breaking algorithm would |
| 62 |
// break after e.g. `hkps://`. |
| 63 |
.word_separator(textwrap::WordSeparator::AsciiSpace) |
| 64 |
}).clone() |
| 65 |
} |
| 66 |
|
| 67 |
/// Returns the terminal width we assume for wrapping. |
| 68 |
pub fn stderr_terminal_width() -> usize { |
| 69 |
// XXX: For compatibility with terminal_size 0.2 and 0.3. Once we |
| 70 |
// depend on 0.4, use terminal_size_of instead. |
| 71 |
platform! { |
| 72 |
unix => { |
| 73 |
use std::os::fd::AsRawFd; |
| 74 |
#[allow(deprecated)] |
| 75 |
unsafe { |
| 76 |
terminal_size::terminal_size_using_fd(std::io::stderr().as_raw_fd()) |
| 77 |
} |
| 78 |
}, |
| 79 |
windows => { |
| 80 |
terminal_size::terminal_size() |
| 81 |
}, |
| 82 |
} |
| 83 |
.map(|(w, _h)| w.0) |
| 84 |
.map(Into::into) |
| 85 |
.unwrap_or(usize::MAX) |
| 86 |
} |