on master help sign in
Truncate paths in the file picker (#6410)
commite72be529968071abcd5fce1d9a06e8e2e2bbaacf authormWalrus <hellkvistoskar@protonmail.com> dateWed 22 Mar 2023, 15:38 (4 years ago) parenta863fd8
4 files changed+43−4
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+16−4
helix-term/src/ui/menu.rs+1
@@ -347,6 +347,7 @@ impl<T: Item + 'static> Component for Menu<T> {
347347 offset: scroll,
348348 selected: self.cursor,
349349 },
350+ false,
350351 );
351352
352353 if let Some(cursor) = self.cursor {
helix-term/src/ui/picker.rs+1
@@ -885,6 +885,7 @@ impl<T: Item + 'static> Component for Picker<T> {
885885 offset: 0,
886886 selected: Some(cursor),
887887 },
888+ self.truncate_start,
888889 );
889890 }
890891
helix-tui/src/buffer.rs+25
@@ -433,6 +433,31 @@ impl Buffer {
433433 (x_offset as u16, y)
434434 }
435435
436+ pub fn set_spans_truncated(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
437+ let mut remaining_width = width;
438+ let mut alt_x = x;
439+ let (text, styles) =
440+ spans
441+ .0
442+ .iter()
443+ .fold((String::new(), vec![]), |(mut s, mut h), span| {
444+ s.push_str(span.content.as_ref());
445+ let mut styles = span
446+ .styled_graphemes(span.style)
447+ .map(|grapheme| grapheme.style)
448+ .collect();
449+ h.append(&mut styles);
450+
451+ let w = span.width() as u16;
452+ alt_x = alt_x + w;
453+ remaining_width = remaining_width.saturating_sub(w);
454+
455+ (s, h)
456+ });
457+ self.set_string_truncated(x, y, &text, width.into(), |idx| styles[idx], true, true);
458+ (x, y)
459+ }
460+
436461 pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
437462 let mut remaining_width = width;
438463 let mut x = x;
helix-tui/src/widgets/table.rs+16−4
@@ -354,7 +354,13 @@ impl TableState {
354354impl<'a> Table<'a> {
355355 // type State = TableState;
356356
357− pub fn render_table(mut self, area: Rect, buf: &mut Buffer, state: &mut TableState) {
357+ pub fn render_table(
358+ mut self,
359+ area: Rect,
360+ buf: &mut Buffer,
361+ state: &mut TableState,
362+ truncate: bool,
363+ ) {
358364 if area.area() == 0 {
359365 return;
360366 }
@@ -401,6 +407,7 @@ impl<'a> Table<'a> {
401407 width: *width,
402408 height: max_header_height,
403409 },
410+ truncate,
404411 );
405412 col += *width + self.column_spacing;
406413 }
@@ -457,6 +464,7 @@ impl<'a> Table<'a> {
457464 width: *width,
458465 height: table_row.height,
459466 },
467+ truncate,
460468 );
461469 col += *width + self.column_spacing;
462470 }
@@ -464,20 +472,24 @@ impl<'a> Table<'a> {
464472 }
465473}
466474
467−fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) {
475+fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect, truncate: bool) {
468476 buf.set_style(area, cell.style);
469477 for (i, spans) in cell.content.lines.iter().enumerate() {
470478 if i as u16 >= area.height {
471479 break;
472480 }
473− buf.set_spans(area.x, area.y + i as u16, spans, area.width);
481+ if truncate {
482+ buf.set_spans_truncated(area.x, area.y + i as u16, spans, area.width);
483+ } else {
484+ buf.set_spans(area.x, area.y + i as u16, spans, area.width);
485+ }
474486 }
475487}
476488
477489impl<'a> Widget for Table<'a> {
478490 fn render(self, area: Rect, buf: &mut Buffer) {
479491 let mut state = TableState::default();
480− Table::render_table(self, area, buf, &mut state);
492+ Table::render_table(self, area, buf, &mut state, false);
481493 }
482494}
483495