From cf3b54a870e0b2b92d4a06265a6ccd63595300e6 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Fri, 11 Oct 2019 02:17:05 +0200 Subject: [PATCH] handle nested shells --- cmd/postcollect/main.go | 2 ++ pkg/histfile/histfile.go | 13 ++++++++----- scripts/hooks.sh | 2 ++ scripts/shellrc.sh | 11 ++++++++--- scripts/widgets.sh | 12 ++++++------ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd/postcollect/main.go b/cmd/postcollect/main.go index edca110..6029bff 100644 --- a/cmd/postcollect/main.go +++ b/cmd/postcollect/main.go @@ -44,6 +44,7 @@ func main() { cmdLine := flag.String("cmdLine", "", "command line") exitCode := flag.Int("exitCode", -1, "exit code") sessionID := flag.String("sessionId", "", "resh generated session id") + shlvl := flag.Int("shlvl", -1, "$SHLVL") // posix variables pwdAfter := flag.String("pwdAfter", "", "$PWD after command") @@ -116,6 +117,7 @@ func main() { CmdLine: *cmdLine, ExitCode: *exitCode, SessionID: *sessionID, + Shlvl: *shlvl, PwdAfter: *pwdAfter, diff --git a/pkg/histfile/histfile.go b/pkg/histfile/histfile.go index 879efe1..5964603 100644 --- a/pkg/histfile/histfile.go +++ b/pkg/histfile/histfile.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "os" + "strconv" "sync" "github.com/curusarn/resh/pkg/records" @@ -67,17 +68,19 @@ func (h *Histfile) writer(input chan records.Record) { h.sessionsMutex.Lock() defer h.sessionsMutex.Unlock() + // allows nested sessions to merge records properly + mergeID := record.SessionID + "_" + strconv.Itoa(record.Shlvl) if record.PartOne { - if _, found := h.sessions[record.SessionID]; found { + if _, found := h.sessions[mergeID]; found { log.Println("histfile WARN: Got another first part of the records before merging the previous one - overwriting! " + "(this happens in bash because bash-preexec runs when it's not supposed to)") } - h.sessions[record.SessionID] = record + h.sessions[mergeID] = record } else { - if part1, found := h.sessions[record.SessionID]; found == false { - log.Println("histfile ERROR: Got second part of records and nothing to merge it with - ignoring!") + if part1, found := h.sessions[mergeID]; found == false { + log.Println("histfile ERROR: Got second part of records and nothing to merge it with - ignoring! (mergeID:", mergeID, ")") } else { - delete(h.sessions, record.SessionID) + delete(h.sessions, mergeID) go h.mergeAndWriteRecord(part1, record) } } diff --git a/scripts/hooks.sh b/scripts/hooks.sh index 1595a64..4963f95 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -114,6 +114,7 @@ __resh_precmd() { local __RESH_GIT_CDUP_EXIT_CODE_AFTER local __RESH_GIT_REMOTE_AFTER local __RESH_GIT_REMOTE_EXIT_CODE_AFTER + local __RESH_SHLVL="$SHLVL" __RESH_RT_AFTER=$(__resh_get_epochrealtime) __RESH_TZ_AFTER=$(date +%z) __RESH_PWD_AFTER="$PWD" @@ -144,6 +145,7 @@ __resh_precmd() { -realtimeBefore "$__RESH_RT_BEFORE" \ -exitCode "$__RESH_EXIT_CODE" \ -sessionId "$__RESH_SESSION_ID" \ + -shlvl "$__RESH_SHLVL" \ -pwdAfter "$__RESH_PWD_AFTER" \ -gitCdupAfter "$__RESH_GIT_CDUP_AFTER" \ -gitCdupExitCodeAfter "$__RESH_GIT_CDUP_EXIT_CODE_AFTER" \ diff --git a/scripts/shellrc.sh b/scripts/shellrc.sh index d5357fd..c4d3afc 100644 --- a/scripts/shellrc.sh +++ b/scripts/shellrc.sh @@ -69,6 +69,8 @@ __RESH_REVISION=$(resh-collect -revision) __resh_run_daemon +# block for anything we only want to do once per session +# NOTE: nested shells are still the same session if [ -z "${__RESH_SESSION_ID+x}" ]; then export __RESH_SESSION_ID; __RESH_SESSION_ID=$(__resh_get_uuid) export __RESH_SESSION_PID="$$" @@ -77,9 +79,12 @@ if [ -z "${__RESH_SESSION_ID+x}" ]; then __resh_session_init fi -# do not add more hooks when shellrc is sourced again -if [ -z "${__RESH_PREEXEC_PRECMD_HOOKS_ADDED+x}" ]; then +# block for anything we only want to do once per shell +if [ -z "${__RESH_INIT_DONE+x}" ]; then preexec_functions+=(__resh_preexec) precmd_functions+=(__resh_precmd) - __RESH_PREEXEC_PRECMD_HOOKS_ADDED=1 + + __resh_reset_variables + + __RESH_INIT_DONE=1 fi diff --git a/scripts/widgets.sh b/scripts/widgets.sh index d1d48e5..82761bd 100644 --- a/scripts/widgets.sh +++ b/scripts/widgets.sh @@ -19,7 +19,7 @@ __resh_helper_arrow_pre() { # "NO_PREFIX_MODE" => set prefix to empty string [ "$__RESH_HIST_NO_PREFIX_MODE" -eq 1 ] && __RESH_PREFIX="" # histno == 0 => save current line - [ $__RESH_HISTNO -eq 0 ] && __RESH_HISTNO_ZERO_LINE=$BUFFER + [ "$__RESH_HISTNO" -eq 0 ] && __RESH_HISTNO_ZERO_LINE=$BUFFER } __resh_helper_arrow_post() { # cursor at the beginning of the line => activate "NO_PREFIX_MODE" @@ -38,7 +38,7 @@ __resh_widget_arrow_up() { # increment histno __RESH_HISTNO=$((__RESH_HISTNO+1)) # back at histno == 0 => restore original line - if [ $__RESH_HISTNO -eq 0 ]; then + if [ "$__RESH_HISTNO" -eq 0 ]; then BUFFER=$__RESH_HISTNO_ZERO_LINE else # run recall @@ -46,7 +46,7 @@ __resh_widget_arrow_up() { NEW_BUFFER="$(__resh_collect --recall --prefix-search "$__RESH_PREFIX" 2> ~/.resh/arrow_up_last_run_out.txt)" # IF new buffer in non-empty THEN use the new buffer ELSE revert histno change # shellcheck disable=SC2015 - [ ${#NEW_BUFFER} -gt 0 ] && BUFFER=$NEW_BUFFER || __RESH_HISTNO=$((__RESH_HISTNO-1)) + [ "${#NEW_BUFFER}" -gt 0 ] && BUFFER=$NEW_BUFFER || __RESH_HISTNO=$((__RESH_HISTNO-1)) fi # run post helper __resh_helper_arrow_post @@ -59,9 +59,9 @@ __resh_widget_arrow_down() { # increment histno __RESH_HISTNO=$((__RESH_HISTNO-1)) # prevent HISTNO from getting negative (for now) - [ $__RESH_HISTNO -lt 0 ] && __RESH_HISTNO=0 + [ "$__RESH_HISTNO" -lt 0 ] && __RESH_HISTNO=0 # back at histno == 0 => restore original line - if [ $__RESH_HISTNO -eq 0 ]; then + if [ "$__RESH_HISTNO" -eq 0 ]; then BUFFER=$__RESH_HISTNO_ZERO_LINE else # run recall @@ -69,7 +69,7 @@ __resh_widget_arrow_down() { NEW_BUFFER="$(__resh_collect --recall --prefix-search "$__RESH_PREFIX" 2> ~/.resh/arrow_down_last_run_out.txt)" # IF new buffer in non-empty THEN use the new buffer ELSE revert histno change # shellcheck disable=SC2015 - [ ${#NEW_BUFFER} -gt 0 ] && BUFFER=$NEW_BUFFER || (( __RESH_HISTNO++ )) + [ "${#NEW_BUFFER}" -gt 0 ] && BUFFER=$NEW_BUFFER || (( __RESH_HISTNO++ )) fi __resh_helper_arrow_post }