Skip to content

Commit

Permalink
Use XDG CACHE for "last run" files, fix header display bug
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Dec 12, 2021
1 parent c212a81 commit eadb9ab
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 43 deletions.
16 changes: 12 additions & 4 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions cmd/control/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"os/user"
"os"
"path/filepath"

"github.com/curusarn/resh/cmd/control/status"
Expand Down Expand Up @@ -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
Expand Down
41 changes: 22 additions & 19 deletions pkg/searchapp/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions scripts/hooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" \
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
7 changes: 3 additions & 4 deletions scripts/reshctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions scripts/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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" \
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions scripts/widgets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit eadb9ab

Please sign in to comment.