2
0
mirror of https://github.com/xvxx/phetch synced 2024-11-10 13:10:54 +00:00

match ctrl or keypress

This commit is contained in:
dvkt 2019-12-26 15:27:01 -08:00
parent bb19994e2e
commit d06b73f142
2 changed files with 41 additions and 53 deletions

View File

@ -398,55 +398,53 @@ impl UI {
self.focused += 1; self.focused += 1;
} }
} }
_ if action.key_or_ctrl('a') => self.open("History", "gopher://phetch/1/history")?, Action::Keypress(Key::Char(key)) | Action::Keypress(Key::Ctrl(key)) => match key {
_ if action.key_or_ctrl('b') => { 'a' => self.open("History", "gopher://phetch/1/history")?,
self.open("Bookmarks", "gopher://phetch/1/bookmarks")? 'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?,
} 'g' => {
_ if action.key_or_ctrl('g') => { if let Some(url) = self.prompt("Go to URL: ") {
if let Some(url) = self.prompt("Go to URL: ") { if !url.contains("://") && !url.starts_with("gopher://") {
if !url.contains("://") && !url.starts_with("gopher://") { self.open(&url, &format!("gopher://{}", url))?;
self.open(&url, &format!("gopher://{}", url))?; } else {
} else { self.open(&url, &url)?;
self.open(&url, &url)?; }
} }
} }
} 'h' => self.open("Help", "gopher://phetch/1/help")?,
_ if action.key_or_ctrl('h') => self.open("Help", "gopher://phetch/1/help")?, 'r' => {
_ if action.key_or_ctrl('r') => { if let Some(page) = self.views.get(self.focused) {
if let Some(page) = self.views.get(self.focused) { let url = page.url();
let url = page.url(); let raw = page.raw();
let raw = page.raw(); let mut text = Text::from(url, raw);
let mut text = Text::from(url, raw); text.wide = true;
text.wide = true; self.add_page(Box::new(text));
self.add_page(Box::new(text));
}
}
_ if action.key_or_ctrl('s') => {
if let Some(page) = self.views.get(self.focused) {
let url = page.url();
match bookmarks::save(&url, &url) {
Ok(()) => self.set_status(format!("Saved bookmark: {}", url)),
Err(e) => return Err(error!("Save failed: {}", e)),
} }
} }
} 's' => {
_ if action.key_or_ctrl('u') => { if let Some(page) = self.views.get(self.focused) {
if let Some(page) = self.views.get(self.focused) { let url = page.url();
let url = page.url(); match bookmarks::save(&url, &url) {
self.set_status(format!("Current URL: {}", url)); Ok(()) => self.set_status(format!("Saved bookmark: {}", url)),
Err(e) => return Err(error!("Save failed: {}", e)),
}
}
} }
} 'u' => {
_ if action.key_or_ctrl('y') => { if let Some(page) = self.views.get(self.focused) {
if let Some(page) = self.views.get(self.focused) { let url = page.url();
let url = page.url(); self.set_status(format!("Current URL: {}", url));
copy_to_clipboard(&url)?; }
self.set_status(format!("Copied {} to clipboard.", url));
} }
} 'y' => {
_ if action.key_or_ctrl('q') => self.running = false, if let Some(page) = self.views.get(self.focused) {
Action::Keypress(key) => { let url = page.url();
return Err(error!("Unknown keypress: {:?}", key)); copy_to_clipboard(&url)?;
} self.set_status(format!("Copied {} to clipboard.", url));
}
}
'q' => self.running = false,
c => return Err(error!("Unknown keypress: {}", c)),
},
_ => (), _ => (),
} }
Ok(()) Ok(())

View File

@ -8,13 +8,3 @@ pub enum Action {
Prompt(String, Box<dyn FnOnce(String) -> Action>), // query string, callback on success Prompt(String, Box<dyn FnOnce(String) -> Action>), // query string, callback on success
Error(String), // error message Error(String), // error message
} }
impl Action {
pub fn key_or_ctrl(&self, target: char) -> bool {
if let Action::Keypress(Key::Ctrl(c)) | Action::Keypress(Key::Char(c)) = self {
*c == target
} else {
false
}
}
}