diff options
author | Aaron Ball <nullspoon@oper.io> | 2018-03-07 13:39:28 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2018-03-07 13:39:28 -0700 |
commit | 86fc3cc21a70513b60c24516caf423dc149319d4 (patch) | |
tree | 8c873670be428937a01ca4dec8a5e3ceb2afc768 | |
parent | 5f740ab6e8c836964497a6c0231e47ad28a7bc53 (diff) | |
download | pkgself-86fc3cc21a70513b60c24516caf423dc149319d4.tar.gz pkgself-86fc3cc21a70513b60c24516caf423dc149319d4.tar.xz |
Added libcolorize
This library provides the 'colorize' function. This function will color
any text from stdin with the specified escape sequence, resetting the
color changes at line end.
Useful for coloring output of commands by piping their stdout or stderr
to this function, specifying to color the output something unique.
-rwxr-xr-x | install-header.sh | 1 | ||||
-rw-r--r-- | libinstall/colorize.sh | 60 |
2 files changed, 61 insertions, 0 deletions
diff --git a/install-header.sh b/install-header.sh index 069a8c9..200e9c5 100755 --- a/install-header.sh +++ b/install-header.sh @@ -61,6 +61,7 @@ main() { source libinstall/template.sh source libinstall/ensure.sh + source libinstall/colorize.sh source run.sh diff --git a/libinstall/colorize.sh b/libinstall/colorize.sh new file mode 100644 index 0000000..69d9e11 --- /dev/null +++ b/libinstall/colorize.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# Pkgself builds self-extracting installers +# Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +set -e + +# +# Useful color codes +# +# Normal colors +export CRED="$(printf '\033[0;31m')" +export CGREEN="$(printf '\033[0;32m')" +export CYELLOW="$(printf '\033[0;33m')" +export CBLUE="$(printf '\033[0;34m')" +export CMAGENTA="$(printf '\033[0;35m')" +export CCYAN="$(printf '\033[0;36m')" +export CGREY="$(printf '\033[0;37m')" +export CWHITE="$(printf '\033[0;38m')" +export CRESET="$(printf '\033[0m')" +# Bold colors +export CBRED="$(printf '\033[1;31m')" +export CBGREEN="$(printf '\033[1;32m')" +export CBYELLOW="$(printf '\033[1;33m')" +export CBBLUE="$(printf '\033[1;34m')" +export CBMAGENTA="$(printf '\033[1;35m')" +export CBCYAN="$(printf '\033[1;36m')" +export CBGREY="$(printf '\033[1;37m')" +export CBWHITE="$(printf '\033[1;38m')" + + +# colorize: +# @color Escape sequence to color text with +# +# Colors the text from stdin with the specified color escape sequence. +# NOTE: Resets escape sequence at the end of each line +# +# Returns 0 always +colorize() { + local color="${1}" + # If the tty is a terminal, support color + if [ -t 1 ]; then + sed -e "s/^/${color}/" -e "s/$/${CRESET}/" + return 0 + fi + # Tty is not a teriminal, print without color support + cat +} |