on master help sign in
Performant and correct set_spans_truncated
67783ddPascal Kuthe
helix-tui/src/buffer.rs+38−22
@@ -434,28 +434,44 @@ impl Buffer {
434 }
434 }
435
435
436 pub fn set_spans_truncated(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
436 pub fn set_spans_truncated(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
437− let mut remaining_width = width;
437+ // prevent panic if out of range
438− let mut alt_x = x;
438+ if !self.in_bounds(x, y) || width == 0 {
439− let (text, styles) =
439+ return (x, y);
440− spans
440+ }
441− .0
441+
442− .iter()
442+ let mut x_offset = x as usize;
443− .fold((String::new(), vec![]), |(mut s, mut h), span| {
443+ let max_offset = min(self.area.right(), width.saturating_add(x));
444− s.push_str(span.content.as_ref());
444+ let mut start_index = self.index_of(x, y);
445− let mut styles = span
445+ let mut index = self.index_of(max_offset as u16, y);
446− .styled_graphemes(span.style)
446+
447− .map(|grapheme| grapheme.style)
447+ let content_width = spans.width();
448− .collect();
448+ let truncated = content_width > width as usize;
449− h.append(&mut styles);
449+ if truncated {
450−
450+ self.content[start_index].set_symbol("…");
451+ start_index += 1;
452+ } else {
453+ index -= width as usize - content_width;
454+ }
455+ for span in spans.0.iter().rev() {
456+ for s in span.content.graphemes(true).rev() {
451− let w = span.width() as u16;
457+ let width = s.width();
452− alt_x = alt_x + w;
458+ if width == 0 {
453− remaining_width = remaining_width.saturating_sub(w);
459+ continue;
454−
460+ }
455− (s, h)
461+ let start = index - width;
462+ if start < start_index {
463+ break;
464+ }
465+ self.content[start].set_symbol(s);
466+ self.content[start].set_style(span.style);
467+ for i in start + 1..index {
468+ self.content[i].reset();
456− });
469+ }
457− self.set_string_truncated(x, y, &text, width.into(), |idx| styles[idx], true, true);
470+ index -= width;
458− (x, y)
471+ x_offset += width;
472+ }
473+ }
474+ (x_offset as u16, y)
459 }
475 }
460
476
461 pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {
477 pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans, width: u16) -> (u16, u16) {