From 4065c5682278400806ca979bef696da4e4de2cfb Mon Sep 17 00:00:00 2001 From: labbots Date: Mon, 22 Jun 2020 13:11:55 +0100 Subject: [PATCH] Added debug functions --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ bash_utility.sh | 2 ++ src/debug.sh | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/debug.sh diff --git a/README.md b/README.md index 14ce09a..e0f8eaf 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,9 @@ Detailed documentation is available at - [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. diff --git a/bash_utility.sh b/bash_utility.sh index 2bd738e..c7dabc0 100644 --- a/bash_utility.sh +++ b/bash_utility.sh @@ -13,4 +13,6 @@ source src/collection.sh source src/json.sh source src/terminal.sh source src/validation.sh +source src/debug.sh + diff --git a/src/debug.sh b/src/debug.sh new file mode 100644 index 0000000..8282873 --- /dev/null +++ b/src/debug.sh @@ -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}" + +}