summary refs log tree commit diff
diff options
context:
space:
mode:
authorSingustromo <singustromo@disroot.org>2023-07-03 12:53:31 +0200
committerSingustromo <singustromo@disroot.org>2023-07-03 12:53:31 +0200
commitcc8e4967c027083a8cc8406ef3d0baa44864834e (patch)
tree2793d0ffa19bf457c808244029a1343b14298ecd
parent6fb9df04f4891aa76a1d120650f9169cf97ad48f (diff)
downloadbash-framework-cc8e4967c027083a8cc8406ef3d0baa44864834e.tar.gz
bash-framework-cc8e4967c027083a8cc8406ef3d0baa44864834e.zip
added changes meant for previous commit
-rw-r--r--lib/string/count_spaces.sh5
-rw-r--r--lib/string/is_number.sh5
-rw-r--r--lib/string/sanitize.sh4
-rw-r--r--lib/string/slash_replacement.sh18
-rw-r--r--lib/string/uuid_generate.sh18
-rw-r--r--lib/util/check_bash4.sh1
-rw-r--r--lib/util/function.sh24
-rw-r--r--lib/util/log.sh19
8 files changed, 94 insertions, 0 deletions
diff --git a/lib/string/count_spaces.sh b/lib/string/count_spaces.sh
new file mode 100644
index 0000000..f9e4511
--- /dev/null
+++ b/lib/string/count_spaces.sh
@@ -0,0 +1,5 @@
+String::count_spaces() {
+	local howMany="${1:?nothing to count}"
+	[[ "$howMany" -gt 0 ]] || return
+	( printf "%*s" "$howMany" )
+}
diff --git a/lib/string/is_number.sh b/lib/string/is_number.sh
new file mode 100644
index 0000000..e11191d
--- /dev/null
+++ b/lib/string/is_number.sh
@@ -0,0 +1,5 @@
+String::is_number() {
+  local input="${1:?no input}"
+  local regex='^-?[0-9]+([.][0-9]+)?$'
+  [[ "$input" =~ $regex ]] || return
+}
diff --git a/lib/string/sanitize.sh b/lib/string/sanitize.sh
new file mode 100644
index 0000000..667515d
--- /dev/null
+++ b/lib/string/sanitize.sh
@@ -0,0 +1,4 @@
+String::sanitize() {
+	local type="${1:?no input}"
+	printf "%s" "${type//[^a-zA-Z0-9]/_}"
+}
diff --git a/lib/string/slash_replacement.sh b/lib/string/slash_replacement.sh
new file mode 100644
index 0000000..f6f4be8
--- /dev/null
+++ b/lib/string/slash_replacement.sh
@@ -0,0 +1,18 @@
+# Workaround for a Bash bug that causes string replacement to fail when a \ is in the string
+#
+
+String::slashes_replace() {
+  local stringToMark="${1:?no input}"
+
+  local slash="\\"
+  local slashReplacement='_%SLASH%_'
+  printf "%s" "${stringToMark/$slash$slash/$slashReplacement}"
+}
+
+String::slashes_restore() {
+  local stringToMark="${1:?no input}"
+
+  local slash="\\"
+  local slashReplacement='_%SLASH%_'
+  printf "%s" "${stringToMark/$slashReplacement/$slash}"
+}
diff --git a/lib/string/uuid_generate.sh b/lib/string/uuid_generate.sh
new file mode 100644
index 0000000..0cba82e
--- /dev/null
+++ b/lib/string/uuid_generate.sh
@@ -0,0 +1,18 @@
+String::uuid_generate() {
+  ## https://gist.github.com/markusfisch/6110640
+  local N B C='89ab'
+
+  for (( N=0; N < 16; ++N )); do
+    B=$(( $RANDOM%256 ))
+
+    case $N in
+      6) printf '4%x' $(( B%16 )) ;;
+      8)
+        printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%16 ))
+	;;
+      3 | 5 | 7 | 9)
+        printf '%02x-' $B ;;
+      *) printf '%02x' $B ;;
+    esac
+  done
+}
diff --git a/lib/util/check_bash4.sh b/lib/util/check_bash4.sh
new file mode 100644
index 0000000..75f33b7
--- /dev/null
+++ b/lib/util/check_bash4.sh
@@ -0,0 +1 @@
+[[ "${BASH_VERSINFO[0]}" -lt 4 ]] && echo "The module you are trying to load requires bash >= 4" && exit 1 || true
diff --git a/lib/util/function.sh b/lib/util/function.sh
new file mode 100644
index 0000000..15313c5
--- /dev/null
+++ b/lib/util/function.sh
@@ -0,0 +1,24 @@
+# no dependencies
+
+Function::exists(){
+  local name="${1:?no function name provided}"
+  declare -f "$name" &> /dev/null
+}
+
+Function::list_fuzzy() {
+  local startsWith="${1:?no search string provided}"
+  compgen -A 'function' "$startsWith" || true
+}
+
+Function::inject_code() {
+  local functionName="${1:?no function name provided}"
+  local injectBefore="${2:-}"
+  local injectAfter="${3:-}"
+  [[ -n "$InjectBefore" && -n "$InjectAfter" ]] || return
+  local body="$(declare -f "$functionName")"
+  [ -n "$body" ] || return
+  body="${body#*{}" # trim start
+  body="${body%\}}" # trim end
+  local enter=$'\n'
+  eval "${functionName}() { ${enter}${injectBefore}${body}${injectAfter}${enter} }"
+}
diff --git a/lib/util/log.sh b/lib/util/log.sh
new file mode 100644
index 0000000..f32d1a0
--- /dev/null
+++ b/lib/util/log.sh
@@ -0,0 +1,19 @@
+#! /usr/bin/env bash
+
+# Prints debug messages to fd2
+# expands escape sequences
+# depends: $DEBUG == 1|true
+Util::log() {
+    { [[ "$DEBUG" == '1' || "$DEBUG" == "true" ]] && [ -n "$*" ]; } || { true; return; }
+    local line_no="${BASH_LINENO[0]}" i=2 out="(${FUNCNAME[1]}:_LNO_) " # called function
+    while [ -n "${FUNCNAME[$i]}" ]; do # caller functions
+        [ "$i" -eq 2 ] && out+="[caller: "
+        out+="${FUNCNAME[$i]}<"
+        [ "${FUNCNAME[$i]}" = "${FUNCNAME[$((i-1))]}" ] \
+            && { i=$(( ${#BASH_LINENO[@]} )); break; }
+        ((i++))
+    done
+    [ "$i" -gt 2 ] && out="${out::-1}] " && line_no="${BASH_LINENO[$((i-2))]}"
+    out="${out//_LNO_/$line_no}"
+    printf "%s%b\n" "$out" "$*" >&2
+}