From c1f0733e3df2d563fe0ff705b5a972f3a910f4c5 Mon Sep 17 00:00:00 2001 From: mWalrus Date: Wed, 22 Mar 2023 15:38:34 +0100 Subject: [PATCH] Truncate paths in the file picker (#6410) --- helix-term/src/ui/menu.rs | 1 + helix-term/src/ui/picker.rs | 1 + helix-tui/src/buffer.rs | 25 +++++++++++++++++++++ helix-tui/src/widgets/table.rs | 41 ++++++++++++++++++++++++---------- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 30625acee60cb..bdad2e4083929 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -347,6 +347,7 @@ impl Component for Menu { offset: scroll, selected: self.cursor, }, + false, ); if let Some(cursor) = self.cursor { diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 3294a2a1d23a8..e73088e528faf 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -885,6 +885,7 @@ impl Component for Picker { offset: 0, selected: Some(cursor), }, + self.truncate_start, ); } diff --git a/helix-tui/src/buffer.rs b/helix-tui/src/buffer.rs index b1fd44787f78b..2c212b125c3a8 100644 --- a/helix-tui/src/buffer.rs +++ b/helix-tui/src/buffer.rs @@ -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; diff --git a/helix-tui/src/widgets/table.rs b/helix-tui/src/widgets/table.rs index 400f65e0ad98a..164d9146300f8 100644 --- a/helix-tui/src/widgets/table.rs +++ b/helix-tui/src/widgets/table.rs @@ -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_rows: bool, + ) { if area.area() == 0 { return; } @@ -448,22 +454,33 @@ impl<'a> Table<'a> { if is_selected { buf.set_style(table_row_area, self.highlight_style); } - render_cell( - buf, - cell, - Rect { - x: col, - y: row, - width: *width, - height: table_row.height, - }, - ); + let rect = Rect { + x: col, + y: row, + width: *width, + height: table_row.height, + }; + if truncate_rows { + render_cell_truncated(buf, cell, rect); + } else { + render_cell(buf, cell, rect); + } col += *width + self.column_spacing; } } } } +fn render_cell_truncated(buf: &mut Buffer, cell: &Cell, area: Rect) { + 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_truncated(area.x, area.y + i as u16, spans, area.width); + } +} + fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) { buf.set_style(area, cell.style); for (i, spans) in cell.content.lines.iter().enumerate() { @@ -477,7 +494,7 @@ fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) { 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); } }