diff --git a/src/main.rs b/src/main.rs index 82b6e84..732cebc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -277,35 +277,59 @@ fn main() { } let mut load_page: bool = true; let mut history: Vec = Vec::new(); + let mut historical_position: usize = 0; let mut links: Vec = Vec::new(); - - if let Ok(url) = parse_url(user_input, &"example.com".to_string()) { + let mut url = Url { + protocol: "internal".to_string(), + hostname: "home".to_string(), + port: 0, + path: "/".to_string(), + }; + if let Ok(parsed_value) = parse_url(user_input, &"example.com".to_string()) { + url = parsed_value; + history.push(Url { + protocol: url.protocol.clone(), + hostname: url.hostname.clone(), + port: url.port.clone(), + path: url.path.clone(), + }); 'mainloop: loop { if load_page { - history.push(Url { - protocol: url.protocol.clone(), - hostname: url.hostname.clone(), - port: url.port.clone(), - path: url.path.clone(), - }); - links = Vec::new(); - links = render_page(url.hostname.clone(), url.port.clone(), url.path.clone()); - - println!("Enter reference number to follow, h for help, or q to quit "); + links = Vec::new(); + links = render_page(history[historical_position].hostname.clone(), history[historical_position].port.clone(), history[historical_position].path.clone()); + println!("Enter reference number to follow, h for help, or q to quit\n{}",historical_position); + for i in 0..history.len() { + println!("{}://{}:{}/{}",history[i].protocol,history[i].hostname, history[i].port, history[i].path); + } } load_page = false; let user_input = input(); if user_input == "q" { - break 'mainloop; + break 'mainloop; } else if user_input == "r" { - load_page = true; - continue; + load_page = true; + continue; + } + else if user_input == "i" { + url.path = "/".to_string(); + load_page = true; + } + else if user_input == "b" { + if historical_position > 1 { + historical_position -= 1; + if let Ok(parsed_value) = parse_url(format!("{}://{}/{}",history[historical_position].protocol.clone(), history[historical_position].hostname.clone(),history[historical_position].path.clone()),&url.hostname) { + url = parsed_value; + load_page = true; + } + else { + println!("Invalid url"); + } + } } else if user_input == "h" { - println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24 - "); + println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24"); } else if user_input.chars().nth(0).unwrap() == 'o' { let number_str = &user_input[1..]; @@ -317,13 +341,26 @@ fn main() { } else if let Ok(number) = user_input.parse::() { if number < links.len() { - let url = parse_url(links[number].clone(), &url.hostname).expect("Error parsing URL"); - load_page = true; + if let Ok(parsed_value) = parse_url(links[number].clone(), &url.hostname) { + url = parsed_value; + history.insert(historical_position+1, Url { + protocol: url.protocol.clone(), + hostname: url.hostname.clone(), + port: url.port.clone(), + path: url.path.clone(), + }); + historical_position += 1; + load_page = true; + } + else { + println!("Invalid url"); + } } else { println!("Invalid reference id"); } } - else if let Ok(url) = parse_url(user_input, &url.hostname) { + else if let Ok(parsed_value) = parse_url(user_input, &url.hostname) { + url = parsed_value; load_page = true; } else {