enable/disable arrow key bindings - both local and global

pull/30/head
Simon Let 6 years ago
parent 992178a9c8
commit 53ab420013
  1. 2
      Makefile
  2. 8
      cmd/control/cmd/disable.go
  3. 85
      cmd/control/cmd/enable.go
  4. 2
      conf/config.toml
  5. 3
      go.mod
  6. 8
      go.sum
  7. 2
      pkg/cfg/cfg.go
  8. 12
      scripts/reshctl.sh
  9. 2
      submodules/bash-zsh-compat-widgets

@ -58,7 +58,7 @@ rebuild:
make build make build
clean: clean:
rm resh-* rm bin/resh-*
install: build submodules/bash-preexec/bash-preexec.sh scripts/shellrc.sh conf/config.toml scripts/uuid.sh \ install: build submodules/bash-preexec/bash-preexec.sh scripts/shellrc.sh conf/config.toml scripts/uuid.sh \
| $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/bash_completion.d $(HOME)/.resh/zsh_completion.d | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/bash_completion.d $(HOME)/.resh/zsh_completion.d

@ -20,9 +20,11 @@ var disableArrowKeyBindingsCmd = &cobra.Command{
var disableArrowKeyBindingsGlobalCmd = &cobra.Command{ var disableArrowKeyBindingsGlobalCmd = &cobra.Command{
Use: "arrow_key_bindings_global", Use: "arrow_key_bindings_global",
Short: "disable bindings for arrow keys (up/down) FOR THIS AND ALL FUTURE SHELL SESSIONS", 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) { Run: func(cmd *cobra.Command, args []string) {
// TODO: config set arrow_key_bindings true exitCode = enableDisableArrowKeyBindingsGlobally(false)
exitCode = status.DisableArrowKeyBindings
}, },
} }

@ -1,7 +1,14 @@
package cmd package cmd
import ( import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/curusarn/resh/cmd/control/status" "github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/cfg"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -20,9 +27,81 @@ var enableArrowKeyBindingsCmd = &cobra.Command{
var enableArrowKeyBindingsGlobalCmd = &cobra.Command{ var enableArrowKeyBindingsGlobalCmd = &cobra.Command{
Use: "arrow_key_bindings_global", Use: "arrow_key_bindings_global",
Short: "enable bindings for arrow keys (up/down) FOR THIS AND ALL FUTURE SHELL SESSIONS", Short: "enable bindings for arrow keys (up/down) FOR FUTURE SHELL SESSIONS",
Long: "Enable 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) { Run: func(cmd *cobra.Command, args []string) {
// TODO: config set arrow_key_bindings true exitCode = enableDisableArrowKeyBindingsGlobally(true)
exitCode = status.EnableArrowKeyBindings
}, },
} }
func enableDisableArrowKeyBindingsGlobally(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
}
shell, found := os.LookupEnv("__RESH_ctl_shell")
// shell env variable must be set and must be equal to either bash or zsh
if found == false || (shell != "bash" && shell != "zsh") {
fmt.Println("Error while determining a shell you are using - your RESH instalation is probably broken. Please reinstall RESH - exiting!")
fmt.Println("found=", found, "shell=", shell)
return status.Fail
}
if shell == "bash" {
err := setConfigBindArrowKey(configPath, &config, &config.BindArrowKeysBash, shell, value)
if err != nil {
return status.Fail
}
} else if shell == "zsh" {
err := setConfigBindArrowKey(configPath, &config, &config.BindArrowKeysZsh, shell, value)
if err != nil {
return status.Fail
}
} else {
fmt.Println("FATAL ERROR while determining a shell you are using - your RESH instalation is probably broken. Please reinstall RESH - exiting!")
}
return status.Success
}
// I don't like the interface this function has - passing both config structure and a part of it feels wrong
// It's ugly and could lead to future errors
func setConfigBindArrowKey(configPath string, config *cfg.Config, configField *bool, shell string, value bool) error {
if *configField == value {
if value {
fmt.Println("The RESH arrow key bindings are ALREADY GLOBALLY ENABLED for all future " + shell + " sessions - nothing to do - exiting.")
} else {
fmt.Println("The RESH arrow key bindings are ALREADY GLOBALLY DISABLED for all future " + shell + " sessions - nothing to do - exiting.")
}
return nil
}
if value {
fmt.Println("ENABLING the RESH arrow key bindings GLOBALLY (in " + shell + ") ...")
} else {
fmt.Println("DISABLING the RESH arrow key bindings GLOBALLY (in " + shell + ") ...")
}
*configField = value
f, err := os.Create(configPath)
if err != nil {
fmt.Println("Error: Failed to create/open file:", configPath, "; error:", err)
return err
}
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 err
}
if value {
fmt.Println("SUCCESSFULLY ENABLED the RESH arrow key bindings GLOBALLY (in " + shell + ") " +
"- every new (" + shell + ") session will start with enabled RESH arrow key bindings!")
} else {
fmt.Println("SUCCESSFULLY DISABLED the RESH arrow key bindings GLOBALLY (in " + shell + ") " +
"- every new (" + shell + ") session will start with " + shell + " default arrow key bindings!")
}
return nil
}

@ -2,3 +2,5 @@ port = 2627
sesswatchPeriodSeconds = 120 sesswatchPeriodSeconds = 120
sesshistInitHistorySize = 1000 sesshistInitHistorySize = 1000
debug = true debug = true
bindArrowKeysBash = true
bindArrowKeysZsh = true

@ -4,14 +4,11 @@ go 1.12
require ( require (
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629 github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629
github.com/mattn/go-shellwords v1.0.6 github.com/mattn/go-shellwords v1.0.6
github.com/mb-14/gomarkov v0.0.0-20190125094512-044dd0dcb5e7 github.com/mb-14/gomarkov v0.0.0-20190125094512-044dd0dcb5e7
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b
github.com/schollz/progressbar v1.0.0 github.com/schollz/progressbar v1.0.0
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
github.com/wcharczuk/go-chart v2.0.1+incompatible
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa
golang.org/x/image v0.0.0-20190902063713-cb417be4ba39 // indirect
) )

@ -7,9 +7,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629 h1:1dSBUfGlorLAua2CRx0zFN7kQsTpE2DQSmr7rrTNgY8= github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629 h1:1dSBUfGlorLAua2CRx0zFN7kQsTpE2DQSmr7rrTNgY8=
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629/go.mod h1:mb5nS4uRANwOJSZj8rlCWAfAcGi72GGMIXx+xGOjA7M= github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629/go.mod h1:mb5nS4uRANwOJSZj8rlCWAfAcGi72GGMIXx+xGOjA7M=
@ -37,15 +36,12 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/wcharczuk/go-chart v2.0.1+incompatible h1:0pz39ZAycJFF7ju/1mepnk26RLVLBCWz1STcD3doU0A=
github.com/wcharczuk/go-chart v2.0.1+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE=
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa h1:rW+Lu6281ed/4XGuVIa4/YebTRNvoUJlfJ44ktEVwZk= github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa h1:rW+Lu6281ed/4XGuVIa4/YebTRNvoUJlfJ44ktEVwZk=
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa/go.mod h1:2rx5KE5FLD0HRfkkpyn8JwbVLBdhgeiOb2D2D9LLKM4= github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa/go.mod h1:2rx5KE5FLD0HRfkkpyn8JwbVLBdhgeiOb2D2D9LLKM4=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/image v0.0.0-20190902063713-cb417be4ba39 h1:4dQcAORh9oYBwVSBVIkP489LUPC+f1HBkTYXgmqfR+o=
golang.org/x/image v0.0.0-20190902063713-cb417be4ba39/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

@ -6,4 +6,6 @@ type Config struct {
SesswatchPeriodSeconds uint SesswatchPeriodSeconds uint
SesshistInitHistorySize int SesshistInitHistorySize int
Debug bool Debug bool
BindArrowKeysBash bool
BindArrowKeysZsh bool
} }

@ -6,7 +6,7 @@
__resh_bind_arrows() { __resh_bind_arrows() {
if [ "${__RESH_arrow_keys_bind_enabled-0}" != 0 ]; then if [ "${__RESH_arrow_keys_bind_enabled-0}" != 0 ]; then
echo "Error: RESH arrow key bindings are already enabled!" echo "RESH arrow key bindings are already enabled!"
return 1 return 1
fi fi
bindfunc --revert '\e[A' __resh_widget_arrow_up_compat bindfunc --revert '\e[A' __resh_widget_arrow_up_compat
@ -28,17 +28,21 @@ __resh_unbind_arrows() {
echo "Error: Can't disable arrow key bindings because they are not enabled!" echo "Error: Can't disable arrow key bindings because they are not enabled!"
return 1 return 1
fi fi
if [ -z "${__RESH_bindfunc_revert_arrow_up_bind+x}" ]; then if [ -z "${__RESH_bindfunc_revert_arrow_up_bind+x}" ]; then
echo "Warn: Couldn't revert arrow UP binding because 'revert command' is empty." echo "Warn: Couldn't revert arrow UP binding because 'revert command' is empty."
else else
eval "$__RESH_bindfunc_revert_arrow_up_bind" eval "$__RESH_bindfunc_revert_arrow_up_bind"
echo "RESH arrow up binding successfully disabled ✓" echo "RESH arrow up binding successfully disabled ✓"
__RESH_arrow_keys_bind_enabled=0
fi fi
if [ -z "${__RESH_bindfunc_revert_arrow_down_bind+x}" ]; then if [ -z "${__RESH_bindfunc_revert_arrow_down_bind+x}" ]; then
echo "Warn: Couldn't revert arrow DOWN binding because 'revert command' is empty." echo "Warn: Couldn't revert arrow DOWN binding because 'revert command' is empty."
else else
eval "$__RESH_bindfunc_revert_arrow_down_bind" eval "$__RESH_bindfunc_revert_arrow_down_bind"
echo "RESH arrow down binding successfully disabled ✓" echo "RESH arrow down binding successfully disabled ✓"
__RESH_arrow_keys_bind_enabled=0
fi fi
return 0 return 0
} }
@ -60,10 +64,16 @@ __resh_unbind_all() {
} }
reshctl() { reshctl() {
# local log=~/.resh/reshctl.log
# export current shell because resh-control needs to know
export __RESH_ctl_shell=$__RESH_SHELL
# run resh-control aka the real reshctl # run resh-control aka the real reshctl
resh-control "$@" resh-control "$@"
# modify current shell session based on exit status # modify current shell session based on exit status
local _status=$? local _status=$?
# unexport current shell
unset __RESH_ctl_shell
case "$_status" in case "$_status" in
0|1) 0|1)
# success | fail # success | fail

@ -1 +1 @@
Subproject commit 8677b8a6b87c6d44371e5aa38f9e480ade06e1a0 Subproject commit c3077fcdf2e20efb95dd27a53766b78533ab7bc4
Loading…
Cancel
Save