Added functions to interaction.sh

This commit is contained in:
labbots
2020-06-07 21:15:14 +01:00
parent 3b18ab39d1
commit dbc19b2ee1
6 changed files with 166 additions and 7 deletions

View File

@@ -58,6 +58,9 @@ Bash library which provides utility functions and helpers for functional program
- [file::dirname()](#filedirname)
- [file::full_path()](#filefull_path)
- [file::mime_type()](#filemime_type)
- [Interaction](#interaction)
- [interaction::prompt_yes_no()](#interactionprompt_yes_no)
- [interaction::prompt_response()](#interactionprompt_response)
- [Miscellaneous](#miscellaneous)
- [misc::check_internet_connection()](#misccheck_internet_connection)
- [String](#string)
@@ -1088,7 +1091,7 @@ Format seconds to human readable format.
```bash
echo "$(date::human_readable_seconds "356786")"
#Output
4 days 3 hrs 6 minute(s) and 26 seconds
4 days 3 hours 6 minute(s) and 26 seconds
```
#### Arguments
@@ -1286,6 +1289,63 @@ application/x-shellscript
- mime type of file/directory.
## Interaction
Functions to enable interaction with the user.
### interaction::prompt_yes_no()
Prompt yes or no question to the user.
#### Example
```bash
interaction::prompt_yes_no "Are you sure to proceed" "yes"
#Output
Are you sure to proceed (y/n)? [y]
```
#### Arguments
- **$1** (string): The question to be prompted to the user.
- **$2** (string): default answer [yes/no] (optional).
#### Exit codes
- **0**: If user responds with yes.
- **1**: If user responds with no.
- **2**: Function missing arguments.
#### Output on stdout
- question to be prompted to the user.
### interaction::prompt_response()
Prompt question to the user.
#### Example
```bash
interaction::prompt_response "Choose directory to install" "/home/path"
#Output
Choose directory to install? [/home/path]
```
#### Arguments
- **$1** (string): The question to be prompted to the user.
- **$2** (string): default answer (optional).
#### Exit codes
- **0**: If user responds with yes.
- **2**: Function missing arguments.
#### Output on stdout
- question to be prompted to the user.
## Miscellaneous
Set of miscellaneous helper functions.
@@ -1325,7 +1385,7 @@ Hello World!
#### Arguments
- **$1** (The): string that will be trimmed.
- **$1** (string): The string to be trimmed.
#### Exit codes

View File

@@ -6,3 +6,4 @@ source src/variable.sh
source src/file.sh
source src/misc.sh
source src/date.sh
source src/interaction.sh

View File

@@ -140,7 +140,7 @@ _insert_shdoc_to_file() {
# src https://stackoverflow.com/questions/2699666/replace-delimited-block-of-text-in-file-with-the-contents-of-another-file
sed -i -ne "/${start_shdoc}/ {p; r ${shdoc_tmp_file}" -e ":a; n; /${end_shdoc}/ {p; b}; ba}; p" "${source_markdown}"
echo -e "Updated shdoc content to ${source_markdown} successfully\n"
echo -e "Updated bashdoc content to ${source_markdown} successfully\n"
else
{
@@ -148,7 +148,7 @@ _insert_shdoc_to_file() {
cat "${shdoc_tmp_file}"
printf "%s\n" "${end_shdoc}"
} >> "${source_markdown}"
echo -e "Created shdoc content to ${source_markdown} successfully\n"
echo -e "Created bashdoc content to ${source_markdown} successfully\n"
fi
}

View File

@@ -748,7 +748,7 @@ date::format() {
# @example
# echo "$(date::human_readable_seconds "356786")"
# #Output
# 4 days 3 hrs 6 minute(s) and 26 seconds
# 4 days 3 hours 6 minute(s) and 26 seconds
#
# @arg $1 int number of seconds.
#
@@ -762,7 +762,7 @@ date::human_readable_seconds() {
declare T="${1}"
declare DAY="$((T / 60 / 60 / 24))" HR="$((T / 60 / 60 % 24))" MIN="$((T / 60 % 60))" SEC="$((T % 60))"
[[ ${DAY} -gt 0 ]] && printf '%d days ' "${DAY}"
[[ ${HR} -gt 0 ]] && printf '%d hrs ' "${HR}"
[[ ${HR} -gt 0 ]] && printf '%d hours ' "${HR}"
[[ ${MIN} -gt 0 ]] && printf '%d minute(s) ' "${MIN}"
[[ ${DAY} -gt 0 || ${HR} -gt 0 || ${MIN} -gt 0 ]] && printf 'and '
printf '%d seconds\n' "${SEC}"

98
src/interaction.sh Normal file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env bash
# @file Interaction
# @brief Functions to enable interaction with the user.
# @description Prompt yes or no question to the user.
#
# @example
# interaction::prompt_yes_no "Are you sure to proceed" "yes"
# #Output
# Are you sure to proceed (y/n)? [y]
#
# @arg $1 string The question to be prompted to the user.
# @arg $2 string default answer [yes/no] (optional).
#
# @exitcode 0 If user responds with yes.
# @exitcode 1 If user responds with no.
# @exitcode 2 Function missing arguments.
#
# @stdout question to be prompted to the user.
interaction::prompt_yes_no() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
declare def_arg response
def_arg=""
response=""
case "${2}" in
[yY] | [yY][eE][sS])
def_arg=y
;;
[nN] | [nN][oO])
def_arg=n
;;
esac
while :; do
printf "%s (y/n)? " "${1}"
[[ -n "${def_arg}" ]] && printf "[%s] " "${def_arg}"
read response
[[ -z "${response}" ]] && response="${def_arg}"
case "${response}" in
[yY] | [yY][eE][sS])
response=y
break
;;
[nN] | [nN][oO])
response=n
break
;;
*)
response=""
;;
esac
done
[[ "${response}" = 'y' ]] && return 0 || return 1
}
# @description Prompt question to the user.
#
# @example
# interaction::prompt_response "Choose directory to install" "/home/path"
# #Output
# Choose directory to install? [/home/path]
#
# @arg $1 string The question to be prompted to the user.
# @arg $2 string default answer (optional).
#
# @exitcode 0 If user responds with yes.
# @exitcode 2 Function missing arguments.
#
# @stdout question to be prompted to the user.
interaction::prompt_response() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
declare def_arg response
response=""
def_arg="${2}"
while :; do
printf "%s ? " "${1}"
[[ -n "${def_arg}" ]] && [[ "${def_arg}" != "-" ]] && printf "[${def_arg}] "
read response
[[ -n "${response}" ]] && break
if [[ -z "${response}" ]] && [[ -n "${def_arg}" ]]; then
response="${def_arg}"
break
fi
done
[[ "${response}" = "-" ]] && response=""
printf "%s" "${response}"
}

View File

@@ -10,7 +10,7 @@
# #Output
# Hello World!
#
# @arg $1 The string that will be trimmed.
# @arg $1 string The string to be trimmed.
#
# @exitcode 0 If successful.
# @exitcode 2 Function missing arguments.