handle nested shells

pull/18/head
Simon Let 6 years ago
parent 1ef128895b
commit cf3b54a870
  1. 2
      cmd/postcollect/main.go
  2. 13
      pkg/histfile/histfile.go
  3. 2
      scripts/hooks.sh
  4. 11
      scripts/shellrc.sh
  5. 12
      scripts/widgets.sh

@ -44,6 +44,7 @@ func main() {
cmdLine := flag.String("cmdLine", "", "command line") cmdLine := flag.String("cmdLine", "", "command line")
exitCode := flag.Int("exitCode", -1, "exit code") exitCode := flag.Int("exitCode", -1, "exit code")
sessionID := flag.String("sessionId", "", "resh generated session id") sessionID := flag.String("sessionId", "", "resh generated session id")
shlvl := flag.Int("shlvl", -1, "$SHLVL")
// posix variables // posix variables
pwdAfter := flag.String("pwdAfter", "", "$PWD after command") pwdAfter := flag.String("pwdAfter", "", "$PWD after command")
@ -116,6 +117,7 @@ func main() {
CmdLine: *cmdLine, CmdLine: *cmdLine,
ExitCode: *exitCode, ExitCode: *exitCode,
SessionID: *sessionID, SessionID: *sessionID,
Shlvl: *shlvl,
PwdAfter: *pwdAfter, PwdAfter: *pwdAfter,

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
"strconv"
"sync" "sync"
"github.com/curusarn/resh/pkg/records" "github.com/curusarn/resh/pkg/records"
@ -67,17 +68,19 @@ func (h *Histfile) writer(input chan records.Record) {
h.sessionsMutex.Lock() h.sessionsMutex.Lock()
defer h.sessionsMutex.Unlock() defer h.sessionsMutex.Unlock()
// allows nested sessions to merge records properly
mergeID := record.SessionID + "_" + strconv.Itoa(record.Shlvl)
if record.PartOne { 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! " + 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)") "(this happens in bash because bash-preexec runs when it's not supposed to)")
} }
h.sessions[record.SessionID] = record h.sessions[mergeID] = record
} else { } else {
if part1, found := h.sessions[record.SessionID]; found == false { if part1, found := h.sessions[mergeID]; found == false {
log.Println("histfile ERROR: Got second part of records and nothing to merge it with - ignoring!") log.Println("histfile ERROR: Got second part of records and nothing to merge it with - ignoring! (mergeID:", mergeID, ")")
} else { } else {
delete(h.sessions, record.SessionID) delete(h.sessions, mergeID)
go h.mergeAndWriteRecord(part1, record) go h.mergeAndWriteRecord(part1, record)
} }
} }

@ -114,6 +114,7 @@ __resh_precmd() {
local __RESH_GIT_CDUP_EXIT_CODE_AFTER local __RESH_GIT_CDUP_EXIT_CODE_AFTER
local __RESH_GIT_REMOTE_AFTER local __RESH_GIT_REMOTE_AFTER
local __RESH_GIT_REMOTE_EXIT_CODE_AFTER local __RESH_GIT_REMOTE_EXIT_CODE_AFTER
local __RESH_SHLVL="$SHLVL"
__RESH_RT_AFTER=$(__resh_get_epochrealtime) __RESH_RT_AFTER=$(__resh_get_epochrealtime)
__RESH_TZ_AFTER=$(date +%z) __RESH_TZ_AFTER=$(date +%z)
__RESH_PWD_AFTER="$PWD" __RESH_PWD_AFTER="$PWD"
@ -144,6 +145,7 @@ __resh_precmd() {
-realtimeBefore "$__RESH_RT_BEFORE" \ -realtimeBefore "$__RESH_RT_BEFORE" \
-exitCode "$__RESH_EXIT_CODE" \ -exitCode "$__RESH_EXIT_CODE" \
-sessionId "$__RESH_SESSION_ID" \ -sessionId "$__RESH_SESSION_ID" \
-shlvl "$__RESH_SHLVL" \
-pwdAfter "$__RESH_PWD_AFTER" \ -pwdAfter "$__RESH_PWD_AFTER" \
-gitCdupAfter "$__RESH_GIT_CDUP_AFTER" \ -gitCdupAfter "$__RESH_GIT_CDUP_AFTER" \
-gitCdupExitCodeAfter "$__RESH_GIT_CDUP_EXIT_CODE_AFTER" \ -gitCdupExitCodeAfter "$__RESH_GIT_CDUP_EXIT_CODE_AFTER" \

@ -69,6 +69,8 @@ __RESH_REVISION=$(resh-collect -revision)
__resh_run_daemon __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 if [ -z "${__RESH_SESSION_ID+x}" ]; then
export __RESH_SESSION_ID; __RESH_SESSION_ID=$(__resh_get_uuid) export __RESH_SESSION_ID; __RESH_SESSION_ID=$(__resh_get_uuid)
export __RESH_SESSION_PID="$$" export __RESH_SESSION_PID="$$"
@ -77,9 +79,12 @@ if [ -z "${__RESH_SESSION_ID+x}" ]; then
__resh_session_init __resh_session_init
fi fi
# do not add more hooks when shellrc is sourced again # block for anything we only want to do once per shell
if [ -z "${__RESH_PREEXEC_PRECMD_HOOKS_ADDED+x}" ]; then if [ -z "${__RESH_INIT_DONE+x}" ]; then
preexec_functions+=(__resh_preexec) preexec_functions+=(__resh_preexec)
precmd_functions+=(__resh_precmd) precmd_functions+=(__resh_precmd)
__RESH_PREEXEC_PRECMD_HOOKS_ADDED=1
__resh_reset_variables
__RESH_INIT_DONE=1
fi fi

@ -19,7 +19,7 @@ __resh_helper_arrow_pre() {
# "NO_PREFIX_MODE" => set prefix to empty string # "NO_PREFIX_MODE" => set prefix to empty string
[ "$__RESH_HIST_NO_PREFIX_MODE" -eq 1 ] && __RESH_PREFIX="" [ "$__RESH_HIST_NO_PREFIX_MODE" -eq 1 ] && __RESH_PREFIX=""
# histno == 0 => save current line # 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() { __resh_helper_arrow_post() {
# cursor at the beginning of the line => activate "NO_PREFIX_MODE" # cursor at the beginning of the line => activate "NO_PREFIX_MODE"
@ -38,7 +38,7 @@ __resh_widget_arrow_up() {
# increment histno # increment histno
__RESH_HISTNO=$((__RESH_HISTNO+1)) __RESH_HISTNO=$((__RESH_HISTNO+1))
# back at histno == 0 => restore original line # back at histno == 0 => restore original line
if [ $__RESH_HISTNO -eq 0 ]; then if [ "$__RESH_HISTNO" -eq 0 ]; then
BUFFER=$__RESH_HISTNO_ZERO_LINE BUFFER=$__RESH_HISTNO_ZERO_LINE
else else
# run recall # 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)" 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 # IF new buffer in non-empty THEN use the new buffer ELSE revert histno change
# shellcheck disable=SC2015 # 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 fi
# run post helper # run post helper
__resh_helper_arrow_post __resh_helper_arrow_post
@ -59,9 +59,9 @@ __resh_widget_arrow_down() {
# increment histno # increment histno
__RESH_HISTNO=$((__RESH_HISTNO-1)) __RESH_HISTNO=$((__RESH_HISTNO-1))
# prevent HISTNO from getting negative (for now) # 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 # back at histno == 0 => restore original line
if [ $__RESH_HISTNO -eq 0 ]; then if [ "$__RESH_HISTNO" -eq 0 ]; then
BUFFER=$__RESH_HISTNO_ZERO_LINE BUFFER=$__RESH_HISTNO_ZERO_LINE
else else
# run recall # 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)" 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 # IF new buffer in non-empty THEN use the new buffer ELSE revert histno change
# shellcheck disable=SC2015 # shellcheck disable=SC2015
[ ${#NEW_BUFFER} -gt 0 ] && BUFFER=$NEW_BUFFER || (( __RESH_HISTNO++ )) [ "${#NEW_BUFFER}" -gt 0 ] && BUFFER=$NEW_BUFFER || (( __RESH_HISTNO++ ))
fi fi
__resh_helper_arrow_post __resh_helper_arrow_post
} }

Loading…
Cancel
Save