diff --git a/config/config.go b/config/config.go index 6d81f22..da99831 100644 --- a/config/config.go +++ b/config/config.go @@ -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_copy_page_url", "C") + viper.SetDefault("keybindings.bind_copy_target_url", "c") viper.SetDefault("keybindings.shift_numbers", "") viper.SetDefault("url-handlers.other", "off") viper.SetDefault("cache.max_size", 0) diff --git a/config/default.go b/config/default.go index bd6221f..410f740 100644 --- a/config/default.go +++ b/config/default.go @@ -158,6 +158,8 @@ scrollbar = "auto" # bind_help # bind_sub: for viewing the subscriptions page # bind_add_sub +# bind_copy_page_url +# bind_copy_target_url [url-handlers] # Allows setting the commands to run for various URL schemes. diff --git a/config/keybindings.go b/config/keybindings.go index f23b1ce..ee5a7a9 100644 --- a/config/keybindings.go +++ b/config/keybindings.go @@ -52,6 +52,8 @@ const ( CmdHelp CmdSub CmdAddSub + CmdCopyPageURL + CmdCopyTargetURL ) type keyBinding struct { @@ -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", + CmdCopyPageURL: "keybindings.bind_copy_page_url", + CmdCopyTargetURL: "keybindings.bind_copy_target_url", } // 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 cc73465..f63c917 100644 --- a/default-config.toml +++ b/default-config.toml @@ -155,6 +155,8 @@ scrollbar = "auto" # bind_help # bind_sub: for viewing the subscriptions page # bind_add_sub +# bind_copy_page_url +# bind_copy_target_url [url-handlers] # Allows setting the commands to run for various URL schemes. diff --git a/display/display.go b/display/display.go index b94c5b9..4771787 100644 --- a/display/display.go +++ b/display/display.go @@ -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" @@ -352,6 +353,36 @@ func Init(version, commit, builtBy string) { case config.CmdAddSub: go addSubscription() return nil + case config.CmdCopyPageURL: + currentURL := tabs[curTab].page.URL + err := clipboard.WriteAll(currentURL) + if err != nil { + Error("Copy Error", err.Error()) + return nil + } + return nil + case config.CmdCopyTargetURL: + currentURL := tabs[curTab].page.URL + selectedURL := tabs[curTab].HighlightedURL() + if selectedURL == "" { + return nil + } + u, _ := url.Parse(currentURL) + copiedURL, err := u.Parse(selectedURL) + if err != nil { + err := clipboard.WriteAll(selectedURL) + if err != nil { + Error("Copy Error", err.Error()) + return nil + } + return nil + } + err = clipboard.WriteAll(copiedURL.String()) + if err != nil { + Error("Copy Error", err.Error()) + return nil + } + return nil } // Number key: 1-9, 0, LINK1-LINK10 diff --git a/display/help.go b/display/help.go index 2f9c437..9d1d927 100644 --- a/display/help.go +++ b/display/help.go @@ -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\tCopy current page URL\n" + + "%s\tCopy current selected 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" + @@ -85,6 +87,8 @@ func helpInit() { config.GetKeyBinding(config.CmdBottom), linkKeys, config.GetKeyBinding(config.CmdEdit), + config.GetKeyBinding(config.CmdCopyPageURL), + config.GetKeyBinding(config.CmdCopyTargetURL), tabKeys, config.GetKeyBinding(config.CmdTab0), config.GetKeyBinding(config.CmdPrevTab), diff --git a/display/tab.go b/display/tab.go index e5acad9..402f5f6 100644 --- a/display/tab.go +++ b/display/tab.go @@ -323,3 +323,15 @@ func (t *tab) applyAll() { t.applyBottomBar() } } + +// HighlightedURL returns the currently selected URL +func (t *tab) HighlightedURL() string { + currentSelection := tabs[curTab].view.GetHighlights() + + if len(currentSelection) > 0 { + linkN, _ := strconv.Atoi(currentSelection[0]) + selectedURL := tabs[curTab].page.Links[linkN] + return selectedURL + } + return "" +} diff --git a/go.mod b/go.mod index dfb59ce..61030ce 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( code.rocketnine.space/tslocum/cview v1.5.4 + github.com/atotto/clipboard v0.1.4 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 diff --git a/go.sum b/go.sum index 69e5e7b..1bf7c0b 100644 --- a/go.sum +++ b/go.sum @@ -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=