From 0ac473cddf03e9228db342ab645af41dabea6888 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 24 May 2015 19:48:52 -0600 Subject: libremessages:flag: Allow specifying multiple flags in one go. This allows the descriptions to all be aligned together. --- src/lib/libremessages | 37 ++++++++++++++++++++++++++----------- src/lib/libremessages.1.ronn | 6 ++++-- 2 files changed, 30 insertions(+), 13 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...] diff --git a/src/lib/libremessages.1.ronn b/src/lib/libremessages.1.ronn index 5d58fb5..520837d 100644 --- a/src/lib/libremessages.1.ronn +++ b/src/lib/libremessages.1.ronn @@ -89,7 +89,7 @@ For each of these, is fed through `gettext` automatically. Similar to `prose`, but prints a bullet point before the first line, and indents the remaining lines. - * `flag` : + * `flag` [ ...]: Print a flag and description formatted for `--help` text. For example:
`flag '-N' 'Disable networking in the chroot'`
@@ -99,7 +99,9 @@ For each of these, is fed through `gettext` automatically. `flag "-C <$(_ FILE)>" 'Use this file instead of pacman.conf'`
Newlines in the description are ignored; it is whitespace-collapsed (so newlines are stripped), then it is - re-word-wrapped, in the same way as `prose` and `bullet`. + re-word-wrapped, in the same way as `prose` and `bullet`. If you + pass in multiple flag/description pairs to the same invocation, + the descriptions are all aligned together. ### NOTIFICATION ROUTINES -- cgit v1.2.2