From 33bf9603b5428439504587f05a46f914af53db4f Mon Sep 17 00:00:00 2001 From: makeworld Date: Tue, 28 Dec 2021 17:35:08 -0500 Subject: [PATCH] Keybinding to open URL with URL handler instead of proxy Fixes #143 --- CHANGELOG.md | 1 + config/config.go | 1 + config/default.go | 1 + config/keybindings.go | 76 ++++++++++++++++++++++--------------------- default-config.toml | 1 + display/handlers.go | 6 ++-- display/help.go | 2 ++ display/tab.go | 27 +++++++++++---- 8 files changed, 70 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cde56a..f327fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [Client certificates](https://github.com/makeworld-the-better-one/amfora/wiki/Client-Certificates) can be restricted to certain paths of a host (#115) - `header` config option in `[subscriptions]` to allow disabling the header text on the subscriptions page (#191) - Selected link and scroll position stays for non-cached pages (#122) +- Keybinding to open URL with URL handler instead of configured proxy (#143) ### Changed - Center text automatically, removing `left_margin` from the config (#233) diff --git a/config/config.go b/config/config.go index 1a1a1d9..f30f6ae 100644 --- a/config/config.go +++ b/config/config.go @@ -255,6 +255,7 @@ func Init() error { viper.SetDefault("keybindings.bind_beginning", []string{"Home", "g"}) viper.SetDefault("keybindings.bind_end", []string{"End", "G"}) viper.SetDefault("keybindings.shift_numbers", "") + viper.SetDefault("keybindings.bind_url_handler_open", "Ctrl-U") viper.SetDefault("url-handlers.other", "default") viper.SetDefault("cache.max_size", 0) viper.SetDefault("cache.max_pages", 20) diff --git a/config/default.go b/config/default.go index eade807..3b81dd8 100644 --- a/config/default.go +++ b/config/default.go @@ -188,6 +188,7 @@ underline = true # bind_copy_target_url # bind_beginning: moving to beginning of page (top left) # bind_end: same but the for the end (bottom left) +# bind_url_handler_open: Open highlighted URL with URL handler (#143) [url-handlers] # Allows setting the commands to run for various URL schemes. diff --git a/config/keybindings.go b/config/keybindings.go index 8b33d59..20a7ef9 100644 --- a/config/keybindings.go +++ b/config/keybindings.go @@ -61,6 +61,7 @@ const ( CmdCopyTargetURL CmdBeginning CmdEnd + CmdURLHandlerOpen // See #143 ) type keyBinding struct { @@ -161,43 +162,44 @@ func parseBinding(cmd Command, binding string) { // Called by config.Init() func KeyInit() { configBindings := map[Command]string{ - CmdLink1: "keybindings.bind_link1", - CmdLink2: "keybindings.bind_link2", - CmdLink3: "keybindings.bind_link3", - CmdLink4: "keybindings.bind_link4", - CmdLink5: "keybindings.bind_link5", - CmdLink6: "keybindings.bind_link6", - CmdLink7: "keybindings.bind_link7", - CmdLink8: "keybindings.bind_link8", - CmdLink9: "keybindings.bind_link9", - CmdLink0: "keybindings.bind_link0", - CmdBottom: "keybindings.bind_bottom", - CmdEdit: "keybindings.bind_edit", - CmdHome: "keybindings.bind_home", - CmdBookmarks: "keybindings.bind_bookmarks", - CmdAddBookmark: "keybindings.bind_add_bookmark", - CmdSave: "keybindings.bind_save", - CmdReload: "keybindings.bind_reload", - CmdBack: "keybindings.bind_back", - CmdForward: "keybindings.bind_forward", - CmdMoveUp: "keybindings.bind_moveup", - CmdMoveDown: "keybindings.bind_movedown", - CmdMoveLeft: "keybindings.bind_moveleft", - CmdMoveRight: "keybindings.bind_moveright", - CmdPgup: "keybindings.bind_pgup", - CmdPgdn: "keybindings.bind_pgdn", - CmdNewTab: "keybindings.bind_new_tab", - CmdCloseTab: "keybindings.bind_close_tab", - CmdNextTab: "keybindings.bind_next_tab", - CmdPrevTab: "keybindings.bind_prev_tab", - CmdQuit: "keybindings.bind_quit", - CmdHelp: "keybindings.bind_help", - CmdSub: "keybindings.bind_sub", - CmdAddSub: "keybindings.bind_add_sub", - CmdCopyPageURL: "keybindings.bind_copy_page_url", - CmdCopyTargetURL: "keybindings.bind_copy_target_url", - CmdBeginning: "keybindings.bind_beginning", - CmdEnd: "keybindings.bind_end", + CmdLink1: "keybindings.bind_link1", + CmdLink2: "keybindings.bind_link2", + CmdLink3: "keybindings.bind_link3", + CmdLink4: "keybindings.bind_link4", + CmdLink5: "keybindings.bind_link5", + CmdLink6: "keybindings.bind_link6", + CmdLink7: "keybindings.bind_link7", + CmdLink8: "keybindings.bind_link8", + CmdLink9: "keybindings.bind_link9", + CmdLink0: "keybindings.bind_link0", + CmdBottom: "keybindings.bind_bottom", + CmdEdit: "keybindings.bind_edit", + CmdHome: "keybindings.bind_home", + CmdBookmarks: "keybindings.bind_bookmarks", + CmdAddBookmark: "keybindings.bind_add_bookmark", + CmdSave: "keybindings.bind_save", + CmdReload: "keybindings.bind_reload", + CmdBack: "keybindings.bind_back", + CmdForward: "keybindings.bind_forward", + CmdMoveUp: "keybindings.bind_moveup", + CmdMoveDown: "keybindings.bind_movedown", + CmdMoveLeft: "keybindings.bind_moveleft", + CmdMoveRight: "keybindings.bind_moveright", + CmdPgup: "keybindings.bind_pgup", + CmdPgdn: "keybindings.bind_pgdn", + CmdNewTab: "keybindings.bind_new_tab", + CmdCloseTab: "keybindings.bind_close_tab", + CmdNextTab: "keybindings.bind_next_tab", + CmdPrevTab: "keybindings.bind_prev_tab", + CmdQuit: "keybindings.bind_quit", + CmdHelp: "keybindings.bind_help", + CmdSub: "keybindings.bind_sub", + CmdAddSub: "keybindings.bind_add_sub", + CmdCopyPageURL: "keybindings.bind_copy_page_url", + CmdCopyTargetURL: "keybindings.bind_copy_target_url", + CmdBeginning: "keybindings.bind_beginning", + CmdEnd: "keybindings.bind_end", + CmdURLHandlerOpen: "keybindings.bind_url_handler_open", } // This is split off to allow shift_numbers to override bind_tab[1-90] // (This is needed for older configs so that the default bind_tab values diff --git a/default-config.toml b/default-config.toml index 1a13574..3cbb7ba 100644 --- a/default-config.toml +++ b/default-config.toml @@ -185,6 +185,7 @@ underline = true # bind_copy_target_url # bind_beginning: moving to beginning of page (top left) # bind_end: same but the for the end (bottom left) +# bind_url_handler_open: Open highlighted URL with URL handler (#143) [url-handlers] # Allows setting the commands to run for various URL schemes. diff --git a/display/handlers.go b/display/handlers.go index 60e9bf1..2be4c3a 100644 --- a/display/handlers.go +++ b/display/handlers.go @@ -216,6 +216,8 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) { } t.mode = tabModeDone + t.preferURLHandler = false + go func(p *structs.Page) { if b && t.hasContent() && !t.isAnAboutPage() && viper.GetBool("subscriptions.popup") { // The current page might be an untracked feed, and the user wants @@ -261,7 +263,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) { } if strings.HasPrefix(u, "http") { - if proxy == "" || proxy == "off" { + if proxy == "" || proxy == "off" || t.preferURLHandler { // No proxy available handleHTTP(u, true) return ret("", false) @@ -280,7 +282,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) { if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "gemini") && !strings.HasPrefix(u, "file") { // Not a Gemini URL - if proxy == "" || proxy == "off" { + if proxy == "" || proxy == "off" || t.preferURLHandler { // No proxy available handleOther(u) return ret("", false) diff --git a/display/help.go b/display/help.go index bc383b3..a6646a4 100644 --- a/display/help.go +++ b/display/help.go @@ -33,6 +33,7 @@ var helpCells = strings.TrimSpace( "Enter, Tab\tOn a page this will start link highlighting.\n" + "\tPress Tab and Shift-Tab to pick different links.\n" + "\tPress Enter again to go to one, or Esc to stop.\n" + + "%s\tOpen the highlighted URL with a URL handler instead of the configured proxy\n" + "%s\tGo to a specific tab. (Default: Shift-NUMBER)\n" + "%s\tGo to the last tab.\n" + "%s\tPrevious tab\n" + @@ -95,6 +96,7 @@ func helpInit() { config.GetKeyBinding(config.CmdEdit), config.GetKeyBinding(config.CmdCopyPageURL), config.GetKeyBinding(config.CmdCopyTargetURL), + config.GetKeyBinding(config.CmdURLHandlerOpen), tabKeys, config.GetKeyBinding(config.CmdTab0), config.GetKeyBinding(config.CmdPrevTab), diff --git a/display/tab.go b/display/tab.go index ec321f8..6c1cfb2 100644 --- a/display/tab.go +++ b/display/tab.go @@ -39,12 +39,13 @@ type tabHistory struct { // tab hold the information needed for each browser tab. type tab struct { - page *structs.Page - view *cview.TextView - history *tabHistory - mode tabMode - barLabel string // The bottomBar label for the tab - barText string // The bottomBar text for the tab + page *structs.Page + view *cview.TextView + history *tabHistory + mode tabMode + barLabel string // The bottomBar label for the tab + barText string // The bottomBar text for the tab + preferURLHandler bool // For #143, use URL handler over proxy } // makeNewTab initializes an tab struct with no content. @@ -98,6 +99,7 @@ func makeNewTab() *tab { linkN, _ := strconv.Atoi(currentSelection[0]) tabs[tab].page.Selected = tabs[tab].page.Links[linkN] tabs[tab].page.SelectedID = currentSelection[0] + tabs[tab].preferURLHandler = false // Reset in case go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN]) return } @@ -216,11 +218,24 @@ func makeNewTab() *tab { return nil } return nil + case config.CmdURLHandlerOpen: + currentSelection := t.view.GetHighlights() + t.preferURLHandler = true + // Copied code from when enter key is pressed + if len(currentSelection) > 0 { + bottomBar.SetLabel("") + linkN, _ := strconv.Atoi(currentSelection[0]) + t.page.Selected = t.page.Links[linkN] + t.page.SelectedID = currentSelection[0] + go followLink(&t, t.page.URL, t.page.Links[linkN]) + } + return nil } // Number key: 1-9, 0, LINK1-LINK10 if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 { if int(cmd) <= len(t.page.Links) { // It's a valid link number + t.preferURLHandler = false // Reset in case go followLink(&t, t.page.URL, t.page.Links[cmd-1]) return nil }