From dbc19b2ee1ac5802e45cffa385f811a9b5edd6a2 Mon Sep 17 00:00:00 2001 From: labbots Date: Sun, 7 Jun 2020 21:15:14 +0100 Subject: [PATCH] Added functions to interaction.sh --- README.md | 64 ++++++++++++++++++++++++++- bash_utilities.sh | 1 + bin/generate_readme.sh | 4 +- src/date.sh | 4 +- src/interaction.sh | 98 ++++++++++++++++++++++++++++++++++++++++++ src/string.sh | 2 +- 6 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 src/interaction.sh diff --git a/README.md b/README.md index a190813..8f03a79 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bash_utilities.sh b/bash_utilities.sh index 358863c..e67d6b7 100644 --- a/bash_utilities.sh +++ b/bash_utilities.sh @@ -6,3 +6,4 @@ source src/variable.sh source src/file.sh source src/misc.sh source src/date.sh +source src/interaction.sh diff --git a/bin/generate_readme.sh b/bin/generate_readme.sh index 533e474..a229eb2 100755 --- a/bin/generate_readme.sh +++ b/bin/generate_readme.sh @@ -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 } diff --git a/src/date.sh b/src/date.sh index 0d7a59a..610b863 100644 --- a/src/date.sh +++ b/src/date.sh @@ -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}" diff --git a/src/interaction.sh b/src/interaction.sh new file mode 100644 index 0000000..d3c69fc --- /dev/null +++ b/src/interaction.sh @@ -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}" +} diff --git a/src/string.sh b/src/string.sh index ff8017c..9a334f0 100644 --- a/src/string.sh +++ b/src/string.sh @@ -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.