commit c042ce279fa708a281b7d1aaef42f37f049200d4 Author: fabs Date: Wed Dec 9 20:09:01 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c47b38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.swp +*.swo +*~ + diff --git a/README b/README new file mode 100644 index 0000000..4e69d84 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +A collection of useful bash "libraries" + +- bashcols: + Allows easy colored output. + diff --git a/bashcols b/bashcols new file mode 100755 index 0000000..bb7fe8f --- /dev/null +++ b/bashcols @@ -0,0 +1,73 @@ +#!/bin/bash + +eval "reset_esc=\"\\e[0m\"" + +# applies the given color id, executes the command, then resets the color +function colout() { + if [[ $# -eq 0 ]]; then + echo "usage: colout " + return + fi + + local color=$1 + shift + tput setaf $color + "$@" + tput sgr0 +} + +# defines a new color function +# and a variable named _colid that contains the actual value for the color function +function defcol() { + if [[ $# -ne 2 ]]; then + echo "usage: defcol " + return + fi + + local name="$1" + local color="$2" + + eval "${name}_colid=$color" + eval "${name}_esc=\"\\e[38;5;${color}m\"" + eval "${name}_escbg=\"\\e[48;5;${color}m\"" + eval "function $name { colout $color \"\$@\"; }" + +} + +# displays all 256 colors +function dispcollist() { + local width=${1:-16} + local idx=0 + for colid in `seq 1 256`; do + colout $colid printf "%3s " $colid + idx=$(($idx+1)) + if [[ $(($idx % $width)) -eq 0 ]]; then + printf "\n" + fi + done +} + +function colthis { + local name="$1" + shift + eval "printf \"\${${name}_esc}$@$reset_esc\"" +} + +# default colors +defcol red 1 +defcol green 2 +defcol brown 3 +defcol blue 4 +defcol magenta 5 +defcol cyan 6 +defcol white 7 +defcol grey 8 +defcol redl 9 +defcol greenl 10 +defcol yellow 11 +defcol bluel 12 +defcol magenta 13 +defcol cyanl 14 +defcol whitel 15 +defcol black 16 +