Truncate the starts of file paths instead of the ends in picker (#951)

* Truncate the starts of file paths in picker

* Simplify the truncate implementation

* Break loop at appropriate point

* Fix alignment and ellipsis presence

* Remove extraneous usage of `x_offset`

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
This commit is contained in:
Omnikar 2021-11-03 23:24:05 -04:00 committed by GitHub
parent e39cfa40df
commit 5b5d1b9dff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 23 deletions

View file

@ -483,6 +483,7 @@ impl<T: 'static> Component for Picker<T> {
text_style text_style
}, },
true, true,
true,
); );
} }
} }

View file

@ -266,12 +266,14 @@ impl Buffer {
where where
S: AsRef<str>, S: AsRef<str>,
{ {
self.set_string_truncated(x, y, string, width, style, false) self.set_string_truncated(x, y, string, width, style, false, false)
} }
/// Print at most the first `width` characters of a string if enough space is available /// Print at most the first `width` characters of a string if enough space is available
/// until the end of the line. If `markend` is true appends a `…` at the end of /// until the end of the line. If `ellipsis` is true appends a `…` at the end of
/// truncated lines. /// truncated lines. If `truncate_start` is `true`, truncate the beginning of the string
/// instead of the end.
#[allow(clippy::too_many_arguments)]
pub fn set_string_truncated<S>( pub fn set_string_truncated<S>(
&mut self, &mut self,
x: u16, x: u16,
@ -280,6 +282,7 @@ impl Buffer {
width: usize, width: usize,
style: Style, style: Style,
ellipsis: bool, ellipsis: bool,
truncate_start: bool,
) -> (u16, u16) ) -> (u16, u16)
where where
S: AsRef<str>, S: AsRef<str>,
@ -289,6 +292,7 @@ impl Buffer {
let width = if ellipsis { width - 1 } else { width }; let width = if ellipsis { width - 1 } else { width };
let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true); let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true);
let max_offset = min(self.area.right() as usize, width.saturating_add(x as usize)); let max_offset = min(self.area.right() as usize, width.saturating_add(x as usize));
if !truncate_start {
for s in graphemes { for s in graphemes {
let width = s.width(); let width = s.width();
if width == 0 { if width == 0 {
@ -312,6 +316,36 @@ impl Buffer {
if ellipsis && x_offset - (x as usize) < string.as_ref().width() { if ellipsis && x_offset - (x as usize) < string.as_ref().width() {
self.content[index].set_symbol(""); self.content[index].set_symbol("");
} }
} else {
let mut start_index = self.index_of(x, y);
let mut index = self.index_of(max_offset as u16, y);
let total_width = string.as_ref().width();
let truncated = total_width > width;
if ellipsis && truncated {
self.content[start_index].set_symbol("");
start_index += 1;
}
if !truncated {
index -= width - total_width;
}
for s in graphemes.rev() {
let width = s.width();
if width == 0 {
continue;
}
let start = index - width;
if start < start_index {
break;
}
self.content[start].set_symbol(s);
self.content[start].set_style(style);
for i in start + 1..index {
self.content[i].reset();
}
index -= width;
}
}
(x_offset as u16, y) (x_offset as u16, y)
} }