forked from extern/nushell
b499e7c682
close #7591 I tend to think it must be addressed. But I'd verify it @rgwood. PS: I've noticed how `table -e` and `table` with the same width wraps a bit differently sometimes. (I guess it also must be addressed......) Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
use tabled::{builder::Builder, object::Cell, Modify, Padding, Style, Width};
|
|
|
|
pub fn string_width(text: &str) -> usize {
|
|
tabled::papergrid::util::string_width_multiline_tab(text, 4)
|
|
}
|
|
|
|
pub fn wrap_string(text: &str, width: usize) -> String {
|
|
// todo: change me...
|
|
//
|
|
// well... it's not effitient to build a table to wrap a string,
|
|
// but ... it's better than a copy paste (is it?)
|
|
|
|
if text.is_empty() {
|
|
return String::new();
|
|
}
|
|
|
|
Builder::from_iter([[text]])
|
|
.build()
|
|
.with(Style::empty())
|
|
.with(Padding::zero())
|
|
.with(Modify::new(Cell(0, 0)).with(Width::wrap(width)))
|
|
.to_string()
|
|
.trim_end()
|
|
.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(),
|
|
};
|
|
|
|
Builder::from_iter([[line]])
|
|
.build()
|
|
.with(Style::empty())
|
|
.with(Padding::zero())
|
|
.with(Width::truncate(width))
|
|
.to_string()
|
|
}
|
|
|
|
pub fn string_wrap(text: &str, width: usize) -> String {
|
|
// todo: change me...
|
|
|
|
if text.is_empty() {
|
|
return String::new();
|
|
}
|
|
|
|
Builder::from_iter([[text]])
|
|
.build()
|
|
.with(Style::empty())
|
|
.with(Padding::zero())
|
|
.with(Width::wrap(width))
|
|
.to_string()
|
|
}
|