on master help sign in
Add … when chars are truncated in picker
commit18beda38ac9dfec843dcbb51adf596e294fc13b2 authorGokul Soumya <gokulps15@gmail.com> dateThu 24 Jun 2021, 21:24 (5 years ago) parent10548bf
2 files changed+24−1
helix-term/src/ui/picker.rs+2−1 helix-tui/src/buffer.rs+22
helix-term/src/ui/picker.rs+2−1
@@ -289,7 +289,7 @@ impl<T: 'static> Component for Picker<T> {
289289 surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
290290 }
291291
292− surface.set_stringn(
292+ surface.set_string_truncated(
293293 inner.x + 3,
294294 inner.y + 2 + i as u16,
295295 (self.format_fn)(option),
@@ -299,6 +299,7 @@ impl<T: 'static> Component for Picker<T> {
299299 } else {
300300 style
301301 },
302+ true,
302303 );
303304 }
304305 }
helix-tui/src/buffer.rs+22
@@ -266,11 +266,30 @@ impl Buffer {
266266 width: usize,
267267 style: Style,
268268 ) -> (u16, u16)
269+ where
270+ S: AsRef<str>,
271+ {
272+ self.set_string_truncated(x, y, string, width, style, false)
273+ }
274+
275+ /// Print at most the first `width` characters of a string if enough space is available
276+ /// until the end of the line. If `markend` is true appends a `…` at the end of
277+ /// truncated lines.
278+ pub fn set_string_truncated<S>(
279+ &mut self,
280+ x: u16,
281+ y: u16,
282+ string: S,
283+ width: usize,
284+ style: Style,
285+ ellipsis: bool,
286+ ) -> (u16, u16)
269287 where
270288 S: AsRef<str>,
271289 {
272290 let mut index = self.index_of(x, y);
273291 let mut x_offset = x as usize;
292+ let width = if ellipsis { width - 1 } else { width };
274293 let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true);
275294 let max_offset = min(self.area.right() as usize, width.saturating_add(x as usize));
276295 for s in graphemes {
@@ -293,6 +312,9 @@ impl Buffer {
293312 index += width;
294313 x_offset += width;
295314 }
315+ if ellipsis && x_offset - (x as usize) < string.as_ref().chars().count() {
316+ self.content[index].set_symbol("…");
317+ }
296318 (x_offset as u16, y)
297319 }
298320