|
|
|
|
@ -12,9 +12,11 @@ import ( |
|
|
|
|
"github.com/spf13/cobra" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Enable commands
|
|
|
|
|
|
|
|
|
|
var enableCmd = &cobra.Command{ |
|
|
|
|
Use: "enable", |
|
|
|
|
Short: "enable RESH features (arrow key bindings)", |
|
|
|
|
Short: "enable RESH features (bindings)", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var enableArrowKeyBindingsCmd = &cobra.Command{ |
|
|
|
|
@ -36,6 +38,64 @@ var enableArrowKeyBindingsGlobalCmd = &cobra.Command{ |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var enableControlRBindingCmd = &cobra.Command{ |
|
|
|
|
Use: "ctrl_r_binding", |
|
|
|
|
Short: "enable binding for control+R FOR THIS SHELL SESSION", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = status.EnableControlRBinding |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var enableControlRBindingGlobalCmd = &cobra.Command{ |
|
|
|
|
Use: "ctrl_r_binding_global", |
|
|
|
|
Short: "enable bindings for control+R FOR FUTURE SHELL SESSIONS", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = enableDisableArrowKeyBindingsGlobally(true) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Disable commands
|
|
|
|
|
|
|
|
|
|
var disableCmd = &cobra.Command{ |
|
|
|
|
Use: "disable", |
|
|
|
|
Short: "disable RESH features (bindings)", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var disableArrowKeyBindingsCmd = &cobra.Command{ |
|
|
|
|
Use: "arrow_key_bindings", |
|
|
|
|
Short: "disable bindings for arrow keys (up/down) FOR THIS SHELL SESSION", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = status.DisableArrowKeyBindings |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var disableArrowKeyBindingsGlobalCmd = &cobra.Command{ |
|
|
|
|
Use: "arrow_key_bindings_global", |
|
|
|
|
Short: "disable bindings for arrow keys (up/down) FOR FUTURE SHELL SESSIONS", |
|
|
|
|
Long: "Disable bindings for arrow keys (up/down) FOR FUTURE SHELL SESSIONS.\n" + |
|
|
|
|
"Note that this only affects sessions of the same shell.\n" + |
|
|
|
|
"(e.g. running this in zsh will only affect future zsh sessions)", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = enableDisableControlRBindingGlobally(false) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var disableControlRBindingCmd = &cobra.Command{ |
|
|
|
|
Use: "ctrl_r_binding", |
|
|
|
|
Short: "disable binding for control+R FOR THIS SHELL SESSION", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = status.DisableControlRBinding |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var disableControlRBindingGlobalCmd = &cobra.Command{ |
|
|
|
|
Use: "ctrl_r_binding_global", |
|
|
|
|
Short: "disable bindings for control+R FOR FUTURE SHELL SESSIONS", |
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { |
|
|
|
|
exitCode = enableDisableControlRBindingGlobally(false) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func enableDisableArrowKeyBindingsGlobally(value bool) status.Code { |
|
|
|
|
usr, _ := user.Current() |
|
|
|
|
dir := usr.HomeDir |
|
|
|
|
@ -105,3 +165,47 @@ func setConfigBindArrowKey(configPath string, config *cfg.Config, configField *b |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func enableDisableControlRBindingGlobally(value bool) status.Code { |
|
|
|
|
usr, _ := user.Current() |
|
|
|
|
dir := usr.HomeDir |
|
|
|
|
configPath := filepath.Join(dir, ".config/resh.toml") |
|
|
|
|
var config cfg.Config |
|
|
|
|
if _, err := toml.DecodeFile(configPath, &config); err != nil { |
|
|
|
|
fmt.Println("Error reading config", err) |
|
|
|
|
return status.Fail |
|
|
|
|
} |
|
|
|
|
if config.BindControlR == value { |
|
|
|
|
if value { |
|
|
|
|
fmt.Println("The RESH control+R binding is ALREADY GLOBALLY ENABLED for all future shell sessions - nothing to do - exiting.") |
|
|
|
|
} else { |
|
|
|
|
fmt.Println("The RESH control+R binding is ALREADY GLOBALLY DISABLED for all future shell sessions - nothing to do - exiting.") |
|
|
|
|
} |
|
|
|
|
return status.Fail |
|
|
|
|
} |
|
|
|
|
if value { |
|
|
|
|
fmt.Println("ENABLING the RESH arrow key bindings GLOBALLY ...") |
|
|
|
|
} else { |
|
|
|
|
fmt.Println("DISABLING the RESH arrow key bindings GLOBALLY ...") |
|
|
|
|
} |
|
|
|
|
config.BindControlR = value |
|
|
|
|
|
|
|
|
|
f, err := os.Create(configPath) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println("Error: Failed to create/open file:", configPath, "; error:", err) |
|
|
|
|
return status.Fail |
|
|
|
|
} |
|
|
|
|
defer f.Close() |
|
|
|
|
if err := toml.NewEncoder(f).Encode(config); err != nil { |
|
|
|
|
fmt.Println("Error: Failed to encode and write the config values to hdd. error:", err) |
|
|
|
|
return status.Fail |
|
|
|
|
} |
|
|
|
|
if value { |
|
|
|
|
fmt.Println("SUCCESSFULLY ENABLED the RESH arrow key bindings GLOBALLY " + |
|
|
|
|
"- every new shell session will start with enabled RESH CLI control+R binding!") |
|
|
|
|
} else { |
|
|
|
|
fmt.Println("SUCCESSFULLY DISABLED the RESH arrow key bindings GLOBALLY " + |
|
|
|
|
"- every new shell session will start with your orignal control+R key binding!") |
|
|
|
|
} |
|
|
|
|
return status.Success |
|
|
|
|
} |
|
|
|
|
|