Use XDG CACHE for "last run" files, fix header display bug

pull/174/head v2.8.0-beta2
Simon Let 4 years ago
parent c212a81934
commit eadb9abb19
No known key found for this signature in database
GPG Key ID: D650C65DD46D431D
  1. 16
      cmd/cli/main.go
  2. 9
      cmd/control/cmd/debug.go
  3. 41
      pkg/searchapp/item.go
  4. 8
      scripts/hooks.sh
  5. 2
      scripts/install.sh
  6. 7
      scripts/reshctl.sh
  7. 11
      scripts/util.sh
  8. 7
      scripts/widgets.sh

@ -452,7 +452,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
header := searchapp.GetHeader(compactRenderingMode) header := searchapp.GetHeader(compactRenderingMode)
longestDateLen := len(header.Date) longestDateLen := len(header.Date)
longestLocationLen := len(header.Host) + len(header.PwdTilde) longestLocationLen := len(header.Host) + 1 + len(header.PwdTilde)
longestFlagsLen := 2 longestFlagsLen := 2
maxPossibleMainViewHeight := maxY - 3 - 1 - 1 - 1 // - top box - header - status - help maxPossibleMainViewHeight := maxY - 3 - 1 - 1 - 1 // - top box - header - status - help
for i, itm := range m.s.data { for i, itm := range m.s.data {
@ -504,19 +504,27 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
// header // header
// header := getHeader() // 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) dispStr = searchapp.DoHighlightHeader(dispStr, maxX*2)
v.WriteString(dispStr + "\n") v.WriteString(dispStr + "\n")
var index int var index int
for index < len(data) { for index < len(data) {
itm := data[index] 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 // page is full
break 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 { if m.s.highlightedItem == index {
// maxX * 2 because there are escape sequences that make it hard to tell the real string length // maxX * 2 because there are escape sequences that make it hard to tell the real string length
displayStr = searchapp.DoHighlightString(displayStr, maxX*3) displayStr = searchapp.DoHighlightString(displayStr, maxX*3)

@ -3,7 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os/user" "os"
"path/filepath" "path/filepath"
"github.com/curusarn/resh/cmd/control/status" "github.com/curusarn/resh/cmd/control/status"
@ -43,12 +43,11 @@ var debugOutputCmd = &cobra.Command{
"collect_last_run_out.txt", "collect_last_run_out.txt",
"postcollect_last_run_out.txt", "postcollect_last_run_out.txt",
"session_init_last_run_out.txt", "session_init_last_run_out.txt",
"cli_last_run_out.txt",
} }
usr, _ := user.Current() dir := os.Getenv("__RESH_XDG_CACHE_HOME")
dir := usr.HomeDir
reshdir := filepath.Join(dir, ".resh")
for _, fpath := range files { for _, fpath := range files {
fpath := filepath.Join(reshdir, fpath) fpath := filepath.Join(dir, fpath)
debugReadFile(fpath) debugReadFile(fpath)
} }
exitCode = status.Success exitCode = status.Success

@ -221,16 +221,21 @@ func produceLocation(length int, host string, pwdTilde string, differentHost boo
pwdLen := len(pwdTilde) pwdLen := len(pwdTilde)
totalLen := hostLen + colonLen + pwdLen totalLen := hostLen + colonLen + pwdLen
// how much we need to shrink/crop the location newHostLen := hostLen
shrinkFactor := float32(length) / float32(totalLen) // 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) newHostLen = minInt(hostLen, shrinkedHostLen, halfLocationLen)
if debug {
log.Printf("shrinkFactor: %f\n", shrinkFactor)
} }
halfLocationLen := length/2 - colonLen // pwd length is the rest of the length
newHostLen := minInt(hostLen, shrinkedHostLen, halfLocationLen)
newPwdLen := length - colonLen - newHostLen newPwdLen := length - colonLen - newHostLen
hostWithColor := rightCutPadString(host, newHostLen) hostWithColor := rightCutPadString(host, newHostLen)
@ -245,7 +250,8 @@ func produceLocation(length int, host string, pwdTilde string, differentHost boo
} }
// ProduceLine ... // 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 := "" line := ""
if showDate { if showDate {
line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor
@ -256,24 +262,21 @@ func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagLength
// FLAGS // FLAGS
line += ic.FlagsWithColor line += ic.FlagsWithColor
flags := ic.Flags if flagsLength >= len(ic.Flags) {
if flagLength < len(ic.Flags) { line += strings.Repeat(" ", flagsLength-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) } else {
} err = fmt.Errorf("actual flags are longer than dedicated flag space. actual: %v, space: %v", len(ic.Flags), flagsLength)
for len(flags) < flagLength {
line += " "
flags += " "
} }
spacer := " " spacer := " "
if flagLength > 5 || header { if flagsLength > 5 || header {
// use shorter spacer // use shorter spacer
// because there is likely a long flag like E130 in the view // because there is likely a long flag like E130 in the view
spacer = " " spacer = " "
} }
line += spacer + ic.CmdLineWithColor line += spacer + ic.CmdLineWithColor
length := dateLength + locationLength + flagLength + len(spacer) + len(ic.CmdLine) length := dateLength + locationLength + flagsLength + len(spacer) + len(ic.CmdLine)
return line, length return line, length, err
} }
func leftCutPadString(str string, newLen int) string { func leftCutPadString(str string, newLen int) string {

@ -16,12 +16,12 @@ __resh_preexec() {
# core # core
__RESH_COLLECT=1 __RESH_COLLECT=1
__RESH_CMDLINE="$1" # not local to preserve it for postcollect (useful as sanity check) __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" \ __resh_collect --cmdLine "$__RESH_CMDLINE" \
--recall-actions "$__RESH_HIST_RECALL_ACTIONS" \ --recall-actions "$__RESH_HIST_RECALL_ACTIONS" \
--recall-strategy "$__RESH_HIST_RECALL_STRATEGY" \ --recall-strategy "$__RESH_HIST_RECALL_STRATEGY" \
--recall-last-cmdline "$__RESH_HIST_PREV_LINE" \ --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 # used for collect and collect --recall
@ -154,7 +154,7 @@ __resh_precmd() {
fi fi
fi fi
if [ "$__RESH_VERSION" = "$(resh-postcollect -version)" ] && [ "$__RESH_REVISION" = "$(resh-postcollect -revision)" ]; then 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" \ resh-postcollect -requireVersion "$__RESH_VERSION" \
-requireRevision "$__RESH_REVISION" \ -requireRevision "$__RESH_REVISION" \
-cmdLine "$__RESH_CMDLINE" \ -cmdLine "$__RESH_CMDLINE" \
@ -171,7 +171,7 @@ __resh_precmd() {
-gitRemoteExitCodeAfter "$__RESH_GIT_REMOTE_EXIT_CODE_AFTER" \ -gitRemoteExitCodeAfter "$__RESH_GIT_REMOTE_EXIT_CODE_AFTER" \
-realtimeAfter "$__RESH_RT_AFTER" \ -realtimeAfter "$__RESH_RT_AFTER" \
-timezoneAfter "$__RESH_TZ_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 fi
__resh_reset_variables __resh_reset_variables
fi fi

@ -190,6 +190,8 @@ if [ -f ~/.resh/resh.pid ]; then
else else
pkill -SIGTERM "resh-daemon" || true pkill -SIGTERM "resh-daemon" || true
fi fi
# daemon uses xdg path variables
__resh_set_xdg_home_paths
__resh_run_daemon __resh_run_daemon

@ -73,14 +73,13 @@ resh() {
elif [ $status_code = 130 ]; then elif [ $status_code = 130 ]; then
true true
else else
local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" local fpath_last_run="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt"
echo "$buffer" >| "$tmp_file" echo "$buffer" >| "$fpath_last_run"
echo "resh-cli failed - check '$tmp_file' and '~/.resh/cli.log'" echo "resh-cli failed - check '$fpath_last_run' and '~/.resh/cli.log'"
fi fi
} }
reshctl() { reshctl() {
# local log=~/.resh/reshctl.log
# export current shell because resh-control needs to know # export current shell because resh-control needs to know
export __RESH_ctl_shell=$__RESH_SHELL export __RESH_ctl_shell=$__RESH_SHELL
# run resh-control aka the real reshctl # run resh-control aka the real reshctl

@ -49,14 +49,15 @@ __resh_run_daemon() {
if [ -n "${ZSH_VERSION-}" ]; then if [ -n "${ZSH_VERSION-}" ]; then
setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR
fi fi
local fpath_last_run="$__RESH_XDG_CACHE_HOME/daemon_last_run_out.txt"
if [ "$(uname)" = Darwin ]; then if [ "$(uname)" = Darwin ]; then
# hotfix # hotfix
gnohup resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown gnohup resh-daemon >| "$fpath_last_run" 2>&1 & disown
else else
# TODO: switch to nohup for consistency once you confirm that daemon is # TODO: switch to nohup for consistency once you confirm that daemon is
# not getting killed anymore on macOS # not getting killed anymore on macOS
# nohup resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown # nohup resh-daemon >| "$fpath_last_run" 2>&1 & disown
setsid resh-daemon >| ~/.resh/daemon_last_run_out.txt 2>&1 & disown setsid resh-daemon >| "$fpath_last_run" 2>&1 & disown
fi fi
} }
@ -134,6 +135,7 @@ __resh_session_init() {
fi fi
fi fi
if [ "$__RESH_VERSION" = "$(resh-session-init -version)" ] && [ "$__RESH_REVISION" = "$(resh-session-init -revision)" ]; then 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" \ resh-session-init -requireVersion "$__RESH_VERSION" \
-requireRevision "$__RESH_REVISION" \ -requireRevision "$__RESH_REVISION" \
-shell "$__RESH_SHELL" \ -shell "$__RESH_SHELL" \
@ -163,7 +165,7 @@ __resh_session_init() {
-osReleaseIdLike "$__RESH_OS_RELEASE_ID_LIKE" \ -osReleaseIdLike "$__RESH_OS_RELEASE_ID_LIKE" \
-osReleaseName "$__RESH_OS_RELEASE_NAME" \ -osReleaseName "$__RESH_OS_RELEASE_NAME" \
-osReleasePrettyName "$__RESH_OS_RELEASE_PRETTY_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 fi
} }
@ -183,6 +185,7 @@ __resh_set_xdg_home_paths() {
__RESH_XDG_CACHE_HOME="$XDG_CACHE_HOME/resh" __RESH_XDG_CACHE_HOME="$XDG_CACHE_HOME/resh"
fi fi
mkdir -p "$__RESH_XDG_CACHE_HOME" >/dev/null 2>/dev/null mkdir -p "$__RESH_XDG_CACHE_HOME" >/dev/null 2>/dev/null
export __RESH_XDG_CACHE_HOME
if [ -z "${XDG_DATA_HOME-}" ]; then if [ -z "${XDG_DATA_HOME-}" ]; then

@ -18,6 +18,8 @@ __resh_widget_control_R() {
local git_remote; git_remote="$(git remote get-url origin 2>/dev/null)" 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") BUFFER=$(resh-cli --sessionID "$__RESH_SESSION_ID" --host "$__RESH_HOST" --pwd "$PWD" --gitOriginRemote "$git_remote" --query "$BUFFER")
status_code=$? status_code=$?
local fpath_last_run="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt"
touch "$fpath_last_run"
if [ $status_code = 111 ]; then if [ $status_code = 111 ]; then
# execute # execute
if [ -n "${ZSH_VERSION-}" ]; then if [ -n "${ZSH_VERSION-}" ]; then
@ -35,9 +37,8 @@ __resh_widget_control_R() {
bind -x '"\u[32~": __resh_nop' bind -x '"\u[32~": __resh_nop'
fi fi
else else
local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" echo "$BUFFER" >| "$fpath_last_run"
echo "$BUFFER" >| "$tmp_file" echo "# RESH SEARCH APP failed - sorry for the inconvinience - check '$fpath_last_run' and '~/.resh/cli.log'"
echo "# RESH SEARCH APP failed - sorry for the inconvinience - check '$tmp_file' and '~/.resh/cli.log'"
BUFFER="$PREVBUFFER" BUFFER="$PREVBUFFER"
fi fi
CURSOR=${#BUFFER} CURSOR=${#BUFFER}

Loading…
Cancel
Save