Add reshctl to enable/disable RESH features

pull/18/head
Simon Let 6 years ago
parent 583224527f
commit 82a40f7f29
  1. 21
      Makefile
  2. 48
      cmd/control/cmd/completion.go
  3. 24
      cmd/control/cmd/disable.go
  4. 24
      cmd/control/cmd/enable.go
  5. 39
      cmd/control/cmd/root.go
  6. 17
      cmd/control/main.go
  7. 17
      cmd/control/status/status.go
  8. 1
      go.mod
  9. 30
      go.sum
  10. 37
      scripts/bindutil.sh
  11. 33
      scripts/reshctl.sh
  12. 41
      scripts/shellrc.sh
  13. 10
      scripts/widgets.sh

@ -41,7 +41,7 @@ sanitize:
#
#
build: test_go submodules bin/resh-collect bin/resh-daemon bin/resh-evaluate bin/resh-sanitize
build: submodules bin/resh-collect bin/resh-daemon bin/resh-evaluate bin/resh-sanitize bin/resh-control
test_go:
# Running tests
@ -50,7 +50,7 @@ test_go:
go test $$dir/*.go ; \
done
test:
test: test_go
scripts/test.sh
rebuild:
@ -60,15 +60,24 @@ rebuild:
clean:
rm resh-*
install: build submodules/bash-preexec/bash-preexec.sh scripts/shellrc.sh conf/config.toml scripts/uuid.sh | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config
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
# Copying files to resh directory ...
cp -f submodules/bash-preexec/bash-preexec.sh ~/.bash-preexec.sh
cp -f submodules/bash-zsh-compat-widgets/bindfunc.sh ~/.resh/bindfunc.sh
cp -f conf/config.toml ~/.config/resh.toml
cp -f scripts/shellrc.sh ~/.resh/shellrc
cp -f scripts/reshctl.sh scripts/bindutil.sh scripts/widgets.sh ~/.resh/
bin/resh-control completion bash > ~/.resh/bash_completion.d/_reshctl
bin/resh-control completion zsh > ~/.resh/zsh_completion.d/_reshctl
cp -f scripts/uuid.sh ~/.resh/bin/resh-uuid
cp -f bin/* ~/.resh/bin/
cp -f scripts/resh-evaluate-plot.py ~/.resh/bin/
cp -fr data/sanitizer ~/.resh/
cp -fr data/sanitizer ~/.resh/sanitizer_data
# backward compatibility: We have a new location for resh history file
[ ! -f ~/.resh/history.json ] || mv ~/.resh/history.json ~/.resh_history.json
# Adding resh shellrc to .bashrc ...
@ -116,10 +125,12 @@ uninstall:
# Uninstalling ...
-rm -rf ~/.resh/
bin/resh-control: cmd/control/cmd/*.go
bin/resh-%: cmd/%/main.go pkg/*/*.go VERSION
go build ${GOFLAGS} -o $@ cmd/$*/*.go
$(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config:
$(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/bash_completion.d $(HOME)/.resh/zsh_completion.d:
# Creating dirs ...
mkdir -p $@

@ -0,0 +1,48 @@
package cmd
import (
"os"
"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash/zsh completion scripts",
Long: `To load completion run
. <(reshctl completion bash)
OR
. <(reshctl completion zsh)
`,
}
var completionBashCmd = &cobra.Command{
Use: "bash",
Short: "Generates bash completion scripts",
Long: `To load completion run
. <(reshctl completion bash)
`,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(os.Stdout)
exitCode = status.Success
},
}
var completionZshCmd = &cobra.Command{
Use: "zsh",
Short: "Generates zsh completion scripts",
Long: `To load completion run
. <(reshctl completion zsh)
`,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenZshCompletion(os.Stdout)
exitCode = status.Success
},
}

@ -0,0 +1,24 @@
package cmd
import (
"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)
var disableCmd = &cobra.Command{
Use: "disable",
Short: "disable RESH features",
Long: `Disables RESH bindings for arrows and C-R.`,
Run: func(cmd *cobra.Command, args []string) {
exitCode = status.DisableAll
},
}
// var disableRecallingCmd = &cobra.Command{
// Use: "keybind",
// Short: "Disables RESH bindings for arrows and C-R.",
// Long: `Disables RESH bindings for arrows and C-R.`,
// Run: func(cmd *cobra.Command, args []string) {
// exitCode = status.DisableAll
// },
// }

@ -0,0 +1,24 @@
package cmd
import (
"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)
var enableCmd = &cobra.Command{
Use: "enable",
Short: "enable RESH features",
Long: `Enables RESH bindings for arrows and C-R.`,
Run: func(cmd *cobra.Command, args []string) {
exitCode = status.EnableAll
},
}
// var enableRecallingCmd = &cobra.Command{
// Use: "keybind",
// Short: "Enables RESH bindings for arrows and C-R.",
// Long: `Enables RESH bindings for arrows and C-R.`,
// Run: func(cmd *cobra.Command, args []string) {
// exitCode = status.EnableAll
// },
// }

@ -0,0 +1,39 @@
package cmd
import (
"fmt"
"log"
"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)
var exitCode status.Code = status.DefaultInvalid
var rootCmd = &cobra.Command{
Use: "reshctl",
Short: "Reshctl (RESH control) - enables you to enable/disable features and more.",
Long: `Enables you to enable/disable RESH bindings for arrows and C-R.`,
}
// Execute reshctl
func Execute() status.Code {
rootCmd.AddCommand(disableCmd)
// disableCmd.AddCommand(disableRecallingCmd)
rootCmd.AddCommand(enableCmd)
// enableCmd.AddCommand(enableRecallingCmd)
rootCmd.AddCommand(completionCmd)
completionCmd.AddCommand(completionBashCmd)
completionCmd.AddCommand(completionZshCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
return status.Fail
}
if exitCode == status.DefaultInvalid {
log.Println("reshctl FATAL ERROR: (sub)command didn't set exitCode!")
return status.Fail
}
return exitCode
}

@ -0,0 +1,17 @@
package main
import (
"os"
"github.com/curusarn/resh/cmd/control/cmd"
)
// Version from git set during build
var Version string
// Revision from git set during build
var Revision string
func main() {
os.Exit(int(cmd.Execute()))
}

@ -0,0 +1,17 @@
package status
// Code - exit code of the resh-control command
type Code int
const (
// DefaultInvalid exit code
DefaultInvalid Code = -1
// Success exit code
Success = 0
// Fail exit code
Fail = 1
// EnableAll exit code - tells reshctl() wrapper to enable_all
EnableAll = 100
// DisableAll exit code - tells reshctl() wrapper to disable_all
DisableAll = 110
)

@ -9,6 +9,7 @@ require (
github.com/mattn/go-shellwords v1.0.6
github.com/mb-14/gomarkov v0.0.0-20190125094512-044dd0dcb5e7
github.com/schollz/progressbar v1.0.0
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
golang.org/x/image v0.0.0-20190902063713-cb417be4ba39 // indirect

@ -1,19 +1,49 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
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/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/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/go.mod h1:mb5nS4uRANwOJSZj8rlCWAfAcGi72GGMIXx+xGOjA7M=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-shellwords v1.0.6 h1:9Jok5pILi5S1MnDirGVTufYGtksUs/V2BWUP3ZkeUUI=
github.com/mattn/go-shellwords v1.0.6/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mb-14/gomarkov v0.0.0-20190125094512-044dd0dcb5e7 h1:VsJjhYhufMGXICLwLYr8mFVMp8/A+YqmagMHnG/BA/4=
github.com/mb-14/gomarkov v0.0.0-20190125094512-044dd0dcb5e7/go.mod h1:zQmHoMvvVJb7cxyt1wGT77lqUaeOFXlogOppOr4uHVo=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/schollz/progressbar v1.0.0 h1:gbyFReLHDkZo8mxy/dLWMr+Mpb1MokGJ1FqCiqacjZM=
github.com/schollz/progressbar v1.0.0/go.mod h1:/l9I7PC3L3erOuz54ghIRKUEFcosiWfLvJv+Eq26UMs=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
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/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/go.mod h1:2rx5KE5FLD0HRfkkpyn8JwbVLBdhgeiOb2D2D9LLKM4=
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/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/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/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

@ -0,0 +1,37 @@
# shellcheck source=../submodules/bash-zsh-compat-widgets/bindfunc.sh
. ~/.resh/bindfunc.sh
# shellcheck source=widgets.sh
. ~/.resh/widgets.sh
__resh_bind_arrows() {
echo "bindfunc __resh_widget_arrow_up"
echo "bindfunc __resh_widget_arrow_down"
return 0
}
__resh_bind_control_R() {
echo "bindfunc __resh_widget_control_R"
return 0
}
__resh_unbind_arrows() {
echo "\ bindfunc __resh_widget_arrow_up"
echo "\ bindfunc __resh_widget_arrow_down"
return 0
}
__resh_unbind_control_R() {
echo "\ bindfunc __resh_widget_control_R"
return 0
}
__resh_bind_all() {
__resh_bind_arrows
__resh_bind_control_R
}
__resh_unbind_all() {
__resh_unbind_arrows
__resh_unbind_control_R
}

@ -0,0 +1,33 @@
# source bindutil - contains functions to bind and unbind RESH widgets
# shellcheck source=bindutil.sh
. ~/.resh/bindutil.sh
reshctl() {
# run resh-control aka the real reshctl
resh-control "$@"
# modify current shell session based on exit status
local status=$?
case "$status" in
0|1)
# success | fail
return "$status"
;;
# enable
100)
# enable all
__resh_bind_all
return 0
;;
# disable
110)
# disable all
__resh_unbind_all
return 0
;;
*)
echo "reshctl() FATAL ERROR: unknown status" >&2
return "$status"
;;
esac
}

@ -4,6 +4,9 @@ PATH=$PATH:~/.resh/bin
# zmodload zsh/datetime
# fi
# shellcheck source=reshctl.sh
. ~/.resh/reshctl.sh
__resh_get_uuid() {
cat /proc/sys/kernel/random/uuid 2>/dev/null || resh-uuid
}
@ -40,6 +43,27 @@ __resh_run_daemon() {
nohup resh-daemon &>/dev/null & disown
}
__resh_bash_completion_init() {
local bash_completion_dir=~/.resh/bash_completion.d
# source user completion directory definitions
# taken from /usr/share/bash-completion/bash_completion
if [[ -d $bash_completion_dir && -r $bash_completion_dir && \
-x $bash_completion_dir ]]; then
for i in $(LC_ALL=C command ls "$bash_completion_dir"); do
i=$bash_completion_dir/$i
# shellcheck disable=SC2154
# shellcheck source=/dev/null
[[ ${i##*/} != @($_backup_glob|Makefile*|$_blacklist_glob) \
&& -f $i && -r $i ]] && . "$i"
done
fi
}
__resh_zsh_completion_init() {
# shellcheck disable=SC2206
fpath=(~/.resh/zsh_completion.d $fpath)
}
__RESH_MACOS=0
__RESH_LINUX=0
__RESH_UNAME=$(uname)
@ -53,19 +77,22 @@ else
fi
if [ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC1009
__RESH_SHELL="zsh"
__RESH_HOST="$HOST"
__RESH_HOSTTYPE="$CPUTYPE"
__resh_zsh_completion_init
elif [ -n "$BASH_VERSION" ]; then
__RESH_SHELL="bash"
__RESH_HOST="$HOSTNAME"
__RESH_HOSTTYPE="$HOSTTYPE"
__resh_bash_completion_init
else
echo "resh PANIC unrecognized shell"
fi
if [ -z "${__RESH_SESSION_ID+x}" ]; then
export __RESH_SESSION_ID=$(__resh_get_uuid)
export __RESH_SESSION_ID; __RESH_SESSION_ID=$(__resh_get_uuid)
export __RESH_SESSION_PID="$$"
# TODO add sesson time
fi
@ -152,20 +179,22 @@ __resh_precmd() {
__RESH_TZ_AFTER=$(date +%z)
__RESH_PWD_AFTER="$PWD"
if [ -n "${__RESH_COLLECT}" ]; then
if [ "$__RESH_VERSION" != $(resh-collect -version) ]; then
if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then
# shellcheck source=shellrc.sh
source ~/.resh/shellrc
if [ "$__RESH_VERSION" != $(resh-collect -version) ]; then
if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then
echo "RESH WARNING: You probably just updated RESH - PLEASE RESTART OR RELOAD THIS TERMINAL SESSION (resh version: $(resh-collect -version); resh version of this terminal session: ${__RESH_VERSION})"
else
echo "RESH INFO: New RESH shellrc script was loaded - if you encounter any issues please restart this terminal session."
fi
elif [ "$__RESH_REVISION" != $(resh-collect -revision) ]; then
elif [ "$__RESH_REVISION" != "$(resh-collect -revision)" ]; then
# shellcheck source=shellrc.sh
source ~/.resh/shellrc
if [ "$__RESH_REVISION" != $(resh-collect -revision) ]; then
if [ "$__RESH_REVISION" != "$(resh-collect -revision)" ]; then
echo "RESH WARNING: You probably just updated RESH - PLEASE RESTART OR RELOAD THIS TERMINAL SESSION (resh revision: $(resh-collect -revision); resh revision of this terminal session: ${__RESH_REVISION})"
fi
fi
if [ "$__RESH_VERSION" = $(resh-collect -version) ] && [ "$__RESH_REVISION" = $(resh-collect -revision) ]; then
if [ "$__RESH_VERSION" = "$(resh-collect -version)" ] && [ "$__RESH_REVISION" = "$(resh-collect -revision)" ]; then
resh-collect -requireVersion "$__RESH_VERSION" \
-requireRevision "$__RESH_REVISION" \
-cmdLine "$__RESH_CMDLINE" \

@ -0,0 +1,10 @@
__resh_widget_arrow_up() {
return 0
}
__resh_widget_arrow_down() {
return 0
}
__resh_widget_control_R() {
return 0
}
Loading…
Cancel
Save