mirror of
https://github.com/xvxx/phetch
synced 2024-11-15 00:12:50 +00:00
better error handling
This commit is contained in:
parent
2c9b38dfb0
commit
c522f6656f
55
src/ui.rs
55
src/ui.rs
@ -34,6 +34,7 @@ pub struct UI {
|
|||||||
dirty: bool, // redraw?
|
dirty: bool, // redraw?
|
||||||
running: bool, // main ui loop running?
|
running: bool, // main ui loop running?
|
||||||
pub size: (usize, usize), // cols, rows
|
pub size: (usize, usize), // cols, rows
|
||||||
|
error: String, // error string, if any
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UI {
|
impl UI {
|
||||||
@ -48,6 +49,7 @@ impl UI {
|
|||||||
dirty: true,
|
dirty: true,
|
||||||
running: true,
|
running: true,
|
||||||
size,
|
size,
|
||||||
|
error: String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,13 +64,29 @@ impl UI {
|
|||||||
|
|
||||||
pub fn draw(&mut self) {
|
pub fn draw(&mut self) {
|
||||||
if self.dirty {
|
if self.dirty {
|
||||||
|
let error = if self.error.is_empty() {
|
||||||
|
"".into()
|
||||||
|
} else {
|
||||||
|
let e = format!(
|
||||||
|
"{}{}{}{}{}",
|
||||||
|
color::Fg(color::LightRed),
|
||||||
|
termion::cursor::Goto(1, self.rows()),
|
||||||
|
termion::clear::CurrentLine,
|
||||||
|
self.error,
|
||||||
|
color::Fg(color::Reset)
|
||||||
|
);
|
||||||
|
self.error.clear();
|
||||||
|
e
|
||||||
|
};
|
||||||
print!(
|
print!(
|
||||||
"{}{}{}{}",
|
"{}{}{}{}{}",
|
||||||
termion::clear::All,
|
termion::clear::All,
|
||||||
termion::cursor::Goto(1, 1),
|
termion::cursor::Goto(1, 1),
|
||||||
termion::cursor::Hide,
|
termion::cursor::Hide,
|
||||||
self.render()
|
self.render(),
|
||||||
|
error
|
||||||
);
|
);
|
||||||
|
|
||||||
self.dirty = false;
|
self.dirty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,8 +96,9 @@ impl UI {
|
|||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
|
|
||||||
let action = self.process_page_input();
|
let action = self.process_page_input();
|
||||||
self.process_action(action)
|
if let Err(e) = self.process_action(action) {
|
||||||
.map_err(|e| error(&e.to_string()));
|
self.error = e.to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open(&mut self, url: &str) -> io::Result<()> {
|
pub fn open(&mut self, url: &str) -> io::Result<()> {
|
||||||
@ -107,12 +126,6 @@ impl UI {
|
|||||||
return self.fetch_help(url);
|
return self.fetch_help(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
status(&format!(
|
|
||||||
"{}Loading...{}",
|
|
||||||
color::Fg(color::LightBlack),
|
|
||||||
termion::cursor::Show
|
|
||||||
));
|
|
||||||
|
|
||||||
// request thread
|
// request thread
|
||||||
let thread_url = url.to_string();
|
let thread_url = url.to_string();
|
||||||
let req = thread::spawn(move || match gopher::fetch_url(&thread_url) {
|
let req = thread::spawn(move || match gopher::fetch_url(&thread_url) {
|
||||||
@ -128,8 +141,9 @@ impl UI {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
print!(
|
print!(
|
||||||
"\r{}{}{}",
|
"\r{}{}{}{}",
|
||||||
termion::cursor::Hide,
|
termion::cursor::Hide,
|
||||||
|
color::Fg(color::LightBlack),
|
||||||
".".repeat(i),
|
".".repeat(i),
|
||||||
termion::clear::AfterCursor
|
termion::clear::AfterCursor
|
||||||
);
|
);
|
||||||
@ -140,6 +154,7 @@ impl UI {
|
|||||||
|
|
||||||
let work = req.join();
|
let work = req.join();
|
||||||
spintx.send(true); // stop spinner
|
spintx.send(true); // stop spinner
|
||||||
|
self.dirty = true;
|
||||||
let res = match work {
|
let res = match work {
|
||||||
Ok(opt) => match opt {
|
Ok(opt) => match opt {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
@ -186,6 +201,10 @@ impl UI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn rows(&self) -> u16 {
|
||||||
|
self.size.1 as u16
|
||||||
|
}
|
||||||
|
|
||||||
fn startup(&mut self) {
|
fn startup(&mut self) {
|
||||||
self.load_history();
|
self.load_history();
|
||||||
}
|
}
|
||||||
@ -439,17 +458,3 @@ pub fn status(s: &str) {
|
|||||||
);
|
);
|
||||||
stdout().flush();
|
stdout().flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display an error message to the user.
|
|
||||||
pub fn error(e: &str) {
|
|
||||||
let (_cols, rows) = terminal_size().unwrap();
|
|
||||||
print!(
|
|
||||||
"{}{}{}{}{}",
|
|
||||||
"\x1b[91m",
|
|
||||||
termion::cursor::Goto(1, rows),
|
|
||||||
termion::clear::CurrentLine,
|
|
||||||
e,
|
|
||||||
color::Fg(color::Reset)
|
|
||||||
);
|
|
||||||
stdout().flush();
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user