rwxrob-dot/scripts/snip

56 lines
891 B
Plaintext
Raw Normal View History

2022-02-09 07:29:05 +00:00
#!/usr/bin/bash
[[ -z "$SNIPPETS" ]] && echo "SNIPPETS directory undefined" && exit 1
snippet() {
> "$SNIPPETS/last"
while IFS= read -r line; do
echo "$line"
echo "$line" >> "$SNIPPETS/last"
done
}
snip() {
if (( $# == 0 ));then
snippet "$@"
return $?
fi
local name="$1"
shift
local path="$SNIPPETS/$name"
if [[ -r $path ]]; then
local buf="$(<$path)"
if [[ -s /dev/stdin ]]; then
local in="$(</dev/stdin)"
else
local in=""
fi
if [[ $# = 0 && -z $in ]]; then
echo "$buf"
return
fi
if [[ $# > 0 ]]; then
local -i n=1
for arg in $@; do
buf=${buf//\{$n\}/$arg}
((n++))
done
echo "$buf"
else
while IFS=$'\n' read -r argline; do
if [[ -n $argline ]]; then
IFS=" " snip $name $argline
fi
done
fi
fi
}
snip "$@"