2023-04-26 18:56:10 +00:00
|
|
|
use tabled::{
|
|
|
|
builder::Builder,
|
|
|
|
grid::util::string::string_width_multiline,
|
|
|
|
settings::{width::Truncate, Modify, Padding, Style, Width},
|
|
|
|
};
|
2022-12-15 14:47:04 +00:00
|
|
|
|
|
|
|
pub fn string_width(text: &str) -> usize {
|
2023-04-26 18:56:10 +00:00
|
|
|
string_width_multiline(text)
|
2022-12-15 14:47:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 13:44:34 +00:00
|
|
|
pub fn string_wrap(text: &str, width: usize, keep_words: bool) -> String {
|
2022-12-15 14:47:04 +00:00
|
|
|
// todo: change me...
|
|
|
|
//
|
2023-01-15 02:03:32 +00:00
|
|
|
// well... it's not efficient to build a table to wrap a string,
|
2022-12-15 14:47:04 +00:00
|
|
|
// but ... it's better than a copy paste (is it?)
|
|
|
|
|
|
|
|
if text.is_empty() {
|
|
|
|
return String::new();
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:44:34 +00:00
|
|
|
let wrap = if keep_words {
|
|
|
|
Width::wrap(width).keep_words()
|
|
|
|
} else {
|
|
|
|
Width::wrap(width)
|
|
|
|
};
|
|
|
|
|
2022-12-15 14:47:04 +00:00
|
|
|
Builder::from_iter([[text]])
|
|
|
|
.build()
|
|
|
|
.with(Style::empty())
|
2022-12-25 00:27:34 +00:00
|
|
|
.with(Padding::zero())
|
2023-04-26 18:56:10 +00:00
|
|
|
.with(Modify::new((0, 0)).with(wrap))
|
2022-12-15 14:47:04 +00:00
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string_truncate(text: &str, width: usize) -> String {
|
|
|
|
// todo: change me...
|
|
|
|
|
|
|
|
let line = match text.lines().next() {
|
|
|
|
Some(first_line) => first_line,
|
|
|
|
None => return String::new(),
|
|
|
|
};
|
|
|
|
|
2023-04-26 18:56:10 +00:00
|
|
|
Truncate::truncate_text(line, width).into_owned()
|
2022-12-15 14:47:04 +00:00
|
|
|
}
|