search shortcut and slightly more DRY code

This commit is contained in:
stjet
2025-03-23 23:45:59 +00:00
parent 042477c6af
commit 5325eaf195
5 changed files with 60 additions and 64 deletions

6
Cargo.lock generated
View File

@@ -560,7 +560,7 @@ dependencies = [
[[package]] [[package]]
name = "koxinga" name = "koxinga"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"ming-wm-lib", "ming-wm-lib",
"reqwest", "reqwest",
@@ -604,9 +604,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]] [[package]]
name = "ming-wm-lib" name = "ming-wm-lib"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de20ca223c0e37c8c05a69174ec8544f4c6f024a9d90321bf997882232510a42" checksum = "9d90e1d57dcc9ff559f34d885a0c62e86ef881b4371e3f3b02c909460d3454b5"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "koxinga" name = "koxinga"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
[[bin]] [[bin]]
@@ -8,5 +8,5 @@ name = "mingInternet_Koxinga_Browser"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
ming-wm-lib = "0.1.4" ming-wm-lib = "0.1.5"
reqwest = { version = "0.12", features = [ "blocking" ] } reqwest = { version = "0.12", features = [ "blocking" ] }

View File

@@ -1,4 +1,4 @@
A web browser for [ming-wm](https://github.com/stjet/ming-wm), and developed in it, that aims to support two things: text, and links. A web browser for [ming-wm](https://github.com/stjet/ming-wm), and mostly developed in it, that aims to support two things: text, and links.
Yes, slightly inspired by [Vimium](https://vimium.github.io). Yes, slightly inspired by [Vimium](https://vimium.github.io).

View File

@@ -4,6 +4,7 @@ Koxinga is a web browser supporting text and links.
- `u`: URL mode, where a URL can be inputted. Hit enter/return to go to that page. - `u`: URL mode, where a URL can be inputted. Hit enter/return to go to that page.
- `l`: Link mode. The page will now show numbers in front of any links. Input the number corresponding to the link to navigate to, then hit enter/return. - `l`: Link mode. The page will now show numbers in front of any links. Input the number corresponding to the link to navigate to, then hit enter/return.
- `s`: Search mode. Search engine is http://frogfind.de. Frogfind doesn't seem to use a valid HTTPS certificate yet. But all the other search engines block Koxinga, do not work without Javascript, or give horribly mangled invalid HTML. Such is the state of the world...
- `j`, `k` to scroll page. - `j`, `k` to scroll page.
- `0`: Go to top of page. - `0`: Go to top of page.
- `G`: Go to bottom of page. - `G`: Go to bottom of page.

View File

@@ -38,6 +38,7 @@ enum Mode {
Normal, Normal,
Url, Url,
Link, Link,
Search,
} }
impl fmt::Display for Mode { impl fmt::Display for Mode {
@@ -46,6 +47,7 @@ impl fmt::Display for Mode {
Mode::Normal => "NORMAL", Mode::Normal => "NORMAL",
Mode::Url => "URL", Mode::Url => "URL",
Mode::Link => "LINK", Mode::Link => "LINK",
Mode::Search => "SEARCH",
})?; })?;
Ok(()) Ok(())
} }
@@ -58,8 +60,7 @@ struct KoxingaBrowser {
max_lines: usize, max_lines: usize,
top_line_no: usize, top_line_no: usize,
url: Option<String>, url: Option<String>,
url_input: String, input: String,
link_input: String,
links: Vec<String>, links: Vec<String>,
top_level_nodes: Vec<Box<Node>>, top_level_nodes: Vec<Box<Node>>,
page: Vec<(usize, usize, String, bool)>, //x, y, text, link colour or not page: Vec<(usize, usize, String, bool)>, //x, y, text, link colour or not
@@ -83,11 +84,15 @@ impl WindowLike for KoxingaBrowser {
let max_lines_screen = (self.dimensions[1] - 4) / LINE_HEIGHT - 1; let max_lines_screen = (self.dimensions[1] - 4) / LINE_HEIGHT - 1;
if key_press.key == 'u' { if key_press.key == 'u' {
self.mode = Mode::Url; self.mode = Mode::Url;
self.input = self.url.clone().unwrap_or(String::new());
WindowMessageResponse::JustRedraw WindowMessageResponse::JustRedraw
} else if key_press.key == 'l' && self.url.is_some() { } else if key_press.key == 'l' && self.url.is_some() {
self.mode = Mode::Link; self.mode = Mode::Link;
self.calc_page(); self.calc_page();
WindowMessageResponse::JustRedraw WindowMessageResponse::JustRedraw
} else if key_press.key == 's' {
self.mode = Mode::Search;
WindowMessageResponse::JustRedraw
} else if key_press.key == 'k' { } else if key_press.key == 'k' {
if self.top_line_no > 0 { if self.top_line_no > 0 {
self.top_line_no -= 1; self.top_line_no -= 1;
@@ -110,65 +115,57 @@ impl WindowLike for KoxingaBrowser {
WindowMessageResponse::DoNothing WindowMessageResponse::DoNothing
} }
}, },
Mode::Url => { Mode::Url | Mode::Search | Mode::Link => {
if key_press.key == '𐘂' { //the enter key if key_press.is_enter() && self.input.len() > 0 {
if let Some(text) = get(&self.url_input) { let new_url = if self.mode == Mode::Search {
self.url = Some(self.url_input.clone()); "http://frogfind.de/?q=".to_string() + &self.input
} else if self.mode == Mode::Link {
let link_index = self.input.parse::<usize>().unwrap();
let url = self.url.as_ref().unwrap();
let mut link;
if link_index < self.links.len() {
link = self.links[link_index].clone();
if link.chars().count() >= 2 {
//remove the quotes
link = link.substring(1, link.len() - 1).to_string();
}
if link.starts_with("/") {
link = get_base_url(&url) + &link;
} else if !link.starts_with("http://") && !link.starts_with("https://") {
link = url.clone() + if url.ends_with("/") { "" } else { "/" } + &link;
}
} else {
return WindowMessageResponse::DoNothing
}
link
} else {
//if Mode::Url
self.input.clone()
};
if let Some(text) = get(&new_url) {
self.url = Some(new_url.clone());
self.top_line_no = 0; self.top_line_no = 0;
self.top_level_nodes = parse(&text); self.top_level_nodes = parse(&text);
self.input = String::new();
self.calc_page(); self.calc_page();
self.mode = Mode::Normal; self.mode = Mode::Normal;
} WindowMessageResponse::JustRedraw
} else if key_press.key == '𐘃' { //escape key
self.mode = Mode::Normal;
} else if key_press.key == '𐘁' { //backspace
if self.url_input.len() > 0 {
self.url_input = self.url_input.remove_last();
} else { } else {
return WindowMessageResponse::DoNothing; WindowMessageResponse::DoNothing
} }
} else { } else if key_press.is_escape() {
self.url_input += &key_press.key.to_string(); let is_link_mode = self.mode == Mode::Link;
} self.mode = Mode::Normal;
WindowMessageResponse::JustRedraw if is_link_mode {
}, self.calc_page();
Mode::Link => {
if key_press.key == '𐘂' && self.link_input.len() > 0 { //the enter key
let link_index = self.link_input.parse::<usize>().unwrap();
let url = self.url.as_ref().unwrap();
if link_index < self.links.len() {
let mut link = self.links[link_index].clone();
if link.chars().count() >= 2 {
//remove the quotes
link = link.substring(1, link.len() - 1).to_string();
}
if link.starts_with("/") {
link = get_base_url(&url) + &link;
} else if !link.starts_with("http://") && !link.starts_with("https://") {
link = url.clone() + if url.ends_with("/") { "" } else { "/" } + &link;
}
if let Some(text) = get(&link) {
self.url = Some(link.to_string());
self.url_input = link.to_string();
self.top_line_no = 0;
self.top_level_nodes = parse(&text);
self.mode = Mode::Normal;
self.calc_page();
}
self.link_input = String::new();
} }
self.mode = Mode::Normal; self.input = String::new();
WindowMessageResponse::JustRedraw WindowMessageResponse::JustRedraw
} else if key_press.key == '𐘃' { //escape key' } else if key_press.is_backspace() && self.input.len() > 0 {
self.link_input = String::new(); self.input = self.input.remove_last();
self.mode = Mode::Normal;
self.calc_page();
WindowMessageResponse::JustRedraw WindowMessageResponse::JustRedraw
} else if key_press.key == '𐘁' { //backspace } else if (self.mode == Mode::Link && key_press.key.is_ascii_digit() && self.input.len() < 10) || (self.mode != Mode::Link && key_press.is_regular()) {
self.link_input = self.link_input.remove_last(); self.input += &key_press.key.to_string();
WindowMessageResponse::JustRedraw
} else if key_press.key.is_ascii_digit() && self.link_input.len() < 10 {
self.link_input += &key_press.key.to_string();
WindowMessageResponse::JustRedraw WindowMessageResponse::JustRedraw
} else { } else {
WindowMessageResponse::DoNothing WindowMessageResponse::DoNothing
@@ -191,15 +188,13 @@ impl WindowLike for KoxingaBrowser {
} }
//mode //mode
let mut bottom_text = self.mode.to_string() + ": "; let mut bottom_text = self.mode.to_string() + ": ";
if self.mode == Mode::Url { if self.mode == Mode::Normal && self.dimensions[0] >= 500 {
bottom_text += &self.url_input; bottom_text += "u (url), s (search)";
} else if self.mode == Mode::Link {
bottom_text += &self.link_input;
} else if self.mode == Mode::Normal && self.dimensions[0] >= 500 {
bottom_text += "u (url)";
if self.url.is_some() { if self.url.is_some() {
bottom_text += ", l (link), j (down), k (up)"; bottom_text += ", l (link), j (down), k (up)";
} }
} else {
bottom_text += &self.input;
} }
instructions.push(DrawInstructions::Text([0, self.dimensions[1] - LINE_HEIGHT], vec!["nimbus-roman".to_string()], bottom_text, theme_info.text, theme_info.background, Some(1), Some(11))); instructions.push(DrawInstructions::Text([0, self.dimensions[1] - LINE_HEIGHT], vec!["nimbus-roman".to_string()], bottom_text, theme_info.text, theme_info.background, Some(1), Some(11)));
instructions instructions