First commit
This commit is contained in:
commit
e1f41d37fa
3 changed files with 121 additions and 0 deletions
32
README.md
Normal file
32
README.md
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
BASH message display library
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
A simple library for display text (bold, underline, color) on screen. I use it
|
||||||
|
for my script to make output more eye candy.
|
||||||
|
|
||||||
|
## Quick example
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#import library
|
||||||
|
source /message/message.sh
|
||||||
|
|
||||||
|
#call msg()
|
||||||
|
msg "green" "This is a **formated** message"
|
||||||
|
```
|
||||||
|
|
||||||
|
## more function
|
||||||
|
|
||||||
|
This little library contain some helper function with pre-selected color. These
|
||||||
|
function automaticaly add `\n` caracter at the end of the message.
|
||||||
|
|
||||||
|
* `error()` : display message in red with ERROR: before the text.If variable
|
||||||
|
`EXIT_ON_ERROR` equal 1, calling this function terminate the script with
|
||||||
|
`$ERROR_CODE`
|
||||||
|
* `warning()` : display message in yellow with WARNING: before the text
|
||||||
|
* `debu()` : display message only if `DEBUG` variable contain `1`, color is
|
||||||
|
cyan and display DEBUG: before message
|
||||||
|
|
||||||
|
## Licence
|
||||||
|
|
||||||
|
Do what you want and feel free to send me a message or offer me a beer if you
|
||||||
|
come near Bordeaux - France.
|
23
example/example.sh
Executable file
23
example/example.sh
Executable file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# message librairy example
|
||||||
|
|
||||||
|
source ../message.sh
|
||||||
|
EXIT_ON_ERROR=1
|
||||||
|
|
||||||
|
debug "This message wille not be displayed because debug is not activated"
|
||||||
|
|
||||||
|
msg "\n**__welcome to message lib example__**\n"
|
||||||
|
msg "red" "\nMessages "
|
||||||
|
msg "green" "can "
|
||||||
|
msg "yellow" "be "
|
||||||
|
msg "blue" "colored"
|
||||||
|
msg " **bold** and __underline__ \n\n"
|
||||||
|
|
||||||
|
warning "Text with **a warning**"
|
||||||
|
msg "and when DEBUG=1\n"
|
||||||
|
|
||||||
|
DEBUG=1
|
||||||
|
debug "with debug message"
|
||||||
|
|
||||||
|
error "Or a **real** and __serious__ error that exit script when EXIT_ON_ERROR=1"
|
66
message.sh
Normal file
66
message.sh
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
#/bin/bash
|
||||||
|
#
|
||||||
|
# message ()
|
||||||
|
# ----------
|
||||||
|
#
|
||||||
|
# External library to display colored messages
|
||||||
|
#
|
||||||
|
|
||||||
|
DEBUG=0
|
||||||
|
EXIT_ON_ERROR=0
|
||||||
|
ERROR_CODE=10
|
||||||
|
|
||||||
|
DATE_FORMAT=""
|
||||||
|
|
||||||
|
msg () {
|
||||||
|
declare -A attr
|
||||||
|
local c="default"
|
||||||
|
|
||||||
|
#foreground colors
|
||||||
|
attr[default]="\e[39m"
|
||||||
|
attr[red]="\e[31m"
|
||||||
|
attr[green]="\e[32m"
|
||||||
|
attr[yellow]="\e[33m"
|
||||||
|
attr[blue]="\e[34m"
|
||||||
|
attr[magenta]="\e[35m"
|
||||||
|
attr[cyan]="\e[36m"
|
||||||
|
|
||||||
|
#formatting
|
||||||
|
attr[reset]="\e[0m"
|
||||||
|
attr[bold]="\e[1m"
|
||||||
|
attr[r_bold]="\e[22m"
|
||||||
|
attr[underl]="\e[4m"
|
||||||
|
attr[r_underl]="\e[24m"
|
||||||
|
if [[ $1 =~ ^(red|green|yellow|blue|magenta|cyan)$ ]]
|
||||||
|
then
|
||||||
|
c=$1
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
c="default"
|
||||||
|
fi
|
||||||
|
#printf "Select color %s with numero %s\n" $c ${attr[$c]}
|
||||||
|
#variables
|
||||||
|
local message=$(echo "$@" | sed -E "
|
||||||
|
s/\*\*([^\*\*]*)\*\*/\\${attr[bold]}\1\\${attr[r_bold]}/g;
|
||||||
|
s/__([^__]*)__/\\${attr[underl]}\1\\${attr[r_underl]}/g
|
||||||
|
")
|
||||||
|
printf "${attr[$c]}${message}${attr[reset]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
error (){
|
||||||
|
msg "red" "ERROR : $@\n"
|
||||||
|
[[ $EXIT_ON_ERROR -eq 1 ]] && exit $ERROR_CODE
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
warning () {
|
||||||
|
msg "yellow" "WARNING : $@\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
debug(){
|
||||||
|
if [ $DEBUG -eq 1 ]
|
||||||
|
then
|
||||||
|
local message=$(echo "$@")
|
||||||
|
msg "cyan" "DEBUG : ${message}\n"
|
||||||
|
fi
|
||||||
|
}
|
Reference in a new issue