]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Don't initialize Powerlevel10k "instant prompt" too early
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2024 Alexander Barton <alex@barton.de>
3
4 script_name="${${(%):-%N}:t}"
5 script_type="$script_name[2,-1]"
6
7 # Handle "initialization stage", load all plugins of that stage, either from an
8 # existing cache file or individually, optionally creating the cache.
9 # - $1: Script name
10 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
11 function axzsh_handle_stage {
12         local name="$1"
13         local type="$2"
14
15         [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
16
17         # Look for some 3rd-party integrations ...
18
19         # --- Powerlevel10k ---
20         # Read in Powerlevel10k configuration file, if not already read:
21         [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
22         # Enable instant prompt. Should stay close to the top of ~/.zshrc.
23         # Initialization code that may require console input (password prompts,
24         # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
25         # plugins should do output in their "ax-io" stage only!
26         # Read the initialization script in the "zprofile" stage for login
27         # shells, and in the "zshrc" stage for non-login sub-shells (which have
28         # the profile already read in and therefore will skip the "ax-io" and
29         # "zprofile" stages and not catch up).
30         if [[ \
31                 ( "$type" == "zprofile" ) || \
32                 ( ! -o login && "$type" == "zshrc" && -n "$AXZSH_ZPROFILE_READ" ) \
33         ]]; then
34                 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
35                 [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
36         fi
37
38         # Initialize cache
39         [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
40         local cache_file="$AXZSH/cache/$type.cache"
41
42         local cat_cmd=${commands[cat]:-cat}
43
44         if [[ -r "$cache_file" ]]; then
45                 # Cache file exists, use it!
46                 # But when in the "zshrc" stage, make sure that the "zprofile" stage
47                 # has already been handled (this uses the "01_zprofile" plugin which
48                 # is used in the "zshrc.cache" as well, but can't be used successfully
49                 # there because it becomes sourced inside of a ZSH function; so we have
50                 # to source it here in the global context manually ...):
51                 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
52                         && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
53                 [[ -n "$AXZSH_DEBUG" ]] \
54                         && echo "   - Reading cache file \"$cache_file\" ..."
55                 source "$cache_file"
56                 unfunction axzsh_plugin_init
57         else
58                 # No cache file available.
59                 local new_cache_file="$cache_file.NEW"
60
61                 # Setup list of plugins to load:
62                 local plugin_list
63                 typeset -U plugin_list
64                 plugin_list=(
65                         "$AXZSH/core/"[0-4]*
66                         "$AXZSH/active_plugins/"*(N)
67                         "$AXZSH/core/"[5-9]*
68                 )
69
70                 # Create new cache file:
71                 [[ -n "$AXZSH_DEBUG" ]] \
72                         && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
73                 if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
74                         unset new_cache_file
75                 else
76                         # New cache file successfully created ...
77                         if [[ "$type" = "ax-io" ]]; then
78                                 # AX-IO Stage:
79                                 # Write an initial PATH variable to the cache
80                                 # file, which becomes overwritten by the path
81                                 # plugin at the "zprofile" stage later on, but
82                                 # this way "ax-io" stage plugins have a somewhat
83                                 # saner PATH to begin with ...
84                                 printf 'export PATH="%s"\n\n' "$PATH" >>"$new_cache_file"
85                         fi
86                 fi
87
88                 # Read in all the plugins for the current "type":
89                 for plugin ($plugin_list); do
90                         # Read the "theme file", if any and in "zshrc" stage.
91                         # This must be done before 99_cleanup is run!
92                         if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
93                                 if [[ -r "$AXZSH_THEME" ]]; then
94                                         source "$AXZSH_THEME"
95                                         if [[ -n "$new_cache_file" ]]; then
96                                                 # Source the theme in the new cache file:
97                                                 echo "# BEGIN Theme" >>"$new_cache_file"
98                                                 echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
99                                                 echo "# END Theme" >>"$new_cache_file"
100                                         fi
101                                 fi
102                         fi
103                         axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
104                 done
105
106                 if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
107                         # Move newly created cache file in place:
108                         mv "$new_cache_file" "$cache_file"
109                 fi
110         fi
111 }
112
113 # Load plugin code of a given type.
114 # - $1: plugin name
115 # - $2: plugin type (optional; defaults to "zshrc")
116 # - $3: cache file (optional)
117 function axzsh_load_plugin {
118         local dname="$1:A"
119         local plugin="$dname:t"
120         [[ -z "$2" ]] && local type="zshrc" || local type="$2"
121         local fname="$dname/$plugin.$type"
122         local cache_file="$3"
123
124         # Strip repository prefix (like "alexbarton#test-plugin"):
125         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
126
127         # "short plugin name": strip ".zsh" suffix:
128         plugin_short=${plugin%.zsh}
129
130         if [[ ! -d "$dname" ]]; then
131                 # Plugin not found!
132                 if [[ -n "$AXZSH_DEBUG" ]]; then
133                         # Show error message for all stages in "debug mode":
134                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
135                 elif [[ "$type" == "zshrc" ]]; then
136                         # Show error message for the "zshrc" stage:
137                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
138                 fi
139                 return 1
140         fi
141
142         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
143                 zsh_themes=("$dname/"*.zsh-theme(NY1))
144                 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
145                         # Native AX-ZSH plugin, but for different stage. Skip it!
146                         :
147                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
148                         # Oh My ZSH plugin
149                         type="plugin.zsh"
150                         fname="$dname/${plugin_short}.plugin.zsh"
151                 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
152                         # Oh My ZSH plugin with "zsh-" prefix stripped
153                         type="plugin.zsh"
154                         fname="$dname/${plugin_short##zsh-}.plugin.zsh"
155                 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
156                         # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
157                         type="plugin.zsh"
158                         fname="$dname/${plugin}"
159                 elif [[ -r "$dname/init.zsh" ]]; then
160                         # Prezto module
161                         type="init.zsh"
162                         fname="$dname/init.zsh"
163                 elif [[ ${#zsh_themes} -gt 0 ]]; then
164                         # ZSH "theme plugin", ignore here!
165                         :
166                 else
167                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
168                         echo "Contents of \"$dname\":" >&2
169                         ls -lh "$dname/" >&2
170                         return 0
171                 fi
172         fi
173
174         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
175                 # Add plugin function path when folder exists
176                 [[ -n "$AXZSH_DEBUG" ]] \
177                         && echo "   - $plugin ($type): functions ..."
178                 axzsh_fpath+=("$dname/functions")
179
180                 # Add function path to cache file.
181                 [[ -n "$cache_file" ]] \
182                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
183         fi
184
185         if [[ -r "$fname" ]]; then
186                 # Read plugin ...
187                 [[ -n "$AXZSH_DEBUG" ]] \
188                         && echo "   - $plugin ($type) ..."
189
190                 # Note for "external" ("repo/*") plugins and unusual ("not so
191                 # modern") terminals: These (modern?) plugins most probably
192                 # don't expect such a terminal configuration and don't behave
193                 # well (echo color sequences, for example). Therefore we DON'T
194                 # load any external plugins at all in that case: this results in
195                 # reduced/disabled functionality, but hopefully in readable
196                 # output ...
197
198                 case "$fname" in
199                         *"/repos/"*)
200                                 axzsh_is_modern_terminal && source "$fname"
201                                 ;;
202                         *)
203                                 source "$fname"
204                 esac
205
206                 if [[ -n "$cache_file" ]]; then
207                         # Add plugin data to cache
208                         printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
209                         case "$fname" in
210                                 *"/repos/"*)
211                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
212                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
213                                         ;;
214                                 *)
215                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
216                                         "$cat_cmd" "$fname" >>"$cache_file"
217                         esac
218                         printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
219                 fi
220         fi
221
222         # It is a success, even if only the plugin directory (and no script!)
223         # exists at all! Rationale: The script could be of an other type ...
224         return 0
225 }
226
227 # Make sure that "my" (=ZSH) directory is in the search path ...
228 if [[ -z "$AXZSH" ]]; then
229         _p="${0:h}"
230         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
231         unset _p
232 fi
233
234 # Make sure that "SHELL" variable is set and exported
235 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
236
237 # Make sure that "AXZSH" variable is set and exported
238 [[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
239
240 # Check for "debug mode" ...
241 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
242         export AXZSH_DEBUG=1
243         export POWERLEVEL9K_INSTANT_PROMPT=quiet
244         echo "AXZSH=$AXZSH"
245         echo "AXZSH_DEBUG=$AXZSH_DEBUG"
246         echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
247         echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
248         echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
249 fi
250
251 if [[ "$script_type" = "zprofile" ]]; then
252         # Load all "output" plugins first, that is, before the "zprofile stage":
253         axzsh_handle_stage "$script_name" "ax-io"
254 fi
255
256 axzsh_handle_stage "$script_name" "$script_type"
257
258 # Clean up ...
259 unfunction axzsh_handle_stage axzsh_load_plugin
260 unset script_name script_type
261
262 # Hints for external installers:
263 # - iTerm2: DON'T install "iterm2_shell_integration"!