scrolling and following proc output

This commit is contained in:
rabite 2019-03-09 23:40:13 +01:00
parent 67e1616efb
commit 9ef59ba050
3 changed files with 118 additions and 5 deletions

View File

@ -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();

View File

@ -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();

View File

@ -9,7 +9,9 @@ use crate::fail::HResult;
pub struct TextView {
pub lines: Vec<String>,
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<TextView> {
@ -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)| {