From 8127bf73501c335519dae40101c3b850c4a64e54 Mon Sep 17 00:00:00 2001 From: James Pannacciulli Date: Sun, 11 Jun 2017 11:06:44 -0700 Subject: ariketa first commit ariketa: bash example code quickloader for presentations --- lib/functions.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lib/functions.sh (limited to 'lib/functions.sh') 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 +} -- cgit v1.2.3