diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 7d9a303..dccfc18 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -4,9 +4,8 @@ import ( "bytes" "encoding/json" "errors" - "flag" "fmt" - "io/ioutil" + "io" "net/http" "os" "sort" @@ -20,9 +19,11 @@ import ( "github.com/curusarn/resh/internal/device" "github.com/curusarn/resh/internal/logger" "github.com/curusarn/resh/internal/msg" + "github.com/curusarn/resh/internal/opt" "github.com/curusarn/resh/internal/output" "github.com/curusarn/resh/internal/recordint" "github.com/curusarn/resh/internal/searchapp" + "github.com/spf13/pflag" "go.uber.org/zap" "strconv" @@ -54,23 +55,28 @@ func main() { } func runReshCli(out *output.Output, config cfg.Config) (string, int) { - sessionID := flag.String("sessionID", "", "resh generated session id") - pwd := flag.String("pwd", "", "present working directory") - gitOriginRemote := flag.String("gitOriginRemote", "DEFAULT", "git origin remote") - query := flag.String("query", "", "search query") - testHistory := flag.String("test-history", "", "load history from a file instead from the daemon (for testing purposes only!)") - testHistoryLines := flag.Int("test-lines", 0, "the number of lines to load from a file passed with --test-history (for testing purposes only!)") - flag.Parse() - - errMsg := "Failed to get necessary command-line arguments" + args := opt.HandleVersionOpts(out, os.Args, version, commit) + + flags := pflag.NewFlagSet("", pflag.ExitOnError) + sessionID := flags.String("session-id", "", "Resh generated session ID") + pwd := flags.String("pwd", "", "$PWD - present working directory") + gitOriginRemote := flags.String("git-origin-remote", "<<>>", "> git origin remote") + query := flags.String("query", "", "Search query") + // TODO: Do we still need this? + testHistory := flags.String("test-history", "", "Load history from a file instead from the daemon (for testing purposes only!)") + testHistoryLines := flags.Int("test-lines", 0, "The number of lines to load from a file passed with --test-history (for testing purposes only!)") + flags.Parse(args) + + // TODO: These errors should tell the user that they should not be running the command directly + errMsg := "Failed to get required command-line arguments" if *sessionID == "" { - out.Fatal(errMsg, errors.New("missing option --sessionId")) + out.Fatal(errMsg, errors.New("missing required option --session-id")) } if *pwd == "" { - out.Fatal(errMsg, errors.New("missing option --pwd")) + out.Fatal(errMsg, errors.New("missing required option --pwd")) } - if *gitOriginRemote == "DEFAULT" { - out.Fatal(errMsg, errors.New("missing option --gitOriginRemote")) + if *gitOriginRemote == "<<>>" { + out.Fatal(errMsg, errors.New("missing required option --git-origin-remote")) } dataDir, err := datadir.GetPath() if err != nil { @@ -244,11 +250,6 @@ func (m manager) AbortPaste(g *gocui.Gui, v *gocui.View) error { return nil } -type dedupRecord struct { - dataIndex int - score float32 -} - func (m manager) UpdateData(input string) { sugar := m.out.Logger.Sugar() sugar.Debugw("Starting data update ...", @@ -432,14 +433,14 @@ func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } -const smallTerminalTresholdWidth = 110 +const smallTerminalThresholdWidth = 110 func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { sugar := m.out.Logger.Sugar() maxX, maxY := g.Size() compactRenderingMode := false - if maxX < smallTerminalTresholdWidth { + if maxX < smallTerminalThresholdWidth { compactRenderingMode = true } @@ -558,7 +559,7 @@ func (m manager) rawMode(g *gocui.Gui, v *gocui.View) error { } displayStr := itm.CmdLineWithColor if m.s.highlightedItem == i { - // use actual min requried length instead of 420 constant + // Use actual min required length instead of 420 constant displayStr = searchapp.DoHighlightString(displayStr, maxX*2) } if strings.Contains(displayStr, "\n") { @@ -599,7 +600,7 @@ func SendCliMsg(out *output.Output, m msg.CliMsg, port string) msg.CliResponse { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { out.Fatal("Failed read response", err) } @@ -609,7 +610,7 @@ func SendCliMsg(out *output.Output, m msg.CliMsg, port string) msg.CliResponse { if err != nil { out.Fatal("Failed decode response", err) } - sugar.Debugw("Recieved records from daemon", + sugar.Debugw("Received records from daemon", "recordCount", len(response.Records), ) return response diff --git a/scripts/hooks.sh b/scripts/hooks.sh index 4e8616a..5ae52b5 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -1,15 +1,5 @@ #!/hint/sh -__resh_reload_msg() { - printf '\n' - printf '+--------------------------------------------------------------+\n' - printf '| New version of RESH shell files was loaded in this terminal. |\n' - printf '| This is an informative message - no action is necessary. |\n' - printf '| Please restart this terminal if you encounter any issues. |\n' - printf '+--------------------------------------------------------------+\n' - printf '\n' -} - # BACKWARDS COMPATIBILITY NOTES: # # Stable names and options: @@ -24,7 +14,16 @@ __resh_reload_msg() { # => The function shows a message from the already updated shell files # => We can drop this function at any time - the old version will be used - +# Backwards compatibilty: Please see notes above before making any changes here. +__resh_reload_msg() { + printf '\n' + printf '+--------------------------------------------------------------+\n' + printf '| New version of RESH shell files was loaded in this terminal. |\n' + printf '| This is an informative message - no action is necessary. |\n' + printf '| Please restart this terminal if you encounter any issues. |\n' + printf '+--------------------------------------------------------------+\n' + printf '\n' +} # (pre)collect # Backwards compatibilty: Please see notes above before making any changes here. @@ -103,4 +102,59 @@ __resh_session_init() { --session-id "$__RESH_SESSION_ID" \ --session-pid "$$" return $? +} + +# Backwards compatibilty: Please see notes above before making any changes here. +__resh_widget_control_R() { + # This is a very bad workaround. + # Force bash-preexec to run repeatedly because otherwise premature run of bash-preexec overshadows the next proper run. + # I honestly think that it's impossible to make widgets work in bash without hacks like this. + # shellcheck disable=2034 + __bp_preexec_interactive_mode="on" + + local PREVBUFFER=$BUFFER + + local status_code + local git_remote; git_remote="$(git remote get-url origin 2>/dev/null)" + if [ "$(resh-cli -version)" != "$__RESH_VERSION" ] && [ -z "${__RESH_NO_RELOAD-}" ]; then + source ~/.resh/shellrc + # Show reload message from the updated shell files + __resh_reload_msg + # Rerun self but prevent another reload. Extra protection against infinite recursion. + __RESH_NO_RELOAD=1 __resh_widget_control_R "$@" + return $? + fi + BUFFER=$(resh-cli -requireVersion "$__RESH_VERSION" \ + --git-origin-remote "$git_remote" \ + --pwd "$PWD" \ + --query "$BUFFER" \ + --session-id "$__RESH_SESSION_ID" \ + ) + status_code=$? + if [ $status_code = 111 ]; then + # execute + if [ -n "${ZSH_VERSION-}" ]; then + # zsh + zle accept-line + elif [ -n "${BASH_VERSION-}" ]; then + # bash + # set chained keyseq to accept-line + bind '"\u[32~": accept-line' + fi + elif [ $status_code = 0 ]; then + if [ -n "${BASH_VERSION-}" ]; then + # bash + # set chained keyseq to nothing + bind -x '"\u[32~": __resh_nop' + fi + else + echo "RESH SEARCH APP failed" + printf "%s" "$buffer" >&2 + BUFFER="$PREVBUFFER" + fi + CURSOR=${#BUFFER} +} + +__resh_widget_control_R_compat() { + __bindfunc_compat_wrapper __resh_widget_control_R } \ No newline at end of file diff --git a/scripts/install.sh b/scripts/install.sh index 6b6b725..9f8c845 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -93,7 +93,7 @@ cp -f submodules/bash-zsh-compat-widgets/bindfunc.sh ~/.resh/bindfunc.sh cp -f scripts/shellrc.sh ~/.resh/shellrc cp -f scripts/resh-daemon-start.sh ~/.resh/bin/resh-daemon-start -cp -f scripts/reshctl.sh scripts/widgets.sh scripts/hooks.sh ~/.resh/ +cp -f scripts/reshctl.sh scripts/hooks.sh ~/.resh/ cp -f scripts/rawinstall.sh ~/.resh/ # Copy all executables. We don't really need to omit install-utils from the bin directory diff --git a/scripts/reshctl.sh b/scripts/reshctl.sh index 2776987..8d620d1 100644 --- a/scripts/reshctl.sh +++ b/scripts/reshctl.sh @@ -2,8 +2,8 @@ # shellcheck source=../submodules/bash-zsh-compat-widgets/bindfunc.sh . ~/.resh/bindfunc.sh -# shellcheck source=widgets.sh -. ~/.resh/widgets.sh +# shellcheck source=hooks.sh +. ~/.resh/hooks.sh __resh_nop() { # does nothing diff --git a/scripts/widgets.sh b/scripts/widgets.sh deleted file mode 100644 index 06d2290..0000000 --- a/scripts/widgets.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/hint/sh - -# shellcheck source=hooks.sh -. ~/.resh/hooks.sh - -__resh_widget_control_R() { - # this is a very bad workaround - # force bash-preexec to run repeatedly because otherwise premature run of bash-preexec overshadows the next poper run - # I honestly think that it's impossible to make widgets work in bash without hacks like this - # shellcheck disable=2034 - __bp_preexec_interactive_mode="on" - - local PREVBUFFER=$BUFFER - - local status_code - local git_remote; git_remote="$(git remote get-url origin 2>/dev/null)" - if ! __resh_maybe_reload; then - return 1 - fi - BUFFER=$(resh-cli --sessionID "$__RESH_SESSION_ID" --pwd "$PWD" --gitOriginRemote "$git_remote" --query "$BUFFER") - status_code=$? - if [ $status_code = 111 ]; then - # execute - if [ -n "${ZSH_VERSION-}" ]; then - # zsh - zle accept-line - elif [ -n "${BASH_VERSION-}" ]; then - # bash - # set chained keyseq to accept-line - bind '"\u[32~": accept-line' - fi - elif [ $status_code = 0 ]; then - if [ -n "${BASH_VERSION-}" ]; then - # bash - # set chained keyseq to nothing - bind -x '"\u[32~": __resh_nop' - fi - else - echo "RESH SEARCH APP failed" - printf "%s" "$buffer" >&2 - BUFFER="$PREVBUFFER" - fi - CURSOR=${#BUFFER} -} - -__resh_widget_control_R_compat() { - __bindfunc_compat_wrapper __resh_widget_control_R -}