diff options
Diffstat (limited to 'src/window.rs')
| -rw-r--r-- | src/window.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/window.rs b/src/window.rs index 3714c4c..a2f2b68 100644 --- a/src/window.rs +++ b/src/window.rs @@ -2,6 +2,7 @@ use std::io::Write; use termion::{color, cursor}; +use unicode_segmentation::UnicodeSegmentation; #[derive(Clone, Copy)] pub struct Area { @@ -16,6 +17,15 @@ pub enum Line { Highlight(String), } +pub fn truncate_dots(s: &str, to: usize) -> String { + let graphemes = s.graphemes(true).collect::<Vec<_>>(); + if graphemes.len() >= to { + format!("{}...", graphemes.iter().take(to - 3).fold(String::new(), |acc, s| format!("{}{}", acc, s))) + } else { + graphemes.join("") + } +} + pub struct Window { pub lines: Vec<Line>, scroll: usize, @@ -37,12 +47,12 @@ impl Window { Some(line) => { write!(out, "{}", cursor::Goto(area.x, y))?; match line { - Line::Normal(s) => write!(out, "{}", s)?, + Line::Normal(s) => write!(out, "{}", truncate_dots(s, area.w as usize))?, Line::Highlight(s) => write!( out, "{}{}{}", color::Fg(color::Red), - s, + truncate_dots(s, area.w as usize), color::Fg(color::Reset), )?, } |
