Bounding Box #531
Replies: 1 comment 2 replies
-
Hi @daniel-wong-dfinity-org, thanks for writing!
If I understand you correctly, then you're looking for something similar to this: fn bound(s: &str, max_width: usize, max_height: usize) -> String {
let mut lines = Vec::new();
let options = textwrap::Options::new(max_width.saturating_sub(3))
.wrap_algorithm(textwrap::wrap_algorithms::WrapAlgorithm::FirstFit);
for line in s.lines() {
if textwrap::core::display_width(line) <= max_width {
lines.push(String::from(line));
} else {
lines.push(format!("{}...", textwrap::wrap(line, &options)[0]));
}
}
if lines.len() > max_height {
format!(
"{}\n...\n{}",
&lines[..max_height / 2].join("\n"),
&lines[lines.len() - max_height / 2..].join("\n")
)
} else {
lines.join("\n")
}
} This is quite brute-force: it wraps the entire long line just to extract the first of the wrapped lines. I don't think this fits super well in Textwrap: it's not so much about wrapping as it is about truncating. I would prefer if this could go into a different tiny crate for "error formatting" or similar. That way Textwrap can stay focused on being a toolbox with primitives that others can use for building more expressive UIs. |
Beta Was this translation helpful? Give feedback.
-
I would like a function like this:
If s is small, a copy gets returned. But if it has too many lines, the middle ones get replaced with "...". Similarly, if a line within s is too long, the right portion of it gets replaced with "...". Would you accept a pull request for this?
Use Case
Something goes wrong, so I want to log it. The log message should contain breadcrumbs. Often, the breadcrumb information is one or more object(s). But the object(s) could be big, and I do not want overwhelmingly large log messages. So, I would like to do this:
Beta Was this translation helpful? Give feedback.
All reactions