diff --git a/zsh/functions.zsh b/zsh/functions.zsh index 3bc4d0f..9448bea 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -102,3 +102,26 @@ sudoedit() { } alias sudoe="sudoedit" alias sue="sudoedit" + +# This idea was taken from +SYNC_WORKING_DIR_STORAGE="${ZSH_CACHE_DIR}/last-working-dir" + +autoload -Uz add-zsh-hook +add-zsh-hook chpwd sync_working_dir_chpwd_hook +sync_working_dir_chpwd_hook() { + if [[ "$ZSH_SUBSHELL" == 0 ]]; then + sync_working_dir_save + fi +} + +sync_working_dir_save() { + pwd >| "$SYNC_WORKING_DIR_STORAGE" +} + +sync_working_dir_load() { + local dir + if dir="$(<"$SYNC_WORKING_DIR_STORAGE")" 2>/dev/null && [[ -n "$dir" ]]; then + cd -- "$dir" + fi +} +alias cds="sync_working_dir_load" diff --git a/zsh/zshrc b/zsh/zshrc index 8fcf10a..e8bf236 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -23,14 +23,17 @@ autoload -U colors && colors } _perf_timer_stop() { + # Record the stop time as precisely as possible even in the case of an error + local stop_time="$EPOCHREALTIME" local name="$1" if [[ -z "$name" ]]; then print >&2 "$0: usage: $0 " return 1 fi - local stop_time="$EPOCHREALTIME" start_time="${_perf_timers[$name]}" + local start_time="${_perf_timers[$name]}" + unset "_perf_timers[${(qq)name}]" local -i duration="$(( (stop_time - start_time) * 1000 ))" - print -- "$(print -P '%F{8}==>%f') ${name}: ${duration}ms" + print -r -- "$(print -P '%F{8}==>%f') ${name}: ${duration}ms" } # }}} @@ -58,4 +61,10 @@ done _perf_timer_stop "total" -welcome +if [[ -z "$DOTFILES_DISABLE_WELCOME" ]]; then + welcome +fi + +if [[ -z "$DOTFILES_SYNC_LAST_WORKING_DIR" ]]; then + sync_working_dir_load +fi