From 57ca7d8dbaf5872faacc73f85ac3ba792a74c62f Mon Sep 17 00:00:00 2001 From: Simon Roberts Date: Mon, 18 Oct 2021 16:16:01 +1100 Subject: [PATCH] Make default shortcuts editable (#234) * Add missing actions * Make default shortcuts editable --- cointop/actions.go | 12 ++++++++++++ cointop/config.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cointop/actions.go b/cointop/actions.go index a00fce0..3b7c567 100644 --- a/cointop/actions.go +++ b/cointop/actions.go @@ -57,6 +57,18 @@ func ActionsMap() map[string]bool { "toggle_show_portfolio": true, "enlarge_chart": true, "shorten_chart": true, + "toggle_chart_fullscreen": true, + "scroll_right": true, + "show_portfolio_edit_menu": true, + "sort_column_percent_holdings": true, + "toggle_portfolio_balances": true, + "scroll_left": true, + "save": true, + "toggle_table_fullscreen": true, + "toggle_price_alerts": true, + "move_down_or_next_page": true, + "show_price_alert_add_menu": true, + "sort_column_balance": true, } } diff --git a/cointop/config.go b/cointop/config.go index 58c467e..aa81a49 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -366,14 +366,44 @@ func (ct *Cointop) loadTableColumnsFromConfig() error { // LoadShortcutsFromConfig loads keyboard shortcuts from config file to struct func (ct *Cointop) loadShortcutsFromConfig() error { log.Debug("loadShortcutsFromConfig()") + + // Load the shortcut config into a key:action map (filtering to actions that exist). Keep track of actions. + config := make(map[string]string) + actions := make(map[string]bool) for k, ifc := range ct.config.Shortcuts { if v, ok := ifc.(string); ok { if !ct.ActionExists(v) { + log.Debugf("Shortcut '%s'=>%s is not a valid action", k, v) continue } - ct.State.shortcutKeys[k] = v + config[k] = v + actions[v] = true } } + + // Count how many keys are configured per action. + actionCount := make(map[string]int) + for _, action := range ct.State.shortcutKeys { + actionCount[action] += 1 + } + + // merge defaults into the loaded config - if the key is not defined, and the action is not found, add it + for key, action := range ct.State.shortcutKeys { + if _, ok := config[key]; ok { + // k is already in the config - ignore it + } else if _, ok := actions[action]; ok { + if actionCount[action] == 1 { + // action is already in the config - ignore it + } else { + // there are multiple bindings, add them anyway + config[key] = action // add action + } + } else { + config[key] = action // add action + } + } + ct.State.shortcutKeys = config + return nil }