From eadb9abb193986d74c98cb92e7b2569cdb99fcef Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sun, 12 Dec 2021 01:32:53 +0100 Subject: [PATCH] Use XDG CACHE for "last run" files, fix header display bug --- cmd/cli/main.go | 16 ++++++++++++---- cmd/control/cmd/debug.go | 9 ++++----- pkg/searchapp/item.go | 41 +++++++++++++++++++++------------------- scripts/hooks.sh | 8 ++++---- scripts/install.sh | 2 ++ scripts/reshctl.sh | 7 +++---- scripts/util.sh | 11 +++++++---- scripts/widgets.sh | 7 ++++--- 8 files changed, 58 insertions(+), 43 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 5f117f2..a58ff10 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -452,7 +452,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { header := searchapp.GetHeader(compactRenderingMode) longestDateLen := len(header.Date) - longestLocationLen := len(header.Host) + len(header.PwdTilde) + longestLocationLen := len(header.Host) + 1 + len(header.PwdTilde) longestFlagsLen := 2 maxPossibleMainViewHeight := maxY - 3 - 1 - 1 - 1 // - top box - header - status - help for i, itm := range m.s.data { @@ -504,19 +504,27 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { // header // header := getHeader() - dispStr, _ := header.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, true, true, debug) + // error is expected for header + dispStr, _, _ := header.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, true, true, debug) dispStr = searchapp.DoHighlightHeader(dispStr, maxX*2) v.WriteString(dispStr + "\n") var index int for index < len(data) { itm := data[index] - if index == mainViewHeight { + if index >= mainViewHeight { + if debug { + log.Printf("Finished drawing page. mainViewHeight: %v, predictedMax: %v\n", + mainViewHeight, maxPossibleMainViewHeight) + } // page is full break } - displayStr, _ := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true, debug) + displayStr, _, err := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true, debug) + if err != nil { + log.Printf("produceLine error: %v\n", err) + } if m.s.highlightedItem == index { // maxX * 2 because there are escape sequences that make it hard to tell the real string length displayStr = searchapp.DoHighlightString(displayStr, maxX*3) diff --git a/cmd/control/cmd/debug.go b/cmd/control/cmd/debug.go index aec7132..f1401c7 100644 --- a/cmd/control/cmd/debug.go +++ b/cmd/control/cmd/debug.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" "io/ioutil" - "os/user" + "os" "path/filepath" "github.com/curusarn/resh/cmd/control/status" @@ -43,12 +43,11 @@ var debugOutputCmd = &cobra.Command{ "collect_last_run_out.txt", "postcollect_last_run_out.txt", "session_init_last_run_out.txt", + "cli_last_run_out.txt", } - usr, _ := user.Current() - dir := usr.HomeDir - reshdir := filepath.Join(dir, ".resh") + dir := os.Getenv("__RESH_XDG_CACHE_HOME") for _, fpath := range files { - fpath := filepath.Join(reshdir, fpath) + fpath := filepath.Join(dir, fpath) debugReadFile(fpath) } exitCode = status.Success diff --git a/pkg/searchapp/item.go b/pkg/searchapp/item.go index 161002b..6908c25 100644 --- a/pkg/searchapp/item.go +++ b/pkg/searchapp/item.go @@ -221,16 +221,21 @@ func produceLocation(length int, host string, pwdTilde string, differentHost boo pwdLen := len(pwdTilde) totalLen := hostLen + colonLen + pwdLen - // how much we need to shrink/crop the location - shrinkFactor := float32(length) / float32(totalLen) + newHostLen := hostLen + // only shrink if the location does not fit + if totalLen > length { + // how much we need to shrink/crop the location + shrinkFactor := float64(length) / float64(totalLen) + + shrinkedHostLen := int(math.Ceil(float64(hostLen) * shrinkFactor)) + if debug { + log.Printf("shrinkFactor: %f\n", shrinkFactor) + } + halfLocationLen := length/2 - colonLen - shrinkedHostLen := int(float32(hostLen) * shrinkFactor) - if debug { - log.Printf("shrinkFactor: %f\n", shrinkFactor) + newHostLen = minInt(hostLen, shrinkedHostLen, halfLocationLen) } - halfLocationLen := length/2 - colonLen - - newHostLen := minInt(hostLen, shrinkedHostLen, halfLocationLen) + // pwd length is the rest of the length newPwdLen := length - colonLen - newHostLen hostWithColor := rightCutPadString(host, newHostLen) @@ -245,7 +250,8 @@ func produceLocation(length int, host string, pwdTilde string, differentHost boo } // ProduceLine ... -func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagLength int, header bool, showDate bool, debug bool) (string, int) { +func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagsLength int, header bool, showDate bool, debug bool) (string, int, error) { + var err error line := "" if showDate { line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor @@ -256,24 +262,21 @@ func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagLength // FLAGS line += ic.FlagsWithColor - flags := ic.Flags - if flagLength < len(ic.Flags) { - log.Printf("produceLine can't specify line w/ flags shorter than the actual size. - len(flags) %v, requested %v\n", len(ic.Flags), flagLength) - } - for len(flags) < flagLength { - line += " " - flags += " " + if flagsLength >= len(ic.Flags) { + line += strings.Repeat(" ", flagsLength-len(ic.Flags)) + } else { + err = fmt.Errorf("actual flags are longer than dedicated flag space. actual: %v, space: %v", len(ic.Flags), flagsLength) } spacer := " " - if flagLength > 5 || header { + if flagsLength > 5 || header { // use shorter spacer // because there is likely a long flag like E130 in the view spacer = " " } line += spacer + ic.CmdLineWithColor - length := dateLength + locationLength + flagLength + len(spacer) + len(ic.CmdLine) - return line, length + length := dateLength + locationLength + flagsLength + len(spacer) + len(ic.CmdLine) + return line, length, err } func leftCutPadString(str string, newLen int) string { diff --git a/scripts/hooks.sh b/scripts/hooks.sh index cbe7b88..02e74c3 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -16,12 +16,12 @@ __resh_preexec() { # core __RESH_COLLECT=1 __RESH_CMDLINE="$1" # not local to preserve it for postcollect (useful as sanity check) - local tmp_file="$__RESH_XDG_CACHE_HOME/collect_last_run_out.txt" + local fpath_last_run="$__RESH_XDG_CACHE_HOME/collect_last_run_out.txt" __resh_collect --cmdLine "$__RESH_CMDLINE" \ --recall-actions "$__RESH_HIST_RECALL_ACTIONS" \ --recall-strategy "$__RESH_HIST_RECALL_STRATEGY" \ --recall-last-cmdline "$__RESH_HIST_PREV_LINE" \ - >| $tmp_file 2>&1 || echo "resh-collect ERROR: $(head -n 1 $tmp_file)" + >| "$fpath_last_run" 2>&1 || echo "resh-collect ERROR: $(head -n 1 $fpath_last_run)" } # used for collect and collect --recall @@ -154,7 +154,7 @@ __resh_precmd() { fi fi if [ "$__RESH_VERSION" = "$(resh-postcollect -version)" ] && [ "$__RESH_REVISION" = "$(resh-postcollect -revision)" ]; then - local tmp_file="$__RESH_XDG_CACHE_HOME/postcollect_last_run_out.txt" + local fpath_last_run="$__RESH_XDG_CACHE_HOME/postcollect_last_run_out.txt" resh-postcollect -requireVersion "$__RESH_VERSION" \ -requireRevision "$__RESH_REVISION" \ -cmdLine "$__RESH_CMDLINE" \ @@ -171,7 +171,7 @@ __resh_precmd() { -gitRemoteExitCodeAfter "$__RESH_GIT_REMOTE_EXIT_CODE_AFTER" \ -realtimeAfter "$__RESH_RT_AFTER" \ -timezoneAfter "$__RESH_TZ_AFTER" \ - >| $tmp_file 2>&1 || echo "resh-postcollect ERROR: $(head -n 1 $tmp_file)" + >| "$fpath_last_run" 2>&1 || echo "resh-postcollect ERROR: $(head -n 1 $fpath_last_run)" fi __resh_reset_variables fi diff --git a/scripts/install.sh b/scripts/install.sh index 91e87fd..080e176 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -190,6 +190,8 @@ if [ -f ~/.resh/resh.pid ]; then else pkill -SIGTERM "resh-daemon" || true fi +# daemon uses xdg path variables +__resh_set_xdg_home_paths __resh_run_daemon diff --git a/scripts/reshctl.sh b/scripts/reshctl.sh index 2c225ce..db02147 100644 --- a/scripts/reshctl.sh +++ b/scripts/reshctl.sh @@ -73,14 +73,13 @@ resh() { elif [ $status_code = 130 ]; then true else - local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" - echo "$buffer" >| "$tmp_file" - echo "resh-cli failed - check '$tmp_file' and '~/.resh/cli.log'" + local fpath_last_run="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" + echo "$buffer" >| "$fpath_last_run" + echo "resh-cli failed - check '$fpath_last_run' and '~/.resh/cli.log'" fi } 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 diff --git a/scripts/util.sh b/scripts/util.sh index 4bb298e..4746eb0 100644 --- a/scripts/util.sh +++ b/scripts/util.sh @@ -49,14 +49,15 @@ __resh_run_daemon() { if [ -n "${ZSH_VERSION-}" ]; then setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR fi + local fpath_last_run="$__RESH_XDG_CACHE_HOME/daemon_last_run_out.txt" if [ "$(uname)" = Darwin ]; then # hotfix - gnohup resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown + gnohup resh-daemon >| "$fpath_last_run" 2>&1 & disown else # TODO: switch to nohup for consistency once you confirm that daemon is # not getting killed anymore on macOS - # nohup resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown - setsid resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown + # nohup resh-daemon >| "$fpath_last_run" 2>&1 & disown + setsid resh-daemon >| "$fpath_last_run" 2>&1 & disown fi } @@ -134,6 +135,7 @@ __resh_session_init() { fi fi if [ "$__RESH_VERSION" = "$(resh-session-init -version)" ] && [ "$__RESH_REVISION" = "$(resh-session-init -revision)" ]; then + local fpath_last_run="$__RESH_XDG_CACHE_HOME/session_init_last_run_out.txt" resh-session-init -requireVersion "$__RESH_VERSION" \ -requireRevision "$__RESH_REVISION" \ -shell "$__RESH_SHELL" \ @@ -163,7 +165,7 @@ __resh_session_init() { -osReleaseIdLike "$__RESH_OS_RELEASE_ID_LIKE" \ -osReleaseName "$__RESH_OS_RELEASE_NAME" \ -osReleasePrettyName "$__RESH_OS_RELEASE_PRETTY_NAME" \ - >| ~/.resh/session_init_last_run_out.txt 2>&1 || echo "resh-session-init ERROR: $(head -n 1 ~/.resh/session_init_last_run_out.txt)" + >| "$fpath_last_run" 2>&1 || echo "resh-session-init ERROR: $(head -n 1 $fpath_last_run)" fi } @@ -183,6 +185,7 @@ __resh_set_xdg_home_paths() { __RESH_XDG_CACHE_HOME="$XDG_CACHE_HOME/resh" fi mkdir -p "$__RESH_XDG_CACHE_HOME" >/dev/null 2>/dev/null + export __RESH_XDG_CACHE_HOME if [ -z "${XDG_DATA_HOME-}" ]; then diff --git a/scripts/widgets.sh b/scripts/widgets.sh index 2c49471..defc605 100644 --- a/scripts/widgets.sh +++ b/scripts/widgets.sh @@ -18,6 +18,8 @@ __resh_widget_control_R() { local git_remote; git_remote="$(git remote get-url origin 2>/dev/null)" BUFFER=$(resh-cli --sessionID "$__RESH_SESSION_ID" --host "$__RESH_HOST" --pwd "$PWD" --gitOriginRemote "$git_remote" --query "$BUFFER") status_code=$? + local fpath_last_run="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" + touch "$fpath_last_run" if [ $status_code = 111 ]; then # execute if [ -n "${ZSH_VERSION-}" ]; then @@ -35,9 +37,8 @@ __resh_widget_control_R() { bind -x '"\u[32~": __resh_nop' fi else - local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" - echo "$BUFFER" >| "$tmp_file" - echo "# RESH SEARCH APP failed - sorry for the inconvinience - check '$tmp_file' and '~/.resh/cli.log'" + echo "$BUFFER" >| "$fpath_last_run" + echo "# RESH SEARCH APP failed - sorry for the inconvinience - check '$fpath_last_run' and '~/.resh/cli.log'" BUFFER="$PREVBUFFER" fi CURSOR=${#BUFFER}