mirror of
https://github.com/armbian/bash-util.git
synced 2026-01-06 10:37:49 -08:00
Added more format functions
This commit is contained in:
104
README.md
104
README.md
@@ -94,6 +94,10 @@ Detailed documentation is available at <https://labbots.github.io/bash-utility/>
|
||||
- [Format](#format)
|
||||
- [format::human_readable_seconds()](#formathuman_readable_seconds)
|
||||
- [format::bytes_to_human()](#formatbytes_to_human)
|
||||
- [format::strip_ansi()](#formatstrip_ansi)
|
||||
- [format::text_center()](#formattext_center)
|
||||
- [format::report()](#formatreport)
|
||||
- [format::trim_text_to_term()](#formattrim_text_to_term)
|
||||
- [Interaction](#interaction)
|
||||
- [interaction::prompt_yes_no()](#interactionprompt_yes_no)
|
||||
- [interaction::prompt_response()](#interactionprompt_response)
|
||||
@@ -1895,6 +1899,106 @@ echo "$(format::bytes_to_human "2250")"
|
||||
2.19 KB
|
||||
```
|
||||
|
||||
### format::strip_ansi()
|
||||
|
||||
Remove Ansi escape sequences from given text.
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): Input text to be ansi stripped.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- Ansi stripped text.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
format::strip_ansi "\e[1m\e[91mThis is bold red text\e(B\e[m.\e[92mThis is green text.\e(B\e[m"
|
||||
#Output
|
||||
This is bold red text.This is green text.
|
||||
```
|
||||
|
||||
### format::text_center()
|
||||
|
||||
Prints the given text to centre of terminal.
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): Text to be printed.
|
||||
- **$2** (string): Filler symbol to be added to prefix and suffix of the text (optional). Defaults to space as filler.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- formatted text.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
format::text_center "This text is in centre of the terminal." "-"
|
||||
```
|
||||
|
||||
### format::report()
|
||||
|
||||
Format String to print beautiful report.
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): Text to be printed on the left.
|
||||
- **$2** (string): Text to be printed within the square brackets.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- formatted text.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
format::report "Initialising mission state" "Success"
|
||||
#Output
|
||||
Initialising mission state ....................................................................[ Success ]
|
||||
```
|
||||
|
||||
### format::trim_text_to_term()
|
||||
|
||||
Trim given text to width of the terminal window.
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): Text of first sentence.
|
||||
- **$2** (string): Text of second sentence (optional).
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- trimmed text.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
format::trim_text_to_term "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." "This is part of second sentence."
|
||||
#Output
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod..This is part of second sentence.
|
||||
```
|
||||
|
||||
## Interaction
|
||||
|
||||
Functions to enable interaction with the user.
|
||||
|
||||
121
src/format.sh
121
src/format.sh
@@ -51,3 +51,124 @@ format::bytes_to_human() {
|
||||
done
|
||||
printf "%s\n" "${b}${d} ${S[${s}]}"
|
||||
}
|
||||
|
||||
# @internal
|
||||
# @description Checks the terminal window size, if necessary, updates the values of LINES and COLUMNS.
|
||||
#
|
||||
# @noargs
|
||||
_check_terminal_window_size() {
|
||||
shopt -s checkwinsize && (: && :)
|
||||
trap 'shopt -s checkwinsize; (:;:)' SIGWINCH
|
||||
}
|
||||
|
||||
# @description Remove Ansi escape sequences from given text.
|
||||
#
|
||||
# @example
|
||||
# format::strip_ansi "\e[1m\e[91mThis is bold red text\e(B\e[m.\e[92mThis is green text.\e(B\e[m"
|
||||
# #Output
|
||||
# This is bold red text.This is green text.
|
||||
#
|
||||
# @arg $1 string Input text to be ansi stripped.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
#
|
||||
# @stdout Ansi stripped text.
|
||||
format::strip_ansi() {
|
||||
declare tmp="${1}"
|
||||
declare esc=$(printf "\x1b")
|
||||
declare tpa=$(printf "\x28")
|
||||
declare re="(.*)$esc[\[$tpa][0-9]*;*[mKB](.*)"
|
||||
while [[ ${tmp} =~ $re ]]; do
|
||||
tmp="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
|
||||
done
|
||||
printf "%s" "${tmp}"
|
||||
}
|
||||
|
||||
# @description Prints the given text to centre of terminal.
|
||||
#
|
||||
# @example
|
||||
# format::text_center "This text is in centre of the terminal." "-"
|
||||
#
|
||||
# @arg $1 string Text to be printed.
|
||||
# @arg $2 string Filler symbol to be added to prefix and suffix of the text (optional). Defaults to space as filler.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout formatted text.
|
||||
format::text_center() {
|
||||
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
|
||||
|
||||
_check_terminal_window_size
|
||||
declare input="${1}" symbol="${2:- }" filler out
|
||||
declare no_ansi_Out=$(format::strip_ansi "$input")
|
||||
declare -i str_len=${#no_ansi_Out}
|
||||
declare -i filler_len="$(((COLUMNS - str_len) / 2))"
|
||||
|
||||
[[ -n "${symbol}" ]] && symbol="${symbol:0:1}"
|
||||
for ((i = 0; i < filler_len; i++)); do
|
||||
filler+="${symbol}"
|
||||
done
|
||||
|
||||
out="${filler}${input}${filler}"
|
||||
[[ $(((COLUMNS - str_len) % 2)) -ne 0 ]] && out+="${symbol}"
|
||||
printf "%s" "${out}"
|
||||
}
|
||||
|
||||
# @description Format String to print beautiful report.
|
||||
#
|
||||
# @example
|
||||
# format::report "Initialising mission state" "Success"
|
||||
# #Output
|
||||
# Initialising mission state ....................................................................[ Success ]
|
||||
#
|
||||
# @arg $1 string Text to be printed on the left.
|
||||
# @arg $2 string Text to be printed within the square brackets.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout formatted text.
|
||||
format::report() {
|
||||
[[ $# -lt 2 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
|
||||
_check_terminal_window_size
|
||||
declare symbol="." to_print y hl hlout out
|
||||
declare input1="${1} " input2="${2}"
|
||||
input2="[ $input2 ]"
|
||||
to_print="$((COLUMNS * 60 / 100))"
|
||||
y=$(($to_print - (${#input1} + ${#input2})))
|
||||
hl="$(printf '%*s' $y '')"
|
||||
hlout=${hl// /${symbol}}
|
||||
out="${input1}${hlout}${input2}"
|
||||
printf "%s\n" "${out}"
|
||||
}
|
||||
|
||||
# @description Trim given text to width of the terminal window.
|
||||
#
|
||||
# @example
|
||||
# format::trim_text_to_term "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." "This is part of second sentence."
|
||||
# #Output
|
||||
# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod..This is part of second sentence.
|
||||
#
|
||||
# @arg $1 string Text of first sentence.
|
||||
# @arg $2 string Text of second sentence (optional).
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout trimmed text.
|
||||
format::trim_text_to_term() {
|
||||
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
|
||||
_check_terminal_window_size
|
||||
declare to_print out input1="$1" input2="$2"
|
||||
if [[ $# = 1 ]]; then
|
||||
to_print="$((COLUMNS * 93 / 100))"
|
||||
{ [[ ${#input1} -gt ${to_print} ]] && out="${input1:0:to_print}.."; } || { out="$input1"; }
|
||||
else
|
||||
to_print="$((COLUMNS * 40 / 100))"
|
||||
{ [[ ${#input1} -gt ${to_print} ]] && out+=" ${input1:0:to_print}.."; } || { out+=" $input1"; }
|
||||
to_print="$((COLUMNS * 53 / 100))"
|
||||
{ [[ ${#input2} -gt ${to_print} ]] && out+="${input2:0:to_print}.. "; } || { out+="$input2 "; }
|
||||
fi
|
||||
printf "%s" "$out"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user