|
| 1 | +#!/usr/bin/env zsh |
| 2 | + |
| 3 | +#! ██████╗░░█████╗░ ███╗░░██╗░█████╗░████████╗ ███████╗██████╗░██╗████████╗ |
| 4 | +#! ██╔══██╗██╔══██╗ ████╗░██║██╔══██╗╚══██╔══╝ ██╔════╝██╔══██╗██║╚══██╔══╝ |
| 5 | +#! ██║░░██║██║░░██║ ██╔██╗██║██║░░██║░░░██║░░░ █████╗░░██║░░██║██║░░░██║░░░ |
| 6 | +#! ██║░░██║██║░░██║ ██║╚████║██║░░██║░░░██║░░░ ██╔══╝░░██║░░██║██║░░░██║░░░ |
| 7 | +#! ██████╔╝╚█████╔╝ ██║░╚███║╚█████╔╝░░░██║░░░ ███████╗██████╔╝██║░░░██║░░░ |
| 8 | +#! ╚═════╝░░╚════╝░ ╚═╝░░╚══╝░╚════╝░░░░╚═╝░░░ ╚══════╝╚═════╝░╚═╝░░░╚═╝░░░ |
| 9 | + |
| 10 | +# HyDE's ZSH env configuration |
| 11 | +# This file is sourced by ZSH on startup |
| 12 | +# And ensures that we have an obstruction-free .zshrc file |
| 13 | +# This also ensures that the proper HyDE $ENVs are loaded |
| 14 | + |
| 15 | +function _load_zsh_plugins { |
| 16 | + unset -f _load_zsh_plugins |
| 17 | + # Oh-my-zsh installation path |
| 18 | + zsh_paths=( |
| 19 | + "$HOME/.oh-my-zsh" |
| 20 | + "/usr/local/share/oh-my-zsh" |
| 21 | + "/usr/share/oh-my-zsh" |
| 22 | + ) |
| 23 | + for zsh_path in "${zsh_paths[@]}"; do [[ -d $zsh_path ]] && export ZSH=$zsh_path && break; done |
| 24 | + # Load Plugins |
| 25 | + hyde_plugins=(git zsh-256color zsh-autosuggestions zsh-syntax-highlighting) |
| 26 | + plugins+=("${plugins[@]}" "${hyde_plugins[@]}") |
| 27 | + # Deduplicate plugins |
| 28 | + plugins=("${plugins[@]}") |
| 29 | + plugins=($(printf "%s\n" "${plugins[@]}" | sort -u)) |
| 30 | + # Defer oh-my-zsh loading until after prompt appears |
| 31 | + typeset -g DEFER_OMZ_LOAD=1 |
| 32 | +} |
| 33 | + |
| 34 | +function _load_post_init() { |
| 35 | + #! Never load time consuming functions here |
| 36 | + |
| 37 | + # Add your completions directory to fpath |
| 38 | + fpath=($ZDOTDIR/completions "${fpath[@]}") |
| 39 | + |
| 40 | + # Initialize completions with optimized performance |
| 41 | + autoload -Uz compinit |
| 42 | + |
| 43 | + HYDE_ZSH_COMPINIT_CHECK=${HYDE_ZSH_COMPINIT_CHECK:-1} |
| 44 | + if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+${HYDE_ZSH_COMPINIT_CHECK}) ]]; then |
| 45 | + compinit |
| 46 | + else |
| 47 | + compinit -C |
| 48 | + fi |
| 49 | + |
| 50 | + _comp_options+=(globdots) # tab complete hidden files |
| 51 | + |
| 52 | + for file in "${ZDOTDIR:-$HOME/.config/zsh}/completions/"*.zsh; do |
| 53 | + [ -r "$file" ] && source "$file" |
| 54 | + done |
| 55 | + |
| 56 | + # Load all custom function files // Directories are ignored |
| 57 | + for file in "${ZDOTDIR:-$HOME/.config/zsh}/functions/"*.zsh; do |
| 58 | + [ -r "$file" ] && source "$file" |
| 59 | + done |
| 60 | + |
| 61 | + # zsh-autosuggestions won't work on first prompt when deferred |
| 62 | + if typeset -f _zsh_autosuggest_start >/dev/null; then |
| 63 | + _zsh_autosuggest_start |
| 64 | + fi |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +function _load_omz_on_init() { |
| 69 | + # Load oh-my-zsh when line editor initializes // before user input |
| 70 | + if [[ -n $DEFER_OMZ_LOAD ]]; then |
| 71 | + unset DEFER_OMZ_LOAD |
| 72 | + [[ -r $ZSH/oh-my-zsh.sh ]] && source $ZSH/oh-my-zsh.sh |
| 73 | + ZDOTDIR="${__ZDOTDIR:-${XDG_CONFIG_HOME:-$HOME/.config}/zsh}" |
| 74 | + _load_post_init |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +function do_render { |
| 79 | + # Check if the terminal supports images |
| 80 | + local type="${1:-image}" |
| 81 | + # TODO: update this list if needed |
| 82 | + TERMINAL_IMAGE_SUPPORT=(kitty konsole ghostty WezTerm) |
| 83 | + local terminal_no_art=(vscode code codium) |
| 84 | + TERMINAL_NO_ART="${TERMINAL_NO_ART:-${terminal_no_art[@]}}" |
| 85 | + CURRENT_TERMINAL="${TERM_PROGRAM:-$(ps -o comm= -p $(ps -o ppid= -p $$))}" |
| 86 | + |
| 87 | + case "${type}" in |
| 88 | + image) |
| 89 | + if [[ " ${TERMINAL_IMAGE_SUPPORT[@]} " =~ " ${CURRENT_TERMINAL} " ]]; then |
| 90 | + return 0 |
| 91 | + else |
| 92 | + return 1 |
| 93 | + fi |
| 94 | + ;; |
| 95 | + art) |
| 96 | + if [[ " ${TERMINAL_NO_ART[@]} " =~ " ${CURRENT_TERMINAL} " ]]; then |
| 97 | + return 1 |
| 98 | + else |
| 99 | + return 0 |
| 100 | + fi |
| 101 | + ;; |
| 102 | + *) |
| 103 | + return 1 |
| 104 | + ;; |
| 105 | + esac |
| 106 | +} |
| 107 | + |
| 108 | +_load_deferred_plugin_system_by_hyde() { |
| 109 | + |
| 110 | + # Exit early if HYDE_ZSH_DEFER is not set to 1 |
| 111 | + if [[ "${HYDE_ZSH_DEFER}" != "1" ]]; then |
| 112 | + unset -f _load_deferred_plugin_system_by_hyde |
| 113 | + return |
| 114 | + fi |
| 115 | + |
| 116 | + # Load plugins |
| 117 | + _load_zsh_plugins |
| 118 | + |
| 119 | + # Load zsh hooks module once |
| 120 | + |
| 121 | + #? Methods to load oh-my-zsh lazily |
| 122 | + __ZDOTDIR="${ZDOTDIR:-${XDG_CONFIG_HOME:-$HOME/.config}/zsh}" |
| 123 | + # Temporarily set ZDOTDIR to /tmp to isolate deferred plugin loading from the user's primary configuration directory. |
| 124 | + ZDOTDIR=/tmp |
| 125 | + zle -N zle-line-init _load_omz_on_init # Loads when the line editor initializes // The best option |
| 126 | + |
| 127 | + # Below this line are the commands that are executed after the prompt appears |
| 128 | + |
| 129 | + autoload -Uz add-zsh-hook |
| 130 | + # add-zsh-hook zshaddhistory load_omz_deferred # loads after the first command is added to history |
| 131 | + # add-zsh-hook precmd load_omz_deferred # Loads when shell is ready to accept commands |
| 132 | + # add-zsh-hook preexec load_omz_deferred # Loads before the first command executes |
| 133 | + |
| 134 | + # TODO: add handlers in pm.sh |
| 135 | + # for these aliases please manually add the following lines to your .zshrc file.(Using yay as the aur helper) |
| 136 | + # pc='yay -Sc' # remove all cached packages |
| 137 | + # po='yay -Qtdq | ${PM_COMMAND[@]} -Rns -' # remove orphaned packages |
| 138 | + |
| 139 | + # Some binds won't work on first prompt when deferred |
| 140 | + bindkey '\e[H' beginning-of-line |
| 141 | + bindkey '\e[F' end-of-line |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +#? Override this environment variable in ~/.zshrc |
| 146 | +# cleaning up home folder |
| 147 | +# ZSH Plugin Configuration |
| 148 | + |
| 149 | +HYDE_ZSH_DEFER="1" #Unset this variable in $ZDOTDIR/user.zsh to disable HyDE's deferred Zsh loading. |
| 150 | +HYDE_ZSH_PROMPT="1" #Unset this variable in $ZDOTDIR/user.zsh to disable HyDE's prompt customization. |
| 151 | + |
| 152 | +ZSH_AUTOSUGGEST_STRATEGY=(history completion) |
| 153 | + |
| 154 | +# # History configuration |
| 155 | +HISTFILE=${HISTFILE:-$ZDOTDIR/.zsh_history} |
| 156 | +if [[ -f $HOME/.zsh_history ]] && [[ ! -f $HISTFILE ]]; then |
| 157 | + echo "Please manually move $HOME/.zsh_history to $HISTFILE" |
| 158 | + echo "Or move it somewhere else to avoid conflicts" |
| 159 | +fi |
| 160 | + |
| 161 | +export HISTFILE ZSH_AUTOSUGGEST_STRATEGY |
| 162 | + |
| 163 | +# HyDE Package Manager |
| 164 | +PM_COMMAND=(hyde-shell pm) |
| 165 | + |
| 166 | +# Optionally load user configuration // useful for customizing the shell without modifying the main file |
| 167 | +if [[ -f $HOME/.hyde.zshrc ]]; then |
| 168 | + source $HOME/.hyde.zshrc # for backward compatibility |
| 169 | +elif [[ -f $HOME/.user.zsh ]]; then |
| 170 | + source $HOME/.user.zsh # renamed to .user.zsh for intuitiveness that it is a user config |
| 171 | +elif [[ -f $ZDOTDIR/user.zsh ]]; then |
| 172 | + source $ZDOTDIR/user.zsh |
| 173 | +fi |
| 174 | + |
| 175 | +# Try to load prompts immediately |
| 176 | +[[ -f $ZDOTDIR/conf.d/hyde/prompt.zsh ]] && source $ZDOTDIR/conf.d/hyde/prompt.zsh |
| 177 | + |
| 178 | +_load_deferred_plugin_system_by_hyde |
| 179 | + |
| 180 | +alias c='clear' \ |
| 181 | + in='${PM_COMMAND[@]} install' \ |
| 182 | + un='${PM_COMMAND[@]} remove' \ |
| 183 | + up='${PM_COMMAND[@]} upgrade' \ |
| 184 | + pl='${PM_COMMAND[@]} search installed' \ |
| 185 | + pa='${PM_COMMAND[@]} search all' \ |
| 186 | + vc='code' \ |
| 187 | + fastfetch='fastfetch --logo-type kitty' \ |
| 188 | + ..='cd ..' \ |
| 189 | + ...='cd ../..' \ |
| 190 | + .3='cd ../../..' \ |
| 191 | + .4='cd ../../../..' \ |
| 192 | + .5='cd ../../../../..' \ |
| 193 | + mkdir='mkdir -p' |
| 194 | + |
| 195 | +# revert to proper ZDOTDIR |
| 196 | +export ZDOTDIR="${__ZDOTDIR:-${XDG_CONFIG_HOME:-$HOME/.config}/zsh}" |
| 197 | +unset __ZDOTDIR |
0 commit comments