Última atividade 21 hours ago

weehong revisou este gist 21 hours ago. Ir para a revisão

2 files changed, 48 insertions, 82 deletions

zsh_ubuntu.sh

@@ -21,7 +21,6 @@ err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
21 21 SKIP_PACKAGES=false
22 22 SKIP_GIT_CONFIG=false
23 23 SKIP_SHELL_CHANGE=false
24 - SKIP_XCLIP=false
25 24 INSTALL_HOMEBREW=false
26 25
27 26 GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw"
@@ -85,20 +84,6 @@ set_timezone() {
85 84 ok "Timezone set to Asia/Singapore"
86 85 }
87 86
88 - install_xclip() {
89 - local os
90 - os="$(detect_os)"
91 - [[ "$os" != "ubuntu" && "$os" != "debian" ]] && return 0
92 - $SKIP_XCLIP && return 0
93 - command -v xclip >/dev/null 2>&1 && ok "xclip already installed" && return 0
94 -
95 - read -r -p "Install xclip? (y/N): " r
96 - case "$r" in
97 - [Yy]) sudo apt-get install -y xclip; ok "xclip installed" ;;
98 - *) log "Skipping xclip installation" ;;
99 - esac
100 - }
101 -
102 87 configure_git() {
103 88 $SKIP_GIT_CONFIG && return
104 89 log "Configuring Git..."
@@ -185,7 +170,7 @@ update_zshrc() {
185 170 log "Checking .zshrc..."
186 171
187 172 if [[ ! -f "$zshrc" ]]; then
188 - warn ".zshrc not found. Run option 11 first."
173 + warn ".zshrc not found. Run option 10 first."
189 174 return 1
190 175 fi
191 176
@@ -209,21 +194,20 @@ show_menu() {
209 194 echo "==========================================="
210 195 echo "Zsh Installer - Choose what to do"
211 196 echo "==========================================="
212 - echo " 0) Run ALL steps (1-13)"
197 + echo " 0) Run ALL steps (1-12)"
213 198 echo " 1) Update system packages"
214 199 echo " 2) Install core packages (zsh, git, vim, etc.)"
215 200 echo " 3) Set Timezone (Asia/Singapore)"
216 - echo " 4) Install xclip"
217 - echo " 5) Configure Git"
218 - echo " 6) Install Homebrew"
219 - echo " 7) Configure shell (chsh - sets default shell)"
220 - echo " 8) Install Oh My Zsh"
221 - echo " 9) Install plugins (autosuggestions, syntax highlighting)"
222 - echo "10) Install Spaceship theme"
223 - echo "11) Download custom configs (~/.alias, .vimrc, etc.)"
224 - echo "12) Check ~/.zshrc load order"
225 - echo "13) Switch to Zsh (Temporary Sub-shell)"
226 - echo "14) Quit"
201 + echo " 4) Configure Git"
202 + echo " 5) Install Homebrew"
203 + echo " 6) Configure shell (chsh - sets default shell)"
204 + echo " 7) Install Oh My Zsh"
205 + echo " 8) Install plugins (autosuggestions, syntax highlighting)"
206 + echo " 9) Install Spaceship theme"
207 + echo "10) Download custom configs (~/.alias, .vimrc, etc.)"
208 + echo "11) Check ~/.zshrc load order"
209 + echo "12) Switch to Zsh (Temporary Sub-shell)"
210 + echo "13) Quit"
227 211 echo "==========================================="
228 212 }
229 213
@@ -239,7 +223,7 @@ run_choices() {
239 223 if [[ "$item" == !* ]]; then
240 224 to_exclude+=("${item:1}")
241 225 elif [[ "$item" == "0" ]]; then
242 - to_run+=(1 2 3 4 5 6 7 8 9 10 11 12 13)
226 + to_run+=(1 2 3 4 5 6 7 8 9 10 11 12)
243 227 else
244 228 to_run+=("$item")
245 229 fi
@@ -261,17 +245,16 @@ run_choices() {
261 245 1) update_system ;;
262 246 2) install_packages ;;
263 247 3) set_timezone ;;
264 - 4) install_xclip ;;
265 - 5) configure_git ;;
266 - 6) install_homebrew ;;
267 - 7) configure_shell ;;
268 - 8) install_oh_my_zsh ;;
269 - 9) install_plugins ;;
270 - 10) install_theme ;;
271 - 11) download_configs ;;
272 - 12) update_zshrc ;;
273 - 13) switch_shell ;;
274 - 14) log "Exiting..."; exit 0 ;;
248 + 4) configure_git ;;
249 + 5) install_homebrew ;;
250 + 6) configure_shell ;;
251 + 7) install_oh_my_zsh ;;
252 + 8) install_plugins ;;
253 + 9) install_theme ;;
254 + 10) download_configs ;;
255 + 11) update_zshrc ;;
256 + 12) switch_shell ;;
257 + 13) log "Exiting..."; exit 0 ;;
275 258 *) warn "Skipping invalid option: $choice" ;;
276 259 esac
277 260 echo
@@ -292,4 +275,4 @@ main() {
292 275 ok "Zsh installation/configuration complete!"
293 276 }
294 277
295 - main "$@"
278 + main "$@"

zsh_wsl.sh

@@ -21,7 +21,6 @@ err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
21 21 SKIP_PACKAGES=false
22 22 SKIP_GIT_CONFIG=false
23 23 SKIP_SHELL_CHANGE=false
24 - SKIP_XCLIP=false
25 24 INSTALL_HOMEBREW=false
26 25
27 26 GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw"
@@ -98,20 +97,6 @@ set_timezone() {
98 97 warn "Skipping timezone change: timedatectl is not available in this WSL environment"
99 98 }
100 99
101 - install_xclip() {
102 - local os
103 - os="$(detect_os)"
104 - [[ "$os" != "ubuntu" && "$os" != "debian" ]] && return 0
105 - $SKIP_XCLIP && return 0
106 - command -v xclip >/dev/null 2>&1 && ok "xclip already installed" && return 0
107 -
108 - read -r -p "Install xclip for Linux/X11 clipboard support? (y/N): " r
109 - case "$r" in
110 - [Yy]) sudo apt-get install -y xclip; ok "xclip installed" ;;
111 - *) log "Skipping xclip installation" ;;
112 - esac
113 - }
114 -
115 100 configure_git() {
116 101 $SKIP_GIT_CONFIG && return
117 102 log "Configuring Git..."
@@ -198,7 +183,7 @@ update_zshrc() {
198 183 log "Checking .zshrc..."
199 184
200 185 if [[ ! -f "$zshrc" ]]; then
201 - warn ".zshrc not found. Run option 11 first."
186 + warn ".zshrc not found. Run option 10 first."
202 187 return 1
203 188 fi
204 189
@@ -222,21 +207,20 @@ show_menu() {
222 207 echo "==========================================="
223 208 echo "WSL Zsh Installer - Choose what to do"
224 209 echo "==========================================="
225 - echo " 0) Run ALL steps (1-13)"
210 + echo " 0) Run ALL steps (1-12)"
226 211 echo " 1) Update system packages"
227 212 echo " 2) Install core packages (zsh, git, vim, etc.)"
228 213 echo " 3) Set Timezone (best effort)"
229 - echo " 4) Install xclip"
230 - echo " 5) Configure Git"
231 - echo " 6) Install Homebrew"
232 - echo " 7) Configure shell (chsh - sets default shell)"
233 - echo " 8) Install Oh My Zsh"
234 - echo " 9) Install plugins (autosuggestions, syntax highlighting)"
235 - echo "10) Install Spaceship theme"
236 - echo "11) Download custom configs (~/.alias, .vimrc, etc.)"
237 - echo "12) Check ~/.zshrc load order"
238 - echo "13) Switch to Zsh (Temporary Sub-shell)"
239 - echo "14) Quit"
214 + echo " 4) Configure Git"
215 + echo " 5) Install Homebrew"
216 + echo " 6) Configure shell (chsh - sets default shell)"
217 + echo " 7) Install Oh My Zsh"
218 + echo " 8) Install plugins (autosuggestions, syntax highlighting)"
219 + echo " 9) Install Spaceship theme"
220 + echo "10) Download custom configs (~/.alias, .vimrc, etc.)"
221 + echo "11) Check ~/.zshrc load order"
222 + echo "12) Switch to Zsh (Temporary Sub-shell)"
223 + echo "13) Quit"
240 224 echo "==========================================="
241 225 }
242 226
@@ -252,7 +236,7 @@ run_choices() {
252 236 if [[ "$item" == !* ]]; then
253 237 to_exclude+=("${item:1}")
254 238 elif [[ "$item" == "0" ]]; then
255 - to_run+=(1 2 3 4 5 6 7 8 9 10 11 12 13)
239 + to_run+=(1 2 3 4 5 6 7 8 9 10 11 12)
256 240 else
257 241 to_run+=("$item")
258 242 fi
@@ -274,17 +258,16 @@ run_choices() {
274 258 1) update_system ;;
275 259 2) install_packages ;;
276 260 3) set_timezone ;;
277 - 4) install_xclip ;;
278 - 5) configure_git ;;
279 - 6) install_homebrew ;;
280 - 7) configure_shell ;;
281 - 8) install_oh_my_zsh ;;
282 - 9) install_plugins ;;
283 - 10) install_theme ;;
284 - 11) download_configs ;;
285 - 12) update_zshrc ;;
286 - 13) switch_shell ;;
287 - 14) log "Exiting..."; exit 0 ;;
261 + 4) configure_git ;;
262 + 5) install_homebrew ;;
263 + 6) configure_shell ;;
264 + 7) install_oh_my_zsh ;;
265 + 8) install_plugins ;;
266 + 9) install_theme ;;
267 + 10) download_configs ;;
268 + 11) update_zshrc ;;
269 + 12) switch_shell ;;
270 + 13) log "Exiting..."; exit 0 ;;
288 271 *) warn "Skipping invalid option: $choice" ;;
289 272 esac
290 273 echo
@@ -305,4 +288,4 @@ main() {
305 288 ok "WSL Zsh installation/configuration complete!"
306 289 }
307 290
308 - main "$@"
291 + main "$@"

weehong revisou este gist 1 day ago. Ir para a revisão

Sem alterações

weehong revisou este gist 2 days ago. Ir para a revisão

Sem alterações

weehong revisou este gist 3 days ago. Ir para a revisão

3 files changed, 159 insertions, 155 deletions

zsh_macos.sh

@@ -18,13 +18,15 @@ err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
18 18 # =============================
19 19 # CONFIG
20 20 # =============================
21 + GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw"
22 +
21 23 CONFIG_FILES=(
22 - ".alias|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/alias"
23 - ".func|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/func"
24 - ".pathrc|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/pathrc"
25 - ".sourcerc|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/sourcerc"
26 - ".vimrc|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/vimrc"
27 - ".zshrc|https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/zshrc"
24 + ".alias"
25 + ".func"
26 + ".pathrc"
27 + ".sourcerc"
28 + ".vimrc"
29 + ".zshrc"
28 30 )
29 31
30 32 # =============================
@@ -44,7 +46,7 @@ configure_git() {
44 46 log "Configuring Git..."
45 47 read -p "Git name (leave empty to skip): " name
46 48 read -p "Git email (leave empty to skip): " email
47 -
49 +
48 50 if [[ -n "$name" ]]; then
49 51 git config --global user.name "$name"
50 52 fi
@@ -59,7 +61,7 @@ install_oh_my_zsh() {
59 61 ok "Oh My Zsh already installed"
60 62 return
61 63 fi
62 -
64 +
63 65 log "Installing Oh My Zsh..."
64 66 RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
65 67 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
@@ -69,8 +71,14 @@ install_oh_my_zsh() {
69 71 install_plugins() {
70 72 log "Installing plugins..."
71 73 local dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
72 - [[ -d "$dir/zsh-autosuggestions" ]] || git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions"
73 - [[ -d "$dir/zsh-syntax-highlighting" ]] || git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting"
74 + mkdir -p "$dir"
75 +
76 + [[ -d "$dir/zsh-autosuggestions" ]] || \
77 + git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions"
78 +
79 + [[ -d "$dir/zsh-syntax-highlighting" ]] || \
80 + git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting"
81 +
74 82 ok "Plugins installed"
75 83 }
76 84
@@ -78,74 +86,52 @@ install_theme() {
78 86 log "Installing Spaceship theme..."
79 87 local themes="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes"
80 88 local dir="$themes/spaceship-prompt"
81 - [[ -d "$dir" ]] || git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1
82 - ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme"
83 - ok "Theme installed"
84 - }
85 89
86 - clean_macos_aliases() {
87 - local alias_file="$HOME/.alias"
88 - if [[ -f "$alias_file" ]]; then
89 - log "Patching .alias for macOS..."
90 - sed -i '' '/# System update & cleanup/,/sudo apt-get clean/d' "$alias_file"
91 - sed -i '' '/# xclip shortcuts/,/xclip -selection clipboard/d' "$alias_file"
92 - sed -i '' '/./,$!d' "$alias_file"
93 - ok "macOS incompatibilities removed from .alias"
94 - fi
95 - }
90 + mkdir -p "$themes"
91 + [[ -d "$dir" ]] || \
92 + git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1
96 93
97 - clean_func_file() {
98 - local func_file="$HOME/.func"
99 - if [[ -f "$func_file" ]]; then
100 - log "Patching .func to remove 'open' function (WSL/Explorer specific)..."
101 -
102 - # Deletes from the comment line containing 'Function: open' down to the closing brace }
103 - # This preserves the native macOS 'open' command.
104 - sed -i '' '/# Function: open/,/^}/d' "$func_file"
105 -
106 - # Trim leading blank lines that might remain at the top
107 - sed -i '' '/./,$!d' "$func_file"
108 -
109 - ok "'open' function removed from .func"
110 - fi
94 + ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme"
95 + ok "Theme installed"
111 96 }
112 97
113 98 download_configs() {
114 99 log "Downloading custom config files..."
115 100 local backup="$HOME/.config_backup_$(date +%Y%m%d_%H%M%S)"
116 101 mkdir -p "$backup"
117 -
118 - for entry in "${CONFIG_FILES[@]}"; do
119 - local f="${entry%%|*}"
120 - local url="${entry##*|}"
121 -
122 - if [[ -f "$HOME/$f" ]]; then
123 - cp "$HOME/$f" "$backup/"
102 +
103 + for f in "${CONFIG_FILES[@]}"; do
104 + local url="$GIST_RAW_BASE/$f"
105 + local target="$HOME/$f"
106 + local tmp="${target}.tmp.$$"
107 +
108 + if [[ -f "$target" ]]; then
109 + cp "$target" "$backup/"
124 110 fi
111 +
125 112 log "Fetching $f ..."
126 - curl -fsSL "$url" -o "$HOME/$f" || warn "Failed to download $f"
113 + if curl -fsSL "$url" -o "$tmp"; then
114 + mv "$tmp" "$target"
115 + else
116 + rm -f "$tmp"
117 + warn "Failed to download $f"
118 + fi
127 119 done
128 -
129 - # Run cleanup patches
130 - clean_macos_aliases
131 - clean_func_file
132 -
120 +
133 121 ok "Configs downloaded (Backup at $backup)"
134 122 }
135 123
136 124 update_zshrc() {
137 125 local zshrc="$HOME/.zshrc"
138 - log "Updating .zshrc..."
139 -
126 + log "Checking .zshrc..."
127 +
140 128 if [[ ! -f "$zshrc" ]]; then
141 - warn ".zshrc not found, creating new one..."
142 - touch "$zshrc"
129 + warn ".zshrc not found. Run option 5 first."
130 + return 1
143 131 fi
144 132
145 - grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i '' 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
146 - grep -q 'zsh-autosuggestions' "$zshrc" || sed -i '' 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
147 - grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i '' 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
148 - ok ".zshrc updated with plugins and theme"
133 + ok ".zshrc already comes from the managed gist."
134 + ok "Load order is built in: .sourcerc -> .func -> .pathrc -> .alias"
149 135 }
150 136
151 137 switch_shell() {
@@ -170,7 +156,7 @@ show_menu() {
170 156 echo " 3) Install plugins (autosuggestions, syntax highlighting)"
171 157 echo " 4) Install Spaceship theme"
172 158 echo " 5) Download custom configs (~/.alias, .func, .vimrc, etc.)"
173 - echo " 6) Update ~/.zshrc for plugins & theme"
159 + echo " 6) Check ~/.zshrc load order"
174 160 echo " 7) Switch to Zsh (Temporary Sub-shell)"
175 161 echo " 8) Quit"
176 162 echo "==========================================="
@@ -179,7 +165,7 @@ show_menu() {
179 165 run_choices() {
180 166 local input
181 167 read -p "Select: " input
182 - input="${input//,/ }"
168 + input="${input//,/ }"
183 169
184 170 local -a to_run=()
185 171 local -a to_exclude=()
@@ -197,16 +183,16 @@ run_choices() {
197 183 if [[ ${#to_run[@]} -gt 0 ]]; then
198 184 for choice in "${to_run[@]}"; do
199 185 local skip=false
200 -
201 - # Array empty-check for older Bash versions (Fixes 'unbound variable' error)
186 +
202 187 if [[ ${#to_exclude[@]} -gt 0 ]]; then
203 188 for ex in "${to_exclude[@]}"; do
204 189 if [[ "$choice" == "$ex" ]]; then
205 - skip=true; break
190 + skip=true
191 + break
206 192 fi
207 193 done
208 194 fi
209 -
195 +
210 196 $skip && continue
211 197
212 198 case "$choice" in
@@ -239,4 +225,4 @@ main() {
239 225 ok "macOS configuration complete!"
240 226 }
241 227
242 - main "$@"
228 + main "$@"

zsh_ubuntu.sh

@@ -21,17 +21,17 @@ err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
21 21 SKIP_PACKAGES=false
22 22 SKIP_GIT_CONFIG=false
23 23 SKIP_SHELL_CHANGE=false
24 - CUSTOM_CONFIG=false
25 24 SKIP_XCLIP=false
26 25 INSTALL_HOMEBREW=false
27 26
28 - declare -A CONFIG_FILES=(
29 - [".alias"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/alias"
30 - [".func"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/func"
31 - [".pathrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/pathrc"
32 - [".sourcerc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/sourcerc"
33 - [".vimrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/vimrc"
34 - [".zshrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/zshrc"
27 + GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw"
28 + CONFIG_FILES=(
29 + ".alias"
30 + ".func"
31 + ".pathrc"
32 + ".sourcerc"
33 + ".vimrc"
34 + ".zshrc"
35 35 )
36 36
37 37 # =============================
@@ -54,7 +54,7 @@ check_requirements() {
54 54 err "Do not run as root"
55 55 exit 1
56 56 fi
57 - if ! command -v sudo >/dev/null; then
57 + if ! command -v sudo >/dev/null 2>&1; then
58 58 err "sudo required"
59 59 exit 1
60 60 fi
@@ -87,10 +87,10 @@ set_timezone() {
87 87
88 88 install_xclip() {
89 89 local os
90 - os=$(detect_os)
90 + os="$(detect_os)"
91 91 [[ "$os" != "ubuntu" && "$os" != "debian" ]] && return 0
92 92 $SKIP_XCLIP && return 0
93 - command -v xclip >/dev/null && ok "xclip already installed" && return 0
93 + command -v xclip >/dev/null 2>&1 && ok "xclip already installed" && return 0
94 94
95 95 read -r -p "Install xclip? (y/N): " r
96 96 case "$r" in
@@ -111,7 +111,7 @@ configure_git() {
111 111
112 112 install_homebrew() {
113 113 ! $INSTALL_HOMEBREW && return
114 - command -v brew >/dev/null && ok "Homebrew already installed" && return
114 + command -v brew >/dev/null 2>&1 && ok "Homebrew already installed" && return
115 115 log "Installing Homebrew..."
116 116 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
117 117 ok "Homebrew installed"
@@ -121,9 +121,9 @@ configure_shell() {
121 121 $SKIP_SHELL_CHANGE && return
122 122 log "Changing default shell to zsh..."
123 123 local zsh_path
124 - zsh_path=$(command -v zsh)
124 + zsh_path="$(command -v zsh)"
125 125 grep -qx "$zsh_path" /etc/shells || echo "$zsh_path" | sudo tee -a /etc/shells >/dev/null
126 - sudo chsh -s "$zsh_path" "$USER"
126 + chsh -s "$zsh_path"
127 127 ok "Shell changed (requires logout/login to take effect)"
128 128 }
129 129
@@ -138,6 +138,7 @@ install_oh_my_zsh() {
138 138 install_plugins() {
139 139 log "Installing plugins..."
140 140 local dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
141 + mkdir -p "$dir"
141 142 [[ -d "$dir/zsh-autosuggestions" ]] || git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions"
142 143 [[ -d "$dir/zsh-syntax-highlighting" ]] || git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting"
143 144 ok "Plugins installed"
@@ -147,6 +148,7 @@ install_theme() {
147 148 log "Installing Spaceship theme..."
148 149 local themes="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes"
149 150 local dir="$themes/spaceship-prompt"
151 + mkdir -p "$themes"
150 152 [[ -d "$dir" ]] || git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1
151 153 ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme"
152 154 ok "Theme installed"
@@ -156,40 +158,42 @@ download_configs() {
156 158 log "Downloading custom config files..."
157 159 local backup="$HOME/.config_backup_$(date +%Y%m%d_%H%M%S)"
158 160 mkdir -p "$backup"
159 - for f in "${!CONFIG_FILES[@]}"; do
160 - if [[ -f "$HOME/$f" ]]; then
161 - cp "$HOME/$f" "$backup/"
161 +
162 + for f in "${CONFIG_FILES[@]}"; do
163 + local url="$GIST_RAW_BASE/$f"
164 + local target="$HOME/$f"
165 + local tmp="${target}.tmp.$$"
166 +
167 + if [[ -f "$target" ]]; then
168 + cp "$target" "$backup/"
162 169 fi
163 - log "Fetching $f ..."
164 - curl -fsSL "${CONFIG_FILES[$f]}" -o "$HOME/$f" || warn "Failed to download $f"
165 170
166 - # --- Logic to remove the open function from .func ---
167 - if [[ "$f" == ".func" ]]; then
168 - # Removes the block starting with open() { and ending with }
169 - sed -i '/^open[[:space:]]*()[[:space:]]*{/,/^}/d' "$HOME/$f"
170 - ok "Stripped 'open' function from .func to prevent system conflicts"
171 + log "Fetching $f ..."
172 + if curl -fsSL "$url" -o "$tmp"; then
173 + mv "$tmp" "$target"
174 + else
175 + rm -f "$tmp"
176 + warn "Failed to download $f"
171 177 fi
172 178 done
179 +
173 180 ok "Configs downloaded (Backup at $backup)"
174 181 }
175 182
176 183 update_zshrc() {
177 184 local zshrc="$HOME/.zshrc"
178 - log "Updating .zshrc..."
179 -
185 + log "Checking .zshrc..."
186 +
180 187 if [[ ! -f "$zshrc" ]]; then
181 - warn ".zshrc not found, creating new one..."
182 - touch "$zshrc"
188 + warn ".zshrc not found. Run option 11 first."
189 + return 1
183 190 fi
184 191
185 - grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
186 - grep -q 'zsh-autosuggestions' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
187 - grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
188 - ok ".zshrc updated with plugins and theme"
192 + ok ".zshrc already comes from the managed gist."
193 + ok "Load order is built in: .sourcerc -> .func -> .pathrc -> .alias"
189 194 }
190 195
191 196 switch_shell() {
192 - $SKIP_SHELL_CHANGE && return
193 197 log "Starting Zsh session..."
194 198 echo -e "${YELLOW}Type 'exit' to return to this installer menu.${NC}"
195 199 echo "----------------------------------------"
@@ -217,7 +221,7 @@ show_menu() {
217 221 echo " 9) Install plugins (autosuggestions, syntax highlighting)"
218 222 echo "10) Install Spaceship theme"
219 223 echo "11) Download custom configs (~/.alias, .vimrc, etc.)"
220 - echo "12) Update ~/.zshrc for plugins & theme"
224 + echo "12) Check ~/.zshrc load order"
221 225 echo "13) Switch to Zsh (Temporary Sub-shell)"
222 226 echo "14) Quit"
223 227 echo "==========================================="
@@ -243,13 +247,14 @@ run_choices() {
243 247
244 248 for choice in "${to_run[@]}"; do
245 249 local skip=false
250 +
246 251 for ex in "${to_exclude[@]}"; do
247 252 if [[ "$choice" == "$ex" ]]; then
248 253 skip=true
249 254 break
250 255 fi
251 256 done
252 -
257 +
253 258 $skip && continue
254 259
255 260 case "$choice" in
@@ -287,4 +292,4 @@ main() {
287 292 ok "Zsh installation/configuration complete!"
288 293 }
289 294
290 - main "$@"
295 + main "$@"

zsh_wsl.sh

@@ -21,21 +21,21 @@ err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
21 21 SKIP_PACKAGES=false
22 22 SKIP_GIT_CONFIG=false
23 23 SKIP_SHELL_CHANGE=false
24 - CUSTOM_CONFIG=false
25 24 SKIP_XCLIP=false
26 25 INSTALL_HOMEBREW=false
27 26
28 - declare -A CONFIG_FILES=(
29 - [".alias"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/alias"
30 - [".func"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/func"
31 - [".pathrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/pathrc"
32 - [".sourcerc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/sourcerc"
33 - [".vimrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/vimrc"
34 - [".zshrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/zshrc"
27 + GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw"
28 + CONFIG_FILES=(
29 + ".alias"
30 + ".func"
31 + ".pathrc"
32 + ".sourcerc"
33 + ".vimrc"
34 + ".zshrc"
35 35 )
36 36
37 37 # =============================
38 - # OS DETECTION
38 + # PLATFORM DETECTION
39 39 # =============================
40 40 detect_os() {
41 41 if [ -f /etc/os-release ]; then
@@ -46,19 +46,26 @@ detect_os() {
46 46 fi
47 47 }
48 48
49 + is_wsl() {
50 + grep -qi "microsoft" /proc/version 2>/dev/null
51 + }
52 +
49 53 # =============================
50 54 # REQUIREMENTS
51 55 # =============================
52 56 check_requirements() {
53 - # Fixed to prevent silent failure with 'set -e'
54 57 if [[ $EUID -eq 0 ]]; then
55 58 err "Do not run as root"
56 59 exit 1
57 60 fi
58 - if ! command -v sudo >/dev/null; then
61 + if ! command -v sudo >/dev/null 2>&1; then
59 62 err "sudo required"
60 63 exit 1
61 64 fi
65 + if ! is_wsl; then
66 + err "This installer is intended for WSL"
67 + exit 1
68 + fi
62 69 }
63 70
64 71 # =============================
@@ -81,19 +88,24 @@ install_packages() {
81 88 }
82 89
83 90 set_timezone() {
84 - log "Setting timezone to Asia/Singapore..."
85 - sudo timedatectl set-timezone Asia/Singapore
86 - ok "Timezone set to Asia/Singapore"
91 + log "Checking timezone configuration..."
92 + if command -v timedatectl >/dev/null 2>&1 && timedatectl status >/dev/null 2>&1; then
93 + sudo timedatectl set-timezone Asia/Singapore
94 + ok "Timezone set to Asia/Singapore"
95 + return
96 + fi
97 +
98 + warn "Skipping timezone change: timedatectl is not available in this WSL environment"
87 99 }
88 100
89 101 install_xclip() {
90 102 local os
91 - os=$(detect_os)
103 + os="$(detect_os)"
92 104 [[ "$os" != "ubuntu" && "$os" != "debian" ]] && return 0
93 105 $SKIP_XCLIP && return 0
94 - command -v xclip >/dev/null && ok "xclip already installed" && return 0
106 + command -v xclip >/dev/null 2>&1 && ok "xclip already installed" && return 0
95 107
96 - read -r -p "Install xclip? (y/N): " r
108 + read -r -p "Install xclip for Linux/X11 clipboard support? (y/N): " r
97 109 case "$r" in
98 110 [Yy]) sudo apt-get install -y xclip; ok "xclip installed" ;;
99 111 *) log "Skipping xclip installation" ;;
@@ -112,7 +124,7 @@ configure_git() {
112 124
113 125 install_homebrew() {
114 126 ! $INSTALL_HOMEBREW && return
115 - command -v brew >/dev/null && ok "Homebrew already installed" && return
127 + command -v brew >/dev/null 2>&1 && ok "Homebrew already installed" && return
116 128 log "Installing Homebrew..."
117 129 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
118 130 ok "Homebrew installed"
@@ -122,16 +134,15 @@ configure_shell() {
122 134 $SKIP_SHELL_CHANGE && return
123 135 log "Changing default shell to zsh..."
124 136 local zsh_path
125 - zsh_path=$(command -v zsh)
137 + zsh_path="$(command -v zsh)"
126 138 grep -qx "$zsh_path" /etc/shells || echo "$zsh_path" | sudo tee -a /etc/shells >/dev/null
127 - sudo chsh -s "$zsh_path" "$USER"
128 - ok "Shell changed (requires logout/login to take effect)"
139 + chsh -s "$zsh_path"
140 + ok "Shell changed (open a new WSL session for it to take effect)"
129 141 }
130 142
131 143 install_oh_my_zsh() {
132 144 [[ -d "$HOME/.oh-my-zsh" ]] && ok "Oh My Zsh already installed" && return
133 145 log "Installing Oh My Zsh..."
134 - # Keep zshrc ensures we don't blow away configs if they exist
135 146 RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
136 147 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
137 148 ok "Oh My Zsh installed"
@@ -140,6 +151,7 @@ install_oh_my_zsh() {
140 151 install_plugins() {
141 152 log "Installing plugins..."
142 153 local dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
154 + mkdir -p "$dir"
143 155 [[ -d "$dir/zsh-autosuggestions" ]] || git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions"
144 156 [[ -d "$dir/zsh-syntax-highlighting" ]] || git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting"
145 157 ok "Plugins installed"
@@ -149,6 +161,7 @@ install_theme() {
149 161 log "Installing Spaceship theme..."
150 162 local themes="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes"
151 163 local dir="$themes/spaceship-prompt"
164 + mkdir -p "$themes"
152 165 [[ -d "$dir" ]] || git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1
153 166 ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme"
154 167 ok "Theme installed"
@@ -158,43 +171,46 @@ download_configs() {
158 171 log "Downloading custom config files..."
159 172 local backup="$HOME/.config_backup_$(date +%Y%m%d_%H%M%S)"
160 173 mkdir -p "$backup"
161 - for f in "${!CONFIG_FILES[@]}"; do
162 - if [[ -f "$HOME/$f" ]]; then
163 - cp "$HOME/$f" "$backup/"
174 +
175 + for f in "${CONFIG_FILES[@]}"; do
176 + local url="$GIST_RAW_BASE/$f"
177 + local target="$HOME/$f"
178 + local tmp="${target}.tmp.$$"
179 +
180 + if [[ -f "$target" ]]; then
181 + cp "$target" "$backup/"
164 182 fi
183 +
165 184 log "Fetching $f ..."
166 - curl -fsSL "${CONFIG_FILES[$f]}" -o "$HOME/$f" || warn "Failed to download $f"
185 + if curl -fsSL "$url" -o "$tmp"; then
186 + mv "$tmp" "$target"
187 + else
188 + rm -f "$tmp"
189 + warn "Failed to download $f"
190 + fi
167 191 done
192 +
168 193 ok "Configs downloaded (Backup at $backup)"
169 194 }
170 195
171 196 update_zshrc() {
172 197 local zshrc="$HOME/.zshrc"
173 - log "Updating .zshrc..."
174 -
175 - # Create zshrc if missing to prevent errors
198 + log "Checking .zshrc..."
199 +
176 200 if [[ ! -f "$zshrc" ]]; then
177 - warn ".zshrc not found, creating new one..."
178 - touch "$zshrc"
201 + warn ".zshrc not found. Run option 11 first."
202 + return 1
179 203 fi
180 204
181 - # Use || true to prevent 'set -e' from exiting if grep finds nothing
182 - grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
183 - grep -q 'zsh-autosuggestions' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
184 - grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
185 - ok ".zshrc updated with plugins and theme"
205 + ok ".zshrc already comes from the managed gist."
206 + ok "Load order is built in: .sourcerc -> .func -> .pathrc -> .alias"
186 207 }
187 208
188 209 switch_shell() {
189 - $SKIP_SHELL_CHANGE && return
190 -
191 210 log "Starting Zsh session..."
192 211 echo -e "${YELLOW}Type 'exit' to return to this installer menu.${NC}"
193 212 echo "----------------------------------------"
194 -
195 - # Run zsh as a subprocess, not exec
196 213 zsh -l
197 -
198 214 echo "----------------------------------------"
199 215 ok "Returned from Zsh session"
200 216 }
@@ -204,12 +220,12 @@ switch_shell() {
204 220 # =============================
205 221 show_menu() {
206 222 echo "==========================================="
207 - echo "Zsh Installer - Choose what to do"
223 + echo "WSL Zsh Installer - Choose what to do"
208 224 echo "==========================================="
209 225 echo " 0) Run ALL steps (1-13)"
210 226 echo " 1) Update system packages"
211 227 echo " 2) Install core packages (zsh, git, vim, etc.)"
212 - echo " 3) Set Timezone (Asia/Singapore)"
228 + echo " 3) Set Timezone (best effort)"
213 229 echo " 4) Install xclip"
214 230 echo " 5) Configure Git"
215 231 echo " 6) Install Homebrew"
@@ -218,25 +234,23 @@ show_menu() {
218 234 echo " 9) Install plugins (autosuggestions, syntax highlighting)"
219 235 echo "10) Install Spaceship theme"
220 236 echo "11) Download custom configs (~/.alias, .vimrc, etc.)"
221 - echo "12) Update ~/.zshrc for plugins & theme"
237 + echo "12) Check ~/.zshrc load order"
222 238 echo "13) Switch to Zsh (Temporary Sub-shell)"
223 239 echo "14) Quit"
224 240 echo "==========================================="
225 - echo "Inputs: '0' (All), '2 3 7' (Specific), '0 !4 !6' (All except 4 and 6)"
226 241 }
227 242
228 243 run_choices() {
229 244 local input
230 245 read -p "Select: " input
231 - input="${input//,/ }" # replace commas with spaces
246 + input="${input//,/ }"
232 247
233 248 local -a to_run=()
234 249 local -a to_exclude=()
235 250
236 - # Parse positive selections and negative exclusions
237 251 for item in $input; do
238 252 if [[ "$item" == !* ]]; then
239 - to_exclude+=("${item:1}") # Strip the '!' character
253 + to_exclude+=("${item:1}")
240 254 elif [[ "$item" == "0" ]]; then
241 255 to_run+=(1 2 3 4 5 6 7 8 9 10 11 12 13)
242 256 else
@@ -244,17 +258,16 @@ run_choices() {
244 258 fi
245 259 done
246 260
247 - # Loop through intended runs and apply exclusions
248 261 for choice in "${to_run[@]}"; do
249 -
250 262 local skip=false
263 +
251 264 for ex in "${to_exclude[@]}"; do
252 265 if [[ "$choice" == "$ex" ]]; then
253 266 skip=true
254 267 break
255 268 fi
256 269 done
257 -
270 +
258 271 $skip && continue
259 272
260 273 case "$choice" in
@@ -289,7 +302,7 @@ main() {
289 302 read -p "Do you want to run more options? (y/n): " again
290 303 [[ "$again" =~ ^[Yy]$ ]] || break
291 304 done
292 - ok "Zsh installation/configuration complete!"
305 + ok "WSL Zsh installation/configuration complete!"
293 306 }
294 307
295 - main "$@"
308 + main "$@"

weehong revisou este gist 1 month ago. Ir para a revisão

1 file changed, 4 insertions

README.md

@@ -6,6 +6,10 @@ The "Bash Script Installer" simplifies the setup of Zsh.
6 6 ```zsh
7 7 bash -c "$(curl -fsSL https://opengist.rmrf.online/weehong/f0d940c3c1214bf5b7996195199fdc09/raw/HEAD/zsh_ubuntu.sh)"
8 8 ```
9 + ### WSL
10 + ```zsh
11 + bash -c "$(curl -fsSL https://opengist.rmrf.online/weehong/f0d940c3c1214bf5b7996195199fdc09/raw/HEAD/zsh_wsl.sh)"
12 + ```
9 13 ### MacOS
10 14 ```zsh
11 15 bash -c "$(curl -fsSL https://opengist.rmrf.online/weehong/f0d940c3c1214bf5b7996195199fdc09/raw/HEAD/zsh_macos.sh)"

weehong revisou este gist 1 month ago. Ir para a revisão

2 files changed, 304 insertions, 14 deletions

zsh_ubuntu.sh

@@ -50,7 +50,6 @@ detect_os() {
50 50 # REQUIREMENTS
51 51 # =============================
52 52 check_requirements() {
53 - # Fixed to prevent silent failure with 'set -e'
54 53 if [[ $EUID -eq 0 ]]; then
55 54 err "Do not run as root"
56 55 exit 1
@@ -131,7 +130,6 @@ configure_shell() {
131 130 install_oh_my_zsh() {
132 131 [[ -d "$HOME/.oh-my-zsh" ]] && ok "Oh My Zsh already installed" && return
133 132 log "Installing Oh My Zsh..."
134 - # Keep zshrc ensures we don't blow away configs if they exist
135 133 RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
136 134 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
137 135 ok "Oh My Zsh installed"
@@ -164,6 +162,13 @@ download_configs() {
164 162 fi
165 163 log "Fetching $f ..."
166 164 curl -fsSL "${CONFIG_FILES[$f]}" -o "$HOME/$f" || warn "Failed to download $f"
165 +
166 + # --- Logic to remove the open function from .func ---
167 + if [[ "$f" == ".func" ]]; then
168 + # Removes the block starting with open() { and ending with }
169 + sed -i '/^open[[:space:]]*()[[:space:]]*{/,/^}/d' "$HOME/$f"
170 + ok "Stripped 'open' function from .func to prevent system conflicts"
171 + fi
167 172 done
168 173 ok "Configs downloaded (Backup at $backup)"
169 174 }
@@ -172,13 +177,11 @@ update_zshrc() {
172 177 local zshrc="$HOME/.zshrc"
173 178 log "Updating .zshrc..."
174 179
175 - # Create zshrc if missing to prevent errors
176 180 if [[ ! -f "$zshrc" ]]; then
177 181 warn ".zshrc not found, creating new one..."
178 182 touch "$zshrc"
179 183 fi
180 184
181 - # Use || true to prevent 'set -e' from exiting if grep finds nothing
182 185 grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
183 186 grep -q 'zsh-autosuggestions' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
184 187 grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
@@ -187,14 +190,10 @@ update_zshrc() {
187 190
188 191 switch_shell() {
189 192 $SKIP_SHELL_CHANGE && return
190 -
191 193 log "Starting Zsh session..."
192 194 echo -e "${YELLOW}Type 'exit' to return to this installer menu.${NC}"
193 195 echo "----------------------------------------"
194 -
195 - # Run zsh as a subprocess, not exec
196 196 zsh -l
197 -
198 197 echo "----------------------------------------"
199 198 ok "Returned from Zsh session"
200 199 }
@@ -222,21 +221,19 @@ show_menu() {
222 221 echo "13) Switch to Zsh (Temporary Sub-shell)"
223 222 echo "14) Quit"
224 223 echo "==========================================="
225 - echo "Inputs: '0' (All), '2 3 7' (Specific), '0 !4 !6' (All except 4 and 6)"
226 224 }
227 225
228 226 run_choices() {
229 227 local input
230 228 read -p "Select: " input
231 - input="${input//,/ }" # replace commas with spaces
229 + input="${input//,/ }"
232 230
233 231 local -a to_run=()
234 232 local -a to_exclude=()
235 233
236 - # Parse positive selections and negative exclusions
237 234 for item in $input; do
238 235 if [[ "$item" == !* ]]; then
239 - to_exclude+=("${item:1}") # Strip the '!' character
236 + to_exclude+=("${item:1}")
240 237 elif [[ "$item" == "0" ]]; then
241 238 to_run+=(1 2 3 4 5 6 7 8 9 10 11 12 13)
242 239 else
@@ -244,9 +241,7 @@ run_choices() {
244 241 fi
245 242 done
246 243
247 - # Loop through intended runs and apply exclusions
248 244 for choice in "${to_run[@]}"; do
249 -
250 245 local skip=false
251 246 for ex in "${to_exclude[@]}"; do
252 247 if [[ "$choice" == "$ex" ]]; then

zsh_wsl.sh(arquivo criado)

@@ -0,0 +1,295 @@
1 + #!/bin/bash
2 + set -euo pipefail
3 +
4 + # =============================
5 + # COLORS & LOGGING
6 + # =============================
7 + RED='\033[0;31m'
8 + GREEN='\033[0;32m'
9 + YELLOW='\033[1;33m'
10 + BLUE='\033[0;34m'
11 + NC='\033[0m'
12 +
13 + log() { echo -e "${BLUE}[INFO]${NC} $*"; }
14 + ok() { echo -e "${GREEN}[OK]${NC} $*"; }
15 + warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
16 + err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
17 +
18 + # =============================
19 + # FLAGS & CONFIG
20 + # =============================
21 + SKIP_PACKAGES=false
22 + SKIP_GIT_CONFIG=false
23 + SKIP_SHELL_CHANGE=false
24 + CUSTOM_CONFIG=false
25 + SKIP_XCLIP=false
26 + INSTALL_HOMEBREW=false
27 +
28 + declare -A CONFIG_FILES=(
29 + [".alias"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/alias"
30 + [".func"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/func"
31 + [".pathrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/pathrc"
32 + [".sourcerc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/sourcerc"
33 + [".vimrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/vimrc"
34 + [".zshrc"]="https://gist.githubusercontent.com/weehongkoh/72bdb76beacacf2ca3dd39a72395b9ee/raw/zshrc"
35 + )
36 +
37 + # =============================
38 + # OS DETECTION
39 + # =============================
40 + detect_os() {
41 + if [ -f /etc/os-release ]; then
42 + . /etc/os-release
43 + echo "$ID"
44 + else
45 + echo "unknown"
46 + fi
47 + }
48 +
49 + # =============================
50 + # REQUIREMENTS
51 + # =============================
52 + check_requirements() {
53 + # Fixed to prevent silent failure with 'set -e'
54 + if [[ $EUID -eq 0 ]]; then
55 + err "Do not run as root"
56 + exit 1
57 + fi
58 + if ! command -v sudo >/dev/null; then
59 + err "sudo required"
60 + exit 1
61 + fi
62 + }
63 +
64 + # =============================
65 + # INSTALLATION FUNCTIONS
66 + # =============================
67 + update_system() {
68 + $SKIP_PACKAGES && return
69 + log "Updating system..."
70 + sudo apt-get update -y
71 + sudo apt-get upgrade -y
72 + ok "System updated"
73 + }
74 +
75 + install_packages() {
76 + $SKIP_PACKAGES && return
77 + log "Installing core packages..."
78 + sudo apt-get install -y \
79 + zsh git vim curl wget unzip zip build-essential xz-utils
80 + ok "Packages installed"
81 + }
82 +
83 + set_timezone() {
84 + log "Setting timezone to Asia/Singapore..."
85 + sudo timedatectl set-timezone Asia/Singapore
86 + ok "Timezone set to Asia/Singapore"
87 + }
88 +
89 + install_xclip() {
90 + local os
91 + os=$(detect_os)
92 + [[ "$os" != "ubuntu" && "$os" != "debian" ]] && return 0
93 + $SKIP_XCLIP && return 0
94 + command -v xclip >/dev/null && ok "xclip already installed" && return 0
95 +
96 + read -r -p "Install xclip? (y/N): " r
97 + case "$r" in
98 + [Yy]) sudo apt-get install -y xclip; ok "xclip installed" ;;
99 + *) log "Skipping xclip installation" ;;
100 + esac
101 + }
102 +
103 + configure_git() {
104 + $SKIP_GIT_CONFIG && return
105 + log "Configuring Git..."
106 + read -p "Git name (leave empty to skip): " name
107 + read -p "Git email (leave empty to skip): " email
108 + [[ -n "$name" ]] && git config --global user.name "$name"
109 + [[ -n "$email" ]] && git config --global user.email "$email"
110 + ok "Git configured"
111 + }
112 +
113 + install_homebrew() {
114 + ! $INSTALL_HOMEBREW && return
115 + command -v brew >/dev/null && ok "Homebrew already installed" && return
116 + log "Installing Homebrew..."
117 + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
118 + ok "Homebrew installed"
119 + }
120 +
121 + configure_shell() {
122 + $SKIP_SHELL_CHANGE && return
123 + log "Changing default shell to zsh..."
124 + local zsh_path
125 + zsh_path=$(command -v zsh)
126 + grep -qx "$zsh_path" /etc/shells || echo "$zsh_path" | sudo tee -a /etc/shells >/dev/null
127 + sudo chsh -s "$zsh_path" "$USER"
128 + ok "Shell changed (requires logout/login to take effect)"
129 + }
130 +
131 + install_oh_my_zsh() {
132 + [[ -d "$HOME/.oh-my-zsh" ]] && ok "Oh My Zsh already installed" && return
133 + log "Installing Oh My Zsh..."
134 + # Keep zshrc ensures we don't blow away configs if they exist
135 + RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
136 + sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
137 + ok "Oh My Zsh installed"
138 + }
139 +
140 + install_plugins() {
141 + log "Installing plugins..."
142 + local dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
143 + [[ -d "$dir/zsh-autosuggestions" ]] || git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions"
144 + [[ -d "$dir/zsh-syntax-highlighting" ]] || git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting"
145 + ok "Plugins installed"
146 + }
147 +
148 + install_theme() {
149 + log "Installing Spaceship theme..."
150 + local themes="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes"
151 + local dir="$themes/spaceship-prompt"
152 + [[ -d "$dir" ]] || git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1
153 + ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme"
154 + ok "Theme installed"
155 + }
156 +
157 + download_configs() {
158 + log "Downloading custom config files..."
159 + local backup="$HOME/.config_backup_$(date +%Y%m%d_%H%M%S)"
160 + mkdir -p "$backup"
161 + for f in "${!CONFIG_FILES[@]}"; do
162 + if [[ -f "$HOME/$f" ]]; then
163 + cp "$HOME/$f" "$backup/"
164 + fi
165 + log "Fetching $f ..."
166 + curl -fsSL "${CONFIG_FILES[$f]}" -o "$HOME/$f" || warn "Failed to download $f"
167 + done
168 + ok "Configs downloaded (Backup at $backup)"
169 + }
170 +
171 + update_zshrc() {
172 + local zshrc="$HOME/.zshrc"
173 + log "Updating .zshrc..."
174 +
175 + # Create zshrc if missing to prevent errors
176 + if [[ ! -f "$zshrc" ]]; then
177 + warn ".zshrc not found, creating new one..."
178 + touch "$zshrc"
179 + fi
180 +
181 + # Use || true to prevent 'set -e' from exiting if grep finds nothing
182 + grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
183 + grep -q 'zsh-autosuggestions' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
184 + grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
185 + ok ".zshrc updated with plugins and theme"
186 + }
187 +
188 + switch_shell() {
189 + $SKIP_SHELL_CHANGE && return
190 +
191 + log "Starting Zsh session..."
192 + echo -e "${YELLOW}Type 'exit' to return to this installer menu.${NC}"
193 + echo "----------------------------------------"
194 +
195 + # Run zsh as a subprocess, not exec
196 + zsh -l
197 +
198 + echo "----------------------------------------"
199 + ok "Returned from Zsh session"
200 + }
201 +
202 + # =============================
203 + # INTERACTIVE MENU
204 + # =============================
205 + show_menu() {
206 + echo "==========================================="
207 + echo "Zsh Installer - Choose what to do"
208 + echo "==========================================="
209 + echo " 0) Run ALL steps (1-13)"
210 + echo " 1) Update system packages"
211 + echo " 2) Install core packages (zsh, git, vim, etc.)"
212 + echo " 3) Set Timezone (Asia/Singapore)"
213 + echo " 4) Install xclip"
214 + echo " 5) Configure Git"
215 + echo " 6) Install Homebrew"
216 + echo " 7) Configure shell (chsh - sets default shell)"
217 + echo " 8) Install Oh My Zsh"
218 + echo " 9) Install plugins (autosuggestions, syntax highlighting)"
219 + echo "10) Install Spaceship theme"
220 + echo "11) Download custom configs (~/.alias, .vimrc, etc.)"
221 + echo "12) Update ~/.zshrc for plugins & theme"
222 + echo "13) Switch to Zsh (Temporary Sub-shell)"
223 + echo "14) Quit"
224 + echo "==========================================="
225 + echo "Inputs: '0' (All), '2 3 7' (Specific), '0 !4 !6' (All except 4 and 6)"
226 + }
227 +
228 + run_choices() {
229 + local input
230 + read -p "Select: " input
231 + input="${input//,/ }" # replace commas with spaces
232 +
233 + local -a to_run=()
234 + local -a to_exclude=()
235 +
236 + # Parse positive selections and negative exclusions
237 + for item in $input; do
238 + if [[ "$item" == !* ]]; then
239 + to_exclude+=("${item:1}") # Strip the '!' character
240 + elif [[ "$item" == "0" ]]; then
241 + to_run+=(1 2 3 4 5 6 7 8 9 10 11 12 13)
242 + else
243 + to_run+=("$item")
244 + fi
245 + done
246 +
247 + # Loop through intended runs and apply exclusions
248 + for choice in "${to_run[@]}"; do
249 +
250 + local skip=false
251 + for ex in "${to_exclude[@]}"; do
252 + if [[ "$choice" == "$ex" ]]; then
253 + skip=true
254 + break
255 + fi
256 + done
257 +
258 + $skip && continue
259 +
260 + case "$choice" in
261 + 1) update_system ;;
262 + 2) install_packages ;;
263 + 3) set_timezone ;;
264 + 4) install_xclip ;;
265 + 5) configure_git ;;
266 + 6) install_homebrew ;;
267 + 7) configure_shell ;;
268 + 8) install_oh_my_zsh ;;
269 + 9) install_plugins ;;
270 + 10) install_theme ;;
271 + 11) download_configs ;;
272 + 12) update_zshrc ;;
273 + 13) switch_shell ;;
274 + 14) log "Exiting..."; exit 0 ;;
275 + *) warn "Skipping invalid option: $choice" ;;
276 + esac
277 + echo
278 + done
279 + }
280 +
281 + # =============================
282 + # MAIN
283 + # =============================
284 + main() {
285 + check_requirements
286 + while true; do
287 + show_menu
288 + run_choices
289 + read -p "Do you want to run more options? (y/n): " again
290 + [[ "$again" =~ ^[Yy]$ ]] || break
291 + done
292 + ok "Zsh installation/configuration complete!"
293 + }
294 +
295 + main "$@"

weehong revisou este gist 1 month ago. Ir para a revisão

1 file changed, 17 insertions, 12 deletions

zsh_macos.sh

@@ -94,13 +94,19 @@ clean_macos_aliases() {
94 94 fi
95 95 }
96 96
97 - clean_pathrc() {
98 - local pathrc_file="$HOME/.pathrc"
99 - if [[ -f "$pathrc_file" ]]; then
100 - log "Patching .pathrc to remove 'open' function..."
101 - # Deletes the function block from name down to the closing brace }
102 - sed -i '' '/^open[ (].*/,/^}/d' "$pathrc_file"
103 - ok "'open' function removed from .pathrc"
97 + clean_func_file() {
98 + local func_file="$HOME/.func"
99 + if [[ -f "$func_file" ]]; then
100 + log "Patching .func to remove 'open' function (WSL/Explorer specific)..."
101 +
102 + # Deletes from the comment line containing 'Function: open' down to the closing brace }
103 + # This preserves the native macOS 'open' command.
104 + sed -i '' '/# Function: open/,/^}/d' "$func_file"
105 +
106 + # Trim leading blank lines that might remain at the top
107 + sed -i '' '/./,$!d' "$func_file"
108 +
109 + ok "'open' function removed from .func"
104 110 fi
105 111 }
106 112
@@ -120,8 +126,9 @@ download_configs() {
120 126 curl -fsSL "$url" -o "$HOME/$f" || warn "Failed to download $f"
121 127 done
122 128
129 + # Run cleanup patches
123 130 clean_macos_aliases
124 - clean_pathrc
131 + clean_func_file
125 132
126 133 ok "Configs downloaded (Backup at $backup)"
127 134 }
@@ -162,7 +169,7 @@ show_menu() {
162 169 echo " 2) Install Oh My Zsh"
163 170 echo " 3) Install plugins (autosuggestions, syntax highlighting)"
164 171 echo " 4) Install Spaceship theme"
165 - echo " 5) Download custom configs (~/.alias, .vimrc, etc.)"
172 + echo " 5) Download custom configs (~/.alias, .func, .vimrc, etc.)"
166 173 echo " 6) Update ~/.zshrc for plugins & theme"
167 174 echo " 7) Switch to Zsh (Temporary Sub-shell)"
168 175 echo " 8) Quit"
@@ -177,7 +184,6 @@ run_choices() {
177 184 local -a to_run=()
178 185 local -a to_exclude=()
179 186
180 - # Parse inputs
181 187 for item in $input; do
182 188 if [[ "$item" == !* ]]; then
183 189 to_exclude+=("${item:1}")
@@ -188,12 +194,11 @@ run_choices() {
188 194 fi
189 195 done
190 196
191 - # Fixed: Loop logic to handle empty arrays in older Bash versions
192 197 if [[ ${#to_run[@]} -gt 0 ]]; then
193 198 for choice in "${to_run[@]}"; do
194 199 local skip=false
195 200
196 - # Only loop if exclusions exist to avoid "unbound variable" error
201 + # Array empty-check for older Bash versions (Fixes 'unbound variable' error)
197 202 if [[ ${#to_exclude[@]} -gt 0 ]]; then
198 203 for ex in "${to_exclude[@]}"; do
199 204 if [[ "$choice" == "$ex" ]]; then

weehong revisou este gist 1 month ago. Ir para a revisão

1 file changed, 11 insertions, 6 deletions

zsh_macos.sh

@@ -120,7 +120,6 @@ download_configs() {
120 120 curl -fsSL "$url" -o "$HOME/$f" || warn "Failed to download $f"
121 121 done
122 122
123 - # Apply cleanup patches
124 123 clean_macos_aliases
125 124 clean_pathrc
126 125
@@ -178,6 +177,7 @@ run_choices() {
178 177 local -a to_run=()
179 178 local -a to_exclude=()
180 179
180 + # Parse inputs
181 181 for item in $input; do
182 182 if [[ "$item" == !* ]]; then
183 183 to_exclude+=("${item:1}")
@@ -188,14 +188,19 @@ run_choices() {
188 188 fi
189 189 done
190 190
191 + # Fixed: Loop logic to handle empty arrays in older Bash versions
191 192 if [[ ${#to_run[@]} -gt 0 ]]; then
192 193 for choice in "${to_run[@]}"; do
193 194 local skip=false
194 - for ex in "${to_exclude[@]}"; do
195 - if [[ "$choice" == "$ex" ]]; then
196 - skip=true; break
197 - fi
198 - done
195 +
196 + # Only loop if exclusions exist to avoid "unbound variable" error
197 + if [[ ${#to_exclude[@]} -gt 0 ]]; then
198 + for ex in "${to_exclude[@]}"; do
199 + if [[ "$choice" == "$ex" ]]; then
200 + skip=true; break
201 + fi
202 + done
203 + fi
199 204
200 205 $skip && continue
201 206

weehong revisou este gist 1 month ago. Ir para a revisão

1 file changed, 21 insertions, 30 deletions

zsh_macos.sh

@@ -87,22 +87,20 @@ clean_macos_aliases() {
87 87 local alias_file="$HOME/.alias"
88 88 if [[ -f "$alias_file" ]]; then
89 89 log "Patching .alias for macOS..."
90 -
91 - # Delete the multiline apt-get block (from the comment down to the final command)
92 90 sed -i '' '/# System update & cleanup/,/sudo apt-get clean/d' "$alias_file"
93 -
94 - # Delete the xclip block
95 91 sed -i '' '/# xclip shortcuts/,/xclip -selection clipboard/d' "$alias_file"
96 -
97 - # Trim leading blank lines from the top of the file
98 92 sed -i '' '/./,$!d' "$alias_file"
99 -
100 - # Trim trailing blank lines from the bottom of the file
101 - local cleaned_content
102 - cleaned_content=$(cat "$alias_file")
103 - printf "%s\n" "$cleaned_content" > "$alias_file"
104 -
105 - ok "macOS incompatibilities and extra blank lines removed from .alias"
93 + ok "macOS incompatibilities removed from .alias"
94 + fi
95 + }
96 +
97 + clean_pathrc() {
98 + local pathrc_file="$HOME/.pathrc"
99 + if [[ -f "$pathrc_file" ]]; then
100 + log "Patching .pathrc to remove 'open' function..."
101 + # Deletes the function block from name down to the closing brace }
102 + sed -i '' '/^open[ (].*/,/^}/d' "$pathrc_file"
103 + ok "'open' function removed from .pathrc"
106 104 fi
107 105 }
108 106
@@ -122,8 +120,9 @@ download_configs() {
122 120 curl -fsSL "$url" -o "$HOME/$f" || warn "Failed to download $f"
123 121 done
124 122
125 - # Automatically clean up the macOS incompatible aliases after downloading
123 + # Apply cleanup patches
126 124 clean_macos_aliases
125 + clean_pathrc
127 126
128 127 ok "Configs downloaded (Backup at $backup)"
129 128 }
@@ -137,7 +136,6 @@ update_zshrc() {
137 136 touch "$zshrc"
138 137 fi
139 138
140 - # macOS BSD sed requires '-i ''' for in-place editing
141 139 grep -q 'ZSH_THEME="spaceship"' "$zshrc" || sed -i '' 's/ZSH_THEME=".*"/ZSH_THEME="spaceship"/' "$zshrc"
142 140 grep -q 'zsh-autosuggestions' "$zshrc" || sed -i '' 's/plugins=(/plugins=(zsh-autosuggestions /' "$zshrc"
143 141 grep -q 'zsh-syntax-highlighting' "$zshrc" || sed -i '' 's/plugins=(/plugins=(zsh-syntax-highlighting /' "$zshrc"
@@ -170,21 +168,19 @@ show_menu() {
170 168 echo " 7) Switch to Zsh (Temporary Sub-shell)"
171 169 echo " 8) Quit"
172 170 echo "==========================================="
173 - echo "Inputs: '0' (All), '1 3 4' (Specific), '0 !1 !5' (All except 1 and 5)"
174 171 }
175 172
176 173 run_choices() {
177 174 local input
178 175 read -p "Select: " input
179 - input="${input//,/ }" # replace commas with spaces if used
176 + input="${input//,/ }"
180 177
181 178 local -a to_run=()
182 179 local -a to_exclude=()
183 180
184 - # Parse positive selections and negative exclusions
185 181 for item in $input; do
186 182 if [[ "$item" == !* ]]; then
187 - to_exclude+=("${item:1}") # Strip the '!' character
183 + to_exclude+=("${item:1}")
188 184 elif [[ "$item" == "0" ]]; then
189 185 to_run+=(1 2 3 4 5 6 7)
190 186 else
@@ -192,19 +188,14 @@ run_choices() {
192 188 fi
193 189 done
194 190
195 - # Protect against empty arrays crashing older bash versions with 'set -u'
196 191 if [[ ${#to_run[@]} -gt 0 ]]; then
197 192 for choice in "${to_run[@]}"; do
198 -
199 193 local skip=false
200 - if [[ ${#to_exclude[@]} -gt 0 ]]; then
201 - for ex in "${to_exclude[@]}"; do
202 - if [[ "$choice" == "$ex" ]]; then
203 - skip=true
204 - break
205 - fi
206 - done
207 - fi
194 + for ex in "${to_exclude[@]}"; do
195 + if [[ "$choice" == "$ex" ]]; then
196 + skip=true; break
197 + fi
198 + done
208 199
209 200 $skip && continue
210 201
@@ -216,7 +207,7 @@ run_choices() {
216 207 5) download_configs ;;
217 208 6) update_zshrc ;;
218 209 7) switch_shell ;;
219 - 8) log "Exiting..."; exit 0 ;;
210 + 8) exit 0 ;;
220 211 *) warn "Skipping invalid option: $choice" ;;
221 212 esac
222 213 echo

weehong revisou este gist 1 month ago. Ir para a revisão

1 file changed, 10 insertions, 2 deletions

zsh_macos.sh

@@ -86,7 +86,7 @@ install_theme() {
86 86 clean_macos_aliases() {
87 87 local alias_file="$HOME/.alias"
88 88 if [[ -f "$alias_file" ]]; then
89 - log "Patching .alias for macOS (removing apt-get and xclip blocks)..."
89 + log "Patching .alias for macOS..."
90 90
91 91 # Delete the multiline apt-get block (from the comment down to the final command)
92 92 sed -i '' '/# System update & cleanup/,/sudo apt-get clean/d' "$alias_file"
@@ -94,7 +94,15 @@ clean_macos_aliases() {
94 94 # Delete the xclip block
95 95 sed -i '' '/# xclip shortcuts/,/xclip -selection clipboard/d' "$alias_file"
96 96
97 - ok "macOS incompatibilities removed from .alias"
97 + # Trim leading blank lines from the top of the file
98 + sed -i '' '/./,$!d' "$alias_file"
99 +
100 + # Trim trailing blank lines from the bottom of the file
101 + local cleaned_content
102 + cleaned_content=$(cat "$alias_file")
103 + printf "%s\n" "$cleaned_content" > "$alias_file"
104 +
105 + ok "macOS incompatibilities and extra blank lines removed from .alias"
98 106 fi
99 107 }
100 108
Próximo Anterior