aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/bindings.sh116
-rw-r--r--lib/functions.sh81
-rw-r--r--lib/prompt.sh5
3 files changed, 202 insertions, 0 deletions
diff --git a/lib/bindings.sh b/lib/bindings.sh
new file mode 100644
index 0000000..a0bb31b
--- /dev/null
+++ b/lib/bindings.sh
@@ -0,0 +1,116 @@
+#!/bin/bash
+
+# source this file to load key bindings for bpresent
+
+# alt-w:
+# clear screen and go back to previous example
+_binding_alt_w=(
+ '"\ew":"' #alt-w
+ '\C-a\C-k' #clear line
+ '$(( _I > 0 && _I-- ))' #decrement unless we've bottomed out
+ '\e\C-e\C-a\C-k' #interpolate to run decrement above and clear the line
+ '\\\n\C-l' #bring us down onto a newline and clear screen
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-W:
+# go back to previous example without clearing the screen
+_binding_alt_W=(
+ '"\eW":"' #alt-W
+ '\C-a\C-k' #clear line
+ '$(( _I > 0 && _I-- ))' #decrement unless we've bottomed out
+ '\e\C-e\C-a\C-k' #interpolate to run decrement above and clear the line
+ '\\\n' #bring us down onto a newline
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-a:
+# clear screen and provide current example again without advancing
+_binding_alt_a=(
+ '"\ea":"' #alt-a
+ '\C-a\C-k' #clear line
+ '\\\n\C-l' #bring us down onto a newline and clear screen
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-A:
+# clear screen and provide current example again without advancing
+_binding_alt_A=(
+ '"\eA":"' #alt-A
+ '\C-a\C-k' #clear line
+ '\\\n' #bring us down onto a newline
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-e:
+# clear screen and advance to next example
+_binding_alt_e=(
+ '"\ee":"' #alt-e
+ '\C-a\C-k' #clear line
+ '$(( _I < ${#_examples[@]} - 1 && _D && _I++,_D=1 ))' #increment,deinitialize
+ '\e\C-e\C-a\C-k' #interpolate to run above and clear the line
+ '\\\n\C-l' #bring us down onto a newline and clear screen
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-E:
+# advance to next example without clearing the screen
+_binding_alt_E=(
+ '"\eE":"' #alt-E
+ '\C-a\C-k' #clear line
+ ' $(( _I < ${#_examples[@]} - 1 && _D && _I++,_D=1 ))' #increment,deinitialize
+ '\e\C-e\C-a\C-k' #interpolate to run above and clear the line
+ '\\\n' #bring us down onto a newline
+ '\"$(_example.unpack \"${_examples[$_I]}\")\"' #pull the current example
+ '\e\C-e"' #interpolate
+ )
+
+# alt-s:
+# side by side diff of previous and current example
+_binding_alt_s=(
+ '"\es":"' #alt-s
+ '\C-a\C-k' #clear line
+ '_example.ydiff' #diff command
+ '\C-l\n"' #clear screen and execute function
+ )
+
+# alt-S:
+# syntax highlighted side by side diff of previous and current example
+_binding_alt_S=(
+ '"\eS":"' #alt-S
+ '\C-a\C-k' #clear line
+ '_example.ydiff | hl --style clarity' #highlighted diff command
+ '\C-l\n"' #clear screen and execute function
+ )
+
+# alt-h:
+# display current example with syntax highlighting
+_binding_alt_h=(
+ '"\eh":"' #alt-h
+ '_example.highlight'
+ '\C-m'
+)
+
+# alt-H:
+# display USAGE text for ariketa
+_binding_alt_H=(
+ '"\eH":"' #alt-H
+ '_ariketa.help'
+ '\C-m'
+)
+
+# alt-P:
+# Toggle between custom PS2 for giving example number and regular '>' prompt
+_binding_alt_P=(
+ '"\eP":"' #alt-P
+ '_ps2.swap'
+ '\C-m"'
+ )
+
+#load bindings
+_bindings.load_from_arrays ${!_binding_*}
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
+}
diff --git a/lib/prompt.sh b/lib/prompt.sh
new file mode 100644
index 0000000..7c0fe43
--- /dev/null
+++ b/lib/prompt.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+[[ $(declare -p PS1) == *\\\$\?* ]] || PS1=' [$?] '"$PS1"
+_old_PS2="$PS2"
+PS2='## example $((_I+1)) / ${#_examples[@]} $(_tag.display)\n\n'