Further simplify shell files, resh-cli improvements

pull/184/head
Simon Let 3 years ago
parent 3e03f4a0d2
commit fcd8c8bfed
  1. 51
      cmd/cli/main.go
  2. 76
      scripts/hooks.sh
  3. 2
      scripts/install.sh
  4. 4
      scripts/reshctl.sh
  5. 48
      scripts/widgets.sh

@ -4,9 +4,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"sort" "sort"
@ -20,9 +19,11 @@ import (
"github.com/curusarn/resh/internal/device" "github.com/curusarn/resh/internal/device"
"github.com/curusarn/resh/internal/logger" "github.com/curusarn/resh/internal/logger"
"github.com/curusarn/resh/internal/msg" "github.com/curusarn/resh/internal/msg"
"github.com/curusarn/resh/internal/opt"
"github.com/curusarn/resh/internal/output" "github.com/curusarn/resh/internal/output"
"github.com/curusarn/resh/internal/recordint" "github.com/curusarn/resh/internal/recordint"
"github.com/curusarn/resh/internal/searchapp" "github.com/curusarn/resh/internal/searchapp"
"github.com/spf13/pflag"
"go.uber.org/zap" "go.uber.org/zap"
"strconv" "strconv"
@ -54,23 +55,28 @@ func main() {
} }
func runReshCli(out *output.Output, config cfg.Config) (string, int) { func runReshCli(out *output.Output, config cfg.Config) (string, int) {
sessionID := flag.String("sessionID", "", "resh generated session id") args := opt.HandleVersionOpts(out, os.Args, version, commit)
pwd := flag.String("pwd", "", "present working directory")
gitOriginRemote := flag.String("gitOriginRemote", "DEFAULT", "git origin remote") flags := pflag.NewFlagSet("", pflag.ExitOnError)
query := flag.String("query", "", "search query") sessionID := flags.String("session-id", "", "Resh generated session ID")
testHistory := flag.String("test-history", "", "load history from a file instead from the daemon (for testing purposes only!)") pwd := flags.String("pwd", "", "$PWD - present working directory")
testHistoryLines := flag.Int("test-lines", 0, "the number of lines to load from a file passed with --test-history (for testing purposes only!)") gitOriginRemote := flags.String("git-origin-remote", "<<<MISSING>>>", "> git origin remote")
flag.Parse() query := flags.String("query", "", "Search query")
// TODO: Do we still need this?
errMsg := "Failed to get necessary command-line arguments" 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 == "" { if *sessionID == "" {
out.Fatal(errMsg, errors.New("missing option --sessionId")) out.Fatal(errMsg, errors.New("missing required option --session-id"))
} }
if *pwd == "" { if *pwd == "" {
out.Fatal(errMsg, errors.New("missing option --pwd")) out.Fatal(errMsg, errors.New("missing required option --pwd"))
} }
if *gitOriginRemote == "DEFAULT" { if *gitOriginRemote == "<<<MISSING>>>" {
out.Fatal(errMsg, errors.New("missing option --gitOriginRemote")) out.Fatal(errMsg, errors.New("missing required option --git-origin-remote"))
} }
dataDir, err := datadir.GetPath() dataDir, err := datadir.GetPath()
if err != nil { if err != nil {
@ -244,11 +250,6 @@ func (m manager) AbortPaste(g *gocui.Gui, v *gocui.View) error {
return nil return nil
} }
type dedupRecord struct {
dataIndex int
score float32
}
func (m manager) UpdateData(input string) { func (m manager) UpdateData(input string) {
sugar := m.out.Logger.Sugar() sugar := m.out.Logger.Sugar()
sugar.Debugw("Starting data update ...", sugar.Debugw("Starting data update ...",
@ -432,14 +433,14 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit
} }
const smallTerminalTresholdWidth = 110 const smallTerminalThresholdWidth = 110
func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
sugar := m.out.Logger.Sugar() sugar := m.out.Logger.Sugar()
maxX, maxY := g.Size() maxX, maxY := g.Size()
compactRenderingMode := false compactRenderingMode := false
if maxX < smallTerminalTresholdWidth { if maxX < smallTerminalThresholdWidth {
compactRenderingMode = true compactRenderingMode = true
} }
@ -558,7 +559,7 @@ func (m manager) rawMode(g *gocui.Gui, v *gocui.View) error {
} }
displayStr := itm.CmdLineWithColor displayStr := itm.CmdLineWithColor
if m.s.highlightedItem == i { 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) displayStr = searchapp.DoHighlightString(displayStr, maxX*2)
} }
if strings.Contains(displayStr, "\n") { 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() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
out.Fatal("Failed read response", err) 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 { if err != nil {
out.Fatal("Failed decode response", err) out.Fatal("Failed decode response", err)
} }
sugar.Debugw("Recieved records from daemon", sugar.Debugw("Received records from daemon",
"recordCount", len(response.Records), "recordCount", len(response.Records),
) )
return response return response

@ -1,15 +1,5 @@
#!/hint/sh #!/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: # BACKWARDS COMPATIBILITY NOTES:
# #
# Stable names and options: # Stable names and options:
@ -24,7 +14,16 @@ __resh_reload_msg() {
# => The function shows a message from the already updated shell files # => 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 # => 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 # (pre)collect
# Backwards compatibilty: Please see notes above before making any changes here. # Backwards compatibilty: Please see notes above before making any changes here.
@ -103,4 +102,59 @@ __resh_session_init() {
--session-id "$__RESH_SESSION_ID" \ --session-id "$__RESH_SESSION_ID" \
--session-pid "$$" --session-pid "$$"
return $? 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
} }

@ -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/shellrc.sh ~/.resh/shellrc
cp -f scripts/resh-daemon-start.sh ~/.resh/bin/resh-daemon-start 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/ cp -f scripts/rawinstall.sh ~/.resh/
# Copy all executables. We don't really need to omit install-utils from the bin directory # Copy all executables. We don't really need to omit install-utils from the bin directory

@ -2,8 +2,8 @@
# shellcheck source=../submodules/bash-zsh-compat-widgets/bindfunc.sh # shellcheck source=../submodules/bash-zsh-compat-widgets/bindfunc.sh
. ~/.resh/bindfunc.sh . ~/.resh/bindfunc.sh
# shellcheck source=widgets.sh # shellcheck source=hooks.sh
. ~/.resh/widgets.sh . ~/.resh/hooks.sh
__resh_nop() { __resh_nop() {
# does nothing # does nothing

@ -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
}
Loading…
Cancel
Save