aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-05-03 23:49:41 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-05-03 23:51:02 +0200
commit13cdb0218c4ba9a0b531f41e49964704932d2510 (patch)
treec7ebd0350f390f774c61928dbd418c032dd0a0dc /src
parent4d102e069e4a0b6064a79915d368773338dbbb8e (diff)
downloadmail-13cdb0218c4ba9a0b531f41e49964704932d2510.tar.gz
truncate long strings
Diffstat (limited to 'src')
-rw-r--r--src/window.rs14
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),
)?,
}