-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshsetup.sh
executable file
·284 lines (238 loc) · 8.04 KB
/
zshsetup.sh
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 [-h] [-v] [-n] [-t THEME] [-p PLUGINS...] [-f PLUGINS_FILE]"
echo "Options:"
echo " -h Show this help message"
echo " -v Verbose output"
echo " -n No backup (skip backing up existing configurations)"
echo " -t THEME Specify oh-my-zsh theme (default: 'powerlevel10k/powerlevel10k')"
echo " -p PLUGINS Specify additional plugins (space-separated list)"
echo " -f FILE Read additional plugins from file (one plugin per line)"
echo
echo "Example: $0 -v -t agnoster -p 'git docker kubectl' -f ~/.zsh_plugins"
exit 1
}
# Function to log messages
log() {
local level=$1
shift
local msg="$@"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
INFO)
[ "$VERBOSE" = true ] && echo "[$timestamp] INFO: $msg"
;;
WARN)
echo "[$timestamp] WARNING: $msg" >&2
;;
ERROR)
echo "[$timestamp] ERROR: $msg" >&2
;;
*)
echo "[$timestamp] $msg"
;;
esac
}
# Function to backup existing configuration
backup_config() {
local backup_dir="$HOME/.zsh_backup_$(date +%Y%m%d_%H%M%S)"
if [ -f "$HOME/.zshrc" ]; then
log INFO "Backing up existing .zshrc to $backup_dir"
mkdir -p "$backup_dir"
cp "$HOME/.zshrc" "$backup_dir/"
fi
if [ -d "$HOME/.oh-my-zsh" ]; then
log INFO "Backing up existing oh-my-zsh to $backup_dir"
cp -r "$HOME/.oh-my-zsh" "$backup_dir/"
fi
}
# Function to install dependencies
install_dependencies() {
log INFO "Installing required packages..."
if command -v apt-get >/dev/null 2>&1; then
sudo -S apt-get update
sudo -S apt-get install -y zsh git curl wget nano
elif command -v yum >/dev/null 2>&1; then
sudo -S yum -y install zsh git curl wget nano
elif command -v pacman >/dev/null 2>&1; then
sudo -S pacman -Sy --noconfirm zsh git curl wget nano
else
log ERROR "Unsupported package manager. Please install zsh, git, curl, wget, and nano manually."
exit 1
fi
}
# Function to install oh-my-zsh
install_oh_my_zsh() {
log INFO "Installing oh-my-zsh..."
if [ -d "$HOME/.oh-my-zsh" ]; then
log WARN "oh-my-zsh is already installed"
return
fi
# Install oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
if [ $? -ne 0 ]; then
log ERROR "Failed to install oh-my-zsh"
exit 1
fi
# Enable auto-updates
log INFO "Enabling oh-my-zsh auto-updates..."
sed -i 's/# zstyle '\'':omz:update'\'' mode auto/zstyle '\'':omz:update'\'' mode auto/' "$HOME/.zshrc"
# Enable command correction
log INFO "Enabling command correction..."
sed -i 's/# ENABLE_CORRECTION="true"/ENABLE_CORRECTION="true"/' "$HOME/.zshrc"
# Set history timestamps
log INFO "Setting history timestamps format..."
sed -i 's/# HIST_STAMPS="mm\/dd\/yyyy"/HIST_STAMPS="mm\/dd\/yyyy"/' "$HOME/.zshrc"
}
# Function to install plugins
install_plugins() {
local plugins=()
# First argument might be a file
if [ -n "$1" ] && [ -f "$1" ]; then
local plugins_file="$1"
shift
# Read plugins from file (one per line)
while IFS= read -r plugin || [ -n "$plugin" ]; do
# Skip empty lines and comments
[[ -z "$plugin" || "$plugin" =~ ^[[:space:]]*# ]] && continue
plugins+=("$plugin")
done < "$plugins_file"
fi
# Add any remaining plugins from command line
plugins+=("$@")
local custom_plugins_dir="$HOME/.oh-my-zsh/custom/plugins"
# Install zsh-autosuggestions
if [ ! -d "$custom_plugins_dir/zsh-autosuggestions" ]; then
log INFO "Installing zsh-autosuggestions..."
git clone https://github.com/zsh-users/zsh-autosuggestions "$custom_plugins_dir/zsh-autosuggestions"
fi
# Install zsh-syntax-highlighting
if [ ! -d "$custom_plugins_dir/zsh-syntax-highlighting" ]; then
log INFO "Installing zsh-syntax-highlighting..."
git clone https://github.com/zsh-users/zsh-syntax-highlighting "$custom_plugins_dir/zsh-syntax-highlighting"
fi
# Install zsh-completions
if [ ! -d "$custom_plugins_dir/zsh-completions" ]; then
log INFO "Installing zsh-completions..."
git clone https://github.com/zsh-users/zsh-completions "$custom_plugins_dir/zsh-completions"
fi
# Install zsh-history-substring-search
if [ ! -d "$custom_plugins_dir/zsh-history-substring-search" ]; then
log INFO "Installing zsh-history-substring-search..."
git clone https://github.com/zsh-users/zsh-history-substring-search "$custom_plugins_dir/zsh-history-substring-search"
fi
# Install apt plugin
if [ ! -d "$custom_plugins_dir/apt-zsh-plugin" ]; then
log INFO "Installing apt plugin..."
git clone https://github.com/GeoLMg/apt-zsh-plugin "$custom_plugins_dir/apt-zsh-plugin"
fi
# Add custom plugins to .zshrc
local plugin_list="git zsh-autosuggestions zsh-syntax-highlighting zsh-completions zsh-history-substring-search history-substring-search apt-zsh-plugin ${plugins[@]}"
sed -i "s|plugins=(git)|plugins=($plugin_list)|" "$HOME/.zshrc"
}
# Function to configure theme
configure_theme() {
local theme=$1
# Install powerlevel10k theme
if [ ! -d "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k" ]; then
log INFO "Installing powerlevel10k theme..."
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
fi
log INFO "Setting theme to $theme..."
sed -i "s|ZSH_THEME=\"[^\"]*\"|ZSH_THEME=\"$theme\"|" "$HOME/.zshrc"
}
# Default values
VERBOSE=false
NO_BACKUP=false
THEME="powerlevel10k/powerlevel10k"
PLUGINS=()
PLUGINS_FILE=""
# Parse arguments
while getopts "hvnt:p:f:" opt; do
case $opt in
h)
usage
;;
v)
VERBOSE=true
;;
n)
NO_BACKUP=true
;;
t)
THEME="$OPTARG"
;;
p)
IFS=' ' read -r -a PLUGINS <<< "$OPTARG"
;;
f)
PLUGINS_FILE="$OPTARG"
if [ ! -f "$PLUGINS_FILE" ]; then
log ERROR "Plugins file not found: $PLUGINS_FILE"
exit 1
fi
;;
\?)
usage
;;
esac
done
# Main installation process
log INFO "Starting zsh setup..."
# Backup existing configuration
if [ "$NO_BACKUP" = false ]; then
backup_config
fi
# Install dependencies
install_dependencies
# Install oh-my-zsh
install_oh_my_zsh
# Install and configure plugins
if [ -n "$PLUGINS_FILE" ]; then
install_plugins "$PLUGINS_FILE" "${PLUGINS[@]}"
else
install_plugins "${PLUGINS[@]}"
fi
# Configure theme
configure_theme "$THEME"
# Set zsh as default shell
if [ "$SHELL" != "$(which zsh)" ]; then
log INFO "Setting zsh as default shell..."
chsh -s "$(which zsh)"
fi
# Add custom configurations
cat >> "$HOME/.zshrc" << 'EOL'
# Custom aliases
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
# History configuration
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
# Key bindings
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
EOL
# Set zsh as default shell
if [ "$SHELL" != "$(which zsh)" ]; then
log INFO "Setting zsh as default shell..."
chsh -s "$(which zsh)"
fi
log INFO "zsh setup completed successfully!"
log INFO "Please restart your terminal or run 'zsh' to start using your new shell"
# echo "Press any key to proceed..."
# # Loop until a key is pressed
# while true; do
# read -rsn1 key # Read a single character silently
# if [[ -n "$key" ]]; then
# nano .zshrc
# break
# fi
# done
exit 0