-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow truncation of table row text #6410
Changes from all commits
adb12ad
772fd46
978e1a4
b23024a
4992429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}, | ||
false, | ||
); | ||
col += *width + self.column_spacing; | ||
} | ||
|
@@ -457,27 +464,32 @@ impl<'a> Table<'a> { | |
width: *width, | ||
height: table_row.height, | ||
}, | ||
truncate, | ||
); | ||
col += *width + self.column_spacing; | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 cell.content.width() > 1 && truncate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think the emptyness check is required here. Why did you add it? In any case it should be using byte Len (or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this check to not truncate table cells that have a content length of 1. If we don't have this check and try to render the buffer picker you will see that the first two cells (the entry number and the you-are-here indicator) are being truncated as well since my current implementation doesn't distinguish between cells but applies to the entire row. With that said, there are probably better ways of doing this, this was just the fix I could come up with without diving too deep into it. |
||
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); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performing grapheme segmentation here is both slow and incorrect. The
idx
used below is not a grapheme index but a byte index. Instead you should create an iterator that is transversed whenever the current style range is exhausted.The current implementation can easily lead to crashes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured this was a slow solution.
Could you elaborate a bit about what you mean and how I could go about correcting this? I'm pretty new to this kind of thing.