Truncate paths in the file picker (#6410)

This commit is contained in:
mWalrus 2023-03-22 15:38:34 +01:00 committed by Blaž Hrastnik
parent a863fd89e1
commit e72be52996
No known key found for this signature in database
GPG key ID: 1238B9C4AD889640
4 changed files with 43 additions and 4 deletions

View file

@ -347,6 +347,7 @@ impl<T: Item + 'static> Component for Menu<T> {
offset: scroll,
selected: self.cursor,
},
false,
);
if let Some(cursor) = self.cursor {

View file

@ -885,6 +885,7 @@ impl<T: Item + 'static> Component for Picker<T> {
offset: 0,
selected: Some(cursor),
},
self.truncate_start,
);
}

View file

@ -433,6 +433,31 @@ impl Buffer {
(x_offset as u16, y)
}
pub fn set_spans_truncated(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
let mut remaining_width = width;
let mut alt_x = x;
let (text, styles) =
spans
.0
.iter()
.fold((String::new(), vec![]), |(mut s, mut h), span| {
s.push_str(span.content.as_ref());
let mut styles = span
.styled_graphemes(span.style)
.map(|grapheme| grapheme.style)
.collect();
h.append(&mut styles);
let w = span.width() as u16;
alt_x = alt_x + w;
remaining_width = remaining_width.saturating_sub(w);
(s, h)
});
self.set_string_truncated(x, y, &text, width.into(), |idx| styles[idx], true, true);
(x, y)
}
pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
let mut remaining_width = width;
let mut x = x;

View file

@ -354,7 +354,13 @@ impl TableState {
impl<'a> Table<'a> {
// type State = TableState;
pub fn render_table(mut self, area: Rect, buf: &mut Buffer, state: &mut TableState) {
pub fn render_table(
mut self,
area: Rect,
buf: &mut Buffer,
state: &mut TableState,
truncate: bool,
) {
if area.area() == 0 {
return;
}
@ -401,6 +407,7 @@ impl<'a> Table<'a> {
width: *width,
height: max_header_height,
},
truncate,
);
col += *width + self.column_spacing;
}
@ -457,6 +464,7 @@ impl<'a> Table<'a> {
width: *width,
height: table_row.height,
},
truncate,
);
col += *width + self.column_spacing;
}
@ -464,20 +472,24 @@ impl<'a> Table<'a> {
}
}
fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) {
fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect, truncate: bool) {
buf.set_style(area, cell.style);
for (i, spans) in cell.content.lines.iter().enumerate() {
if i as u16 >= area.height {
break;
}
buf.set_spans(area.x, area.y + i as u16, spans, area.width);
if truncate {
buf.set_spans_truncated(area.x, area.y + i as u16, spans, area.width);
} else {
buf.set_spans(area.x, area.y + i as u16, spans, area.width);
}
}
}
impl<'a> Widget for Table<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut state = TableState::default();
Table::render_table(self, area, buf, &mut state);
Table::render_table(self, area, buf, &mut state, false);
}
}