From 1b8614b9569d2d72a1781ff122ad27165ac9b801 Mon Sep 17 00:00:00 2001 From: deadvey Date: Tue, 21 Jan 2025 19:04:40 +0000 Subject: [PATCH] open http links in web browser now :D --- Cargo.toml | 1 + src/main.rs | 53 +++++++++++++++++++---------------------------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2ca4b43..2fe87a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ colored = "2.2.0" regex = "1.11.1" url = "2.5.4" termion = "4.0.3" +open = "5.3.2" diff --git a/src/main.rs b/src/main.rs index e315f88..524bd5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,11 +212,9 @@ fn parse_url(user_input: String, previous_host: &String) -> Result { let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com - let accept_this_as_mttp = Regex::new(r"^(.*?)\.(.*?)$").unwrap(); // example.com - let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md + //let accept_this_as_mttp = Regex::new(r"^(.*?)\.(.*?)$").unwrap(); // example.com + //let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md - let http_regex = Regex::new(r"^http:\/\/(.*?)\/(.*?)$").unwrap(); // http://example.com/index.md - let http_regex_no_path = Regex::new(r"^http:\/\/(.*?)$").unwrap(); // http://example.com if let Some(caps) = mttp_regex.captures(&user_input) { url.hostname = caps[1].to_string(); @@ -239,35 +237,22 @@ fn parse_url(user_input: String, previous_host: &String) -> Result { url.protocol = "mttp".to_string(); Ok(url) } - else if let Some(caps) = no_protocol_but_path.captures(&user_input) { - url.hostname = caps[1].to_string(); - url.port = 3477; - url.path = caps[2].to_string(); - url.protocol = "mttp".to_string(); - Ok(url) - } - else if let Some(caps) = http_regex.captures(&user_input) { - url.hostname = caps[1].to_string(); - url.port = 80; - url.path = caps[2].to_string(); - url.protocol = "http".to_string(); - Ok(url) - } - else if let Some(caps) = http_regex_no_path.captures(&user_input) { - url.hostname = caps[1].to_string(); - url.port = 80; - url.path = "/".to_string(); - url.protocol = "http".to_string(); - Ok(url) - } - else if let Some(caps) = accept_this_as_mttp.captures(&user_input) { - url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string()); - url.port = 3477; - url.path = "/".to_string(); - url.protocol = "mttp".to_string(); - Ok(url) - } + //else if let Some(caps) = no_protocol_but_path.captures(&user_input) { + // url.hostname = caps[1].to_string(); + //url.port = 3477; + //url.path = caps[2].to_string(); + //url.protocol = "mttp".to_string(); + //Ok(url) + //} + //else if let Some(caps) = accept_this_as_mttp.captures(&user_input) { + // url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string()); + //url.port = 3477; + // url.path = "/".to_string(); + // url.protocol = "mttp".to_string(); + //Ok(url) + //} else { + open::that(user_input); // Fallback to open it in the web browser Err(1) } } @@ -361,7 +346,7 @@ fn main() { load_page = true; } else { - println!("Invalid url"); + println!("Invalid url\nAttempting to open url in web browser"); } } else { println!("Invalid reference id"); @@ -377,7 +362,7 @@ fn main() { } } else { - println!("Invalid url"); + println!("Invalid mttp url, try mttp:// at the start of your input."); } }