-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutable_dot_zshrc
More file actions
217 lines (183 loc) · 6.48 KB
/
executable_dot_zshrc
File metadata and controls
217 lines (183 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/zsh
GITHUB_USERNAME="ThePhaseless"
# Disable Aloy "magic enter"
ALOY_MAGICENTER=()
bindkey '^M' accept-line
export PATH=$HOME/.local/bin:$PATH
export _ZO_EXCLUDE_DIRS="!(@(node_modules|dist))"
export DISABLE_AUTO_UPDATE="true"
[[ -f ~/.zprofile ]] && source ~/.zprofile
if command -v zed &>/dev/null; then
export EDITOR="zed --wait"
elif command -v code &>/dev/null; then
export EDITOR="code --wait"
else
export EDITOR="nano"
fi
if [[ -n $INTELLIJ_ENVIRONMENT_READER || -n $VSCODE_RESOLVING_ENVIRONMENT ]]; then
return 0
fi
# Functions
init_chezmoi() {
if ! command -v chezmoi &>/dev/null; then
return
fi
# Only init if the repo doesn't exist; skip the synchronous update to save startup time
if [ ! -d "$HOME/.local/share/chezmoi" ]; then
(cd "$HOME" && sh -c "$(curl -fsLS get.chezmoi.io/lb)" -- init --apply "$GITHUB_USERNAME")
fi
}
init_antidote() {
local antidote_dir="$HOME/.antidote"
local plugins_txt="$HOME/.zsh_plugins.txt"
local plugins_zsh="$HOME/.zsh_plugins.zsh"
# Nothing to do without a plugin list
if [[ ! -f "$plugins_txt" ]]; then
return 0
fi
# Install antidote if missing
if [[ ! -d "$antidote_dir" ]]; then
update_repo "https://github.com/mattmc3/antidote.git" "$antidote_dir" || return 1
fi
if [[ ! -f "$antidote_dir/antidote.zsh" ]]; then
echo "antidote install appears broken, skipping plugins"
return 1
fi
# Lazy-load antidote and generate the static load file only when needed
if [[ ! -s "$plugins_zsh" ]] || [[ "$plugins_txt" -nt "$plugins_zsh" ]]; then
(
source "$antidote_dir/antidote.zsh"
antidote bundle < "$plugins_txt" > "$plugins_zsh"
zcompile "$plugins_zsh" # Pre-compile to binary for maximum speed
)
fi
# Source the static file if it exists and is non-empty
if [[ -s "$plugins_zsh" ]]; then
source "$plugins_zsh"
else
echo "antidote plugin file is empty or missing, skipping"
return 1
fi
}
init_locale() {
# Check if en_US.utf-8 is generated
if ! locale -a | grep -qE "en_US\.(utf|UTF)-?8"; then
echo "Locale missing! Please run manually: sudo locale-gen en_US.UTF-8"
fi
# Set locale if not set to en_US.UTF-8
if ! echo "$LANG" | grep -qE "en_US\.(utf|UTF)-?8"; then
export LANG="en_US.UTF-8"
fi
}
update_repo() {
if ! command -v git &>/dev/null; then
echo "git not found, skipping repo update for $1"
return 1
fi
local repo_url=$1
local output_path=$2
# Extract repo name
local repo_name=$(basename "$1" .git)
repo_name=${repo_name#.}
# Set default output path
if [ -z "$output_path" ]; then
output_path=$HOME/.${repo_name}
fi
# Check if repo already exists
if [ -d "$output_path" ]; then
git -qC "$output_path" pull
else
# Clone repo
git clone --depth=1 "$repo_url" "$output_path"
fi
}
bind_keys() {
bindkey '^I' menu-select # TAB
bindkey -M menuselect '^I' menu-complete # TAB
bindkey "${terminfo[kcbt]}" menu-select # shift-tab
bindkey -M menuselect "${terminfo[kcbt]}" reverse-menu-complete # shift-tab
bindkey -M menuselect '^[[D' .backward-char '^[OD' .backward-char # arrow left
bindkey -M menuselect '^[[C' .forward-char '^[OC' .forward-char # arrow right
}
error_occurred=0
init_locale || error_occurred=1
init_chezmoi || error_occurred=1
init_antidote || error_occurred=1
# Only remove the cache on error, no clear screen yet
if [[ $error_occurred -ne 0 ]]; then
echo "An error occurred during initialization. Clearing antidote cache... Please check the output above."
rm -f "$HOME/.zsh_plugins.zsh"
fi
# Config
bind_keys
# Completion
function safe_smartcache() {
local subcmd="$1"; shift
local binary="$1"
if ! (( $+functions[smartcache] )); then
return 0
fi
if (( $+commands[$binary] )); then
smartcache "$subcmd" "$@" 2>/dev/null
fi
}
safe_smartcache comp gh completion -s zsh
safe_smartcache comp chezmoi completion zsh
safe_smartcache comp uv generate-shell-completion zsh
safe_smartcache comp uvx --generate-shell-completion zsh
safe_smartcache comp ng completion script zsh
safe_smartcache eval direnv hook zsh
safe_smartcache eval zoxide init zsh --no-cmd
if [[ -f /home/linuxbrew/.linuxbrew/bin/brew ]]; then
export HOMEBREW_NO_ENV_HINTS=1
export HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew";
export HOMEBREW_CELLAR="/home/linuxbrew/.linuxbrew/Cellar";
export HOMEBREW_REPOSITORY="/home/linuxbrew/.linuxbrew/Homebrew";
fpath[1,0]="/home/linuxbrew/.linuxbrew/share/zsh/site-functions";
export PATH="/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin${PATH+:$PATH}";
[ -z "${MANPATH-}" ] || export MANPATH=":${MANPATH#:}";
export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:${INFOPATH:-}";
fi
# Custom cd wrapper to suppress error if already in target directory
function cd() {
if ! (( $+functions[__zoxide_z] )); then
builtin cd "$@"
return $?
fi
__zoxide_z "$@"
local ret=$?
# If zoxide failed (ret != 0), check if we are already in the target
if [[ $ret -ne 0 ]]; then
local target
if [[ -z $@ ]]; then
target="$HOME"
else
target="$(zoxide query "$@" 2>/dev/null)"
fi
# If the intended target is the current directory, return success (0)
if [[ $target == "$PWD" ]]; then
return 0
fi
fi
return $ret
}
# Final error check and single screen clear before MOTD
if [[ $error_occurred -eq 0 ]]; then
# clear
else
echo "An error occurred during initialization. Please check the output above."
fi
if [ -f /var/run/motd.dynamic ]; then
reset
cat /var/run/motd.dynamic
fi
## Start async chezmoi auto-updater (Rate-limited to once per hour)
if command -v bash >/dev/null 2>&1 && [[ -f "$HOME/.local/share/chezmoi/chezmoi-auto-update.sh" ]]; then
local update_marker="/tmp/.chezmoi_last_update"
# Check if marker is missing OR older than 60 minutes
if [[ ! -f "$update_marker" ]] || [[ -z $(find "$update_marker" -mmin -60 2>/dev/null) ]]; then
touch "$update_marker" # Reset the clock immediately
# Run in background, log output for debugging, and disown
( bash "$HOME/.local/share/chezmoi/chezmoi-auto-update.sh" > /tmp/chezmoi-update.log 2>&1 ) &|
fi
fi