add color.load function

This commit is contained in:
a-sansara 2018-12-22 15:11:56 +01:00
parent 862de41e05
commit 72d2a21b8d
3 changed files with 127 additions and 39 deletions

View File

@ -8,7 +8,7 @@ bes-color is a small bash bes display true colors utility library
add bes-color to your require section in bes.ini add bes-color to your require section in bes.ini
``` ```
[require] [require]
bes.color = 1.0 bes.color = 1.1
``` ```
then do then do
``` ```
@ -42,12 +42,63 @@ predefined colors :
use special var *$Coff* to disable previous color effects. use special var *$Coff* to disable previous color effects.
ex :
```
echo -e "${Ctitle} mytitle${Coff}"
```
### use config file to load colors
```
color.load "/your/path/colors.ini"
```
without parameter, `color.load` function try to load
the default location `/usr/share/bes/colors.ini`
contents of colors.ini file :
```
[set]
# background foreground
# R G B R G B
head = 53 114 160 195 223 255
headsep = 53 114 160 252 212 102
[bg]
# background
# R G B
one = 63 172 138
fail = 172 63 85
[fg]
# foreground
# R G B
title = 133 92 181
headline = 22 74 133
sep = 80 80 80
err = 194 48 64
val = 255 175 95
key = 40 168 134
action = 106 183 241
symbol = 255 175 95
item = 92 147 181
usa = 255 172 0
spe = 255 214 166
opt = 94 215 255
com = 175 135 175
text = 0 132 101
meta = 39 100 170
```
in colors.ini config file, var names are without 'C' prefix
### set foreground ### set foreground
``` ```
# R G B # R G B
color.fg "Cmyfgcolor" 185 69 69 color.fg "myfgcolor" 185 69 69
echo -e "$Cmyfgcolor foreground $Coff" echo -e "$Cmyfgcolor foreground $Coff"
``` ```
@ -56,7 +107,7 @@ echo -e "$Cmyfgcolor foreground $Coff"
``` ```
# R G B # R G B
color.bg "Cmybgcolor" 69 185 185 color.bg "mybgcolor" 69 185 185
echo -e "$Cmybgcolor background $Coff" echo -e "$Cmybgcolor background $Coff"
``` ```
@ -74,7 +125,7 @@ echo -e "$Cmyfgcolor foreground $Cmybgcolor backgound $Coff"
``` ```
# background foreground # background foreground
# R G B R G B # R G B R G B
color.set "Cmycolor" 69 185 185 185 69 69 color.set "mycolor" 69 185 185 185 69 69
echo -e "$Cmycolor cool $Coff" echo -e "$Cmycolor cool $Coff"
``` ```

View File

@ -1,10 +1,13 @@
[project] [project]
vendor = bes vendor = bes
name = color name = color
version = 1.0 version = 1.1
license = "GNU GPL v3" license = "GNU GPL v3"
author = a-Sansara author = a-Sansara
type = library type = library
homepage = "https://git.pluie.org/meta-tech/bes-color" homepage = "https://git.pluie.org/meta-tech/bes-color"
description = "bash bes display utility true colors library" description = "bash bes display utility true colors library"
keywords = "bash, bes, color" keywords = "bash, bes, color"
[require]
bes.ini = 1.1

View File

@ -1,4 +1,3 @@
#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# #
# @author a-Sansara - https://git.pluie.org/meta-tech/bes-color # @author a-Sansara - https://git.pluie.org/meta-tech/bes-color
@ -10,52 +9,67 @@
function bes.color.boot () { function bes.color.boot () {
BES_NOCOLOR=${BES_NOCOLOR:-0} BES_NOCOLOR=${BES_NOCOLOR:-0}
if [ "$BES_NOCOLOR" -eq 0 ]; then if [ "$BES_NOCOLOR" -eq 0 ]; then
color.load
if [ $? -eq 1 ]; then
# by convention all colors begins with 'C' uppercase # by convention all colors begins with 'C' uppercase
# followed by a name in lowercase # followed by a name in lowercase
# but in this calls you dont need to prefix names
#
# background foreground # background foreground
# R G B R G B # R G B R G B
color.set "Chead" 53 114 160 195 223 255 color.set "head" 53 114 160 195 223 255
color.set "Cheadsep" 53 114 160 252 212 102 color.set "headsep" 53 114 160 252 212 102
# background # background
# R G B # R G B
color.bg "Cdone" 63 172 138 color.bg "done" 63 172 138
color.bg "Cfail" 172 63 85 color.bg "fail" 172 63 85
# foreground # foreground
# R G B # R G B
color.fg "Ctitle" 133 92 181 color.fg "title" 133 92 181
color.fg "Cheadline" 22 74 133 color.fg "headline" 22 74 133
color.fg "Csep" 80 80 80 color.fg "sep" 80 80 80
color.fg "Cerr" 194 48 64 color.fg "err" 194 48 64
color.fg "Cval" 255 175 95 color.fg "val" 255 175 95
color.fg "Ckey" 40 168 134 color.fg "key" 40 168 134
color.fg "Caction" 106 183 241 color.fg "action" 106 183 241
color.fg "Csymbol" 255 175 95 color.fg "symbol" 255 175 95
color.fg "Citem" 92 147 181 color.fg "item" 92 147 181
color.fg "Cusa" 255 172 0 color.fg "usa" 255 172 0
color.fg "Cspe" 255 214 166 color.fg "spe" 255 214 166
color.fg "Copt" 94 215 255 color.fg "opt" 94 215 255
color.fg "Ccom" 175 135 175 color.fg "com" 175 135 175
color.fg "Ctext" 0 132 101 color.fg "text" 0 132 101
color.fg "Cmeta" 39 100 170 color.fg "meta" 39 100 170
Coff="\\033[m" Coff="\\033[m"
echo -e "$Cerr warning ${Coff}cannot load colors config file"
fi
fi fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function color.fg() function color.fg()
{ {
local -n var=$1 local s=${1}
if [ ${s:0:1} = "C" ]; then s=${s:1}; fi
s=${s,,}
local -n var=C$1
var="\033[1;38;2;$2;$3;$4m" var="\033[1;38;2;$2;$3;$4m"
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function color.bg() function color.bg()
{ {
local -n var=$1 local s=${1}
if [ ${s:0:1} = "C" ]; then s=${s:1}; fi
s=${s,,}
local -n var=C$1
var="\033[1;48;2;$2;$3;$4m" var="\033[1;48;2;$2;$3;$4m"
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function color.set() function color.set()
{ {
local -n var=$1 local s=${1}
if [ ${s:0:1} = "C" ]; then s=${s:1}; fi
s=${s,,}
local -n var=C$1
var="\033[1;48;2;$2;$3;$4;1;38;2;$5;$6;$7m" var="\033[1;48;2;$2;$3;$4;1;38;2;$5;$6;$7m"
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -96,4 +110,24 @@ function color.env () {
var=${!v} var=${!v}
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function color.load () {
local file=${1:-/usr/share/bes/colors.ini}
if [ -f "$file" ]; then
local prefix="bes_colors"
bes.ini "$file" -p "$prefix" -b 1
for name in ${bes_colors_ALL_VARS}; do
key="${name:${#prefix}+1}"
if [ "${key:0:2}" = "fg" ]; then
color.fg "${key:3}" ${!name}
elif [ "${key:0:2}" == "bg" ]; then
color.bg "${key:3}" ${!name}
elif [ "${key:0:3}" == "set" ]; then
color.set "${key:4}" ${!name}
fi
done
else
return 1
fi
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bes.color.boot bes.color.boot