From 46a807eee1ae2c7e91681d6c75418d4556e78688 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 7 Dec 2019 01:36:05 +0200 Subject: [PATCH] melib: remove control characters from subject --- melib/src/email.rs | 10 +++++++++- ui/src/components/mail/view.rs | 32 ------------------------------ ui/src/terminal/cells.rs | 36 ---------------------------------- 3 files changed, 9 insertions(+), 69 deletions(-) diff --git a/melib/src/email.rs b/melib/src/email.rs index 3ede0a60..b8d089e4 100644 --- a/melib/src/email.rs +++ b/melib/src/email.rs @@ -467,7 +467,15 @@ impl Envelope { }; self.in_reply_to = Some(MessageID::new(new_val, slice)); } - pub fn set_subject(&mut self, new_val: Vec) { + pub fn set_subject(&mut self, mut new_val: Vec) { + while new_val + .last() + .map(|&u| char::is_control(u as char)) + .unwrap_or(false) + { + new_val.pop(); + } + self.subject = Some(new_val); } pub fn set_message_id(&mut self, new_val: &[u8]) { diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index 51af4a83..657c268a 100644 --- a/ui/src/components/mail/view.rs +++ b/ui/src/components/mail/view.rs @@ -265,38 +265,6 @@ impl MailView { } } } - pub fn plain_text_to_buf(s: &str, highlight_urls: bool) -> CellBuffer { - let mut buf = CellBuffer::from(s); - - if highlight_urls { - let lines: Vec<&str> = s.split('\n').map(|l| l.trim_end()).collect(); - let mut shift = 0; - let mut lidx_total = 0; - let finder = LinkFinder::new(); - for r in &lines { - for l in finder.links(&r) { - let offset = if lidx_total < 10 { - 3 - } else if lidx_total < 100 { - 4 - } else if lidx_total < 1000 { - 5 - } else { - panic!("BUG: Message body with more than 100 urls"); - }; - for i in 1..=offset { - buf[(l.start() + shift - i, 0)].set_fg(Color::Byte(226)); - //buf[(l.start() + shift - 2, 0)].set_fg(Color::Byte(226)); - //buf[(l.start() + shift - 3, 0)].set_fg(Color::Byte(226)); - } - lidx_total += 1; - } - // Each Cell represents one char so next line will be: - shift += r.chars().count() + 1; - } - } - buf - } pub fn update(&mut self, new_coordinates: (usize, usize, EnvelopeHash)) { self.coordinates = new_coordinates; diff --git a/ui/src/terminal/cells.rs b/ui/src/terminal/cells.rs index 8bbf47ac..042a2fcc 100644 --- a/ui/src/terminal/cells.rs +++ b/ui/src/terminal/cells.rs @@ -138,24 +138,6 @@ impl CellBuffer { self.rows = newrows; } - pub fn split_newlines(self) -> Self { - let lines: Vec<&[Cell]> = self.split(|cell| cell.ch() == '\n').collect(); - let height = lines.len(); - let width = lines.iter().map(|l| l.len()).max().unwrap_or(0) + 1; - let mut content = CellBuffer::new(width, height, Cell::with_char(' ')); - { - let mut x; - let c_slice: &mut [Cell] = &mut content; - for (y, l) in lines.iter().enumerate() { - let y_r = y * width; - x = l.len() + y_r; - c_slice[y_r..x].copy_from_slice(l); - c_slice[x].set_ch('\n'); - } - } - content - } - pub fn is_empty(&self) -> bool { self.buf.is_empty() } @@ -422,24 +404,6 @@ impl Default for CellBuffer { } } -impl<'a> From<&'a str> for CellBuffer { - fn from(s: &'a str) -> Self { - let lines: Vec<&str> = s.lines().map(|l| l.trim_end()).collect(); - let len = s.len() + lines.len(); - let mut buf = CellBuffer::new(len, 1, Cell::default()); - let mut x = 0; - for l in &lines { - for (idx, c) in l.chars().enumerate() { - buf[(x + idx, 0)].set_ch(c); - } - x += l.chars().count(); - buf[(x, 0)].set_ch('\n'); - x += 1; - } - buf - } -} - impl fmt::Display for CellBuffer { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { '_y: for y in 0..self.rows {