Browse Source

Can move with j and k

openbsd
Julien Blanchard 7 years ago
parent
commit
afb1e589fc
  1. 36
      src/main.rs

36
src/main.rs

@ -1,7 +1,8 @@
use cursive::traits::*;
use cursive::align::HAlign;
use cursive::view::Scrollable;
use cursive::views::{Dialog, Panel, TextView, EditView, SelectView};
use cursive::views::{Dialog, Panel, TextView, EditView, SelectView, OnEventView};
use cursive::event::{EventResult};
use cursive::Cursive;
extern crate native_tls;
@ -13,7 +14,6 @@ use regex::Regex;
use std::net::TcpStream;
use std::io::{Read, Write};
use std::env;
use std::fs::File;
use std::fs::OpenOptions;
use std::str;
@ -99,6 +99,16 @@ fn visit_url(s: &mut Cursive, url: &str) {
select.add_all_str(new_content.lines());
select.set_on_submit(follow_link);
let select = OnEventView::new(select)
.on_pre_event_inner('k', |s, _| {
s.select_up(1);
Some(EventResult::Consumed(None))
})
.on_pre_event_inner('j', |s, _| {
s.select_down(1);
Some(EventResult::Consumed(None))
});
s.add_layer(
Dialog::around(select.scrollable().full_screen()).title(url),
);
@ -121,9 +131,9 @@ fn follow_link(s: &mut Cursive, line: &str) {
let next_url = if url.starts_with("gemini://") {
url.to_owned()
} else if url.starts_with("/") {
format!("{}{}", get_last_url(), url)
format!("gemini://{}{}", get_last_host(), url)
} else {
format!("{}/{}", get_last_url(), url)
format!("gemini://{}/{}", get_last_host(), url)
};
visit_url(s, &next_url)
} else if re2.is_match(&text) {
@ -133,9 +143,9 @@ fn follow_link(s: &mut Cursive, line: &str) {
let next_url = if url.starts_with("gemini://") {
url.to_owned()
} else if url.starts_with("/") {
format!("{}{}", get_last_url(), url)
format!("gemini://{}{}", get_last_host(), url)
} else {
format!("{}/{}", get_last_url(), url)
format!("gemini://{}/{}", get_last_host(), url)
};
visit_url(s, &next_url)
} else {
@ -143,6 +153,14 @@ fn follow_link(s: &mut Cursive, line: &str) {
}
}
fn is_link(line: &str) -> bool {
let text = format!("{}", line);
let re1 = Regex::new(r"^\[(.*)\|(URL:)?(.*)\]$").unwrap(); // `[Foo|URL:link]` links
let re2 = Regex::new(r"^=>\s(\S*)\s(\w*)$").unwrap(); // `=> link Foo` links
re1.is_match(&text) || re2.is_match(&text)
}
fn get_data(url: &str) -> Result<String, String> {
let urlp = Url::parse(url).unwrap();
let host = urlp.host_str().unwrap();
@ -184,11 +202,13 @@ fn go_back(s: &mut Cursive) {
}
}
fn get_last_url() -> String {
fn get_last_host() -> String {
let mut file = File::open("/tmp/asuka_history").expect("file not found");
let mut content = String::new();
file.read_to_string(&mut content).expect("Unable to read file");
content.lines().last().unwrap().to_owned()
let url = content.lines().last().unwrap();
let urlp = Url::parse(url).unwrap();
urlp.host_str().unwrap().to_owned()
}
fn get_previous_url() -> Option<String> {

Loading…
Cancel
Save