Added debug functions

This commit is contained in:
labbots
2020-06-22 13:11:55 +01:00
parent 5ba65a2391
commit 4065c56822
3 changed files with 113 additions and 0 deletions

View File

@@ -80,6 +80,9 @@ Detailed documentation is available at <https://labbots.github.io/bash-utility/>
- [date::sub_minutes()](#datesub_minutes)
- [date::sub_seconds()](#datesub_seconds)
- [date::format()](#dateformat)
- [Debug](#debug)
- [debug::print_array()](#debugprint_array)
- [debug::print_ansi()](#debugprint_ansi)
- [File](#file)
- [file::make_temp_file()](#filemake_temp_file)
- [file::name()](#filename)
@@ -1597,6 +1600,65 @@ echo echo "$(date::format "1594143480")"
2020-07-07 18:38:00
```
## Debug
Functions to facilitate debugging scripts.
### debug::print_array()
Prints the content of array as key value pair for easier debugging.
Pass the variable name of the array instead of value of the variable.
#### Arguments
- **$1** (string): variable name of the array.
#### Output on stdout
- Formatted key value of array.
#### Example
```bash
array=(foo bar baz)
printf "Array\n"
printarr "array"
declare -A assoc_array
assoc_array=([foo]=bar [baz]=foobar)
printf "Assoc Array\n"
printarr "assoc_array"
#Output
Array
0 = foo
1 = bar
2 = baz
Assoc Array
baz = foobar
foo = bar
```
### debug::print_ansi()
Function to print ansi escape sequence as is.
This function helps debug ansi escape sequence in text by displaying the escape codes.
#### Arguments
- **$1** (string): input with ansi escape sequence.
#### Output on stdout
- Ansi escape sequence printed in output as is.
#### Example
```bash
txt="$(tput bold)$(tput setaf 9)This is bold red text$(tput sgr0).$(tput setaf 10)This is green text$(tput sgr0)"
debug::print_ansi "$txt"
#Output
\e[1m\e[91mThis is bold red text\e(B\e[m.\e[92mThis is green text\e(B\e[m
```
## File
Functions for handling files.

View File

@@ -13,4 +13,6 @@ source src/collection.sh
source src/json.sh
source src/terminal.sh
source src/validation.sh
source src/debug.sh

49
src/debug.sh Normal file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# @file Debug
# @brief Functions to facilitate debugging scripts.
# @description Prints the content of array as key value pair for easier debugging.
# Pass the variable name of the array instead of value of the variable.
# @example
# array=(foo bar baz)
# printf "Array\n"
# printarr "array"
# declare -A assoc_array
# assoc_array=([foo]=bar [baz]=foobar)
# printf "Assoc Array\n"
# printarr "assoc_array"
# #Output
# Array
# 0 = foo
# 1 = bar
# 2 = baz
# Assoc Array
# baz = foobar
# foo = bar
#
# @arg $1 string variable name of the array.
#
# @stdout Formatted key value of array.
debug::print_array() {
declare -n __arr="$1"
for k in "${!__arr[@]}"; do printf "%s = %s\n" "$k" "${__arr[$k]}"; done
}
# @description Function to print ansi escape sequence as is.
# This function helps debug ansi escape sequence in text by displaying the escape codes.
#
# @example
# txt="$(tput bold)$(tput setaf 9)This is bold red text$(tput sgr0).$(tput setaf 10)This is green text$(tput sgr0)"
# debug::print_ansi "$txt"
# #Output
# \e[1m\e[91mThis is bold red text\e(B\e[m.\e[92mThis is green text\e(B\e[m
#
# @arg $1 string input with ansi escape sequence.
#
# @stdout Ansi escape sequence printed in output as is.
debug::print_ansi() {
#echo $(tr -dc '[:print:]'<<<$1)
printf "%s\n" "${1//$'\e'/\\e}"
}