on master help sign in
Add … when chars are truncated in picker
18beda3Gokul Soumya
helix-term/src/ui/picker.rs+2−1
@@ -289,7 +289,7 @@ impl<T: 'static> Component for Picker<T> {
289 surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
289 surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
290 }
290 }
291
291
292− surface.set_stringn(
292+ surface.set_string_truncated(
293 inner.x + 3,
293 inner.x + 3,
294 inner.y + 2 + i as u16,
294 inner.y + 2 + i as u16,
295 (self.format_fn)(option),
295 (self.format_fn)(option),
@@ -299,6 +299,7 @@ impl<T: 'static> Component for Picker<T> {
299 } else {
299 } else {
300 style
300 style
301 },
301 },
302+ true,
302 );
303 );
303 }
304 }
304 }
305 }
helix-tui/src/buffer.rs+22
@@ -266,11 +266,30 @@ impl Buffer {
266 width: usize,
266 width: usize,
267 style: Style,
267 style: Style,
268 ) -> (u16, u16)
268 ) -> (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)
269 where
287 where
270 S: AsRef<str>,
288 S: AsRef<str>,
271 {
289 {
272 let mut index = self.index_of(x, y);
290 let mut index = self.index_of(x, y);
273 let mut x_offset = x as usize;
291 let mut x_offset = x as usize;
292+ let width = if ellipsis { width - 1 } else { width };
274 let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true);
293 let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true);
275 let max_offset = min(self.area.right() as usize, width.saturating_add(x as usize));
294 let max_offset = min(self.area.right() as usize, width.saturating_add(x as usize));
276 for s in graphemes {
295 for s in graphemes {
@@ -293,6 +312,9 @@ impl Buffer {
293 index += width;
312 index += width;
294 x_offset += width;
313 x_offset += width;
295 }
314 }
315+ if ellipsis && x_offset - (x as usize) < string.as_ref().chars().count() {
316+ self.content[index].set_symbol("…");
317+ }
296 (x_offset as u16, y)
318 (x_offset as u16, y)
297 }
319 }
298
320