You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phetch/src/text.rs

91 lines
2.4 KiB
Rust

5 years ago
use ui::{Action, Key, View};
pub struct TextView {
url: String,
raw: String,
scroll: usize, // offset
5 years ago
lines: usize, // # of lines
5 years ago
}
impl View for TextView {
fn process_input(&mut self, c: Key) -> Action {
let jump = 15;
match c {
5 years ago
Key::Char('t') | Key::Char('g') => {
self.scroll = 0;
Action::Redraw
}
Key::Char('b') | Key::Char('G') => {
self.scroll = self.lines - jump;
Action::Redraw
}
Key::Down => {
5 years ago
if self.scroll < self.lines - 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(' ') => {
5 years ago
if self.scroll < self.lines - 1 - jump {
self.scroll += jump;
5 years ago
if self.scroll >= self.lines {
self.scroll = self.lines - 1;
}
Action::Redraw
} else {
Action::None
}
}
_ => Action::Unknown,
}
5 years ago
}
fn render(&self, width: u16, height: u16) -> String {
5 years ago
let mut out = String::new();
for (i, line) in self.raw.split_terminator('\n').enumerate() {
if i > (self.scroll + height as usize) - 2 {
5 years ago
break;
}
if i < self.scroll {
continue;
}
5 years ago
out.push_str(line);
out.push('\n');
}
out
5 years ago
}
}
impl TextView {
pub fn from(url: String, response: String) -> TextView {
5 years ago
let lines = response.split_terminator('\n').count();
TextView {
url,
raw: response,
scroll: 0,
5 years ago
lines,
}
5 years ago
}
}