mirror of
https://github.com/armbian/bash-util.git
synced 2026-01-06 10:37:49 -08:00
Added debug functions
This commit is contained in:
62
README.md
62
README.md
@@ -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.
|
||||
|
||||
@@ -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
49
src/debug.sh
Normal 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}"
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user