summaryrefslogtreecommitdiff
path: root/src/lib/libremessages
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libremessages')
-rwxr-xr-xsrc/lib/libremessages78
1 files changed, 68 insertions, 10 deletions
diff --git a/src/lib/libremessages b/src/lib/libremessages
index c9b6b83..46a45fb 100755
--- a/src/lib/libremessages
+++ b/src/lib/libremessages
@@ -30,27 +30,85 @@
. $(librelib common.sh)
################################################################################
-# gettext initialization #
-################################################################################
-
-export TEXTDOMAIN='libretools'
-export TEXTDOMAINDIR='/usr/share/locale'
-
-################################################################################
# Own functions #
################################################################################
-# Usage: print fmt arg1 arg2...
+panic() {
+ echo "$(_ 'panic: malformed call to internal function')" >&2
+ exit 1
+}
+
+# Usage: print MESG ARG1 ARG2...
# Like printf, but gettext-aware, and prints a trailing newline
print() {
- local fmt=$1
+ [[ $# -ge 1 ]] || panic
+ local mesg="$(_ "$1")"
shift
- printf -- "$(gettext "$fmt")\n" "$@"
+ printf -- "$mesg\n" "$@"
+}
+
+# Do HTML-style whitespace collapsing on standard IO. It considers newline,
+# tab, and space to be whitespace.
+_html_whitespace_collapse() {
+ [[ $# == 0 ]] || panic
+ tr '\n' ' ' | sed -r -e 's/\t/ /g' -e 's/ +/ /g'
+}
+
+
+# Usage: prose MESG
+# Do HTML-style whitespace collapsing on the first argument, translate it
+# (gettext), then word-wrap it to 75 columns.
+# This is useful for printing a paragraph of prose in --help text.
+prose() {
+ [[ $# -ge 1 ]] || panic
+ local mesg="$(_ "$(_html_whitespace_collapse <<<"$1")")"; shift
+ printf -- "$mesg" "$@" | fmt -u
+}
+
+# Usage: bullet MESG
+# Like prose, but print a bullet "-" before the first line, and indent the
+# remaining lines.
+bullet() {
+ [[ $# -ge 1 ]] || panic
+ local mesg="$(_ "$(_html_whitespace_collapse <<<"$1")")"; shift
+ # Wrap the text to 71 columns; 75 (the default) minus a 4 column indent
+ printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/ - /' -e '2,$s/^/ /'
+}
+
+# Usage: flag FLAG DESCRIPTION
+# Print a flag and description formatted for --help text.
+# ex: flag '-C <FILE>' 'Use this file instead of pacman.conf'
+# The description is fed through gettext, the flag is not, so if part of the
+# flag needs to be translated, you must do that yourself:
+# ex: flag "-C <$(_ FILE)>" 'Use this file instead of pacman.conf'
+# 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.
+flag() {
+ [[ $# == 2 ]] || panic
+ local n='
+'
+ local flag=$1
+ local desc="$(_ "$(_html_whitespace_collapse <<<"$2")")"
+
+ declare -i indent=13
+ while [[ $indent -le ${#flag} ]]; do
+ indent=$((indent+8))
+ done
+
+ local lines
+ IFS=$n lines=($(fmt -u -w $((73-indent)) <<<"$desc"))
+ local line
+ for line in "${lines[@]}"; do
+ printf " %-${indent}s %s\n" "$flag" "$line"
+ flag=''
+ done
}
# Usage: term_title This will be the term title
# Sets the terminal title
term_title() {
+ [[ $# -ge 1 ]] || panic
local fmt=''
case "$TERM" in
screen|tmux) fmt='\ek%s\e\\';;