mirror of
https://github.com/tonydamage/nux-env.git
synced 2025-12-11 13:24:28 +01:00
Added skeleton of new library functions.
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
parent
e7bb96f730
commit
ff4e509d1c
4 changed files with 249 additions and 0 deletions
22
inc/nux.cfg.inc.sh
Normal file
22
inc/nux.cfg.inc.sh
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
## #nux.cfg - NUX Script configuration management library
|
||||||
|
##
|
||||||
|
## #Public functions:
|
||||||
|
##
|
||||||
|
## nux.cfg.include [<app>] <config>
|
||||||
|
## Includes app specific configuration file.
|
||||||
|
##
|
||||||
|
function nux.cfg.include {
|
||||||
|
:
|
||||||
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
## nux.cfg.global.path [<path>]
|
||||||
|
## Returns global path., otherwise provides specified.
|
||||||
|
##
|
||||||
|
function nux.cfg.global.path {
|
||||||
|
:
|
||||||
|
}
|
||||||
|
|
||||||
|
function nux.cfg.list {
|
||||||
|
:
|
||||||
|
}
|
||||||
107
inc/nux.json.inc.sh
Normal file
107
inc/nux.json.inc.sh
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
## #nux.json NUX Script JSON Helper library
|
||||||
|
##
|
||||||
|
## #Public functions
|
||||||
|
|
||||||
|
## nux.json.start
|
||||||
|
## Starts operation on new JSON. Resets write operations stack and JSON
|
||||||
|
## source file to empty (useful for creating JSON from scratch.)
|
||||||
|
function nux.json.start {
|
||||||
|
nux_json_opstack=".";
|
||||||
|
nux_json_source="-n";
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.json.open <file>
|
||||||
|
## Opens JSON File for reading and writing using *nux.json.open* and
|
||||||
|
## *nux.json.write* commands.
|
||||||
|
##
|
||||||
|
## In order to work with empty JSON use *-n* as *file*. This allows
|
||||||
|
## for creating new JSON from scratch.
|
||||||
|
##
|
||||||
|
## NOTE: Open does not reset operation stack. To reset operation stack
|
||||||
|
## and opened file use *nux.json.start*
|
||||||
|
function nux.json.open {
|
||||||
|
local file="$1"
|
||||||
|
if [ -f $file ]; then
|
||||||
|
nux_json_source="$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.json.read <path>
|
||||||
|
## Reads *path* from currently opened JSON file.
|
||||||
|
## NOTE: Read does not see changes performed by *nux.json.write* unless
|
||||||
|
## these changes were flushed using *nux.json.flush*.
|
||||||
|
function nux.json.read {
|
||||||
|
local path=".$1";
|
||||||
|
jq -r "$path" "$nux_json_source";
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.json.write <path> <value>
|
||||||
|
## Adds write operation to action stack. Writes are not performed
|
||||||
|
## immediately, but rather when *nux.json.flush* is invoked.
|
||||||
|
## This allows for batching of operations or opting out of actually
|
||||||
|
## modifying file.
|
||||||
|
function nux.json.write {
|
||||||
|
local path=".$1";
|
||||||
|
local value="$2";
|
||||||
|
nux_json_opstack="${nux_json_opstack} | $path |= \"$value\""
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.json.flush [<target>]
|
||||||
|
## Flushes any write operations to specified *target* file.
|
||||||
|
## If *target* is not specified JSON is outputted to *STDIN*.
|
||||||
|
##
|
||||||
|
## NOTE: Flush does not reset operation stack. To reset
|
||||||
|
## operation stack and opened file use *nux.json.start*
|
||||||
|
##
|
||||||
|
function nux.json.flush {
|
||||||
|
local target="$1"
|
||||||
|
if [ -n "$target" ]; then
|
||||||
|
local write_target="$target";
|
||||||
|
if [ "$nux_json_source" == "$target" ]; then
|
||||||
|
write_target=$(mktemp $(dirname "$target")/tempXXXXXX.json)
|
||||||
|
fi
|
||||||
|
jq -r "$nux_json_opstack" "$nux_json_source" > "$write_target"
|
||||||
|
if [ "$nux_json_source" == "$target" ]; then
|
||||||
|
mv -f "$write_target" "$target"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
jq -r "$nux_json_opstack" "$nux_json_source"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.json.shorthands
|
||||||
|
## Exposes shorthands for writing and reading from JSON file.
|
||||||
|
## *njw* - nux.json.write
|
||||||
|
## *njr* - nux.json.read
|
||||||
|
##
|
||||||
|
function nux.json.shorthands {
|
||||||
|
function njw {
|
||||||
|
nux.json.write "$@"
|
||||||
|
}
|
||||||
|
function njr {
|
||||||
|
nux.json.read "$@"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
## # Usage Notes
|
||||||
|
##
|
||||||
|
## ## Operation stack and flush
|
||||||
|
##
|
||||||
|
## As mentioned in documentation for *nux.json.flush* write stack
|
||||||
|
## is not removed, but rather kept alone, separatly from reference
|
||||||
|
## to open file. This allows for having modification template
|
||||||
|
## which could be executed on multiple files.
|
||||||
|
##
|
||||||
|
## The following example adds meta.author and meta.email
|
||||||
|
## to every JSON in directory.
|
||||||
|
##
|
||||||
|
## *nux.json.start*
|
||||||
|
## *nux.json.write* meta.author "Tony Tkacik"
|
||||||
|
## *nux.json.write* meta.email "example@example.com"
|
||||||
|
## *for* f *in* "*.json";
|
||||||
|
## *do*
|
||||||
|
## *nux.json.open* "$f"
|
||||||
|
## *nux.json.flush* "$f"
|
||||||
|
## *done*;
|
||||||
|
##
|
||||||
|
##
|
||||||
119
inc/nux.repl.inc.sh
Normal file
119
inc/nux.repl.inc.sh
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
|
||||||
|
|
||||||
|
## nux.repl.start <processor> [<prompt>] [<complete>]
|
||||||
|
## Starts NUX REPL
|
||||||
|
##
|
||||||
|
## FIXME: Add autocompletion
|
||||||
|
function nux.repl.start {
|
||||||
|
function .null() {
|
||||||
|
:
|
||||||
|
}
|
||||||
|
local processor=$1;
|
||||||
|
local prompt=${2:-.null};
|
||||||
|
local complete=$3;
|
||||||
|
local command=""
|
||||||
|
|
||||||
|
if nux.check.function $complete; then
|
||||||
|
bind -x '"\C-i": "nux.repl.completion $prompt $complete"' &> /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
while IFS=" " read -e -p "$($prompt)" options
|
||||||
|
do
|
||||||
|
history -s "$options" > /dev/null 2>&1
|
||||||
|
nux.log debug Readed line is: $nc_white$options$nc_end
|
||||||
|
command=$(echo $options | cut -d" " -f1)
|
||||||
|
arguments=${options#"$command"}
|
||||||
|
nux.log debug Command: $command, arguments: $arguments
|
||||||
|
case "$command" in
|
||||||
|
exit)
|
||||||
|
break;;
|
||||||
|
*)
|
||||||
|
$processor $command $arguments
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
## nux.repl.completion <prompt> <completion>
|
||||||
|
##
|
||||||
|
##
|
||||||
|
## This function is modified version of
|
||||||
|
## https://github.com/mchav/with/blob/master/with
|
||||||
|
|
||||||
|
function nux.repl.completion() {
|
||||||
|
local prompt=$1;
|
||||||
|
local compgen_command=$2;
|
||||||
|
shift;shift;
|
||||||
|
# print readline's prompt for visual separation
|
||||||
|
if [ "$#" -eq 0 ]; then
|
||||||
|
echo "$($prompt)$READLINE_LINE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
pmpt=$($prompt)
|
||||||
|
# remove part after readline cursor from completion line
|
||||||
|
local completion_line completion_word
|
||||||
|
completion_line="${READLINE_LINE:0:READLINE_POINT}"
|
||||||
|
completion_word="${completion_line##* }"
|
||||||
|
|
||||||
|
# set completion cursor according to pmpt length
|
||||||
|
COMP_POINT=$((${#pmpt}+${#completion_line}+1))
|
||||||
|
COMP_WORDBREAKS="\n\"'><=;|&(:"
|
||||||
|
COMP_LINE="$pmpt $completion_line"
|
||||||
|
# TODO: the purpose of these variables is still unclear
|
||||||
|
# COMP_TYPE=63
|
||||||
|
# COMP_KEY=9
|
||||||
|
|
||||||
|
# get index of word to be completed
|
||||||
|
local whitespaces_count escaped_whitespaces_count
|
||||||
|
whitespaces_count=$(echo "$COMP_LINE" | grep -o ' ' | wc -l)
|
||||||
|
escaped_whitespaces_count=$(echo "$COMP_LINE" | grep -o '\\ ' | wc -l)
|
||||||
|
COMP_CWORD=$((whitespaces_count-escaped_whitespaces_count))
|
||||||
|
|
||||||
|
# get sourced completion command
|
||||||
|
local program_name complete_command
|
||||||
|
program_name=${COMP_WORDS[0]}
|
||||||
|
program_name=$(basename "$program_name")
|
||||||
|
complete_command=$(complete -p | grep " ${program_name}$")
|
||||||
|
|
||||||
|
COMPREPLY=()
|
||||||
|
COMPREPLY=($($compgen_command "$completion_line"))
|
||||||
|
|
||||||
|
# get commmon prefix of available completions
|
||||||
|
local completions_prefix readline_prefix readline_suffix
|
||||||
|
completions_prefix=$(printf "%s\n" "${COMPREPLY[@]}" | \
|
||||||
|
sed -e '$!{N;s/^\(.*\).*\n\1.*$/\1\n\1/;D;}' | xargs)
|
||||||
|
readline_prefix="${READLINE_LINE:0:READLINE_POINT}"
|
||||||
|
readline_suffix="${READLINE_LINE:READLINE_POINT}"
|
||||||
|
# remove the word to be completed
|
||||||
|
readline_prefix=$(sed s/'\w*$'// <(echo "$readline_prefix") | xargs)
|
||||||
|
|
||||||
|
READLINE_LINE=""
|
||||||
|
if [[ "$readline_prefix" != "" ]]; then
|
||||||
|
READLINE_LINE="$readline_prefix "
|
||||||
|
fi
|
||||||
|
|
||||||
|
READLINE_LINE="$READLINE_LINE$completions_prefix"
|
||||||
|
# adjust readline cursor position
|
||||||
|
READLINE_POINT=$((${#READLINE_LINE}+1))
|
||||||
|
|
||||||
|
if [[ "$readline_suffix" != "" ]]; then
|
||||||
|
READLINE_LINE="$READLINE_LINE $readline_suffix"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local completions_count display_all
|
||||||
|
completions_count=${#COMPREPLY[@]}
|
||||||
|
display_all="y"
|
||||||
|
if [[ $completions_count -eq 1 ]]; then
|
||||||
|
READLINE_LINE=$(echo "$READLINE_LINE" | xargs)
|
||||||
|
READLINE_LINE="$READLINE_LINE "
|
||||||
|
return
|
||||||
|
elif [[ $completions_count -gt 80 ]]; then
|
||||||
|
echo -en "Display all $completions_count possibilities? (y or n) "
|
||||||
|
read -N 1 display_all
|
||||||
|
echo "$display_all"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$display_all" = "y" ]]; then
|
||||||
|
for completion in "${COMPREPLY[@]}"; do echo "$completion"; done | column
|
||||||
|
fi
|
||||||
|
}
|
||||||
1
inc/nux.thumb.inc.sh
Normal file
1
inc/nux.thumb.inc.sh
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
nux.use nux.fs
|
||||||
Loading…
Add table
Add a link
Reference in a new issue