]> arthur.barton.de Git - ax-zsh.git/blobdiff - ax.zsh
P10k: Read config after enabling instant prompt
[ax-zsh.git] / ax.zsh
diff --git a/ax.zsh b/ax.zsh
index 2d7bd66b6093f705d3cca365871fcb098612285e..05f20021a7e60af4bfca676b3e0d666531b0a9d2 100644 (file)
--- a/ax.zsh
+++ b/ax.zsh
@@ -1,5 +1,5 @@
 # AX-ZSH: Alex' Modular ZSH Configuration
-# Copyright (c) 2015-2020 Alexander Barton <alex@barton.de>
+# Copyright (c) 2015-2024 Alexander Barton <alex@barton.de>
 
 script_name="${${(%):-%N}:t}"
 script_type="$script_name[2,-1]"
@@ -9,16 +9,41 @@ script_type="$script_name[2,-1]"
 # - $1: Script name
 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
 function axzsh_handle_stage {
-       name="$1"
-       type="$2"
+       local name="$1"
+       local type="$2"
 
        [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
 
+       # Look for some 3rd-party integrations ...
+
+       # --- Powerlevel10k ---
+       # Enable instant prompt. Should stay close to the top of ~/.zshrc.
+       # Initialization code that may require console input (password prompts,
+       # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
+       # plugins should do output in their "ax-io" stage only!
+       # Read the initialization script in the "zprofile" stage for login
+       # shells, and in the "zshrc" stage for non-login sub-shells (which have
+       # the profile already read in and therefore will skip the "ax-io" and
+       # "zprofile" stages and not catch up).
+       if [[ \
+               ( "$type" == "zprofile" ) || \
+               ( ! -o login && "$type" == "zshrc" && -n "$AXZSH_ZPROFILE_READ" ) \
+       ]]; then
+               p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
+               if [[ -r "$p10k_instant_prompt" ]]; then
+                       [[ -n "$AXZSH_DEBUG" ]] && echo "  Reading \"$p10k_instant_prompt\" ..."
+                       source "$p10k_instant_prompt"
+               fi
+               unset p10k_instant_prompt
+       fi
+       # Read in Powerlevel10k configuration file, if not already read:
+       [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
+
        # Initialize cache
-       mkdir -p "$AXZSH/cache"
-       cache_file="$AXZSH/cache/$type.cache"
+       [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
+       local cache_file="$AXZSH/cache/$type.cache"
 
-       cat_cmd=${commands[cat]:-cat}
+       local cat_cmd=${commands[cat]:-cat}
 
        if [[ -r "$cache_file" ]]; then
                # Cache file exists, use it!
@@ -32,22 +57,36 @@ function axzsh_handle_stage {
                [[ -n "$AXZSH_DEBUG" ]] \
                        && echo "   - Reading cache file \"$cache_file\" ..."
                source "$cache_file"
-               unfunction ax_plugin_init
+               (( $+functions[axzsh_plugin_init] )) && unfunction axzsh_plugin_init
        else
                # No cache file available.
+               local new_cache_file="$cache_file.NEW"
+
                # Setup list of plugins to load:
+               local plugin_list
                typeset -U plugin_list
                plugin_list=(
-                       "$AXZSH/core/"[0-5]*
+                       "$AXZSH/core/"[0-4]*
                        "$AXZSH/active_plugins/"*(N)
-                       "$AXZSH/core/"[6-9]*
+                       "$AXZSH/core/"[5-9]*
                )
 
                # Create new cache file:
-               if [[ -n "$cache_file" ]]; then
-                       [[ -n "$AXZSH_DEBUG" ]] \
-                               && echo "   (Writing new cache file to \"$cache_file\" ...)"
-                       printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
+               [[ -n "$AXZSH_DEBUG" ]] \
+                       && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
+               if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
+                       unset new_cache_file
+               else
+                       # New cache file successfully created ...
+                       if [[ "$type" = "ax-io" ]]; then
+                               # AX-IO Stage:
+                               # Write an initial PATH variable to the cache
+                               # file, which becomes overwritten by the path
+                               # plugin at the "zprofile" stage later on, but
+                               # this way "ax-io" stage plugins have a somewhat
+                               # saner PATH to begin with ...
+                               printf 'export PATH="%s"\n\n' "$PATH" >>"$new_cache_file"
+                       fi
                fi
 
                # Read in all the plugins for the current "type":
@@ -57,16 +96,21 @@ function axzsh_handle_stage {
                        if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
                                if [[ -r "$AXZSH_THEME" ]]; then
                                        source "$AXZSH_THEME"
-                                       if [[ -n "$cache_file" ]]; then
+                                       if [[ -n "$new_cache_file" ]]; then
                                                # Source the theme in the new cache file:
-                                               echo "# BEGIN Theme" >>"$cache_file"
-                                               echo 'source "$AXZSH_THEME"' >>"$cache_file"
-                                               echo "# END Theme" >>"$cache_file"
+                                               echo "# BEGIN Theme" >>"$new_cache_file"
+                                               echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
+                                               echo "# END Theme" >>"$new_cache_file"
                                        fi
                                fi
                        fi
-                       axzsh_load_plugin "$plugin" "$type" "$cache_file"
+                       axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
                done
+
+               if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
+                       # Move newly created cache file in place:
+                       mv "$new_cache_file" "$cache_file"
+               fi
        fi
 }
 
@@ -112,6 +156,10 @@ function axzsh_load_plugin {
                        # Oh My ZSH plugin with "zsh-" prefix stripped
                        type="plugin.zsh"
                        fname="$dname/${plugin_short##zsh-}.plugin.zsh"
+               elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
+                       # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
+                       type="plugin.zsh"
+                       fname="$dname/${plugin}"
                elif [[ -r "$dname/init.zsh" ]]; then
                        # Prezto module
                        type="init.zsh"
@@ -161,7 +209,7 @@ function axzsh_load_plugin {
 
                if [[ -n "$cache_file" ]]; then
                        # Add plugin data to cache
-                       printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
+                       printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
                        case "$fname" in
                                *"/repos/"*)
                                        echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
@@ -171,7 +219,7 @@ function axzsh_load_plugin {
                                        echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
                                        "$cat_cmd" "$fname" >>"$cache_file"
                        esac
-                       printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
+                       printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
                fi
        fi
 
@@ -191,14 +239,17 @@ fi
 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
 
 # Make sure that "AXZSH" variable is set and exported
-if [[ -z "$AXZSH" ]]; then
-       export AXZSH="$HOME/.axzsh"
-       if [[ -f "$HOME/.axzsh.debug" ]]; then
-               export AXZSH_DEBUG=1
-               echo "AXZSH=$AXZSH"
-               echo "AXZSH_DEBUG=$AXZSH_DEBUG"
-               echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
-       fi
+[[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
+
+# Check for "debug mode" ...
+if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
+       export AXZSH_DEBUG=1
+       export POWERLEVEL9K_INSTANT_PROMPT=quiet
+       echo "AXZSH=$AXZSH"
+       echo "AXZSH_DEBUG=$AXZSH_DEBUG"
+       echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
+       echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
+       echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
 fi
 
 if [[ "$script_type" = "zprofile" ]]; then
@@ -211,3 +262,6 @@ axzsh_handle_stage "$script_name" "$script_type"
 # Clean up ...
 unfunction axzsh_handle_stage axzsh_load_plugin
 unset script_name script_type
+
+# Hints for external installers:
+# - iTerm2: DON'T install "iterm2_shell_integration"!