refactor nearly everything: use section, subsection and paragraph with easier color access log functionality AND the new try command

This commit is contained in:
moritz münch 2020-11-27 17:11:10 +01:00
parent 29cb17026c
commit 92e06f1e4c
2 changed files with 344 additions and 102 deletions

199
bsf
View File

@ -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
LOG=False
for arg in "$@"; do
if [[ "$arg" == "--log" ]]; then
LOG=False
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
View File

@ -0,0 +1,251 @@
diff --git a/bsf b/bsf
index 83cf953..6f55713 100644
--- a/bsf
+++ b/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
+LOG=False
+for arg in "$@"; do
if [[ "$arg" == "--log" ]]; then
- LOG=False
+ 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 "$*"
+# Styling
+ section_prefix=' ::: '
+subsection_prefix=' :: '
+ paragraph_prefix=' : '
+ section_color=$lbl
+ subsection_color=$ora
+ paragraph_color=$nor
+
+
+# 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
+ echo -ne "$arg"
+ fi
+ shift
+ done
return 0
}


-sayn () {
- # Like say without linebreak.
-
- # @param what to print
-
- printf %s "$*"
- 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"
}

-sayc() {
- # quick styler, usage "style *color*"

- if [[ "$1" == "" ]]; then
- blue
+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
- $1
+ SUBSECTION=$(($SUBSECTION+1))
fi
- sayn " ::: "
- shift
- say "$*"
- normal
- return 0
+ print n n "$subsection_color$subsection_prefix${SECTION}.${SUBSECTION} $*$nor"
}

-saycn() {
- $1
- sayn " ::: "
- shift
- sayn "$*"
- normal
- return 0
-}

+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>

- $* && ok
-}
-
-trycm() {
- # Do a job, write a message, color it and check if it ran successfull.
- # Usage: trycmd <color> <message> <command>
+try() {
+ # Print a message, execute a command silent print errors to stdout
+ # and abort the command goes wrong
+ # Usage: try message command [command2 ...]

- 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