Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding bindings to yank the current URL and the selected URL #225

Merged
merged 5 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func Init() error {
viper.SetDefault("keybindings.bind_tab8", "*")
viper.SetDefault("keybindings.bind_tab9", "(")
viper.SetDefault("keybindings.bind_tab0", ")")
viper.SetDefault("keybindings.bind_yank_page_uri", "Y")
viper.SetDefault("keybindings.bind_yank_target_uri", "y")
viper.SetDefault("keybindings.shift_numbers", "")
viper.SetDefault("url-handlers.other", "off")
viper.SetDefault("cache.max_size", 0)
Expand Down
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ scrollbar = "auto"
# bind_help
# bind_sub: for viewing the subscriptions page
# bind_add_sub
# bind_yank_page_uri
# bind_yank_target_uri

[url-handlers]
# Allows setting the commands to run for various URL schemes.
Expand Down
62 changes: 33 additions & 29 deletions config/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const (
CmdHelp
CmdSub
CmdAddSub
CmdYankPageURI
CmdYankTargetURI
)

type keyBinding struct {
Expand Down Expand Up @@ -147,35 +149,37 @@ 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",
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",
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",
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",
CmdYankPageURI: "keybindings.bind_yank_page_uri",
CmdYankTargetURI: "keybindings.bind_yank_target_uri",
}
// 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
2 changes: 2 additions & 0 deletions default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ scrollbar = "auto"
# bind_help
# bind_sub: for viewing the subscriptions page
# bind_add_sub
# bind_yank_page_uri
# bind_yank_target_uri

[url-handlers]
# Allows setting the commands to run for various URL schemes.
Expand Down
28 changes: 28 additions & 0 deletions display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"code.rocketnine.space/tslocum/cview"
"github.com/atotto/clipboard"
"github.com/gdamore/tcell/v2"
"github.com/makeworld-the-better-one/amfora/cache"
"github.com/makeworld-the-better-one/amfora/config"
Expand Down Expand Up @@ -352,6 +353,26 @@ func Init(version, commit, builtBy string) {
case config.CmdAddSub:
go addSubscription()
return nil
case config.CmdYankPageURI:
currentURI := tabs[curTab].page.URL
addToClipboard(currentURI)
return nil
case config.CmdYankTargetURI:
currentURI := tabs[curTab].page.URL
selectedURI := GetCurrentURI()
// Handle absolute URIs
if strings.HasPrefix(selectedURI, "gemini") || strings.HasPrefix(selectedURI, "http") {
addToClipboard(selectedURI)
return nil
} else if strings.HasPrefix(selectedURI, "/") {
// Handle relative URIs
u, _ := url.Parse(currentURI)
addToClipboard(u.Scheme + "://" + u.Host + selectedURI)
return nil
} else {
addToClipboard(currentURI + selectedURI)
return nil
}
}

// Number key: 1-9, 0, LINK1-LINK10
Expand Down Expand Up @@ -576,3 +597,10 @@ func URL(u string) {
func NumTabs() int {
return len(tabs)
}

func addToClipboard(text string) {
err := clipboard.WriteAll(text)
if err != nil {
return
}
}
4 changes: 4 additions & 0 deletions display/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var helpCells = strings.TrimSpace(
"\tinstead of the current one.\n" +
"%s\tGo to links 1-10 respectively.\n" +
"%s\tEdit current URL\n" +
"%s\tYank current page URL\n" +
"%s\tYank current target URL\n" +
"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" +
Expand Down Expand Up @@ -85,6 +87,8 @@ func helpInit() {
config.GetKeyBinding(config.CmdBottom),
linkKeys,
config.GetKeyBinding(config.CmdEdit),
config.GetKeyBinding(config.CmdYankPageURI),
config.GetKeyBinding(config.CmdYankTargetURI),
tabKeys,
config.GetKeyBinding(config.CmdTab0),
config.GetKeyBinding(config.CmdPrevTab),
Expand Down
12 changes: 12 additions & 0 deletions display/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,15 @@ func (t *tab) applyAll() {
t.applyBottomBar()
}
}

// GetCurrentURI returns the currently selected URI
func GetCurrentURI() string {
lostleonardo marked this conversation as resolved.
Show resolved Hide resolved
currentSelection := tabs[curTab].view.GetHighlights()

if len(currentSelection) > 0 {
linkN, _ := strconv.Atoi(currentSelection[0])
selectedURI := tabs[curTab].page.Links[linkN]
return selectedURI
}
return ""
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
code.rocketnine.space/tslocum/cview v1.5.4
github.com/atotto/clipboard v0.1.4 // indirect
makew0rld marked this conversation as resolved.
Show resolved Hide resolved
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gdamore/tcell/v2 v2.2.1-0.20210305060500-f4d402906fa3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
Expand Down