on master help sign in
Truncate paths in the file picker (#6410)
e72be52mWalrus
helix-term/src/ui/menu.rs+1
@@ -347,6 +347,7 @@ impl<T: Item + 'static> Component for Menu<T> {
347 offset: scroll,
347 offset: scroll,
348 selected: self.cursor,
348 selected: self.cursor,
349 },
349 },
350+ false,
350 );
351 );
351
352
352 if let Some(cursor) = self.cursor {
353 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> {
885 offset: 0,
885 offset: 0,
886 selected: Some(cursor),
886 selected: Some(cursor),
887 },
887 },
888+ self.truncate_start,
888 );
889 );
889 }
890 }
890
891
helix-tui/src/buffer.rs+25
@@ -433,6 +433,31 @@ impl Buffer {
433 (x_offset as u16, y)
433 (x_offset as u16, y)
434 }
434 }
435
435
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+
436 pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
461 pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
437 let mut remaining_width = width;
462 let mut remaining_width = width;
438 let mut x = x;
463 let mut x = x;
helix-tui/src/widgets/table.rs+16−4
@@ -354,7 +354,13 @@ impl TableState {
354impl<'a> Table<'a> {
354impl<'a> Table<'a> {
355 // type State = TableState;
355 // type State = TableState;
356
356
357+ pub fn render_table(
358+ mut self,
359+ area: Rect,
360+ buf: &mut Buffer,
357− pub fn render_table(mut self, area: Rect, buf: &mut Buffer, state: &mut TableState) {
361+ state: &mut TableState,
362+ truncate: bool,
363+ ) {
358 if area.area() == 0 {
364 if area.area() == 0 {
359 return;
365 return;
360 }
366 }
@@ -401,6 +407,7 @@ impl<'a> Table<'a> {
401 width: *width,
407 width: *width,
402 height: max_header_height,
408 height: max_header_height,
403 },
409 },
410+ truncate,
404 );
411 );
405 col += *width + self.column_spacing;
412 col += *width + self.column_spacing;
406 }
413 }
@@ -457,6 +464,7 @@ impl<'a> Table<'a> {
457 width: *width,
464 width: *width,
458 height: table_row.height,
465 height: table_row.height,
459 },
466 },
467+ truncate,
460 );
468 );
461 col += *width + self.column_spacing;
469 col += *width + self.column_spacing;
462 }
470 }
@@ -464,20 +472,24 @@ impl<'a> Table<'a> {
464 }
472 }
465}
473}
466
474
467−fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) {
475+fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect, truncate: bool) {
468 buf.set_style(area, cell.style);
476 buf.set_style(area, cell.style);
469 for (i, spans) in cell.content.lines.iter().enumerate() {
477 for (i, spans) in cell.content.lines.iter().enumerate() {
470 if i as u16 >= area.height {
478 if i as u16 >= area.height {
471 break;
479 break;
472 }
480 }
481+ if truncate {
482+ buf.set_spans_truncated(area.x, area.y + i as u16, spans, area.width);
483+ } else {
473− buf.set_spans(area.x, area.y + i as u16, spans, area.width);
484+ buf.set_spans(area.x, area.y + i as u16, spans, area.width);
485+ }
474 }
486 }
475}
487}
476
488
477impl<'a> Widget for Table<'a> {
489impl<'a> Widget for Table<'a> {
478 fn render(self, area: Rect, buf: &mut Buffer) {
490 fn render(self, area: Rect, buf: &mut Buffer) {
479 let mut state = TableState::default();
491 let mut state = TableState::default();
480− Table::render_table(self, area, buf, &mut state);
492+ Table::render_table(self, area, buf, &mut state, false);
481 }
493 }
482}
494}
483
495