Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·2569 lines (2307 loc) · 76.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·2569 lines (2307 loc) · 76.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
# shellcheck disable=SC1091 # /etc/os-release is only sourced at runtime
# =============================================================================
#
# Dotfiles Installer for macOS and Debian (Trixie)
#
# Author: AnalogCyan
# License: Unlicense
#
# =============================================================================
# Targets the stock macOS bash 3.2: no associative arrays, mapfile or
# namerefs, and empty arrays are expanded with ${a[@]+"${a[@]}"} under set -u.
set -uo pipefail
IFS=$'\n\t'
usage() {
cat <<'EOF'
Usage: ./install.sh [options]
Installs dotfiles and configures a macOS or Debian environment. Run it in a
terminal to pick steps and optional packages interactively.
Options:
-n, --dry-run Show what each step would change, without changing anything
-y, --yes No prompts: run every step, skip optional packages, keep a
non-empty ~/Downloads, continue on non-Debian Linux
-h, --help Show this help message and exit
Environment:
NO_COLOR Disable colors
EOF
}
DRY_RUN=0
ASSUME_YES=0
while [[ "${#}" -gt 0 ]]; do
case "${1}" in
-n|--dry-run) DRY_RUN=1 ;;
-y|--yes) ASSUME_YES=1 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: ${1}" >&2; echo >&2; usage >&2; exit 1 ;;
esac
shift
done
# =============================================================================
# ENVIRONMENT
# =============================================================================
OS="$(uname -s)"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="${SCRIPT_DIR}"
CURRENT_USER="${USER:-$(id -un)}"
# Kept out of /tmp, which is RAM-backed on trixie and cleared on reboot
LOG_DIR="${XDG_STATE_HOME:-${HOME}/.local/state}/dotfiles"
LOG_FILE="${LOG_DIR}/install-$(date +%Y%m%d-%H%M%S).log"
WORK_DIR=""
# TTY means a terminal that can take cursor movement and the alternate screen
TTY=0
[[ -t 0 && -t 1 && -n "${TERM:-}" && "${TERM}" != "dumb" ]] && TTY=1
INTERACTIVE="${TTY}"
(( ASSUME_YES )) && INTERACTIVE=0
# =============================================================================
# CONFIGURATION
# =============================================================================
BREW_FORMULAE=(
"bat"
"btop"
"chojs23/tap/concord"
"ctop"
"dust"
"eza"
"fd"
"forgejo-cli"
"fortune"
"fzf"
"gh"
"git"
"go"
"gromgit/brewtils/taproom"
"helix"
"imagemagick"
"lazygit"
"mole"
"mpv"
"neurosnap/tap/zmx"
"nmap"
"node"
"pandoc"
"philocalyst/tap/caligula"
"python@3.13"
"ripgrep"
"rsync"
"sevenzip"
"shellcheck"
"starship"
"tmux"
"wget"
"xz"
"yt-dlp"
"zoxide"
"zsh"
croc
pfetch-rs
yazi
)
BREW_CASKS=(
"1password"
"1password-cli"
"balenaetcher"
"chatgpt"
"claude"
"cleanshot"
"codex"
"crystalfetch"
"discord"
"firefox@beta"
"ghostty"
"google-chrome@canary"
"iina"
"keka"
"kekaexternalhelper"
"mactracker"
"mole-app"
"obsidian"
"orbstack"
"raspberry-pi-imager"
"t3-code@nightly"
"tailscale-app"
"transmission"
"utm"
"zed@preview"
)
BREW_FORMULAE_OPTIONAL=(
"cdrtools:CD/DVD burning and ISO building (mkisofs)"
"cmake:C/C++ build system"
"colima:Docker runtime in a Lima VM"
"f3:Fake-capacity flash drive tester"
"gifsicle:GIF optimizer and editor"
"make:GNU make (as gmake)"
"powershell:PowerShell (pwsh)"
"qpdf:Inspect, repair and transform PDFs"
"scrcpy:Mirror and control Android over USB"
"sox:Audio conversion and effects"
"sshpass:Non-interactive SSH password auth"
"zig:Zig compiler"
)
BREW_CASKS_OPTIONAL=(
"android-platform-tools:adb and fastboot (needed by scrcpy)"
"gimp:Image editor"
"inkscape:Vector graphics editor"
"obs:Screen recording and streaming"
"steam:Game store"
"typeless:AI voice dictation"
)
declare -a APT_PACKAGES=(
bat
btop
ca-certificates
curl
eza
fd-find
fontconfig
fortune-mod
fzf
gh
git
gnupg
hx
jq
lazygit
nvme-cli
ripgrep
rsync
starship
tmux
unzip
xz-utils
yt-dlp
zoxide
zsh
)
# Filled in by the package picker
SELECTED_FORMULAE_OPTIONAL=()
SELECTED_CASKS_OPTIONAL=()
PACKAGES_VISITED=0
# Set by the planner when ~/Downloads is a non-empty real directory
REPLACE_DOWNLOADS=0
# =============================================================================
# UI
# =============================================================================
ui_init() {
C_RESET="" C_BOLD="" C_DIM="" C_RED="" C_GREEN="" C_YELLOW=""
C_BLUE="" C_MAGENTA="" C_CYAN=""
if [[ -t 1 && -z "${NO_COLOR:-}" && "${TERM:-dumb}" != "dumb" ]]; then
C_RESET=$'\e[0m' C_BOLD=$'\e[1m' C_DIM=$'\e[2m'
C_RED=$'\e[31m' C_GREEN=$'\e[32m' C_YELLOW=$'\e[33m'
C_BLUE=$'\e[34m' C_MAGENTA=$'\e[35m' C_CYAN=$'\e[36m'
fi
case "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" in
*UTF-8*|*utf-8*|*UTF8*|*utf8*)
G_PTR="❯" G_ON="◉" G_OFF="○" G_OK="✓" G_WARN="▲" G_FAIL="✗"
G_DOT="·" G_ARROW="›" G_UP="↑" G_DOWN="↓" G_BULLET="•"
G_RULE="─" G_ELL="…"
SPIN=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏")
;;
*)
G_PTR=">" G_ON="[x]" G_OFF="[ ]" G_OK="+" G_WARN="!" G_FAIL="x"
G_DOT="|" G_ARROW=">" G_UP="^" G_DOWN="v" G_BULLET="*"
G_RULE="-" G_ELL="~"
SPIN=("|" "/" "-" "\\")
;;
esac
}
SCREEN_ALT=0
CURSOR_HIDDEN=0
screen_enter() {
(( TTY )) || return 0
printf '\e[?1049h\e[H\e[2J'
SCREEN_ALT=1
cursor_hide
}
screen_leave() {
(( SCREEN_ALT )) || return 0
printf '\e[?1049l'
SCREEN_ALT=0
}
cursor_hide() { (( TTY )) && { printf '\e[?25l'; CURSOR_HIDDEN=1; }; return 0; }
cursor_show() { (( CURSOR_HIDDEN )) && { printf '\e[?25h'; CURSOR_HIDDEN=0; }; return 0; }
term_size() {
local size
size=$( { stty size </dev/tty; } 2>/dev/null ) || size=""
ROWS="${size%% *}"
COLS="${size##* }"
[[ "${ROWS}" =~ ^[0-9]+$ && "${ROWS}" -gt 0 ]] || ROWS=24
[[ "${COLS}" =~ ^[0-9]+$ && "${COLS}" -gt 0 ]] || COLS=80
}
# Sets KEY to up, down, space, enter, all, back, quit, yes, no, diff, tick or other
read_key() {
local k="" rest=""
KEY="other"
if ! IFS= read -rsn1 -t 1 k </dev/tty; then
KEY="tick"
return
fi
case "${k}" in
$'\e')
IFS= read -rsn2 -t 1 rest </dev/tty || rest=""
case "${rest}" in
"[A"|"OA") KEY="up" ;;
"[B"|"OB") KEY="down" ;;
"") KEY="back" ;;
esac
;;
" ") KEY="space" ;;
"") KEY="enter" ;;
k|K) KEY="up" ;;
j|J) KEY="down" ;;
a|A) KEY="all" ;;
b|B|h|H) KEY="back" ;;
q|Q) KEY="quit" ;;
y|Y) KEY="yes" ;;
n|N) KEY="no" ;;
d|D) KEY="diff" ;;
esac
}
# Text width for prose, capped so wide terminals stay readable
text_width() {
local width=$(( COLS - ${1:-4} ))
(( width > 76 )) && width=76
printf '%s' "${width}"
}
rule() {
local width=$(( COLS - 4 ))
(( width > 72 )) && width=72
(( width < 1 )) && width=1
repeat_char "${G_RULE}" "${width}"
}
tildify() {
case "$1" in
"${HOME}"|"${HOME}"/*) printf '%s%s' "~" "${1#"${HOME}"}" ;;
*) printf '%s' "$1" ;;
esac
}
# in_list needle item... succeeds if needle is one of the items
in_list() {
local needle="$1" item
shift
for item in "$@"; do
[[ "${item}" == "${needle}" ]] && return 0
done
return 1
}
join_by() {
local sep="$1" out="" item
shift
for item in "$@"; do out+="${out:+${sep}}${item}"; done
printf '%s' "${out}"
}
repeat_char() {
local out="" i
for (( i = 0; i < $2; i++ )); do out+="$1"; done
printf '%s' "${out}"
}
format_duration() {
local s="$1"
if (( s < 1 )); then
printf '<1s'
elif (( s < 60 )); then
printf '%ds' "${s}"
else
printf '%dm %02ds' $(( s / 60 )) $(( s % 60 ))
fi
}
strip_ansi() {
LC_ALL=C sed -e $'s/\e\\[[0-9;?]*[A-Za-z]//g' -e $'s/\e[()][A-Za-z0-9]//g'
}
system_info() {
local os_name arch
arch="$(uname -m)"
case "${OS}" in
Darwin) os_name="macOS $(sw_vers -productVersion 2>/dev/null)" ;;
Linux)
os_name="Linux"
if [[ -r /etc/os-release ]]; then
os_name="$(. /etc/os-release && printf '%s' "${PRETTY_NAME:-${NAME:-Linux}}")"
fi
;;
*) os_name="${OS}" ;;
esac
SYS_INFO="$(join_by " ${G_DOT} " "${os_name}" "${arch}" "$(hostname -s 2>/dev/null || hostname)")"
}
# Frame builder for full-screen views: every line clears to end of line so
# redraws never leave stale characters behind.
FRAME=""
FRAME_LINES=0
BODY=()
BODY_MODE=0
fl() {
if (( BODY_MODE )); then
BODY+=("$1")
return
fi
FRAME+="$1"$'\e[K\n'
FRAME_LINES=$(( FRAME_LINES + 1 ))
}
# Emits buffered body lines, trimming them to the space left above the footer
flush_body() {
local room i n="${#BODY[@]}"
BODY_MODE=0
room=$(( ROWS - FRAME_LINES - $(footer_lines) ))
if (( n <= room )); then
for (( i = 0; i < n; i++ )); do fl "${BODY[i]}"; done
else
for (( i = 0; i < room - 1; i++ )); do fl "${BODY[i]}"; done
fl " ${C_DIM}${G_DOWN} $(( n - room + 1 )) more lines; enlarge the terminal to see them${C_RESET}"
fi
BODY=()
}
# Truncate plain text to a width, marking the cut
fit() {
local text="$1" width="$2"
if (( width < 4 )); then
printf '%s' "${text:0:${width}}"
elif (( ${#text} > width )); then
printf '%s%s' "${text:0:$(( width - 1 ))}" "${G_ELL}"
else
printf '%s' "${text}"
fi
}
# Word-wrap plain text to a width, one output line per wrapped line
wrap() {
local text="$1" width="$2" line="" word
local IFS=' '
for word in ${text}; do
if [[ -z "${line}" ]]; then
line="${word}"
elif (( ${#line} + 1 + ${#word} <= width )); then
line+=" ${word}"
else
printf '%s\n' "$(fit "${line}" "${width}")"
line="${word}"
fi
done
[[ -n "${line}" ]] && printf '%s\n' "$(fit "${line}" "${width}")"
return 0
}
# Wrap key hints between items rather than mid-hint
wrap_hints() {
local width="$1" line="" item sep=" ${G_DOT} "
shift
for item in "$@"; do
if [[ -z "${line}" ]]; then
line="${item}"
elif (( ${#line} + ${#sep} + ${#item} <= width )); then
line+="${sep}${item}"
else
printf '%s\n' "${line}"
line="${item}"
fi
done
[[ -n "${line}" ]] && printf '%s\n' "${line}"
return 0
}
MIN_COLS=50
MIN_ROWS=16
# Draws a notice instead of the screen when the terminal is too small
too_small() {
(( COLS >= MIN_COLS && ROWS >= MIN_ROWS )) && return 1
FRAME=$'\e[H' FRAME_LINES=0
fl ""
fl " ${C_YELLOW}Terminal too small${C_RESET}"
fl " ${C_DIM}$(fit "Resize to at least ${MIN_COLS}x${MIN_ROWS} (now ${COLS}x${ROWS})." $(( COLS - 2 )))${C_RESET}"
# No newline after the last line, or a full-height frame scrolls
FRAME="${FRAME%$'\n'}"$'\e[J'
printf '%s' "${FRAME}"
return 0
}
frame_header() {
local title="$1" subtitle="$2" badge="" badge_len=0 line
if (( DRY_RUN )); then
badge=" ${C_YELLOW}${C_BOLD}DRY RUN${C_RESET}"
badge_len=9
fi
FRAME=$'\e[H' FRAME_LINES=0
(( ROWS >= 24 )) && fl ""
fl " ${C_BOLD}${C_MAGENTA}dotfiles${C_RESET} ${C_DIM}$(fit "${SYS_INFO}" $(( COLS - 14 - badge_len )))${C_RESET}${badge}"
fl " ${C_DIM}$(rule)${C_RESET}"
fl " ${BREADCRUMB}"
(( ROWS >= 24 )) && fl ""
fl " ${C_BOLD}$(fit "${title}" $(( COLS - 4 )))${C_RESET}"
if [[ -n "${subtitle}" ]]; then
while IFS= read -r line; do
fl " ${C_DIM}${line}${C_RESET}"
done < <(wrap "${subtitle}" "$(text_width)")
fi
fl ""
}
# Footer hints are passed as separate items so they wrap cleanly
FOOTER_HINTS=()
FOOTER_MSG=""
footer_lines() {
local count=1
[[ -n "${FOOTER_MSG}" ]] && count=$(( count + 1 ))
count=$(( count + $(wrap_hints $(( COLS - 4 )) "${FOOTER_HINTS[@]}" | wc -l) ))
printf '%s' "${count}"
}
frame_footer() {
local line
fl ""
[[ -n "${FOOTER_MSG}" ]] && fl " ${FOOTER_MSG}"
while IFS= read -r line; do
fl " ${C_DIM}${line}${C_RESET}"
done < <(wrap_hints $(( COLS - 4 )) "${FOOTER_HINTS[@]}")
# No newline after the last line, or a full-height frame scrolls
FRAME="${FRAME%$'\n'}"$'\e[J'
printf '%s' "${FRAME}"
}
set_hints() {
FOOTER_HINTS=("$@")
(( CAN_GO_BACK )) && FOOTER_HINTS+=("b back")
FOOTER_HINTS+=("q quit")
}
# Breadcrumb over the wizard screens, current one highlighted
WIZARD_SCREENS=()
set_breadcrumb() {
local current="$1" name parts=()
for name in ${WIZARD_SCREENS[@]+"${WIZARD_SCREENS[@]}"}; do
if [[ "${name}" == "${current}" ]]; then
parts+=("${C_CYAN}${C_BOLD}${name}${C_RESET}")
else
parts+=("${C_DIM}${name}${C_RESET}")
fi
done
BREADCRUMB="$(join_by " ${C_DIM}${G_ARROW}${C_RESET} " ${parts[@]+"${parts[@]}"})"
}
# Checklist over CL_LABELS/CL_DESCS/CL_ON/CL_TAGS. CL_ON holds 1 or 0, or "h"
# for a section header row. Returns 0 on enter and 1 on back.
CL_LABELS=() CL_DESCS=() CL_ON=() CL_TAGS=()
CL_CUR=0 CL_TOP=0 CL_MSG=""
CL_LIVE_STEPS=0
CAN_GO_BACK=0
ui_checklist() {
local title="$1" subtitle="$2" n="${#CL_LABELS[@]}" i step last_size=""
(( CL_CUR < n )) || CL_CUR=0
while [[ "${CL_ON[CL_CUR]}" == "h" ]]; do CL_CUR=$(( CL_CUR + 1 )); done
while true; do
term_size
if [[ "${KEY:-}" != "tick" || "${ROWS}x${COLS}" != "${last_size}" ]]; then
too_small || render_checklist "${title}" "${subtitle}"
last_size="${ROWS}x${COLS}"
fi
read_key
[[ "${KEY}" != "tick" ]] && CL_MSG=""
case "${KEY}" in
up|down)
step=1
[[ "${KEY}" == "up" ]] && step=-1
i="${CL_CUR}"
while true; do
i=$(( i + step ))
(( i < 0 || i >= n )) && break
if [[ "${CL_ON[i]}" != "h" ]]; then CL_CUR="${i}"; break; fi
done
;;
space)
if [[ "${CL_ON[CL_CUR]}" == "1" ]]; then CL_ON[CL_CUR]=0; else CL_ON[CL_CUR]=1; fi
;;
all)
local any_off=0 v
for (( i = 0; i < n; i++ )); do [[ "${CL_ON[i]}" == "0" ]] && any_off=1; done
v=0
(( any_off )) && v=1
for (( i = 0; i < n; i++ )); do [[ "${CL_ON[i]}" != "h" ]] && CL_ON[i]="${v}"; done
;;
enter) return 0 ;;
back) (( CAN_GO_BACK )) && return 1 ;;
quit) quit_wizard ;;
esac
done
}
render_checklist() {
local title="$1" subtitle="$2" n="${#CL_LABELS[@]}" i lw=0 selected=0 total=0
local visible end line name desc avail ptr mark tag dw=0 has_tags=0 sections=0
for (( i = 0; i < n; i++ )); do
(( ${#CL_LABELS[i]} > lw )) && lw="${#CL_LABELS[i]}"
if [[ "${CL_ON[i]}" == "h" ]]; then
(( i > 0 )) && sections=1
continue
fi
(( ${#CL_DESCS[i]} > dw )) && dw="${#CL_DESCS[i]}"
[[ -n "${CL_TAGS[i]:-}" ]] && has_tags=1
total=$(( total + 1 ))
[[ "${CL_ON[i]}" == "1" ]] && selected=$(( selected + 1 ))
done
if (( CL_LIVE_STEPS )); then
STEP_ON=("${CL_ON[@]}")
wizard_screens
set_breadcrumb "Steps"
fi
FOOTER_MSG="${C_DIM}${selected} of ${total} selected${C_RESET}"
[[ -n "${CL_MSG}" ]] && FOOTER_MSG="${C_YELLOW}$(fit "${CL_MSG}" $(( COLS - 4 )))${C_RESET}"
set_hints "${G_UP}/${G_DOWN} move" "space toggle" "a all/none" "enter continue"
frame_header "${title}" "${subtitle}"
# Header, footer, two scroll indicators and a spare for section spacing
local scrolls=0
visible=$(( ROWS - FRAME_LINES - $(footer_lines) - sections ))
if (( n + sections > visible )); then
# Room for the two "more" indicators only when the list scrolls
scrolls=1
visible=$(( visible - 2 ))
fi
(( visible < 1 )) && visible=1
(( CL_CUR < CL_TOP )) && CL_TOP="${CL_CUR}"
(( CL_CUR >= CL_TOP + visible )) && CL_TOP=$(( CL_CUR - visible + 1 ))
# Keep a section header visible above the first item in its section
if (( CL_TOP > 0 )) && [[ "${CL_ON[CL_TOP - 1]}" == "h" ]] && (( CL_CUR < CL_TOP + visible - 1 )); then
CL_TOP=$(( CL_TOP - 1 ))
fi
end=$(( CL_TOP + visible ))
(( end > n )) && end="${n}"
# Room for pointer, mark, label and the tag column
avail=$(( COLS - lw - 12 ))
(( has_tags )) && avail=$(( avail - 11 ))
(( dw > avail )) && dw="${avail}"
if (( CL_TOP > 0 )); then
fl " ${C_DIM}${G_UP} ${CL_TOP} more${C_RESET}"
elif (( scrolls )); then
fl ""
fi
for (( i = CL_TOP; i < end; i++ )); do
if [[ "${CL_ON[i]}" == "h" ]]; then
(( i > CL_TOP )) && fl ""
fl " ${C_BOLD}${C_BLUE}${CL_LABELS[i]}${C_RESET}"
continue
fi
ptr=" "
(( i == CL_CUR )) && ptr="${C_CYAN}${G_PTR}${C_RESET}"
if [[ "${CL_ON[i]}" == "1" ]]; then
mark="${C_GREEN}${G_ON}${C_RESET}"
else
mark="${C_DIM}${G_OFF}${C_RESET}"
fi
printf -v name "%-${lw}s" "${CL_LABELS[i]}"
if (( i == CL_CUR )); then
name="${C_BOLD}${name}${C_RESET}"
elif [[ "${CL_ON[i]}" != "1" ]]; then
name="${C_DIM}${name}${C_RESET}"
fi
tag="${CL_TAGS[i]:-}"
desc="${CL_DESCS[i]:-}"
if (( dw < 8 )); then
desc=""
else
desc="$(fit "${desc}" "${dw}")"
fi
(( has_tags && ${#desc} < dw )) && desc+="$(repeat_char " " $(( dw - ${#desc} )))"
line=" ${ptr} ${mark} ${name} ${C_DIM}${desc}${C_RESET}"
case "${tag}" in
sudo) line+=" ${C_YELLOW}${C_DIM}${tag}${C_RESET}" ;;
installed) line+=" ${C_GREEN}${C_DIM}${tag}${C_RESET}" ;;
?*) line+=" ${C_DIM}${tag}${C_RESET}" ;;
esac
fl "${line}"
done
if (( end < n )); then
fl " ${C_DIM}${G_DOWN} $(( n - end )) more${C_RESET}"
elif (( scrolls )); then
fl ""
fi
frame_footer
}
US=$'\x1f'
# Full-screen yes/no question. Returns 0 for yes, 1 for no, 2 for back.
ui_question() {
local title="$1" body="$2" default="$3" last_size="" line
KEY=""
while true; do
term_size
if [[ "${KEY}" != "tick" || "${ROWS}x${COLS}" != "${last_size}" ]]; then
last_size="${ROWS}x${COLS}"
too_small && { read_key; [[ "${KEY}" == "quit" ]] && quit_wizard; continue; }
frame_header "${title}" ""
# Body lines are "color<US>text" so wrapping never splits an escape
local color text wrapped
while IFS=$'\x1f' read -r color text; do
if [[ -z "${text}" ]]; then fl ""; continue; fi
while IFS= read -r wrapped; do
fl " ${color}${wrapped}${C_RESET}"
done < <(wrap "${text}" "$(text_width)")
done <<<"${body}"
local yes_label="y yes" no_label="n no"
if [[ "${default}" == "y" ]]; then yes_label="Y yes"; else no_label="N no"; fi
FOOTER_MSG=""
set_hints "${yes_label}" "${no_label}" "enter default (${default})"
frame_footer
fi
read_key
case "${KEY}" in
yes) return 0 ;;
no) return 1 ;;
enter) [[ "${default}" == "y" ]] && return 0; return 1 ;;
back) (( CAN_GO_BACK )) && return 2 ;;
quit) quit_wizard ;;
esac
done
}
quit_wizard() {
screen_leave
cursor_show
echo "Cancelled. Nothing was changed."
exit 130
}
pager() {
if command -v less >/dev/null 2>&1; then
less -R
elif command -v more >/dev/null 2>&1; then
more
else
cat
fi
}
# =============================================================================
# UTILITY FUNCTIONS
# =============================================================================
# Step functions write these to the log; the TUI shows a live tail of it.
log_info() { echo "INFO: $*"; }
log_success() { echo "SUCCESS: $*"; }
log_warning() { echo "WARNING: $*"; }
log_error() { echo "ERROR: $*"; }
curl_https() { curl --proto '=https' --tlsv1.2 -fsSL "$@"; }
# Non-interactive apt-get that keeps existing config files and waits for the
# dpkg lock (unattended-upgrades often holds it on a fresh install)
apt_get() {
sudo DEBIAN_FRONTEND=noninteractive apt-get -y \
-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold \
-o DPkg::Lock::Timeout=300 "$@"
}
# GitHub API request; GITHUB_TOKEN, when set, lifts the 60/hour anonymous limit
github_api() {
local auth=()
[[ -n "${GITHUB_TOKEN:-}" ]] && auth=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
curl_https ${auth[@]+"${auth[@]}"} -H "Accept: application/vnd.github+json" "https://api.github.com/$1"
}
# Latest release tag from the releases/latest redirect, which has no API quota
github_latest_tag() {
local url
url="$(curl_https -o /dev/null -w '%{url_effective}' "https://github.com/$1/releases/latest")" || return 1
[[ "${url}" == */tag/* ]] || return 1
printf '%s' "${url##*/}"
}
load_brew_env() {
local brew_bin
for brew_bin in /opt/homebrew/bin/brew /usr/local/bin/brew; do
if [[ -x "${brew_bin}" ]]; then
eval "$("${brew_bin}" shellenv)"
return 0
fi
done
return 0
}
login_shell() {
case "${OS}" in
Darwin) dscl . -read "/Users/${CURRENT_USER}" UserShell 2>/dev/null | awk '{print $2}' ;;
Linux) getent passwd "${CURRENT_USER}" 2>/dev/null | cut -d: -f7 ;;
esac
}
target_zsh() {
case "${OS}" in
Darwin) command -v brew >/dev/null 2>&1 && printf '%s' "$(brew --prefix)/bin/zsh" ;;
Linux) command -v zsh 2>/dev/null || true ;;
esac
}
# Sorted dotfile paths relative to home/, excluding Finder metadata
dotfile_list() {
(cd "${DOTFILES_DIR}/home" && find . -type f ! -name .DS_Store | sed 's|^\./||' | LC_ALL=C sort)
}
# Sets DF_NEW, DF_CHANGED and DF_SAME
dotfile_changes() {
local rel
DF_NEW=() DF_CHANGED=() DF_SAME=0
while IFS= read -r rel; do
[[ -z "${rel}" ]] && continue
if [[ ! -e "${HOME}/${rel}" ]]; then
DF_NEW+=("${rel}")
elif cmp -s "${DOTFILES_DIR}/home/${rel}" "${HOME}/${rel}"; then
DF_SAME=$(( DF_SAME + 1 ))
else
DF_CHANGED+=("${rel}")
fi
done < <(dotfile_list)
}
# "+3 -1" for how deploying would change a live file
diff_counts() {
local out adds dels
out="$(diff "${HOME}/$1" "${DOTFILES_DIR}/home/$1" 2>/dev/null)"
adds=$(printf '%s\n' "${out}" | grep -c '^>')
dels=$(printf '%s\n' "${out}" | grep -c '^<')
printf '+%s -%s' "${adds}" "${dels}"
}
# shellcheck disable=SC2088
show_dotfile_diff() {
local rel
{
for rel in ${DF_CHANGED[@]+"${DF_CHANGED[@]}"}; do
diff -u --label "~/${rel} (current)" --label "~/${rel} (dotfiles)" \
"${HOME}/${rel}" "${DOTFILES_DIR}/home/${rel}" | colorize_diff
echo
done
for rel in ${DF_NEW[@]+"${DF_NEW[@]}"}; do
diff -u --label "~/${rel} (not present)" --label "~/${rel} (dotfiles)" \
/dev/null "${DOTFILES_DIR}/home/${rel}" | colorize_diff
echo
done
} | pager
}
colorize_diff() {
if [[ -z "${C_RESET}" ]]; then
cat
return
fi
local line
while IFS= read -r line; do
case "${line}" in
"+++"*|"---"*) printf '%s\n' "${C_BOLD}${line}${C_RESET}" ;;
"+"*) printf '%s\n' "${C_GREEN}${line}${C_RESET}" ;;
"-"*) printf '%s\n' "${C_RED}${line}${C_RESET}" ;;
"@@"*) printf '%s\n' "${C_CYAN}${line}${C_RESET}" ;;
*) printf '%s\n' "${line}" ;;
esac
done
}
# =============================================================================
# MACOS FUNCTIONS
# =============================================================================
install_updates_macos() {
local status=0
log_info "Checking for macOS system updates..."
sudo softwareupdate -ia --force --verbose || {
log_warning "Some macOS updates may have failed."
status=1
}
if command -v brew >/dev/null 2>&1; then
log_info "Updating Homebrew..."
if ! brew update || ! brew upgrade || ! brew cleanup; then
log_warning "Homebrew update/upgrade had issues."
status=1
fi
fi
if (( status == 0 )); then
log_success "System update completed."
else
log_warning "System update finished with warnings."
fi
return "${status}"
}
install_homebrew() {
if command -v brew >/dev/null 2>&1; then
log_info "Homebrew already installed."
eval "$("$(brew --prefix)/bin/brew" shellenv)"
return
fi
log_info "Installing Homebrew..."
local script="${WORK_DIR}/homebrew-install.sh"
if ! curl_https -o "${script}" https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh; then
log_error "Failed to download the Homebrew installer."
return 2
fi
NONINTERACTIVE=1 /bin/bash "${script}" || {
log_error "Failed to install Homebrew."
return 2
}
local brew_path
if [[ -f /opt/homebrew/bin/brew ]]; then
brew_path="/opt/homebrew/bin/brew"
elif [[ -f /usr/local/bin/brew ]]; then
brew_path="/usr/local/bin/brew"
fi
if [[ -n "${brew_path:-}" ]]; then
if [[ ! -f "${HOME}/.zprofile" ]] || ! grep -qF "eval \"\$(${brew_path} shellenv)\"" "${HOME}/.zprofile"; then
echo "eval \"\$(${brew_path} shellenv)\"" >>"${HOME}/.zprofile"
fi
eval "$("${brew_path}" shellenv)"
fi
log_success "Homebrew installed."
}
install_homebrew_packages() {
local status=0
if ! command -v brew >/dev/null 2>&1; then
log_error "Homebrew is not installed. Run the Homebrew step first."
return 2
fi
# The updates step already ran brew update
step_selected install_updates_macos && export HOMEBREW_NO_AUTO_UPDATE=1
log_info "Installing formulae..."
brew install "${BREW_FORMULAE[@]}" || {
log_warning "Some formulae failed."
status=1
}
log_info "Installing casks..."
brew install --cask "${BREW_CASKS[@]}" || {
log_warning "Some casks failed."
status=1
}
if (( ${#SELECTED_FORMULAE_OPTIONAL[@]} > 0 )); then
log_info "Installing optional formulae..."
brew install "${SELECTED_FORMULAE_OPTIONAL[@]}" || {
log_warning "Some optional formulae failed."
status=1
}
fi
if (( ${#SELECTED_CASKS_OPTIONAL[@]} > 0 )); then
log_info "Installing optional casks..."
brew install --cask "${SELECTED_CASKS_OPTIONAL[@]}" || {
log_warning "Some optional casks failed."
status=1
}
fi
if (( status == 0 )); then
log_success "Packages installed."
else
log_warning "Package installation finished with warnings."
fi
return "${status}"
}
setup_icloud_links() {
local status=0
log_info "Creating iCloud symlinks..."
ln -snf "${HOME}/Library/Mobile Documents/com~apple~CloudDocs" "${HOME}/iCloud" || {
log_warning "Failed to create iCloud symlink."
status=1
}
local downloads_target="${HOME}/Library/Mobile Documents/com~apple~CloudDocs/Downloads"
if [[ -d "${downloads_target}" ]]; then
if [[ -e "${HOME}/Downloads" && ! -L "${HOME}/Downloads" ]]; then
if [[ -z "$(ls -A "${HOME}/Downloads" 2>/dev/null)" ]]; then
rm -rf "${HOME}/Downloads"
elif (( REPLACE_DOWNLOADS )); then
rm -rf "${HOME}/Downloads"
else
log_info "Kept the non-empty Downloads directory as chosen."
log_success "iCloud links configured."
return "${status}"
fi
fi
ln -snf "${downloads_target}" "${HOME}/Downloads" || {
log_warning "Failed to create Downloads symlink."
status=1
}
fi
if (( status == 0 )); then
log_success "iCloud links configured."
else
log_warning "iCloud link setup finished with warnings."
fi
return "${status}"
}
setup_ghostty_config() {
local status=0
log_info "Setting up Ghostty config..."
mkdir -p "$HOME/Library/Application Support/com.mitchellh.ghostty"
ln -sf "$HOME/.config/ghostty/config.ghostty" \
"$HOME/Library/Application Support/com.mitchellh.ghostty/config.ghostty" || {
log_warning "Failed to set up Ghostty config."