mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-26 15:14:13 +02:00
refactor: add fast branch if the string is short enough to not be truncated (#1333)
* refactor: add fast branch if the entire string is definitely not truncated * update comments
This commit is contained in:
parent
a93521d2b1
commit
94e4573ebc
@ -101,21 +101,26 @@ fn grapheme_width(g: &str) -> usize {
|
|||||||
fn truncate_str<U: Into<usize>>(content: &str, width: U) -> String {
|
fn truncate_str<U: Into<usize>>(content: &str, width: U) -> String {
|
||||||
let width = width.into();
|
let width = width.into();
|
||||||
|
|
||||||
if width > 0 {
|
|
||||||
if content.is_ascii() {
|
|
||||||
// If the entire string is ASCII, we can use a much simpler approach.
|
|
||||||
|
|
||||||
if content.len() <= width {
|
if content.len() <= width {
|
||||||
|
// If the entire string fits in the width, then we just
|
||||||
|
// need to copy the entire string over.
|
||||||
|
|
||||||
content.to_owned()
|
content.to_owned()
|
||||||
} else {
|
} else if width > 0 {
|
||||||
|
if content.is_ascii() {
|
||||||
|
// If the entire string is ASCII, we can use a much simpler approach
|
||||||
|
// in regards to what we truncate.
|
||||||
|
|
||||||
let mut text = String::with_capacity(width);
|
let mut text = String::with_capacity(width);
|
||||||
let (keep, _throw) = content.split_at(width - 1);
|
let (keep, _throw) = content.split_at(width - 1);
|
||||||
text.push_str(keep);
|
text.push_str(keep);
|
||||||
text.push('…');
|
text.push('…');
|
||||||
|
|
||||||
text
|
text
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
// Otherwise iterate by grapheme and greedily fit as many graphemes
|
||||||
|
// as width will allow.
|
||||||
|
|
||||||
let mut text = String::with_capacity(width);
|
let mut text = String::with_capacity(width);
|
||||||
let mut curr_width = 0;
|
let mut curr_width = 0;
|
||||||
let mut early_break = false;
|
let mut early_break = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user