mercuriev / bash-colors Goto Github PK
View Code? Open in Web Editor NEWThe library defines verbal constants and functions for terminal colors.
The library defines verbal constants and functions for terminal colors.
The use of $2
in bash_colors.sh
causes an unbound variable error "bash: $2: unbound variable"
when using the following bash options:
set -o errexit
set -o nounset
set -o pipefail
set -e
To work around this I've updated the code, and attempted to make create a pull request, but I lack the permissions to do so.
Diff vs Master:
diff --git a/bash_colors.sh b/bash_colors.sh
index e8e66cf..7f86d5c 100755
--- a/bash_colors.sh
+++ b/bash_colors.sh
@@ -3,6 +3,11 @@
# Constants and functions for terminal colors.
# Author: Max Tsepkov <[email protected]>
+set -o errexit
+set -o nounset
+set -o pipefail
+set -e
+
CLR_ESC="\033["
# All these varibles has a function with the same name, but in lower case.
@@ -40,13 +45,13 @@ CLR_WHITEB=47 # set white background
# Ex: console_escape foobar $CLR_RED $CLR_BOLD
function clr_escape
{
- local result="$1"
- until [ -z "$2" ]; do
- if ! [ $2 -ge 0 -a $2 -le 47 ] 2>/dev/null; then
- echo "clr_escape: argument \"$2\" is out of range" >&2 && return 1
- fi
- result="${CLR_ESC}${2}m${result}${CLR_ESC}${CLR_RESET}m"
- shift || break
+ local result="$1" w="$2"
+ until [ -z "$w" ]; do
+ if ! [ "$w" -ge 0 -a "$w" -le 47 ] 2>/dev/null; then
+ echo "clr_escape: argument \"$w\" is out of range" >&2 && return 1
+ fi
+ result="${CLR_ESC}${w}m${result}${CLR_ESC}${CLR_RESET}m"
+ shift || break
done
echo -e "$result"
@@ -61,7 +66,7 @@ function clr_bold { clr_escape "$1" $CLR_BOLD; }
function clr_bright { clr_escape "$1" $CLR_BRIGHT; }
function clr_underscore { clr_escape "$1" $CLR_UNDERSCORE; }
function clr_reverse { clr_escape "$1" $CLR_REVERSE; }
-function clr_black { clr_escape "$1" $CLR_BLANK; }
+function clr_black { clr_escape "$1" $CLR_BLACK; }
function clr_red { clr_escape "$1" $CLR_RED; }
function clr_green { clr_escape "$1" $CLR_GREEN; }
function clr_brown { clr_escape "$1" $CLR_BROWN; }
@@ -100,7 +105,7 @@ function clr_dump
echo
clr_bold " Code Function Variable"
echo \
-' 0 clr_reset $CLR_RESET
+" 0 clr_reset $CLR_RESET
1 clr_bold $CLR_BOLD
2 clr_bright $CLR_BRIGHT
4 clr_underscore $CLR_UNDERSCORE
@@ -123,6 +128,6 @@ function clr_dump
45 clr_magentab $CLR_MAGENTAB
46 clr_cyanb $CLR_CYANB
47 clr_whiteb $CLR_WHITEB
-'
+"
}
Reference: https://github.com/maxtsepkov/bash_colors/blob/v1.0/bash_colors.sh#L44-L48
Using http://www.shellcheck.net/ for code validation.
Hi,
Would you like to extend the clr_
functions to wrap escape sequences in \[
- \]
pairs?
Could I enter an escape sequence and not do a reset (tput sgr0
) at the end?
Hello,
Shellcheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell scripts.
Shellcheck stable version:
$ shellcheck --version
ShellCheck - shell script analysis tool
version: 0.7.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net
Output:
$ shellcheck bash_colors.sh
In bash_colors.sh line 5:
if [[ "$BASH_SOURCE" == "$0" ]]; then
^----------^ SC2128: Expanding an array without an index only gives the first element.
In bash_colors.sh line 81:
if [ -n "$ARG" ] && fn_exists $ARG; then
^--^ SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
if [ -n "$ARG" ] && fn_exists "$ARG"; then
In bash_colors.sh line 83:
CLR_STACK=$($ARG "$CLR_STACK" $CLR_SWITCHES)
^-----------^ SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
CLR_STACK=$($ARG "$CLR_STACK" "$CLR_SWITCHES")
In bash_colors.sh line 90:
clr_escape "$CLR_STACK" $1;
^-- SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
clr_escape "$CLR_STACK" "$1";
In bash_colors.sh line 99:
if ! [ $2 -ge 0 -a $2 -le 47 ] 2>/dev/null; then
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
^-- SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
if ! [ "$2" -ge 0 -a "$2" -le 47 ] 2>/dev/null; then
In bash_colors.sh line 157:
' 0 clr_reset $CLR_RESET
^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.
For more information:
https://www.shellcheck.net/wiki/SC2128 -- Expanding an array without an ind...
https://www.shellcheck.net/wiki/SC2166 -- Prefer [ p ] && [ q ] as [ p -a q...
https://www.shellcheck.net/wiki/SC2016 -- Expressions don't expand in singl...
Misspelling of clr_escape in this function:
function clr_defaultb { clr_esacpe "$1" $CLR_DEFAULTB; }
Would you mind adding (or having me add) a license?
For this kind of code I would recommend the UNLICENSE otherwise https://choosealicense.com can guide you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.