page up / down on text

pull/6/head
dvkt 5 years ago
parent e82f4d7bb9
commit 64caf57d99

@ -3,19 +3,65 @@ use ui::{Action, Key, View};
pub struct TextView {
url: String,
raw: String,
scroll: usize, // offset
count: usize, // # of lines
}
impl View for TextView {
fn process_input(&mut self, c: Key) -> Action {
Action::Unknown
let jump = 15;
match c {
Key::Down => {
if self.scroll < self.count - 1 {
self.scroll += 1;
Action::Redraw
} else {
Action::None
}
}
Key::Up => {
if self.scroll > 0 {
self.scroll -= 1;
Action::Redraw
} else {
Action::None
}
}
Key::PageUp | Key::Char('-') => {
if self.scroll > 0 {
self.scroll -= jump;
if self.scroll <= 0 {
self.scroll = 0;
}
Action::Redraw
} else {
Action::None
}
}
Key::PageDown | Key::Char(' ') => {
if self.scroll < self.count - 1 - jump {
self.scroll += jump;
if self.scroll >= self.count {
self.scroll = self.count - 1;
}
Action::Redraw
} else {
Action::None
}
}
_ => Action::Unknown,
}
}
fn render(&self, width: u16, height: u16) -> String {
let mut out = String::new();
for (i, line) in self.raw.split_terminator('\n').enumerate() {
if i as u16 > height - 4 {
if i > (self.scroll + height as usize) - 2 {
break;
}
if i < self.scroll {
continue;
}
out.push_str(line);
out.push('\n');
}
@ -25,6 +71,12 @@ impl View for TextView {
impl TextView {
pub fn from(url: String, response: String) -> TextView {
TextView { url, raw: response }
let count = response.split_terminator('\n').count();
TextView {
url,
raw: response,
scroll: 0,
count,
}
}
}

Loading…
Cancel
Save