indenting and errors

pull/6/head
dvkt 5 years ago
parent 2c4ac1ed47
commit c3d1bd29a7

@ -70,6 +70,11 @@ pub fn type_for_char(c: char) -> Option<Type> {
}
}
// produces an io::Error more easily
pub fn io_error(msg: String) -> io::Error {
io::Error::new(io::ErrorKind::Other, msg)
}
// Fetches a gopher URL and returns a raw Gopher response.
pub fn fetch_url(url: &str) -> io::Result<String> {
let (_, host, port, sel) = parse_url(url);
@ -86,10 +91,7 @@ pub fn fetch(host: &str, port: &str, selector: &str) -> io::Result<String> {
.and_then(|mut socks| {
socks
.next()
.ok_or(io::Error::new(
io::ErrorKind::Other,
"Can't create socket".to_string(),
))
.ok_or(io_error("Can't create socket".to_string()))
.and_then(|sock| {
TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)
.and_then(|mut stream| {

@ -86,11 +86,13 @@ impl View for TextView {
} else {
self.longest
};
let indent = if longest > cols {
let indent = if cols >= longest && cols - longest <= 6 {
String::from("")
} else {
} else if cols >= longest {
let left = (cols - longest) / 2;
" ".repeat(left)
} else {
String::from("")
};
let iter = self
.raw

@ -6,6 +6,7 @@ use termion::input::TermRead;
use termion::raw::IntoRawMode;
use gopher;
use gopher::io_error;
use gopher::Type;
use menu::MenuView;
use text::TextView;
@ -112,12 +113,9 @@ impl UI {
Type::Text | Type::HTML => {
Ok(self.add_page(TextView::from(url.to_string(), response)))
}
_ => Err(io::Error::new(
io::ErrorKind::Other,
format!("Unsupported Gopher Response: {:?}", typ),
)),
_ => Err(io_error(format!("Unsupported Gopher Response: {:?}", typ))),
})
.map_err(|e| io::Error::new(e.kind(), format!("Error loading {}: {}", url, e)))
.map_err(|e| io_error(format!("Error loading {}: {} ({:?})", url, e, e.kind())))
}
pub fn render(&mut self) -> String {
@ -169,7 +167,7 @@ impl UI {
Action::Quit | Action::Keypress(Key::Ctrl('q')) | Action::Keypress(Key::Ctrl('c')) => {
self.running = false
}
Action::Error(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
Action::Error(e) => return Err(io_error(e)),
Action::Redraw => self.dirty = true,
Action::Open(url) => self.open(&url)?,
Action::Back | Action::Keypress(Key::Left) | Action::Keypress(Key::Backspace) => {
@ -211,7 +209,7 @@ fn copy_to_clipboard(data: &str) -> io::Result<()> {
status!("Copied URL to clipboard.");
Ok(())
})
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Clipboard error: {}", e)))
.map_err(|e| io_error(format!("Clipboard error: {}", e)))
}
fn spawn_os_clipboard() -> io::Result<process::Child> {

Loading…
Cancel
Save