From 461aa38a7abbae783ed130db27b71c026df971f7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 2 Dec 2017 21:40:41 -0800 Subject: track dirty bit to know if we should redraw display also clean up a bunch of TODOs --- src/display/mod.rs | 125 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 97 insertions(+), 28 deletions(-) (limited to 'src/display/mod.rs') diff --git a/src/display/mod.rs b/src/display/mod.rs index c3c4692..04b200f 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -35,14 +35,15 @@ pub enum Infos { const COMPOSE_HEIGHT: u16 = 5; pub struct DisplayInfo { - pub log_height: u16, - pub prompt_height: u16, - pub mode: Option, - pub log_seek: u32, - pub infos_seek: u32, - pub log: Vec, - pub infos: Vec, - pub input_buf: Vec + log_height: u16, + prompt_height: u16, + mode: Option, + log_seek: u32, + infos_seek: u32, + log: Vec, + infos: Vec, + input_buf: Vec, + dirty: bool } impl Default for DisplayInfo { @@ -55,18 +56,91 @@ impl Default for DisplayInfo { infos_seek: 0, log: Vec::new(), infos: Vec::new(), - input_buf: Vec::new() + input_buf: Vec::new(), + dirty: false } } } impl DisplayInfo { + pub fn set_mode(&mut self, new_mode: Option) { + self.mode = new_mode; + self.dirty = true; + } + + pub fn get_mode(&self) -> &Option { + &self.mode + } + + pub fn adjust_infos_seek(&mut self, seek_adjust: Option) { + self.infos_seek = match seek_adjust { + Some(adjust) => { + /* + * So this might be weird to read. + * + * I don't know how to add an i32 in a saturating manner to a u32. + * + * That's what this does: + * if adjust is negative, negate to positive and saturating_sub it + * if adjust is positive, we can just saturating_add it + */ + if adjust < 0 { + self.infos_seek.saturating_sub(-adjust as u32) + } else { + self.infos_seek.saturating_add(adjust as u32) + } + }, + None => 0 + }; + self.dirty = true; + } + + pub fn adjust_log_seek(&mut self, seek_adjust: Option) { + self.log_seek = match seek_adjust { + Some(adjust) => { + /* + * So this might be weird to read. + * + * I don't know how to add an i32 in a saturating manner to a u32. + * + * That's what this does: + * if adjust is negative, negate to positive and saturating_sub it + * if adjust is positive, we can just saturating_add it + */ + if adjust < 0 { + self.log_seek.saturating_sub(-adjust as u32) + } else { + self.log_seek.saturating_add(adjust as u32) + } + }, + None => 0 + }; + self.dirty = true; + } + pub fn status(&mut self, stat: String) { self.log.push(stat); + self.dirty = true; } pub fn recv(&mut self, info: Infos) { self.infos.push(info); + self.dirty = true; + } + + pub fn input_buf_push(&mut self, c: char) { + self.input_buf.push(c); + self.dirty = true; + } + + pub fn input_buf_pop(&mut self) { + self.input_buf.pop(); + self.dirty = true; + } + + pub fn input_buf_drain(&mut self) -> String { + self.dirty = true; + self.input_buf.drain(..).collect() } pub fn ui_height(&self) -> u16 { @@ -79,20 +153,6 @@ impl DisplayInfo { */ fn into_display_lines(x: Vec, width: u16) -> Vec { ansi_aware_into_display_lines(x, width) - /* - let split_on_newline: Vec = x.into_iter() - .flat_map(|x| x.split("\n") - .map(|x| x.to_owned()) - .collect::>() - ).collect(); - let wrapped: Vec = split_on_newline.iter() - .map(|x| x.chars().collect::>()) - .flat_map(|x| x.chunks(width as usize) - .map(|x| x.into_iter().collect::()) - .collect::>()) - .collect(); - wrapped - */ } #[derive(Clone)] @@ -211,7 +271,6 @@ fn ansi_aware_into_display_lines(x: Vec, width: u16) -> Vec { "".to_owned() }, Some(AnsiInfo::Esc) => { - // TODO: flush ansi_code = None; format!("{}{}", AnsiInfo::Esc, c) }, @@ -342,6 +401,9 @@ fn ansi_aware_into_display_lines(x: Vec, width: u16) -> Vec { pub fn paint(tweeter: &::tw::TwitterCache, display_info: &mut DisplayInfo) -> Result<(), std::io::Error> { match termion::terminal_size() { Ok((width, height)) => { + if !display_info.dirty { + return Ok(()); + } // draw input prompt let mut i = 0; let log_size = 4; @@ -405,7 +467,7 @@ pub fn paint(tweeter: &::tw::TwitterCache, display_info: &mut DisplayInfo) -> Re lines_drawn += 1; } h += lines_drawn - 3; - (cursor_idx as u16 + 3, height as u16 - 5) // TODO: panic on underflow + (cursor_idx as u16 + 3, height as u16 - 5) // TODO: this panics on underflow } Some(DisplayMode::Reply(twid, msg)) => { let mut lines: Vec = vec![]; @@ -432,7 +494,7 @@ pub fn paint(tweeter: &::tw::TwitterCache, display_info: &mut DisplayInfo) -> Re lines_drawn += 1; } h += lines_drawn - 3; - (cursor_idx as u16 + 3, height as u16 - 5) // TODO: panic on underflow + (cursor_idx as u16 + 3, height as u16 - 5) // TODO: this panics on underflow } }; @@ -522,6 +584,7 @@ pub fn paint(tweeter: &::tw::TwitterCache, display_info: &mut DisplayInfo) -> Re println!("Can't get term dimensions: {}", e); } } + display_info.dirty = false; Ok(()) } @@ -778,6 +841,14 @@ pub fn render_twete_no_recurse(twete_id: &TweetId, tweeter: &tw::TwitterCache, d } } } + /* + for elem in tweet.media.iter() { + if line.contains(elem.0) { + result = result.replace(elem.0, &format!("[{}]", urls_to_include.len())); + urls_to_include.push(elem.0); + } + } + */ result }) .collect(); @@ -792,8 +863,6 @@ pub fn render_twete_no_recurse(twete_id: &TweetId, tweeter: &tw::TwitterCache, d if expanded.len() < (width - 9) as usize { // "[XX]: " is 6 + some padding space? text.push(format!("[{}]: {}", i, expanded)); } else { - // TODO: try to just show domain, THEN fall back to just a link if the - // domain is too long text.push(format!("[{}]: {}", i, short_url)); } } -- cgit v1.1