refactor nearly everything: use section, subsection and paragraph with easier color access log functionality AND the new try command
This commit is contained in:
parent
29cb17026c
commit
92e06f1e4c
199
bsf
199
bsf
@ -1,9 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Bash Framework
|
||||
# Bash Framework bsf
|
||||
# Some useful bash functions to make things easier.
|
||||
#
|
||||
# Copyright (C) 2017-2019 willipink.eu
|
||||
# Copyright (C) 2017-2020 willipink.eu
|
||||
# Author Moritz Münch moritzmuench@mailbox.org
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
@ -20,107 +20,103 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
|
||||
# Variables
|
||||
|
||||
# supress colors if used to write to a log
|
||||
LOG=True
|
||||
for arg in $*; do
|
||||
if [[ "$arg" == "--log" ]]; then
|
||||
LOG=False
|
||||
for arg in "$@"; do
|
||||
if [[ "$arg" == "--log" ]]; then
|
||||
LOG=True
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
## colors (use ANSI escape codes)
|
||||
if [[ "$LOG" == True ]]; then
|
||||
normal () { printf '\033[0m' ; }
|
||||
red () { printf '\033[0;31m' ; }
|
||||
green () { printf '\033[0;32m' ; }
|
||||
black () { printf '\033[0;30m' ; }
|
||||
brown () { printf '\033[0;33m' ; }
|
||||
orange () { printf '\033[0;33m' ; }
|
||||
blue () { printf '\033[0;34m' ; }
|
||||
purple () { printf '\033[0;35m' ; }
|
||||
cyan () { printf '\033[0;36m' ; }
|
||||
gray () { printf '\033[0;37m' ; }
|
||||
grey () { printf '\033[1;30m' ; }
|
||||
lred () { printf '\033[1;31m' ; }
|
||||
lgreen () { printf '\033[1;32m' ; }
|
||||
yellow () { printf '\033[1;33m' ; }
|
||||
lblue () { printf '\033[1;34m' ; }
|
||||
lpurple (){ printf '\033[1;35m' ; }
|
||||
lcyan () { printf '\033[1;36m' ; }
|
||||
white () { printf '\033[1;37m' ; }
|
||||
else
|
||||
normal () { return; }
|
||||
red () { return; }
|
||||
green () { return; }
|
||||
black () { return; }
|
||||
brown () { return; }
|
||||
orange () { return; }
|
||||
blue () { return; }
|
||||
purple () { return; }
|
||||
cyan () { return; }
|
||||
gray () { return; }
|
||||
grey () { return; }
|
||||
lred () { return; }
|
||||
lgreen () { return; }
|
||||
yellow () { return; }
|
||||
lblue () { return; }
|
||||
lpurple (){ return; }
|
||||
lcyan () { return; }
|
||||
white () { return; }
|
||||
# colors (ANSI escape codes)
|
||||
if [[ "$LOG" == False ]]; then
|
||||
nor='\033[0m'
|
||||
red='\033[0;31m'
|
||||
gre='\033[0;32m'
|
||||
bla='\033[0;30m'
|
||||
bro='\033[0;33m'
|
||||
ora='\033[0;33m'
|
||||
blu='\033[0;34m'
|
||||
pur='\033[0;35m'
|
||||
cya='\033[0;36m'
|
||||
gra='\033[0;37m'
|
||||
gre='\033[1;30m'
|
||||
lre='\033[1;31m'
|
||||
lgr='\033[1;32m'
|
||||
yel='\033[1;33m'
|
||||
lbl='\033[1;34m'
|
||||
lpu='\033[1;35m'
|
||||
lcy='\033[1;36m'
|
||||
whi='\033[1;37m'
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# Functions
|
||||
say () {
|
||||
# Print something to stdout.
|
||||
|
||||
# Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html
|
||||
|
||||
# @param what to print
|
||||
|
||||
printf %s\\n "$*"
|
||||
return 0
|
||||
}
|
||||
# Styling
|
||||
section_prefix=' ::: '
|
||||
subsection_prefix=' :: '
|
||||
paragraph_prefix=' : '
|
||||
section_color=$lbl
|
||||
subsection_color=$ora
|
||||
paragraph_color=$nor
|
||||
|
||||
|
||||
sayn () {
|
||||
# Like say without linebreak.
|
||||
|
||||
# @param what to print
|
||||
|
||||
printf %s "$*"
|
||||
return 0
|
||||
}
|
||||
|
||||
sayc() {
|
||||
# quick styler, usage "style *color*"
|
||||
|
||||
if [[ "$1" == "" ]]; then
|
||||
blue
|
||||
# Formatting functions
|
||||
print () {
|
||||
# Print something safely to stdout
|
||||
# Usage: print [n] stuff to print
|
||||
# n will print a newline
|
||||
for arg in "$@"; do
|
||||
if [[ "$arg" == 'n' ]]; then
|
||||
printf \\n
|
||||
else
|
||||
$1
|
||||
echo -ne "$arg"
|
||||
fi
|
||||
sayn " ::: "
|
||||
shift
|
||||
say "$*"
|
||||
normal
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
saycn() {
|
||||
$1
|
||||
sayn " ::: "
|
||||
shift
|
||||
sayn "$*"
|
||||
normal
|
||||
return 0
|
||||
|
||||
section() {
|
||||
# print a styled section
|
||||
# initialize $SECTION to 0 or increment by 1
|
||||
if [[ -z ${SECTION+x} ]]; then # +x seems to be a trick to also catch empty strings like SECTION=''
|
||||
SECTION=1
|
||||
else
|
||||
SECTION=$(($SECTION+1))
|
||||
fi
|
||||
print n n n "$section_color$section_prefix${SECTION}. $*$nor"
|
||||
}
|
||||
|
||||
|
||||
subsection() {
|
||||
# print a styled subsection
|
||||
# initialize $SUBSECTION to 0 or increment by 1
|
||||
if [[ -z ${SUBSECTION+x} ]]; then # +x seems to be a trick to also catch empty strings like SECTION=''
|
||||
SUBSECTION=1
|
||||
else
|
||||
SUBSECTION=$(($SUBSECTION+1))
|
||||
fi
|
||||
print n n "$subsection_color$subsection_prefix${SECTION}.${SUBSECTION} $*$nor"
|
||||
}
|
||||
|
||||
|
||||
paragraph() {
|
||||
# print a styled subsection
|
||||
print n "$paragraph_color$paragraph_prefix$*$nor"
|
||||
}
|
||||
# examples
|
||||
# paragraph lol
|
||||
# paragraph next
|
||||
# section big
|
||||
# section relly big
|
||||
# subsection a
|
||||
# paragraph 1
|
||||
# subsection b
|
||||
# paragraph 2
|
||||
|
||||
|
||||
# Helper functions
|
||||
root () {
|
||||
# Exit immediately if user is not root
|
||||
# Usage: root
|
||||
@ -132,35 +128,30 @@ root () {
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
ok() {
|
||||
# Check if last call had errors. If so, print error message and exit.
|
||||
# Usage: ok <command>
|
||||
# Usage: ok
|
||||
|
||||
if [[ "$?" -ne 0 ]]; then
|
||||
sayc red "ERROR, aborting."
|
||||
print $red ' ... ERROR, aborting!' $nor n
|
||||
exit 1
|
||||
else
|
||||
sayc green "$0 ... done"
|
||||
print " ...$lgr done$nor" n
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
try() {
|
||||
# Do a job and check if it ran successfull.
|
||||
# Usage: try <command>
|
||||
# Print a message, execute a command silent print errors to stdout
|
||||
# and abort the command goes wrong
|
||||
# Usage: try message command [command2 ...]
|
||||
|
||||
$* && ok
|
||||
}
|
||||
|
||||
trycm() {
|
||||
# Do a job, write a message, color it and check if it ran successfull.
|
||||
# Usage: trycmd <color> <message> <command>
|
||||
|
||||
if [[ "$2" != "" ]]; then
|
||||
say
|
||||
sayc "$1" "$2"
|
||||
fi
|
||||
shift && shift
|
||||
saycn normal "$*"
|
||||
try $*
|
||||
message="$1"
|
||||
shift
|
||||
paragraph "$pur${message}$nor: $*"
|
||||
$* 1> /dev/null && ok
|
||||
}
|
||||
# examples
|
||||
# try testnachricht lsblk
|
||||
|
251
qgds
Normal file
251
qgds
Normal file
@ -0,0 +1,251 @@
|
||||
[1mdiff --git a/bsf b/bsf[m
|
||||
[1mindex 83cf953..6f55713 100644[m
|
||||
[1m--- a/bsf[m
|
||||
[1m+++ b/bsf[m
|
||||
[36m@@ -1,9 +1,9 @@[m
|
||||
#!/bin/bash[m
|
||||
[m
|
||||
[31m-# Bash Framework[m
|
||||
[32m+[m[32m# Bash Framework bsf[m
|
||||
# Some useful bash functions to make things easier.[m
|
||||
#[m
|
||||
[31m-# Copyright (C) 2017-2019 willipink.eu[m
|
||||
[32m+[m[32m# Copyright (C) 2017-2020 willipink.eu[m
|
||||
# Author Moritz Münch moritzmuench@mailbox.org[m
|
||||
#[m
|
||||
# This program is free software: you can redistribute it and/or modify[m
|
||||
[36m@@ -20,107 +20,103 @@[m
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.[m
|
||||
[m
|
||||
[m
|
||||
[31m-[m
|
||||
[31m-# Variables[m
|
||||
[31m-[m
|
||||
# supress colors if used to write to a log[m
|
||||
[31m-LOG=True[m
|
||||
[31m-for arg in $*; do[m
|
||||
[32m+[m[32mLOG=False[m
|
||||
[32m+[m[32mfor arg in "$@"; do[m
|
||||
if [[ "$arg" == "--log" ]]; then[m
|
||||
[31m- LOG=False[m
|
||||
[32m+[m[32m LOG=True[m
|
||||
fi[m
|
||||
done[m
|
||||
[m
|
||||
[31m-[m
|
||||
[31m-## colors (use ANSI escape codes)[m
|
||||
[31m-if [[ "$LOG" == True ]]; then[m
|
||||
[31m- normal () { printf '\033[0m' ; }[m
|
||||
[31m- red () { printf '\033[0;31m' ; }[m
|
||||
[31m- green () { printf '\033[0;32m' ; }[m
|
||||
[31m- black () { printf '\033[0;30m' ; }[m
|
||||
[31m- brown () { printf '\033[0;33m' ; }[m
|
||||
[31m- orange () { printf '\033[0;33m' ; }[m
|
||||
[31m- blue () { printf '\033[0;34m' ; }[m
|
||||
[31m- purple () { printf '\033[0;35m' ; }[m
|
||||
[31m- cyan () { printf '\033[0;36m' ; }[m
|
||||
[31m- gray () { printf '\033[0;37m' ; }[m
|
||||
[31m- grey () { printf '\033[1;30m' ; }[m
|
||||
[31m- lred () { printf '\033[1;31m' ; }[m
|
||||
[31m- lgreen () { printf '\033[1;32m' ; }[m
|
||||
[31m- yellow () { printf '\033[1;33m' ; }[m
|
||||
[31m- lblue () { printf '\033[1;34m' ; }[m
|
||||
[31m- lpurple (){ printf '\033[1;35m' ; }[m
|
||||
[31m- lcyan () { printf '\033[1;36m' ; }[m
|
||||
[31m- white () { printf '\033[1;37m' ; }[m
|
||||
[31m-else[m
|
||||
[31m- normal () { return; }[m
|
||||
[31m- red () { return; }[m
|
||||
[31m- green () { return; }[m
|
||||
[31m- black () { return; }[m
|
||||
[31m- brown () { return; }[m
|
||||
[31m- orange () { return; }[m
|
||||
[31m- blue () { return; }[m
|
||||
[31m- purple () { return; }[m
|
||||
[31m- cyan () { return; }[m
|
||||
[31m- gray () { return; }[m
|
||||
[31m- grey () { return; }[m
|
||||
[31m- lred () { return; }[m
|
||||
[31m- lgreen () { return; }[m
|
||||
[31m- yellow () { return; }[m
|
||||
[31m- lblue () { return; }[m
|
||||
[31m- lpurple (){ return; }[m
|
||||
[31m- lcyan () { return; }[m
|
||||
[31m- white () { return; }[m
|
||||
[32m+[m[32m# colors (ANSI escape codes)[m
|
||||
[32m+[m[32mif [[ "$LOG" == False ]]; then[m
|
||||
[32m+[m[32m nor='\033[0m'[m
|
||||
[32m+[m[32m red='\033[0;31m'[m
|
||||
[32m+[m[32m gre='\033[0;32m'[m
|
||||
[32m+[m[32m bla='\033[0;30m'[m
|
||||
[32m+[m[32m bro='\033[0;33m'[m
|
||||
[32m+[m[32m ora='\033[0;33m'[m
|
||||
[32m+[m[32m blu='\033[0;34m'[m
|
||||
[32m+[m[32m pur='\033[0;35m'[m
|
||||
[32m+[m[32m cya='\033[0;36m'[m
|
||||
[32m+[m[32m gra='\033[0;37m'[m
|
||||
[32m+[m[32m gre='\033[1;30m'[m
|
||||
[32m+[m[32m lre='\033[1;31m'[m
|
||||
[32m+[m[32m lgr='\033[1;32m'[m
|
||||
[32m+[m[32m yel='\033[1;33m'[m
|
||||
[32m+[m[32m lbl='\033[1;34m'[m
|
||||
[32m+[m[32m lpu='\033[1;35m'[m
|
||||
[32m+[m[32m lcy='\033[1;36m'[m
|
||||
[32m+[m[32m whi='\033[1;37m'[m
|
||||
fi[m
|
||||
[m
|
||||
[m
|
||||
[31m-[m
|
||||
[31m-# Functions[m
|
||||
[31m-say () {[m
|
||||
[31m- # Print something to stdout.[m
|
||||
[31m-[m
|
||||
[31m- # Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html[m
|
||||
[31m-[m
|
||||
[31m- # @param what to print[m
|
||||
[31m-[m
|
||||
[31m- printf %s\\n "$*"[m
|
||||
[32m+[m[32m# Styling[m
|
||||
[32m+[m[32m section_prefix=' ::: '[m
|
||||
[32m+[m[32msubsection_prefix=' :: '[m
|
||||
[32m+[m[32m paragraph_prefix=' : '[m
|
||||
[32m+[m[32m section_color=$lbl[m
|
||||
[32m+[m[32m subsection_color=$ora[m
|
||||
[32m+[m[32m paragraph_color=$nor[m
|
||||
[32m+[m
|
||||
[32m+[m
|
||||
[32m+[m[32m# Formatting functions[m[41m [m
|
||||
[32m+[m[32mprint () {[m
|
||||
[32m+[m[32m # Print something safely to stdout[m
|
||||
[32m+[m[32m # Usage: print [n] stuff to print[m
|
||||
[32m+[m[32m # n will print a newline[m
|
||||
[32m+[m[32m for arg in "$@"; do[m
|
||||
[32m+[m[32m if [[ "$arg" == 'n' ]]; then[m
|
||||
[32m+[m[32m printf \\n[m
|
||||
[32m+[m[32m else[m
|
||||
[32m+[m[32m echo -ne "$arg"[m
|
||||
[32m+[m[32m fi[m
|
||||
[32m+[m[32m shift[m
|
||||
[32m+[m[32m done[m
|
||||
return 0[m
|
||||
}[m
|
||||
[m
|
||||
[m
|
||||
[31m-sayn () {[m
|
||||
[31m- # Like say without linebreak.[m
|
||||
[31m-[m
|
||||
[31m- # @param what to print[m
|
||||
[31m-[m
|
||||
[31m- printf %s "$*"[m
|
||||
[31m- return 0[m
|
||||
[32m+[m[32msection() {[m
|
||||
[32m+[m[32m # print a styled section[m
|
||||
[32m+[m[32m # initialize $SECTION to 0 or increment by 1[m
|
||||
[32m+[m[32m if [[ -z ${SECTION+x} ]]; then # +x seems to be a trick to also catch empty strings like SECTION=''[m
|
||||
[32m+[m[32m SECTION=1[m
|
||||
[32m+[m[32m else[m
|
||||
[32m+[m[32m SECTION=$(($SECTION+1))[m
|
||||
[32m+[m[32m fi[m
|
||||
[32m+[m[32m print n n n "$section_color$section_prefix${SECTION}. $*$nor"[m
|
||||
}[m
|
||||
[m
|
||||
[31m-sayc() {[m
|
||||
[31m- # quick styler, usage "style *color*"[m
|
||||
[m
|
||||
[31m- if [[ "$1" == "" ]]; then[m
|
||||
[31m- blue[m
|
||||
[32m+[m[32msubsection() {[m
|
||||
[32m+[m[32m # print a styled subsection[m
|
||||
[32m+[m[32m # initialize $SUBSECTION to 0 or increment by 1[m
|
||||
[32m+[m[32m if [[ -z ${SUBSECTION+x} ]]; then # +x seems to be a trick to also catch empty strings like SECTION=''[m
|
||||
[32m+[m[32m SUBSECTION=1[m
|
||||
else[m
|
||||
[31m- $1[m
|
||||
[32m+[m[32m SUBSECTION=$(($SUBSECTION+1))[m
|
||||
fi[m
|
||||
[31m- sayn " ::: "[m
|
||||
[31m- shift[m
|
||||
[31m- say "$*"[m
|
||||
[31m- normal[m
|
||||
[31m- return 0[m
|
||||
[32m+[m[32m print n n "$subsection_color$subsection_prefix${SECTION}.${SUBSECTION} $*$nor"[m
|
||||
}[m
|
||||
[m
|
||||
[31m-saycn() {[m
|
||||
[31m- $1[m
|
||||
[31m- sayn " ::: "[m
|
||||
[31m- shift[m
|
||||
[31m- sayn "$*"[m
|
||||
[31m- normal[m
|
||||
[31m- return 0[m
|
||||
[31m-}[m
|
||||
[m
|
||||
[32m+[m[32mparagraph() {[m
|
||||
[32m+[m[32m # print a styled subsection[m
|
||||
[32m+[m[32m print n "$paragraph_color$paragraph_prefix$*$nor"[m
|
||||
[32m+[m[32m}[m
|
||||
[32m+[m[32m# examples[m
|
||||
[32m+[m[32m# paragraph lol[m
|
||||
[32m+[m[32m# paragraph next[m
|
||||
[32m+[m[32m# section big[m
|
||||
[32m+[m[32m# section relly big[m
|
||||
[32m+[m[32m# subsection a[m
|
||||
[32m+[m[32m# paragraph 1[m
|
||||
[32m+[m[32m# subsection b[m
|
||||
[32m+[m[32m# paragraph 2[m
|
||||
[32m+[m
|
||||
[32m+[m
|
||||
[32m+[m[32m# Helper functions[m
|
||||
root () {[m
|
||||
# Exit immediately if user is not root[m
|
||||
# Usage: root[m
|
||||
[36m@@ -132,35 +128,30 @@[m [mroot () {[m
|
||||
fi[m
|
||||
}[m
|
||||
[m
|
||||
[32m+[m
|
||||
ok() {[m
|
||||
# Check if last call had errors. If so, print error message and exit.[m
|
||||
[31m- # Usage: ok <command>[m
|
||||
[32m+[m[32m # Usage: ok[m
|
||||
[m
|
||||
if [[ "$?" -ne 0 ]]; then[m
|
||||
[31m- sayc red "ERROR, aborting."[m
|
||||
[32m+[m[32m print $red ' ... ERROR, aborting!' $nor n[m
|
||||
exit 1[m
|
||||
else[m
|
||||
[31m- sayc green "$0 ... done"[m
|
||||
[32m+[m[32m print " ...$lgr done$nor" n[m
|
||||
return 0[m
|
||||
fi[m
|
||||
}[m
|
||||
[m
|
||||
[31m-try() {[m
|
||||
[31m- # Do a job and check if it ran successfull.[m
|
||||
[31m- # Usage: try <command>[m
|
||||
[m
|
||||
[31m- $* && ok[m
|
||||
[31m-}[m
|
||||
[31m-[m
|
||||
[31m-trycm() {[m
|
||||
[31m- # Do a job, write a message, color it and check if it ran successfull.[m
|
||||
[31m- # Usage: trycmd <color> <message> <command>[m
|
||||
[32m+[m[32mtry() {[m
|
||||
[32m+[m[32m # Print a message, execute a command silent print errors to stdout[m
|
||||
[32m+[m[32m # and abort the command goes wrong[m
|
||||
[32m+[m[32m # Usage: try message command [command2 ...][m
|
||||
[m
|
||||
[31m- if [[ "$2" != "" ]]; then[m
|
||||
[31m- say[m
|
||||
[31m- sayc "$1" "$2"[m
|
||||
[31m- fi[m
|
||||
[31m- shift && shift[m
|
||||
[31m- saycn normal "$*"[m
|
||||
[31m- try $*[m
|
||||
[32m+[m[32m message="$1"[m
|
||||
[32m+[m[32m shift[m
|
||||
[32m+[m[32m paragraph "$pur${message}$nor: $*"[m
|
||||
[32m+[m[32m $* 1> /dev/null && ok[m
|
||||
}[m
|
||||
[32m+[m[32m# examples[m
|
||||
[32m+[m[32m# try testnachricht lsblk[m
|
Loading…
x
Reference in New Issue
Block a user