0
0
mirror of https://github.com/tj/n.git synced 2024-11-24 19:46:56 +01:00

Add support for tty detection, NO_COLOR, and CLICOLOR

This commit is contained in:
John Gee 2019-05-23 22:16:07 +12:00
parent ae2207612e
commit 1747b7a819
2 changed files with 44 additions and 10 deletions

View File

@ -13,11 +13,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- log message after install from cache (previously silent)
- extra logging after install if the active and installed node locations are different
- support for [NO_COLOR](http://no-color.org) and [CLICOLOR=0](https://bixense.com/clicolors)
- suppress progress and colour if not interactive tty
### Changed
- log message for installing using menu now same format as `npm install` message
- reinstalling active node version does reinstall (previously silently did nothing)
- log message for installing using menu now same format as `npm install` message
- updates to GitHub templates and guidelines for contributing et al
## [4.1.0]

50
bin/n
View File

@ -15,7 +15,7 @@ BASE_VERSIONS_DIR="$N_PREFIX/n/versions"
#
log() {
printf " \033[36m%10s\033[0m : \e[2m%s\e[22m\033[0m\n" "$1" "$2"
printf " ${SGR_CYAN}%10s${SGR_RESET} : ${SGR_FAINT}%s${SGR_RESET}${SGR_RESET}\n" "$1" "$2"
}
#
@ -23,7 +23,7 @@ log() {
#
abort() {
printf "\n \033[31mError: %s\033[0m\n\n" "$*" && exit 1
printf "\n ${SGR_RED}Error: %s${SGR_RESET}\n\n" "$*" && exit 1
}
#
@ -81,10 +81,40 @@ test -z "$GET" && abort "curl or wget required"
#
DEFAULT=0
QUIET=true
if [[ -t 1 ]]; then
QUIET=false
else
QUIET=true
fi
ACTIVATE=true
ARCH=
# ANSI escape codes
# https://en.wikipedia.org/wiki/ANSI_escape_code
# http://no-color.org
# https://bixense.com/clicolors
USE_COLOR="true"
if [[ -n "${CLICOLOR_FORCE+defined}" && "${CLICOLOR_FORCE}" != "0" ]]; then
USE_COLOR="true"
elif [[ -n "${NO_COLOR+defined}" || "${CLICOLOR}" = "0" || ! -t 1 ]]; then
USE_COLOR="false"
fi
readonly USE_COLOR
# Select Graphic Rendition codes
if [[ "${USE_COLOR}" = "true" ]]; then
# KISS and use codes rather than tput, avoid dealing with missing tput or TERM.
readonly SGR_RESET="\033[0m"
readonly SGR_FAINT="\033[2m"
readonly SGR_RED="\033[31m"
readonly SGR_CYAN="\033[36m"
else
readonly SGR_RESET=
readonly SGR_FAINT=
readonly SGR_RED=
readonly SGR_CYAN=
fi
#
# set_arch <arch> to override $(uname -a)
#
@ -178,7 +208,7 @@ EOF
}
err_no_installed_print_help() {
printf "\n \033[31mError: no installed version\033[0m\n"
printf "\n ${SGR_RED}Error: no installed version${SGR_RESET}\n"
display_help
exit 1
}
@ -251,9 +281,9 @@ display_versions_with_selected() {
echo
for version in $(versions_paths); do
if test "$version" = "$selected"; then
printf " \033[36mο\033[0m %s\033[0m\n" "$version"
printf " ${SGR_CYAN}ο${SGR_RESET} %s\n" "$version"
else
printf " \e[2m%s\e[22m\n" "$version"
printf " ${SGR_FAINT}%s${SGR_RESET}\n" "$version"
fi
done
echo
@ -705,12 +735,12 @@ display_remote_versions() {
local bin="${BINS[$DEFAULT]}"
for v in $versions; do
if test "$active" = "$bin/$v"; then
printf " \033[36mο\033[0m %s \033[0m\n" "$v"
printf " ${SGR_CYAN}ο${SGR_RESET} %s\n" "$v"
else
if test -d "$BASE_VERSIONS_DIR/$bin/$v"; then
printf " %s \033[0m\n" "$v"
printf " %s\n" "$v"
else
printf " \e[2m%s\e[22m\n" "$v"
printf " ${SGR_FAINT}%s${SGR_RESET}\n" "$v"
fi
fi
done
@ -767,6 +797,8 @@ uninstall_installed() {
# Handle arguments.
#
[[ "${QUIET}" = "true" ]] && set_quiet
if test $# -eq 0; then
test -z "$(versions_paths)" && err_no_installed_print_help
display_versions