summaryrefslogtreecommitdiff
path: root/src/lib/libremessages
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2015-05-24 19:48:52 -0600
committerLuke Shumaker <lukeshu@sbcglobal.net>2015-05-24 19:48:52 -0600
commit0ac473cddf03e9228db342ab645af41dabea6888 (patch)
treeb44052049f30bae2c02b44895a54f1615fe34985 /src/lib/libremessages
parent16c7f5d2571fcd4b698942990c7e0fd36744a9a0 (diff)
libremessages:flag: Allow specifying multiple flags in one go.
This allows the descriptions to all be aligned together.
Diffstat (limited to 'src/lib/libremessages')
-rwxr-xr-xsrc/lib/libremessages37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/lib/libremessages b/src/lib/libremessages
index 943aca3..10b1818 100755
--- a/src/lib/libremessages
+++ b/src/lib/libremessages
@@ -89,7 +89,7 @@ bullet() {
printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/ - /' -e '2,$s/^/ /'
}
-# Usage: flag FLAG DESCRIPTION
+# Usage: flag FLAG DESCRIPTION [FLAG2 DESCRIPTION2...]
#
# Print a flag and description formatted for --help text.
#
@@ -102,14 +102,23 @@ bullet() {
#
# If you want to line-break the description in the source, so it isn't
# crazy-long, feel free, it is reflowed/wrapped the same way as prose
-# and bullet.
+# and bullet. If you pass in multiple flag/description pairs at once,
+# the descriptions are all alligned together.
flag() {
- [[ $# == 2 ]] || panic
- local flag=$1
- local desc="$(_ "$(_libremessages_html_whitespace_collapse <<<"$2")")"
+ [[ $# == $(($#/2*2)) ]] || panic
+
+ local args=("$@")
+ declare -i flaglen=0
+ declare -i i=0
+ while [[ $i -lt $# ]]; do
+ if [[ ${#args[i]} -gt $flaglen ]]; then
+ flaglen=${#args[$i]}
+ fi
+ i+=2
+ done
- # Unless the $flag is extra-wide, the $desc should start at
- # column 16 (that is two literal-tabs). If $flag is wide,
+ # Unless the $flaglen is extra-wide, the $desc should start at
+ # column 16 (that is two literal-tabs). If $flaglen is wide,
# this should be increased in increments of 8 (that is, a
# literal-tab). Everything should be wrapped to 75 columns.
@@ -126,10 +135,16 @@ flag() {
fmt2=" %-${indent}s %s\n"
printf -v fmt1 " %-${indent}s %%s\n" ''
- local lines
- IFS=$'\n' lines=($(fmt -u -w $((71-indent)) <<<"$desc"))
- printf -- "$fmt2" "$flag" "${lines[0]}"
- [[ ${#lines[@]} -lt 2 ]] || printf -- "$fmt1" "${lines[@]:1}"
+ while [[ $# -gt 0 ]]; do
+ local flag=$1
+ local desc="$(_ "$(_libremessages_html_whitespace_collapse <<<"$2")")"
+ shift 2
+
+ local lines
+ IFS=$'\n' lines=($(fmt -u -w $((71-indent)) <<<"$desc"))
+ printf -- "$fmt2" "$flag" "${lines[0]}"
+ [[ ${#lines[@]} -lt 2 ]] || printf -- "$fmt1" "${lines[@]:1}"
+ done
}
# Usage: term_title MESG [ARGS...]