Skip to content

Commit 17dc098

Browse files
authored
Format script outputs with colors / bold (#42)
1 parent 003852c commit 17dc098

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

starkup.sh

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ ASDF_SHIMS_ESCAPED="\${ASDF_DATA_DIR:-\$HOME/.asdf}/shims"
1515
LOCAL_BIN="${HOME}/.local/bin"
1616
LOCAL_BIN_ESCAPED="\${HOME}/.local/bin"
1717

18+
BOLD=""
19+
RED=""
20+
YELLOW=""
21+
RESET=""
22+
23+
# Check whether colors are supported and should be enabled
24+
if [ -z "${NO_COLOR:-}" ] && echo "${TERM:-}" | grep -q "^xterm"; then
25+
BOLD="\033[1m"
26+
RED="\033[31m"
27+
YELLOW="\033[33m"
28+
RESET="\033[0m"
29+
fi
30+
1831
SCARB_UNINSTALL_INSTRUCTIONS="For uninstallation instructions, refer to https://docs.swmansion.com/scarb/download#uninstall"
1932
# TODO(#2): Link snfoundry uninstall docs once they are available
2033
GENERAL_UNINSTALL_INSTRUCTIONS="Try removing TOOL binaries from ${LOCAL_BIN}"
@@ -96,22 +109,22 @@ main() {
96109
_completion_message="Run '. ${_shell_config}'"
97110
;;
98111
*)
99-
say "Could not detect shell. Make sure ${LOCAL_BIN_ESCAPED} and ${ASDF_SHIMS_ESCAPED} are added to your PATH."
112+
warn "Could not detect shell. Make sure ${LOCAL_BIN_ESCAPED} and ${ASDF_SHIMS_ESCAPED} are added to your PATH."
100113
_completion_message="Source your shell configuration file"
101114
;;
102115
esac
103116

104117
add_alias "${_shell_config}"
105118

106-
say "Installation complete. ${_completion_message} or start a new terminal session to use the installed tools."
119+
info "Installation complete. ${_completion_message} or start a new terminal session to use the installed tools."
107120
}
108121

109122
add_alias() {
110123
_shell_config="$1"
111124
_alias_def="alias starkup=\"curl --proto '=https' --tlsv1.2 -sSf ${SCRIPT_URL} | sh -s --\""
112125

113126
if [ -z "$_shell_config" ]; then
114-
say "Could not detect shell. To simplify access to the installer, add the following to your shell configuration file:\n$_alias_def"
127+
warn "Could not detect shell. To simplify access to the installer, add the following to your shell configuration file:\nalias starkup=\"curl --proto '=https' --tlsv1.2 -sSf ${SCRIPT_URL} | sh -s --\""
115128
return
116129
fi
117130

@@ -120,7 +133,7 @@ add_alias() {
120133
# Alias for running starkup installer
121134
$_alias_def
122135
EOF
123-
say "'starkup' alias added to ${_shell_config}. You can use 'starkup' to directly access the installer next time."
136+
info "'starkup' alias added to ${_shell_config}. You can use 'starkup' to directly access the installer next time."
124137
fi
125138
}
126139

@@ -195,7 +208,7 @@ install_latest_version() {
195208
_tool="$1"
196209
_latest_version=$(asdf latest "$_tool")
197210
if check_version_installed "$_tool" "$_latest_version"; then
198-
say "$_tool $_latest_version is already installed"
211+
info "$_tool $_latest_version is already installed"
199212
else
200213
ensure asdf install "$_tool" latest
201214
fi
@@ -236,7 +249,7 @@ get_latest_gh_version_or_default() {
236249

237250
# shellcheck disable=SC2015
238251
_latest_version=$(get_latest_gh_version "$_repo") && [ -n "$_latest_version" ] || {
239-
say "Failed to fetch latest version for $_repo (possibly due to GitHub server rate limit or error). Using default version $_default_version." >&2
252+
warn "Failed to fetch latest version for $_repo (possibly due to GitHub server rate limit or error). Using default version $_default_version." >&2
240253
_latest_version="$_default_version"
241254
}
242255

@@ -260,8 +273,16 @@ say() {
260273
printf 'starkup: %b\n' "$1"
261274
}
262275

276+
info() {
277+
say "${BOLD}info:${RESET} $1"
278+
}
279+
280+
warn() {
281+
say "${BOLD}${YELLOW}warn:${RESET} ${YELLOW}$1${RESET}"
282+
}
283+
263284
err() {
264-
say "$1" >&2
285+
say "${BOLD}${RED}error:${RESET} ${RED}$1${RESET}" >&2
265286
exit 1
266287
}
267288

@@ -299,7 +320,7 @@ install_asdf() {
299320
_need_interaction="$1"
300321
_answer=""
301322
if "$_need_interaction"; then
302-
say "asdf-vm is required but not found.\nFor seamless updates, install it using a package manager (e.g., Homebrew, AUR helpers). See details: ${ASDF_INSTALL_DOCS}.\nAlternatively, the script can install asdf-vm directly, but manual updates might be needed later.\nProceed with direct installation? (y/N):"
323+
info "asdf-vm is required but not found.\nFor seamless updates, install it using a package manager (e.g., Homebrew, AUR helpers). See details: ${ASDF_INSTALL_DOCS}.\nAlternatively, the script can install asdf-vm directly, but manual updates might be needed later.\nProceed with direct installation? (y/N):"
303324
if [ ! -t 0 ]; then
304325
# Starkup is going to want to ask for confirmation by
305326
# reading stdin. This script may be piped into `sh` though
@@ -348,7 +369,7 @@ install_asdf() {
348369
echo >>"$_profile" && echo "export PATH=\"${LOCAL_BIN_ESCAPED}:\$PATH\"" >>"$_profile"
349370
echo >>"$_profile" && echo "export PATH=\"${ASDF_SHIMS_ESCAPED}:\$PATH\"" >>"$_profile"
350371
fi
351-
say "asdf-vm has been installed."
372+
info "asdf-vm has been installed."
352373
else
353374
err "cancelled asdf-vm installation. Please install it manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}."
354375
fi
@@ -357,29 +378,29 @@ install_asdf() {
357378
update_asdf() {
358379
_current_version=$(get_asdf_version)
359380
if is_asdf_legacy; then
360-
say "asdf-vm $_current_version is legacy and cannot be updated. Please update manually. For migration instructions, refer to ${ASDF_MIGRATION_DOCS}."
381+
warn "asdf-vm $_current_version is legacy and cannot be updated. Please update manually. For migration instructions, refer to ${ASDF_MIGRATION_DOCS}."
361382
return
362383
fi
363384

364385
_latest_version=$(get_latest_gh_version_or_default "asdf-vm/asdf" "$ASDF_DEFAULT_VERSION")
365386
if ! version_less_than "$_current_version" "$_latest_version"; then
366-
say "asdf-vm is up to date."
387+
info "asdf-vm is up to date."
367388
return
368389
fi
369390

370391
if [ "$(command -v asdf)" != "${LOCAL_BIN}/asdf" ]; then
371-
say "asdf-vm $_current_version is was not installed by starkup. Please update manually. See details: ${ASDF_INSTALL_DOCS}."
392+
warn "asdf-vm $_current_version was not installed by starkup and cannot be updated. Please update manually. See details: ${ASDF_INSTALL_DOCS}."
372393
return
373394
fi
374395

375396
download_asdf "$_latest_version"
376-
say "asdf-vm updated to $_latest_version."
397+
info "asdf-vm updated to $_latest_version."
377398
}
378399

379400
download_asdf() {
380401
_version="$1"
381402

382-
say "Downloading asdf-vm $_version..."
403+
info "Downloading asdf-vm $_version..."
383404

384405
_os="$(uname -s)"
385406
_arch="$(uname -m)"

0 commit comments

Comments
 (0)