Skip to content

Commit

Permalink
fix(zsh): reduce bootstrap time ~2100 => ~1600 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
Avivbens committed May 20, 2024
1 parent 6cb252e commit fb46ee4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
36 changes: 10 additions & 26 deletions zsh/.entry-point.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export TERMINAL_INSTANCE_ID=$(date +%s)

source "$HOME/shell-config/zsh/.utils.sh"


# compinit and completions
autoload -Uz compinit
compinit

# Homebrew paths
BROW="/usr/local/Homebrew/bin/brew"
BREW="/opt/homebrew/bin/brew"
Expand Down Expand Up @@ -33,12 +38,7 @@ export NVM_DIR="$HOME/.nvm"

# ----- shell-config -----
export PATH="$HOME/shell-config/executable:$PATH"
source <(shell-config completion-script)

function grant_permissions() {
sudo chown -R "$USER":admin "$1"
chmod -R 770 "$1"
}
cache_completion "shell-config completion-script" "shell-config" 120

function shell-doctor() {
chmod -R 755 $(compaudit)
Expand All @@ -64,23 +64,20 @@ if find $HOME/shell-config/zsh -type d ! -perm 770 -print -quit | grep -q .; the
fi

# check for shell-config updates once in 10 times
silent_background() {
{ "$@" 2>&3 & } 3>&2 2>/dev/null
disown &>/dev/null # Prevent whine if job has already completed
}
if [ $((RANDOM % 10)) -eq 0 ]; then
silent_background shell-config update -m
fi
# ----- shell-config -----

# if google-cloud-sdk installed manually via download
sourceIf "$HOME/Downloads/google-cloud-sdk/path.zsh.inc"
sourceIf "$HOME/Downloads/google-cloud-sdk/completion.zsh.inc"

# if google-cloud-sdk installed via Homebrew
sourceIf "$BREW_PERFIX/share/google-cloud-sdk/path.zsh.inc"
sourceIf "$BREW_PERFIX/share/google-cloud-sdk/completion.zsh.inc"

# if google-cloud-sdk installed via Homebrew legacy
sourceIf "$BROW_PERFIX/share/google-cloud-sdk/path.zsh.inc"
sourceIf "$BROW_PERFIX/share/google-cloud-sdk/completion.zsh.inc"

# Autosuggest
sourceIf "$BREW_PERFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
sourceIf "$BROW_PERFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
Expand All @@ -89,19 +86,11 @@ sourceIf "$BROW_PERFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
# only if zsh-completions installed via Homebrew
if [ -f "$BREW_PERFIX/share/zsh-completions" ]; then
FPATH="$BREW_PERFIX/share/zsh-completions:$FPATH"
# load compinit as a function on an exported path to avoid overlapping with other compinit commands
autoload -Uz compinit
# toggle ON completions with tab key
compinit
fi

# only if zsh-completions installed via legacy Homebrew
if [ -f "$BROW_PERFIX/share/zsh-completions" ]; then
FPATH=$BROW_PERFIX/share/zsh-completions:$FPATH
# load compinit as a function on an exported path to avoid overlapping with other compinit commands
autoload -Uz compinit
# toggle ON completions with tab key
compinit
fi

# Colored correct code
Expand Down Expand Up @@ -152,11 +141,6 @@ function killport() {
autoload -U add-zsh-hook
add-zsh-hook -Uz chpwd (){ ls; }

# github-copilot-cli alias setup
if command -v github-copilot-cli &>/dev/null; then
eval "$(github-copilot-cli alias -- "$0")"
fi

# sync
alias reload="exec /bin/zsh"

Expand Down
35 changes: 35 additions & 0 deletions zsh/.utils.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env zsh

# Usage: performance_mark "marker_name"; ...; performance_mark "marker_name"
function performance_mark() {
# if `gdate` not exists, install
if ! command -v gdate &>/dev/null; then
Expand Down Expand Up @@ -55,8 +56,42 @@ function wait_for_input() {
done
}

# Usage: sourceIf <file>
function sourceIf() {
if [ -f "$1" ]; then
source $1
fi
}

# Usage: grant_permissions <path>
function grant_permissions() {
sudo chown -R "$USER":admin "$1"
chmod -R 770 "$1"
}

# Usage: silent_background <command>
function silent_background() {
{ "$@" 2>&3 & } 3>&2 2>/dev/null
disown &>/dev/null # Prevent whine if job has already completed
}

# cache completion scripts to speed up shell startup
# Usage: cache_completion <completion_script> <completion_name> <TTL_in_minutes>
function cache_completion() {
local completion_script=$1
local completion_name=$2
local ttl=$3

local cache_dir="$HOME/.cache/completions"
local cache_file="$cache_dir/$completion_name"

if [ ! -d "$cache_dir" ]; then
mkdir -p "$cache_dir"
fi

if [ ! -f "$cache_file" ] || [ $(find "$cache_file" -mmin +$ttl) ]; then
eval "$completion_script" >$cache_file
fi

source $cache_file
}
2 changes: 0 additions & 2 deletions zsh/.zshrc.extends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

echo "extends loaded"

source "$HOME/shell-config/zsh/.utils.sh"

function get_all_enabled_files() {
find "$1" -type f -not -name "*.disabled" -print0 | xargs -0 -I {} basename {} | tr '\n' ' '
}
Expand Down
1 change: 1 addition & 0 deletions zsh/extends/.zshrc.extends.angular.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ function ngrxcreate() {
# Load Angular CLI autocompletion.
if command -v ng &>/dev/null; then
source <(ng completion script)
cache_completion "ng completion script" "angular" 120
fi
2 changes: 0 additions & 2 deletions zsh/external/.external-registry.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#! /usr/bin/env zsh

source "$HOME/shell-config/zsh/.utils.sh"

function get_all_external_files() {
find "$1" -type f -not -name ".gitkeep" -print0 | xargs -0 -I {} basename {} | tr '\n' ' '
}
Expand Down

0 comments on commit fb46ee4

Please sign in to comment.