2
0
mirror of https://github.com/xvxx/phetch synced 2024-11-12 13:10:40 +00:00

rendering

This commit is contained in:
dvkt 2019-12-16 19:11:47 -08:00
parent 30e8640fa1
commit 8dbb7b7550
3 changed files with 50 additions and 13 deletions

View File

@ -35,6 +35,13 @@ pub fn fetch(host: &str, port: &str, selector: &str) -> io::Result<String> {
})
.and_then(|mut stream| {
stream.read_to_string(&mut body);
let last: String = body.chars().rev().take(3).collect();
// Gopher responses should end .\r\n, but don't always.
if &last == ".\r\n" {
body.pop(); body.pop(); body.pop();
}
Ok(())
});

View File

@ -26,10 +26,7 @@ pub struct Line {
impl View for MenuView {
fn render(&self) -> String {
let mut out = self.menu.raw.to_string();
out.push('\n');
out.push_str(&format!("{:#?}", self));
out
self.render_lines()
}
fn process_input(&mut self, key: Key) -> Action {
@ -50,6 +47,40 @@ impl MenuView {
}
}
fn lines(&self) -> &Vec<Line> {
&self.menu.lines
}
fn render_lines(&self) -> String {
let mut out = String::new();
for line in self.lines() {
self.render_line_into(line, &mut out);
}
out
}
fn render_line_into(&self, line: &Line, s: &mut String) {
macro_rules! push {
($c:expr, $e:expr) => {{
s.push_str("\x1b[");
s.push_str($c);
s.push_str("m");
s.push_str($e);
s.push_str("\x1b[0m");
}};
}
s.push('\t');
match line.typ {
Type::Text => push!("90", &line.name),
Type::Menu => push!("94", &line.name),
Type::Info => push!("93", &line.name),
Type::HTML => push!("92", &line.name),
_ => {}
}
s.push('\n');
}
fn action_page_down(&self) {}
fn action_page_up(&self) {}
fn action_up(&self) {}

View File

@ -49,7 +49,13 @@ impl UI {
pub fn draw(&self) {
print!("{}", self.render());
// print!("{:#?}", self);
}
pub fn update(&mut self) {
match self.process_input() {
Action::Quit => std::process::exit(1),
_ => {}
}
}
pub fn render(&self) -> String {
@ -85,13 +91,6 @@ impl UI {
}
}
fn update(&mut self) {
match self.process_input() {
Action::Quit => std::process::exit(1),
_ => {}
}
}
fn process_input(&mut self) -> Action {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
@ -102,7 +101,7 @@ impl UI {
let key = c.expect("UI error on stdin.keys"); // TODO
match page.process_input(key) {
Action::Unknown => match key {
Key::Ctrl('q') => return Action::Quit,
Key::Ctrl('q') | Key::Ctrl('c') => return Action::Quit,
Key::Left => return Action::Back,
Key::Right => return Action::Forward,
_ => {}