aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/lib-bash/color-echo.sh
diff options
context:
space:
mode:
Diffstat (limited to 'home-manager/modules/lib-bash/color-echo.sh')
-rw-r--r--home-manager/modules/lib-bash/color-echo.sh37
1 files changed, 37 insertions, 0 deletions
diff --git a/home-manager/modules/lib-bash/color-echo.sh b/home-manager/modules/lib-bash/color-echo.sh
new file mode 100644
index 00000000000..ef708b29c4d
--- /dev/null
+++ b/home-manager/modules/lib-bash/color-echo.sh
@@ -0,0 +1,37 @@
+# The check for terminal output and color support is heavily inspired
+# by https://unix.stackexchange.com/a/10065.
+
+function setupColors() {
+ normalColor=""
+ errorColor=""
+ warnColor=""
+ noteColor=""
+
+ # Check if stdout is a terminal.
+ if [[ -t 1 ]]; then
+ # See if it supports colors.
+ local ncolors
+ ncolors=$(tput colors)
+
+ if [[ -n "$ncolors" && "$ncolors" -ge 8 ]]; then
+ normalColor="$(tput sgr0)"
+ errorColor="$(tput bold)$(tput setaf 1)"
+ warnColor="$(tput setaf 3)"
+ noteColor="$(tput bold)$(tput setaf 6)"
+ fi
+ fi
+}
+
+setupColors
+
+function errorEcho() {
+ echo "${errorColor}$*${normalColor}"
+}
+
+function warnEcho() {
+ echo "${warnColor}$*${normalColor}"
+}
+
+function noteEcho() {
+ echo "${noteColor}$*${normalColor}"
+}