Skip to content

Commit

Permalink
Keybinding to open URL with URL handler instead of proxy
Browse files Browse the repository at this point in the history
Fixes #143
  • Loading branch information
makew0rld committed Dec 28, 2021
1 parent 1aa13f2 commit 33bf960
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 39 additions & 37 deletions config/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
CmdCopyTargetURL
CmdBeginning
CmdEnd
CmdURLHandlerOpen // See #143
)

type keyBinding struct {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions display/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions display/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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),
Expand Down
27 changes: 21 additions & 6 deletions display/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 33bf960

Please sign in to comment.