From 9ef59ba0501f7d29edc42c6861ff259ab1914b37 Mon Sep 17 00:00:00 2001 From: rabite Date: Sat, 9 Mar 2019 23:40:13 +0100 Subject: scrolling and following proc output --- src/preview.rs | 4 ++- src/proclist.rs | 42 +++++++++++++++++++++++++++++++ src/textview.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 118 insertions(+), 5 deletions(-) diff --git a/src/preview.rs b/src/preview.rs index be99b31..f10928c 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -406,7 +406,9 @@ impl Previewer { let mut textview = TextView { lines: output.lines().map(|s| s.to_string()).collect(), buffer: String::new(), - core: core.clone()}; + core: core.clone(), + follow: false, + offset: 0}; textview.set_coordinates(&core.coordinates).log(); textview.refresh().log(); textview.animate_slide_up().log(); diff --git a/src/proclist.rs b/src/proclist.rs index c758980..e2468db 100644 --- a/src/proclist.rs +++ b/src/proclist.rs @@ -261,6 +261,41 @@ impl ProcView { self.viewing = Some(self.get_listview().get_selection()); Ok(()) } + + pub fn toggle_follow(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.toggle_follow(); + Ok(()) + } + + pub fn scroll_up(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.scroll_up(); + Ok(()) + } + + pub fn scroll_down(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.scroll_down(); + Ok(()) + } + + pub fn page_up(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.page_up(); + Ok(()) + } + + pub fn page_down(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.page_down(); + Ok(()) + } + + pub fn scroll_top(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.scroll_top(); + Ok(()) + } + + pub fn scroll_bottom(&mut self) -> HResult<()> { + self.get_textview().widget()?.lock()?.as_mut()?.scroll_bottom(); + Ok(()) + } } impl Widget for ProcView { @@ -293,6 +328,13 @@ impl Widget for ProcView { Key::Down | Key::Char('n') => { self.get_listview().move_down(); } + Key::Char('f') => { self.toggle_follow().log(); } + Key::Ctrl('n') => { self.scroll_down().log(); }, + Key::Ctrl('p') => { self.scroll_up().log(); }, + Key::Ctrl('v') => { self.page_down().log(); }, + Key::Alt('v') => { self.page_up().log(); }, + Key::Char('>') => { self.scroll_bottom().log(); }, + Key::Char('<') => { self.scroll_top().log(); } _ => {} } self.refresh().log(); diff --git a/src/textview.rs b/src/textview.rs index 2d7fafd..25dc63e 100644 --- a/src/textview.rs +++ b/src/textview.rs @@ -9,7 +9,9 @@ use crate::fail::HResult; pub struct TextView { pub lines: Vec, pub buffer: String, - pub core: WidgetCore + pub core: WidgetCore, + pub follow: bool, + pub offset: usize, } impl TextView { @@ -17,7 +19,9 @@ impl TextView { TextView { lines: vec![], buffer: String::new(), - core: core.clone() + core: core.clone(), + follow: false, + offset: 0, } } pub fn new_from_file(core: &WidgetCore, file: &File) -> HResult { @@ -32,7 +36,9 @@ impl TextView { Ok(TextView { lines: lines, buffer: String::new(), - core: core.clone() + core: core.clone(), + follow: false, + offset: 0, }) } pub fn new_from_file_limit_lines(core: &WidgetCore, @@ -51,7 +57,9 @@ impl TextView { Ok(TextView { lines: lines, buffer: String::new(), - core: core.clone() + core: core.clone(), + follow: false, + offset: 0, }) } @@ -60,6 +68,60 @@ impl TextView { self.lines = lines; self.refresh() } + + pub fn toggle_follow(&mut self) { + self.follow = !self.follow + } + + pub fn scroll(&mut self, amount: isize) { + let ysize = self.get_coordinates().unwrap().ysize() as isize; + let offset = self.offset as isize; + let len = self.lines.len() as isize; + + if len <= ysize + offset { return } + + if amount > 0 { + if ysize + amount + offset + 1 >= len { + // Too far down + self.offset = (len - ysize - 1) as usize; + } else { + self.offset = (offset as isize + amount) as usize; + } + } else if amount < 0 { + if offset + amount >= 0 { + self.offset = (offset + amount) as usize; + } else { + self.offset = 0; + } + } + } + + pub fn scroll_up(&mut self) { + self.scroll(-1); + } + + pub fn scroll_down(&mut self) { + self.scroll(1); + } + + pub fn page_up(&mut self) { + let ysize = self.get_coordinates().unwrap().ysize() as isize; + self.scroll(0 - ysize + 1); + } + + pub fn page_down(&mut self) { + let ysize = self.get_coordinates().unwrap().ysize() as isize; + self.scroll(ysize - 1); + } + + pub fn scroll_top(&mut self) { + self.offset = 0; + } + + pub fn scroll_bottom(&mut self) { + let len = self.lines.len() as isize; + self.scroll(len); + } } impl Widget for TextView { @@ -72,11 +134,18 @@ impl Widget for TextView { fn refresh(&mut self) -> HResult<()> { let (xsize, ysize) = self.get_coordinates()?.size().size(); let (xpos, ypos) = self.get_coordinates()?.position().position(); + let len = self.lines.len(); + + if self.follow { + self.scroll_bottom(); + } + self.buffer = self.get_clearlist()? + &self .lines .iter() + .skip(self.offset) .take(ysize as usize) .enumerate() .map(|(i, line)| { -- cgit v1.2.3