aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormerakor <cem@ckyln.com>2021-08-10 23:01:32 +0000
committermerakor <cem@ckyln.com>2021-08-10 23:01:32 +0000
commitf9999114eb726ab9cdbeda8a830b170fd9d3cd07 (patch)
tree83950c6f3c87b04f862757f723c75a547b4e6f10 /src
parent181fbd8fb3c341aee9bab44fabbfc52175f66dd9 (diff)
downloadcpt-f9999114eb726ab9cdbeda8a830b170fd9d3cd07.tar.gz
cpt-lib: add --color option to global parser
FossilOrigin-Name: 0fad6b9563db7abf93b711993929b8fc97da161072b2401c28c085a8c216d56c
Diffstat (limited to 'src')
-rw-r--r--src/cpt-lib.in20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/cpt-lib.in b/src/cpt-lib.in
index adfcb0c..a882594 100644
--- a/src/cpt-lib.in
+++ b/src/cpt-lib.in
@@ -66,6 +66,15 @@ die() {
exit 1
}
+colors_enabled() {
+ case ${CPT_COLOR:=auto} in
+ auto) [ -t 1 ] ;;
+ 1|always) return 0 ;;
+ 0|never) return 1 ;;
+ *) die "Unknown color value: '$CPT_COLOR'"
+ esac
+}
+
trap_set() {
# Function to set the trap value.
case ${1:-cleanup} in
@@ -444,6 +453,7 @@ global_options() {
flag CPT_FORCE -f --force $_h init:@export -- "Force operation"
flag CPT_PROMPT -y --no-prompt on:0 off:0 $_h init:@export -- "Do not prompt for confirmation"
param CPT_ROOT --root $_h init:@export -- "Use an alternate root directory"
+ param CPT_COLOR --color $_h init:@export -- "Colorize the output [default:auto]"
disp :usage -h --help $_c -- "Show this help message"
disp :version -v --version $_c -- "Print version information"
flag CPT_VERBOSE --verbose $_h init:@export -- "Be more verbose"
@@ -2226,13 +2236,9 @@ create_cache() {
# Set a value for CPT_COMPRESS if it isn't set.
: "${CPT_COMPRESS:=gz}"
- # Unless being piped or the user specifically doesn't want colors, set
- # colors. This can of course be overriden if the user specifically want
- # colors during piping.
- if { [ "$CPT_COLOR" != 0 ] && [ -t 1 ] ;} || [ "$CPT_COLOR" = 1 ]; then
- colory="\033[1;33m" colorb="\033[1;34m" colre="\033[m"
- fi
-
+ # Set colors if they are to be enabled.
+ # shellcheck disable=2034
+ colors_enabled && colory="\033[1;33m" colorb="\033[1;34m" colre="\033[m" colbold="\033[1m"
}
# If the library is being called with its own name, run arguments.