forked from XuehaiPan/Dev-Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_macOS.sh
executable file
·2499 lines (2153 loc) · 83.5 KB
/
setup_macOS.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Options
export SET_MIRRORS="${SET_MIRRORS:-false}"
# Set PATH
export HOMEBREW_PREFIX="/usr/local"
if [[ "$(uname -m)" == "arm64" ]]; then
export HOMEBREW_PREFIX="/opt/homebrew"
fi
export PATH="${HOMEBREW_PREFIX}/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin${PATH:+:"${PATH}"}"
# Set USER
export USER="${USER:-"$(whoami)"}"
# Set configuration backup directory
DATETIME="$(date +"%Y-%m-%d-%T")"
BACKUP_DIR="${HOME}/.dotfiles/backups/${DATETIME}"
mkdir -p "${BACKUP_DIR}/.dotfiles"
ln -sfn "${DATETIME}" "${HOME}/.dotfiles/backups/latest"
chmod 755 "${HOME}/.dotfiles"
# Set temporary directory
TMP_DIR="$(mktemp -d -t dev-setup)"
# Set default Conda installation directory
CONDA_DIR=''
CONDA_BASE_PREFIX=''
CONDA_BASE_PREFIX_EXIST=true
if [[ -d "${HOME}/Miniconda3" || -L "${HOME}/Miniconda3" ]]; then
CONDA_BASE_PREFIX="${HOME}/Miniconda3"
elif [[ -d "${HOME}/miniconda3" || -L "${HOME}/miniconda3" ]]; then
CONDA_BASE_PREFIX="${HOME}/miniconda3"
elif [[ -d "${HOME}/Miniforge3" || -L "${HOME}/Miniforge3" ]]; then
CONDA_BASE_PREFIX="${HOME}/Miniforge3"
elif [[ -d "${HOME}/miniforge3" || -L "${HOME}/miniforge3" ]]; then
CONDA_BASE_PREFIX="${HOME}/miniforge3"
elif [[ -d "${HOME}/Anaconda3" || -L "${HOME}/Anaconda3" ]]; then
CONDA_BASE_PREFIX="${HOME}/Anaconda3"
elif [[ -d "${HOME}/anaconda3" || -L "${HOME}/anaconda3" ]]; then
CONDA_BASE_PREFIX="${HOME}/anaconda3"
elif [[ -d "${HOMEBREW_PREFIX}/Caskroom/miniconda/base" ]]; then
CONDA_BASE_PREFIX="${HOMEBREW_PREFIX}/Caskroom/miniconda/base"
elif [[ -d "${HOMEBREW_PREFIX}/Caskroom/miniforge/base" ]]; then
CONDA_BASE_PREFIX="${HOMEBREW_PREFIX}/Caskroom/miniforge/base"
elif [[ -d "${HOMEBREW_PREFIX}/Caskroom/mambaforge/base" ]]; then
CONDA_BASE_PREFIX="${HOMEBREW_PREFIX}/Caskroom/mambaforge/base"
elif [[ -d "${HOMEBREW_PREFIX}/Caskroom/anaconda/base" ]]; then
CONDA_BASE_PREFIX="${HOMEBREW_PREFIX}/Caskroom/anaconda/base"
fi
if [[ -z "${CONDA_BASE_PREFIX}" ]]; then
CONDA_BASE_PREFIX="${HOME}/Miniconda3"
CONDA_BASE_PREFIX_EXIST=false
fi
if [[ "${CONDA_BASE_PREFIX/#${HOME}\//}" == "${CONDA_BASE_PREFIX}" ]]; then
CONDA_DIR="${CONDA_BASE_PREFIX}"
else
CONDA_DIR="\${HOME}/${CONDA_BASE_PREFIX/#${HOME}\//}"
fi
# Common functions
function exec_cmd() {
printf "%s" "$@" | awk \
'BEGIN {
RESET = "\033[0m";
BOLD = "\033[1m";
UNDERLINE = "\033[4m";
UNDERLINEOFF = "\033[24m";
RED = "\033[31m";
GREEN = "\033[32m";
YELLOW = "\033[33m";
WHITE = "\033[37m";
GRAY = "\033[90m";
IDENTIFIER = "[_a-zA-Z][_a-zA-Z0-9]*";
idx = 0;
in_string = 0;
double_quoted = 1;
printf("%s$", BOLD WHITE);
}
{
for (i = 1; i <= NF; ++i) {
style = WHITE;
post_style = WHITE;
if (!in_string) {
if ($i ~ /^-/)
style = YELLOW;
else if ($i == "sudo" && idx == 0) {
style = UNDERLINE GREEN;
post_style = UNDERLINEOFF WHITE;
}
else if ($i ~ "^" IDENTIFIER "=" && idx == 0) {
style = GRAY;
'"if (\$i ~ \"^\" IDENTIFIER \"=[\\\"']\") {"'
in_string = 1;
double_quoted = ($i ~ "^" IDENTIFIER "=\"");
}
}
else if ($i ~ /^[12&]?>>?/ || $i == "\\")
style = RED;
else {
++idx;
'"if (\$i ~ /^[\"']/) {"'
in_string = 1;
double_quoted = ($i ~ /^"/);
}
if (idx == 1)
style = GREEN;
}
}
if (in_string) {
if (style == WHITE)
style = "";
post_style = "";
'"if ((double_quoted && \$i ~ /\";?\$/ && \$i !~ /\\\\\";?\$/) || (!double_quoted && \$i ~ /';?\$/))"'
in_string = 0;
}
if (($i ~ /;$/ && $i !~ /\\;$/) || $i == "|" || $i == "||" || $i == "&&") {
if (!in_string) {
idx = 0;
if ($i !~ /;$/)
style = RED;
}
}
if ($i ~ /;$/ && $i !~ /\\;$/)
printf(" %s%s%s;%s", style, substr($i, 1, length($i) - 1), (in_string ? WHITE : RED), post_style);
else
printf(" %s%s%s", style, $i, post_style);
if ($i == "\\")
printf("\n\t");
}
}
END {
printf("%s\n", RESET);
}' >&2
eval "$@"
}
unset HAVE_SUDO_ACCESS
function have_sudo_access() {
if [[ "${EUID:-"${UID}"}" == "0" ]]; then
return 0
fi
if [[ ! -x "/usr/bin/sudo" ]]; then
return 1
fi
local -a SUDO=("/usr/bin/sudo")
if [[ -n "${SUDO_ASKPASS-}" ]]; then
SUDO+=("-A")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
local RESET="\033[0m"
local BOLD="\033[1m"
local BLUE="\033[34m"
local WHITE="\033[37m"
echo -e "${BOLD}${BLUE}==> ${WHITE}Checking sudo access.${RESET}" >&2
exec_cmd "${SUDO[*]} -v && ${SUDO[*]} -l mkdir &>/dev/null"
HAVE_SUDO_ACCESS="$?"
fi
if [[ "${HAVE_SUDO_ACCESS}" -ne 0 ]]; then
echo "Need sudo access on macOS (e.g. the user ${USER} needs to be an Administrator)" >&2
exit 1
fi
return "${HAVE_SUDO_ACCESS}"
}
function realpath() {
/usr/bin/python3 -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$1"
}
function backup_dotfiles() {
local file original_file
for file in "$@"; do
if [[ -f "${file}" || -d "${file}" ]]; then
if [[ -L "${file}" ]]; then
original_file="$(realpath "${file}")"
rm -f "${file}"
cp -rf "${original_file}" "${file}"
fi
cp -rf "${file}" "${BACKUP_DIR}/${file}"
fi
done
}
function wget() {
command wget --no-verbose --timeout=10 --show-progress --progress=bar:force:noscroll "$@"
}
# Install and setup Homebrew
if ${SET_MIRRORS}; then
exec_cmd 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"'
exec_cmd 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"'
exec_cmd 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"'
exec_cmd 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"'
exec_cmd 'export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"'
fi
if [[ ! -x "${HOMEBREW_PREFIX}/bin/brew" ]]; then
exec_cmd 'xcode-select --install'
if ${SET_MIRRORS}; then
exec_cmd "git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git \"${TMP_DIR}/brew-install\""
exec_cmd "NONINTERACTIVE=1 /bin/bash \"${TMP_DIR}/brew-install/install.sh\""
else
exec_cmd 'NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://github.com/Homebrew/install/raw/HEAD/install.sh)"'
fi
fi
exec_cmd "eval \"\$(${HOMEBREW_PREFIX}/bin/brew shellenv)\""
exec_cmd 'brew update'
if ${SET_MIRRORS}; then
for tap in cask{-fonts,-versions} command-not-found; do
exec_cmd "brew tap --custom-remote --force-auto-update homebrew/${tap} https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done
else
for tap in cask{-fonts,-versions} command-not-found; do
exec_cmd "brew tap --force-auto-update homebrew/${tap}"
done
fi
exec_cmd 'brew update --verbose'
# Install and setup shells
exec_cmd 'brew install zsh bash'
if ! grep -qF "${HOMEBREW_PREFIX}/bin/bash" /etc/shells; then
exec_cmd "echo '${HOMEBREW_PREFIX}/bin/bash' | sudo tee -a /etc/shells"
fi
if ! grep -qF "${HOMEBREW_PREFIX}/bin/zsh" /etc/shells; then
exec_cmd "echo '${HOMEBREW_PREFIX}/bin/zsh' | sudo tee -a /etc/shells"
fi
# Install packages
if [[ "$(uname -m)" == "arm64" ]]; then
exec_cmd 'brew install gcc llvm make cmake automake autoconf'
else
exec_cmd 'brew install gcc gdb llvm make cmake automake autoconf'
fi
exec_cmd 'brew install bash-completion wget curl git git-lfs macvim tmux'
exec_cmd 'brew install coreutils ranger fd bat highlight ripgrep git-extras'
exec_cmd 'brew install jq shfmt shellcheck diffutils colordiff diff-so-fancy'
exec_cmd 'brew install htop openssh atool tree reattach-to-user-namespace libarchive'
exec_cmd 'brew install ruby perl'
export PATH="${HOMEBREW_PREFIX}/opt/ruby/bin${PATH:+:"${PATH}"}"
export PATH="$(ruby -r rubygems -e 'puts Gem.dir')/bin${PATH:+:"${PATH}"}"
export PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin${PATH:+:"${PATH}"}"
# Install casks and fonts
exec_cmd 'brew install --cask iterm2 xquartz'
exec_cmd 'brew install --cask font-dejavu-sans-mono-nerd-font font-cascadia{-code,-mono}{,-pl}'
exec_cmd 'brew install --cask visual-studio-code'
exec_cmd 'brew install --cask keka typora iina google-chrome'
exec_cmd 'brew cleanup -s --prune 7'
# Change the login shell to Zsh
if [[ "$(basename "${SHELL}")" != "zsh" ]]; then
if grep -qF "${HOMEBREW_PREFIX}/bin/zsh" /etc/shells; then
exec_cmd "sudo chsh --shell ${HOMEBREW_PREFIX}/bin/zsh ${USER}"
elif grep -qF '/bin/zsh' /etc/shells; then
exec_cmd "sudo chsh --shell /bin/zsh ${USER}"
fi
fi
exec_cmd 'cd "${HOME}"'
# Configurations for Git
export GIT_HTTP_LOW_SPEED_LIMIT=0
export GIT_HTTP_LOW_SPEED_TIME=999999
backup_dotfiles .gitconfig .dotfiles/.gitconfig
git config --global core.compression -1
git config --global core.excludesfile '~/.gitignore_global'
git config --global core.eol lf
git config --global core.autocrlf false
git config --global core.editor vim
git config --global diff.tool vimdiff
git config --global diff.guitool gvimdiff
git config --global diff.algorithm histogram
git config --global difftool.prompt false
git config --global merge.tool vimdiff
git config --global merge.guitool gvimdiff
git config --global mergetool.prompt false
git config --global http.postBuffer 524288000
git config --global init.defaultBranch main
git config --global pull.ff only
git config --global fetch.prune true
git config --global fetch.parallel 0
git config --global submodule.recurse true
git config --global submodule.fetchJobs 0
git config --global filter.lfs.clean 'git-lfs clean -- %f'
git config --global filter.lfs.smudge 'git-lfs smudge -- %f'
git config --global filter.lfs.process 'git-lfs filter-process'
git config --global filter.lfs.required true
git config --global alias.ignore-list '! cd -- "${GIT_PREFIX:-.}" && git ls-files -v "${1:-.}" | sed -n -e "s,^[a-z] \\(.*\\)\$,${GIT_PREFIX:-./}\\1,p" && git status --ignored --porcelain "${1:-.}" 2>/dev/null | sed -n -e "s/^\\(\\!\\! \\)\\(.*\\)$/\\2/p" #'
git config --global color.ui true
git config --global color.diff-highlight.oldNormal 'red bold'
git config --global color.diff-highlight.oldHighlight 'red bold 52'
git config --global color.diff-highlight.newNormal 'green bold'
git config --global color.diff-highlight.newHighlight 'green bold 22'
git config --global color.diff.meta 'yellow'
git config --global color.diff.frag 'magenta bold'
git config --global color.diff.func '146 bold'
git config --global color.diff.commit 'yellow bold'
git config --global color.diff.old 'red bold'
git config --global color.diff.new 'green bold'
git config --global color.diff.whitespace 'red reverse'
if [[ -x "$(command -v diff-so-fancy)" ]]; then
git config --global core.pager 'diff-so-fancy | less --tabs=4 -RFX'
git config --global interactive.diffFilter 'diff-so-fancy --patch'
fi
mv -f .gitconfig .dotfiles/.gitconfig
ln -sf .dotfiles/.gitconfig .
chmod 644 .dotfiles/.gitconfig
# Install Oh-My-Zsh
export ZSH="${ZSH:-"${HOME}/.oh-my-zsh"}"
export ZSH_CUSTOM="${ZSH_CUSTOM:-"${ZSH}/custom"}"
export ZSH_CACHE_DIR="${ZSH_CACHE_DIR:-"${ZSH}/cache"}"
if [[ -d "${ZSH}/.git" && -f "${ZSH}/tools/upgrade.sh" ]]; then
rm -f "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null
zsh "${ZSH}/tools/check_for_upgrade.sh" 2>/dev/null
exec_cmd 'zsh "${ZSH}/tools/upgrade.sh" 2>&1'
elif [[ -d "${ZSH}" && ! -d "${ZSH}/.git" ]]; then
exec_cmd 'git clone -c core.eol=lf -c core.autocrlf=false \
-c fsck.zeroPaddedFilemode=ignore \
-c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \
--depth=1 https://github.com/ohmyzsh/ohmyzsh.git "${TMP_DIR}/ohmyzsh" 2>&1'
exec_cmd 'mv -f "${TMP_DIR}/ohmyzsh/.git" "${ZSH:-"${HOME}/.oh-my-zsh"}/.git"'
exec_cmd 'git -C "${ZSH:-"${HOME}/.oh-my-zsh"}" reset --hard'
rm -f "${HOME}"/.zcompdump* 2>/dev/null
else
exec_cmd 'git clone -c core.eol=lf -c core.autocrlf=false \
-c fsck.zeroPaddedFilemode=ignore \
-c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \
--depth=1 https://github.com/ohmyzsh/ohmyzsh.git "${ZSH:-"${HOME}/.oh-my-zsh"}" 2>&1'
rm -f "${HOME}"/.zcompdump* 2>/dev/null
fi
# Install Powerlevel10k theme and Zsh plugins
while read -r repo target; do
if [[ ! -d "${ZSH_CUSTOM}/${target}/.git" ]]; then
exec_cmd "git clone --depth=1 https://github.com/${repo}.git \"\${ZSH_CUSTOM}/${target}\" 2>&1"
else
exec_cmd "git -C \"\${ZSH_CUSTOM}/${target}\" pull --ff-only 2>&1"
fi
done <<EOS
romkatv/powerlevel10k themes/powerlevel10k
zsh-users/zsh-syntax-highlighting plugins/zsh-syntax-highlighting
zsh-users/zsh-autosuggestions plugins/zsh-autosuggestions
zsh-users/zsh-completions plugins/zsh-completions
conda-incubator/conda-zsh-completion plugins/conda-zsh-completion
EOS
# Install GitStatus
if [[ "${ZSH_CUSTOM}" == "${HOME}/.oh-my-zsh/custom" ]]; then
GITSTATUS_DIR="../.oh-my-zsh/custom/themes/powerlevel10k/gitstatus"
else
GITSTATUS_DIR="${ZSH_CUSTOM}/themes/powerlevel10k/gitstatus"
fi
ln -sfn "${GITSTATUS_DIR}" .dotfiles/gitstatus
# Install fzf
if [[ ! -d "${HOME}/.fzf" ]]; then
exec_cmd 'git clone --depth=1 https://github.com/junegunn/fzf.git "${HOME}/.fzf" 2>&1'
else
exec_cmd 'git -C "${HOME}/.fzf" pull --ff-only 2>&1'
fi
exec_cmd '"${HOME}/.fzf/install" --key-bindings --completion --no-update-rc'
exec_cmd 'chmod -R go-w "${ZSH}" "${HOME}/.fzf"'
# Configurations for RubyGems
backup_dotfiles .gemrc .dotfiles/.gemrc
cat >.dotfiles/.gemrc <<'EOF'
---
:backtrace: false
:bulk_threshold: 1000
:update_sources: true
:verbose: true
:concurrent_downloads: 8
EOF
if ${SET_MIRRORS}; then
cat >>.dotfiles/.gemrc <<-'EOF'
:sources:
- https://mirrors.tuna.tsinghua.edu.cn/rubygems/
EOF
fi
ln -sf .dotfiles/.gemrc .
chmod 644 .dotfiles/.gemrc
# Install Color LS
exec_cmd 'gem install colorls'
exec_cmd 'gem cleanup'
# Configurations for Perl
export PERL_MB_OPT="--install_base \"${HOMEBREW_PREFIX}/opt/perl\""
export PERL_MM_OPT="INSTALL_BASE=\"${HOMEBREW_PREFIX}/opt/perl\""
export PERL_MM_USE_DEFAULT=1
exec_cmd "PERL_MM_USE_DEFAULT=1 http_proxy=\"\" https_proxy=\"\" ftp_proxy=\"\" perl -MCPAN -e 'mkmyconfig'"
exec_cmd "perl -MCPAN -e 'CPAN::HandleConfig->load();' \\
-e 'CPAN::HandleConfig->edit(\"cleanup_after_install\", \"1\");' \\
-e 'CPAN::HandleConfig->commit()'"
if ${SET_MIRRORS}; then
if ! (
perl -MCPAN -e 'CPAN::HandleConfig->load();' -e 'CPAN::HandleConfig->prettyprint("urllist")' |
grep -qF 'https://mirrors.tuna.tsinghua.edu.cn/CPAN/'
); then
exec_cmd "perl -MCPAN -e 'CPAN::HandleConfig->load();' \\
-e 'CPAN::HandleConfig->edit(\"urllist\", \"unshift\", \"https://mirrors.tuna.tsinghua.edu.cn/CPAN/\");' \\
-e 'CPAN::HandleConfig->commit()'"
fi
fi
exec_cmd 'PERL_MM_OPT="INSTALL_BASE=\"${HOMEBREW_PREFIX}/opt/perl\"" cpan -i -T local::lib'
exec_cmd 'eval "$(perl -I${HOMEBREW_PREFIX}/opt/perl/lib/perl5 -Mlocal::lib=${HOMEBREW_PREFIX}/opt/perl)"'
exec_cmd "cpan -i CPAN"
exec_cmd "AUTOMATED_TESTING=1 cpan -i Term::ReadLine::Perl Term::ReadKey"
# Configurations for Zsh
backup_dotfiles .dotfiles/.zshrc
HOMEBREW_SETTINGS='# Homebrew
'"eval \"\$(${HOMEBREW_PREFIX}/bin/brew shellenv)\""'
export HOMEBREW_EDITOR="vim"
export HOMEBREW_BAT=true'
if ${SET_MIRRORS}; then
HOMEBREW_SETTINGS+='
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"'
fi
HOMEBREW_SETTINGS+='
__COMMAND_NOT_FOUND_HANDLER="$(brew --repository homebrew/command-not-found)/handler.sh"
if [[ -f "${__COMMAND_NOT_FOUND_HANDLER}" ]]; then
source "${__COMMAND_NOT_FOUND_HANDLER}"
fi
unset __COMMAND_NOT_FOUND_HANDLER'
cat >.dotfiles/.zshrc <<'EOF'
# Source global definitions
# Include /etc/zprofile if it exists
if [[ -f /etc/zprofile ]]; then
source /etc/zprofile
fi
# Include /etc/zshrc if it exists
if [[ -f /etc/zshrc ]]; then
source /etc/zshrc
fi
# Set PATH so it includes user's private bin if it exists
if [[ -d "${HOME}/.local/bin" ]]; then
export PATH="${HOME}/.local/bin${PATH:+:"${PATH}"}"
fi
# Set C_INCLUDE_PATH and CPLUS_INCLUDE_PATH so it includes user's private include if it exists
if [[ -d "${HOME}/.local/include" ]]; then
export C_INCLUDE_PATH="${HOME}/.local/include${C_INCLUDE_PATH:+:"${C_INCLUDE_PATH}"}"
export CPLUS_INCLUDE_PATH="${HOME}/.local/include${CPLUS_INCLUDE_PATH:+:"${CPLUS_INCLUDE_PATH}"}"
fi
# Set LIBRARY_PATH and DYLD_LIBRARY_PATH so it includes user's private lib if it exists
if [[ -d "${HOME}/.local/lib" ]]; then
export LIBRARY_PATH="${HOME}/.local/lib${LIBRARY_PATH:+:"${LIBRARY_PATH}"}"
export DYLD_LIBRARY_PATH="${HOME}/.local/lib${DYLD_LIBRARY_PATH:+:"${DYLD_LIBRARY_PATH}"}"
fi
if [[ -d "${HOME}/.local/lib64" ]]; then
export LIBRARY_PATH="${HOME}/.local/lib64${LIBRARY_PATH:+:"${LIBRARY_PATH}"}"
export DYLD_LIBRARY_PATH="${HOME}/.local/lib64${DYLD_LIBRARY_PATH:+:"${DYLD_LIBRARY_PATH}"}"
fi
# User specific environment
export TERM="xterm-256color"
export LESS="-R -M -i -j5"
export CLICOLOR=1
export LSCOLORS="GxFxCxDxBxegedabagaced"
# Locale
export LC_ALL="en_US.UTF-8"
EOF
cat >>.dotfiles/.zshrc <<EOF
${HOMEBREW_SETTINGS}
# Anaconda
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="\$(CONDA_REPORT_ERRORS=false "${CONDA_DIR}/bin/conda" shell.zsh hook 2>/dev/null)"
if [[ \$? -eq 0 ]]; then
eval "\${__conda_setup}"
else
if [[ -f "${CONDA_DIR}/etc/profile.d/conda.sh" ]]; then
source "${CONDA_DIR}/etc/profile.d/conda.sh"
else
export PATH="${CONDA_DIR}/bin\${PATH:+:"\${PATH}"}"
fi
fi
unset __conda_setup
if [[ -f "${CONDA_DIR}/etc/profile.d/mamba.sh" ]]; then
source "${CONDA_DIR}/etc/profile.d/mamba.sh"
fi
__CONDA_PREFIX="\${CONDA_PREFIX}"
while [[ -n "\${CONDA_PREFIX}" ]]; do
conda deactivate
done
# <<< conda initialize <<<
EOF
cat >>.dotfiles/.zshrc <<'EOF'
# CXX Compilers
export CC="/usr/bin/gcc"
export CXX="/usr/bin/g++"
export FC="${HOMEBREW_PREFIX}/bin/gfortran"
export OMPI_CC="${CC}" MPICH_CC="${CC}"
export OMPI_CXX="${CXX}" MPICH_CXX="${CXX}"
export OMPI_FC="${FC}" MPICH_FC="${FC}"
# Ruby
export RUBYOPT="-W0"
export PATH="${HOMEBREW_PREFIX}/opt/ruby/bin${PATH:+:"${PATH}"}"
export PATH="$(ruby -r rubygems -e 'puts Gem.dir')/bin${PATH:+:"${PATH}"}"
export PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin${PATH:+:"${PATH}"}"
# Perl
eval "$(perl -I"${HOMEBREW_PREFIX}/opt/perl/lib/perl5" -Mlocal::lib="${HOMEBREW_PREFIX}/opt/perl")"
# cURL
export PATH="${HOMEBREW_PREFIX}/opt/curl/bin${PATH:+:"${PATH}"}"
# OpenSSL
export PATH="${HOMEBREW_PREFIX}/opt/openssl/bin${PATH:+:"${PATH}"}"
# gettext
export PATH="${HOMEBREW_PREFIX}/opt/gettext/bin${PATH:+:"${PATH}"}"
# NCURSES
export PATH="${HOMEBREW_PREFIX}/opt/ncurses/bin${PATH:+:"${PATH}"}"
export C_INCLUDE_PATH="${HOMEBREW_PREFIX}/opt/ncurses/include${C_INCLUDE_PATH:+:"${C_INCLUDE_PATH}"}"
export CPLUS_INCLUDE_PATH="${HOMEBREW_PREFIX}/opt/ncurses/include${CPLUS_INCLUDE_PATH:+:"${CPLUS_INCLUDE_PATH}"}"
export LIBRARY_PATH="${HOMEBREW_PREFIX}/opt/ncurses/lib${LIBRARY_PATH:+:"${LIBRARY_PATH}"}"
export DYLD_LIBRARY_PATH="${HOMEBREW_PREFIX}/opt/ncurses/lib${DYLD_LIBRARY_PATH:+:"${DYLD_LIBRARY_PATH}"}"
# SQLite
export PATH="${HOMEBREW_PREFIX}/opt/sqlite/bin${PATH:+:"${PATH}"}"
# LLVM
export PATH="${HOMEBREW_PREFIX}/opt/llvm/bin${PATH:+:"${PATH}"}"
# libarchive
export DYLD_FALLBACK_LIBRARY_PATH="${DYLD_FALLBACK_LIBRARY_PATH:+"${DYLD_FALLBACK_LIBRARY_PATH}":}${HOMEBREW_PREFIX}/opt/libarchive/lib"
# fzf
if [[ -f "${HOME}/.fzf.zsh" ]]; then
source "${HOME}/.fzf.zsh"
fi
export FZF_DEFAULT_COMMAND="fd --type file --follow --hidden --no-ignore-vcs --exclude '.git' --exclude '[Mm]iniconda3' --exclude '[Aa]naconda3' --color=always"
export FZF_CTRL_T_COMMAND="${FZF_DEFAULT_COMMAND}"
FZF_PREVIEW_COMMAND="(bat --color=always {} || highlight -O ansi {} || cat {}) 2>/dev/null | head -100"
export FZF_DEFAULT_OPTS="--height=40% --layout=reverse --ansi --preview='${FZF_PREVIEW_COMMAND}'"
# bat
export BAT_THEME="Monokai Extended"
# iTerm
if [[ -f "${HOME}/.iterm2/.iterm2_shell_integration.zsh" ]]; then
source "${HOME}/.iterm2/.iterm2_shell_integration.zsh"
fi
# Conda
if [[ -n "${__CONDA_PREFIX}" ]]; then
conda activate "${__CONDA_PREFIX}"
fi
unset __CONDA_PREFIX
# Remove duplicate entries
function __remove_duplicate() {
local SEP="$1" NAME="$2" VALUE
VALUE="$(
eval "printf \"%s%s\" \"\$${NAME}\" \"${SEP}\"" |
/usr/bin/awk -v RS="${SEP}" 'BEGIN { idx = 0; }
{ if (!(exists[$0]++)) printf("%s%s", (!(idx++) ? "" : RS), $0); }'
)"
if [[ -n "${VALUE}" ]]; then
export "${NAME}"="${VALUE}"
else
unset "${NAME}"
fi
}
__remove_duplicate ':' PATH
__remove_duplicate ':' C_INCLUDE_PATH
__remove_duplicate ':' CPLUS_INCLUDE_PATH
__remove_duplicate ':' LIBRARY_PATH
__remove_duplicate ':' DYLD_LIBRARY_PATH
unset -f __remove_duplicate
# Utilities
if [[ -f "${HOME}/.dotfiles/utilities.sh" ]]; then
source "${HOME}/.dotfiles/utilities.sh"
fi
# X11
export DISPLAY=":0.0"
xhost +local: &>/dev/null
# Path to your oh-my-zsh installation.
export ZSH="${HOME}/.oh-my-zsh"
ZSH_COMPDUMP="${HOME}/.zcompdump"
HISTFILE="${HOME}/.zsh_history"
EOF
cat >>.dotfiles/.zshrc <<EOF
DEFAULT_USER="${USER}"
EOF
cat >>.dotfiles/.zshrc <<'EOF'
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo "${RANDOM_THEME}"
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"
# Powerlevel10k configurations
typeset -g POWERLEVEL9K_MODE="nerdfont-complete"
typeset -g POWERLEVEL9K_PROMPT_ON_NEWLINE=true
typeset -g POWERLEVEL9K_RPROMPT_ON_NEWLINE=false
typeset -g POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX=""
typeset -g POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="%K{white}%F{black} \ue795 \uf155 %f%k%F{white}\ue0b0%f "
typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv anaconda pyenv context root_indicator dir dir_writable vcs)
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(command_execution_time status background_jobs time ssh)
typeset -g POWERLEVEL9K_SHORTEN_DIR_LENGTH=1
typeset -g POWERLEVEL9K_SHORTEN_STRATEGY="truncate_to_unique"
typeset -g POWERLEVEL9K_DIR_ANCHOR_BOLD=true
typeset -g POWERLEVEL9K_DIR_MAX_LENGTH='75%'
typeset -g GITSTATUS_NUM_THREADS=4
typeset -g POWERLEVEL9K_VCS_PUSH_INCOMING_CHANGES_ICON='\uf0a8 '
typeset -g POWERLEVEL9K_VCS_PUSH_OUTGOING_CHANGES_ICON='\uf0a9 '
typeset -g POWERLEVEL9K_VCS_SHORTEN_DELIMITER="…"
typeset -g POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY=-1
typeset -g POWERLEVEL9K_VCS_{STAGED,UNSTAGED,UNTRACKED,CONFLICTED,COMMITS_AHEAD,COMMITS_BEHIND}_MAX_NUM=-1
typeset -g POWERLEVEL9K_VCS_DISABLED_WORKDIR_PATTERN='~'
# Formatter for Git status.
#
# Example output: master wip ⇣42⇡42 *42 merge ~42 +42 !42 ?42.
#
# VCS_STATUS_* parameters are set by gitstatus plugin. See reference:
# https://github.com/romkatv/gitstatus/blob/master/gitstatus.plugin.zsh.
function _p9k_gitstatus_formatter() {
emulate -L zsh
if [[ -n "${P9K_CONTENT}" ]]; then
# If P9K_CONTENT is not empty, use it. It's either "loading" or from vcs_info (not from
# gitstatus plugin). VCS_STATUS_* parameters are not available in this case.
_p9k_gitstatus_format="${P9K_CONTENT}"
return
fi
# Styling for different parts of Git status.
local meta='%7F' # white foreground
local clean='%0F' # black foreground
local modified='%0F' # black foreground
local untracked='%0F' # black foreground
local conflicted='%1F' # red foreground
local res="${meta}${clean}$(print_icon VCS_COMMIT_ICON)${VCS_STATUS_COMMIT[1,6]}"
if [[ -n "${VCS_STATUS_LOCAL_BRANCH}" ]]; then
local branch="${(V)VCS_STATUS_LOCAL_BRANCH}"
# If local branch name is at most 9 characters long, show it in full.
# Otherwise show the first 4 … the last 4.
(( ${#branch} > 9 )) && branch[5,-5]="${(g::)POWERLEVEL9K_VCS_SHORTEN_DELIMITER}"
res+=" ${clean}$(print_icon VCS_BRANCH_ICON)${branch//\%/%%}"
fi
if [[ -n "${VCS_STATUS_TAG}" ]]; then
local tag="${(V)VCS_STATUS_TAG}"
(( ${#tag} > 9 )) && tag[5,-5]="${(g::)POWERLEVEL9K_VCS_SHORTEN_DELIMITER}"
res+=" ${meta}$(print_icon VCS_TAG_ICON)${clean}${tag//\%/%%}"
fi
# Show tracking branch name if it differs from local branch.
if [[ -n "${VCS_STATUS_REMOTE_BRANCH:#"${VCS_STATUS_LOCAL_BRANCH}"}" ]]; then
res+=" ${meta}:${clean}${(V)VCS_STATUS_REMOTE_BRANCH//\%/%%}"
fi
# Display "wip" if the latest commit's summary contains "wip" or "WIP".
if [[ "${VCS_STATUS_COMMIT_SUMMARY}" == (|*[^[:alnum:]])(wip|WIP)(|[^[:alnum:]]*) ]]; then
res+=" ${modified}wip"
fi
# ⇣42 if behind the remote.
(( VCS_STATUS_COMMITS_BEHIND )) && res+=" ${clean}$(print_icon VCS_INCOMING_CHANGES_ICON)${VCS_STATUS_COMMITS_BEHIND}"
# ⇡42 if ahead of the remote.
(( VCS_STATUS_COMMITS_AHEAD )) && res+=" ${clean}$(print_icon VCS_OUTGOING_CHANGES_ICON)${VCS_STATUS_COMMITS_AHEAD}"
# ⇠42 if behind the push remote.
(( VCS_STATUS_PUSH_COMMITS_BEHIND )) && res+=" ${clean}$(print_icon VCS_PUSH_INCOMING_CHANGES_ICON)${VCS_STATUS_PUSH_COMMITS_BEHIND}"
# ⇢42 if ahead of the push remote.
(( VCS_STATUS_PUSH_COMMITS_AHEAD )) && res+=" ${clean}$(print_icon VCS_PUSH_OUTGOING_CHANGES_ICON)${VCS_STATUS_PUSH_COMMITS_AHEAD}"
# *42 if have stashes.
(( VCS_STATUS_STASHES )) && res+=" ${clean}$(print_icon VCS_STASH_ICON)${VCS_STATUS_STASHES}"
# 'merge' if the repo is in an unusual state.
[[ -n "${VCS_STATUS_ACTION}" ]] && res+=" ${conflicted}${VCS_STATUS_ACTION//\%/%%}"
# ~42 if have merge conflicts.
(( VCS_STATUS_NUM_CONFLICTED )) && res+=" ${conflicted}${VCS_STATUS_NUM_CONFLICTED}"
# +42 if have staged changes.
(( VCS_STATUS_NUM_STAGED )) && res+=" ${modified}$(print_icon VCS_STAGED_ICON)${VCS_STATUS_NUM_STAGED}"
# !42 if have unstaged changes.
(( VCS_STATUS_NUM_UNSTAGED )) && res+=" ${modified}$(print_icon VCS_UNSTAGED_ICON)${VCS_STATUS_NUM_UNSTAGED}"
(( VCS_STATUS_NUM_UNTRACKED )) && res+=" ${untracked}$(print_icon VCS_UNTRACKED_ICON)${VCS_STATUS_NUM_UNTRACKED}"
(( VCS_STATUS_HAS_UNSTAGED == -1 )) && res+=" ${modified}─"
_p9k_gitstatus_format="${res}"
}
functions -M _p9k_gitstatus_formatter 2>/dev/null
typeset -g POWERLEVEL9K_VCS_DISABLE_GITSTATUS_FORMATTING=true
typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='${$((_p9k_gitstatus_formatter()))+${_p9k_gitstatus_format}}'
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ${ZSH}/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than ${ZSH}/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in ${ZSH}/plugins/
# Custom plugins may be added to ${ZSH_CUSTOM}/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
macos
zsh-syntax-highlighting
zsh-autosuggestions
zsh-completions
conda-zsh-completion
colorize
colored-man-pages
fd
fzf
copyfile
copypath
cp
rsync
alias-finder
git
git-auto-fetch
python
pip
pylint
docker
tmux
brew
vscode
)
ZSH_COLORIZE_STYLE="monokai"
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
source "${ZSH}/oh-my-zsh.sh"
# User configuration
# export MANPATH="/usr/local/man${MANPATH:+:"${MANPATH}"}"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n "${SSH_CONNECTION}" ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# SSH
# export SSH_KEY_PATH="~/.ssh/rsa_id"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run $(alias).
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# Set personal aliases
alias bubo='brew update --verbose && brew outdated'
alias bubc='brew upgrade && brew cleanup -s --prune 7'
alias lsa='ls -A'
alias l='ls -alh'
alias ll='ls -lh'
alias la='ls -Alh'
if [[ -z "${P10K_LEAN_STYLE}" ]]; then
# Setup Color LS
source "$(dirname "$(gem which colorls)")"/tab_complete.sh
alias ls='colorls --sort-dirs --git-status'
else
# Use Powerlevel10k Lean style
source "${ZSH_CUSTOM}/themes/powerlevel10k/config/p10k-lean.zsh"
POWERLEVEL9K_MODE="compatible"
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv anaconda pyenv context root_indicator dir vcs newline prompt_char)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status command_execution_time background_jobs time ssh)
POWERLEVEL9K_MULTILINE_FIRST_PROMPT_GAP_CHAR='·'
POWERLEVEL9K_LEFT_PROMPT_LAST_SEGMENT_END_SYMBOL=' '
POWERLEVEL9K_RIGHT_PROMPT_FIRST_SEGMENT_START_SYMBOL=' '
POWERLEVEL9K_EMPTY_LINE_LEFT_PROMPT_FIRST_SEGMENT_END_SYMBOL='%{%}'
POWERLEVEL9K_EMPTY_LINE_RIGHT_PROMPT_FIRST_SEGMENT_START_SYMBOL='%{%}'
POWERLEVEL9K_STATUS_ERROR=true
POWERLEVEL9K_ANACONDA_SHOW_PYTHON_VERSION=true
POWERLEVEL9K_ANACONDA_CONTENT_EXPANSION='${P9K_CONTENT}'
POWERLEVEL9K_ANACONDA_LEFT_DELIMITER='('
POWERLEVEL9K_ANACONDA_RIGHT_DELIMITER=')'
POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=true
POWERLEVEL9K_PYENV_CONTENT_EXPANSION='${P9K_CONTENT}'
POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER='('
POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER=')'
p10k reload
fi
EOF
ln -sf .dotfiles/.zshrc .
chmod 644 .dotfiles/.zshrc
# Configurations for Zsh Powerlevel10k Lean style
SHEBANG='#!/bin/sh'
COMMAND='P10K_LEAN_STYLE=true exec /bin/zsh "$@"'
cat >"${TMP_DIR}/zsh-lean" <<EOF
${SHEBANG}
${COMMAND}
EOF
if [[ ! -x "${HOMEBREW_PREFIX}/bin/zsh-lean" || -L "${HOMEBREW_PREFIX}/bin/zsh-lean" ]] ||
! diff -EB "${HOMEBREW_PREFIX}/bin/zsh-lean" "${TMP_DIR}/zsh-lean" &>/dev/null; then
if [[ -f "${HOMEBREW_PREFIX}/bin/zsh-lean" ]]; then
exec_cmd "sudo rm -f ${HOMEBREW_PREFIX}/bin/zsh-lean"
fi
exec_cmd "printf \"%s\\n\" '${SHEBANG}' '${COMMAND}' | sudo tee ${HOMEBREW_PREFIX}/bin/zsh-lean"
exec_cmd "sudo chmod 755 ${HOMEBREW_PREFIX}/bin/zsh-lean"
fi
if ! grep -qF "${HOMEBREW_PREFIX}/bin/zsh-lean" /etc/shells; then
exec_cmd "echo '${HOMEBREW_PREFIX}/bin/zsh-lean' | sudo tee -a /etc/shells"
fi
# Add utility script file
backup_dotfiles .dotfiles/utilities.sh
cat >.dotfiles/utilities.sh <<'EOF'
function exec_cmd() {
printf "%s" "$@" | awk \
'BEGIN {
RESET = "\033[0m";
BOLD = "\033[1m";
UNDERLINE = "\033[4m";
UNDERLINEOFF = "\033[24m";
RED = "\033[31m";
GREEN = "\033[32m";
YELLOW = "\033[33m";
WHITE = "\033[37m";
GRAY = "\033[90m";
IDENTIFIER = "[_a-zA-Z][_a-zA-Z0-9]*";
idx = 0;
in_string = 0;
double_quoted = 1;
printf("%s$", BOLD WHITE);
}
{
for (i = 1; i <= NF; ++i) {
style = WHITE;
post_style = WHITE;
if (!in_string) {
if ($i ~ /^-/)
style = YELLOW;
else if ($i == "sudo" && idx == 0) {
style = UNDERLINE GREEN;
post_style = UNDERLINEOFF WHITE;
}
else if ($i ~ "^" IDENTIFIER "=" && idx == 0) {
style = GRAY;
'"if (\$i ~ \"^\" IDENTIFIER \"=[\\\"']\") {"'
in_string = 1;
double_quoted = ($i ~ "^" IDENTIFIER "=\"");
}
}
else if ($i ~ /^[12&]?>>?/ || $i == "\\")
style = RED;
else {
++idx;
'"if (\$i ~ /^[\"']/) {"'
in_string = 1;
double_quoted = ($i ~ /^"/);
}
if (idx == 1)
style = GREEN;
}
}
if (in_string) {
if (style == WHITE)
style = "";
post_style = "";
'"if ((double_quoted && \$i ~ /\";?\$/ && \$i !~ /\\\\\";?\$/) || (!double_quoted && \$i ~ /';?\$/))"'
in_string = 0;
}
if (($i ~ /;$/ && $i !~ /\\;$/) || $i == "|" || $i == "||" || $i == "&&") {
if (!in_string) {
idx = 0;
if ($i !~ /;$/)
style = RED;
}
}
if ($i ~ /;$/ && $i !~ /\\;$/)
printf(" %s%s%s;%s", style, substr($i, 1, length($i) - 1), (in_string ? WHITE : RED), post_style);
else
printf(" %s%s%s", style, $i, post_style);
if ($i == "\\")
printf("\n\t");
}
}
END {
printf("%s\n", RESET);
}' >&2
eval "$@"
}
function have_sudo_access() {
if [[ "${EUID:-"${UID}"}" == "0" ]]; then
return 0
fi
if [[ ! -x "/usr/bin/sudo" ]]; then
return 1
fi
local -a SUDO=("/usr/bin/sudo")
if [[ -n "${SUDO_ASKPASS-}" ]]; then
SUDO+=("-A")
fi