Skip to content

Commit 043d27f

Browse files
committed
ZSH: Add dot::defer cleanup system, simplify zshrc to dot::bootstrap
1 parent 8259cbd commit 043d27f

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

home/dot_config/zsh/exact_private_zsh.d/bootstrap.zsh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dot::timing::setup()
2424
ZSH_TIMES_FILE="/tmp/zshtimes.$$"
2525
echo -n > "$ZSH_TIMES_FILE"
2626
fi
27+
dot::defer "dot::timing::teardown"
2728
}
2829

2930
# Unset ZSH_TIME_STARTUP and ZSH_TIMES_FILE to leave the shell clean
@@ -37,6 +38,36 @@ dot::timing::teardown()
3738
done
3839
}
3940

41+
# Deferred cleanup system {{{
42+
#
43+
# Register commands to run after all startup files have been sourced.
44+
# Deferred commands run in LIFO order (last registered, first executed).
45+
#
46+
# Usage (from any zsh.d/ file):
47+
# dot::defer "unset _my_temp_var"
48+
# dot::defer "unfunction _my_helper"
49+
#
50+
typeset -a _dot_deferred=()
51+
52+
dot::defer()
53+
{
54+
_dot_deferred+=("$1")
55+
}
56+
57+
# Run all deferred commands in LIFO order, then clean up the defer
58+
# machinery itself.
59+
dot::run_deferred()
60+
{
61+
local i
62+
for (( i=${#_dot_deferred[@]}; i >= 1; i-- )); do
63+
eval "${_dot_deferred[$i]}"
64+
done
65+
unset _dot_deferred
66+
unfunction dot::defer
67+
unfunction dot::run_deferred
68+
}
69+
# }}}
70+
4071
dot::timing::timed_source()
4172
{
4273
local file before after
@@ -90,4 +121,12 @@ dot::source_zsh_d()
90121
dot::source_dir ~/.config/zsh/zsh.d
91122
dot::source_dir ~/.config/zsh/zsh.d/local/after
92123
}
124+
125+
dot::bootstrap()
126+
{
127+
dot::timing::setup
128+
dot::defer "unfunction dot::source_file dot::source_dir dot::source_zsh_d dot::bootstrap"
129+
dot::source_zsh_d
130+
dot::run_deferred
131+
}
93132
# }}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Initialize fzf - fuzzy finder
2+
# https://github.com/junegunn/fzf
3+
4+
[[ -f ~/.fzf.zsh ]] && source ~/.fzf.zsh

home/dot_zshrc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@
33
# This is a thin loader: it sources the bootstrap which handles
44
# the modular config under ~/.config/zsh/zsh.d/
55

6-
# Bootstrap config
7-
[[ -f ~/.config/zsh/zsh.d/bootstrap.zsh ]] && source ~/.config/zsh/zsh.d/bootstrap.zsh
8-
9-
dot::timing::setup
10-
11-
dot::source_zsh_d # Load all zsh.d files
12-
13-
# Additional files that I generally want to source
14-
dot::source_file ~/.fzf.zsh
15-
16-
# Timing teardown
17-
dot::timing::teardown
6+
source ~/.config/zsh/zsh.d/bootstrap.zsh
7+
dot::bootstrap
188

199
# -----------------------------------------------------------------------------
2010
# This is where all sort of random commands will append their stuff

0 commit comments

Comments
 (0)