diff options
Diffstat (limited to 'lib/functions.sh')
-rw-r--r-- | lib/functions.sh | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/functions.sh b/lib/functions.sh new file mode 100644 index 0000000..f8e679f --- /dev/null +++ b/lib/functions.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +### ariketa helper functions + +# establish key bindings in current session +## accepts split string arrays or strings, one array or string per binding +_bindings.load_from_arrays () { + local _binding + for _binding; do + declare -n _binding_array="${_binding}" + bind "${_binding_array[*]}" + done +} + +_example.highlight () { + hl < <(echo "${_examples[$_I]}") +} + +### prompt / tagging helpers + +#custom tag:value tuple handlers for _examples array +## tag delineator is ":\n" +_example.unpack() { + printf "%s" "${1#*:$'\n'}" +} + +_tag.unpack() { + local _tag + printf -v _tag "%s" "${1%%:$'\n'*}" + (( ${#_tag} < ${#1} )) || return 1 + printf "%s" "$_tag" +} + +#presentation page header prompt display functions +## specify custom indicator as argument, '+' is default +_ps2.swap () { + local _cur_ps2="${PS2}" + PS2="$_old_ps2" + _old_ps2="$_cur_ps2" +} + +_tag.same_as_next? () { + [[ "$(_tag.unpack "${_examples[$_I]}")" \ + == "$(_tag.unpack "${_examples[$((_I+1))]}")" ]] \ + || return 1 + printf "%s" "${1:-+}" +} + +_tag.same_as_prev? () { + [[ "$(_tag.unpack "${_examples[$_I]}")" \ + == "$(_tag.unpack "${_examples[$((_I-1))]}")" ]] \ + || return 1 + printf "%s" "${1:-+}" +} + +_tag.display () { + local _tag=$(_tag.unpack "${_examples[$_I]}") \ + && printf "[$(_tag.same_as_prev?) %s $(_tag.same_as_next?)]" "${_tag}" +} + +### public functions + +#simple bash code highlighting if the 'highlight' program is installed +## http://www.andre-simon.de/doku/highlight/en/highlight.php +hl () { + local hl=$(which highlight 2>/dev/null) || { + printf "%s\n" "${@}" + return + } + "$hl" --syntax bash -O xterm256 "${@}" +} + +#diff between adjacent examples +ydiff () { + diff -wyW${COLUMNS:-80} \ + <(echo "${_examples[(( _I - ${1:-1} ))]}") \ + <(echo "${_examples[(( _I ))]}") + local diff_err=$? + (( diff_err <= 1 )) || return $diff_err + return 0 +} |