shell-base/modules/editing/block/block.sh

165 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
BLOCK_FILE=""
BLOCK_NAME='BLOCK'
BLOCK_COMMENT_CHAR="#"
# a comment added before the block
BLOCK_DESCRIPTION=""
function __update_block () {
if [ ! -z "$BLOCK_FILE" ]; then
mkdir -p "$(dirname $BLOCK_FILE)"
touch $BLOCK_FILE
tail -c1 $BLOCK_FILE | read -r _ || echo >> $BLOCK_FILE
fi
BLOCK_BEGIN="$BLOCK_COMMENT_CHAR --- ${BLOCK_NAME} BEGIN ---"
BLOCK_END="$BLOCK_COMMENT_CHAR --- ${BLOCK_NAME} END ---"
BLOCK="\n$([ -z "$BLOCK_DESCRIPTION" ] || echo "${BLOCK_COMMENT_CHAR} ${BLOCK_DESCRIPTION}\n")${BLOCK_BEGIN}\n${BLOCK_END}"
# for testing
# BLOCK="a line before\n${BLOCK}\nanother line outside"
}
# set block to defaults
__update_block
# echo -e "default block template follows use: block-set before block-add to change>\n" $BLOCK
function set_block () {
declare OPTION
declare OPTARG
declare OPTIND
while getopts ':f:c:d:n:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
f)
BLOCK_FILE=$OPTARG
echo File set to $BLOCK_FILE
;;
c)
BLOCK_COMMENT_CHAR=$OPTARG
;;
d)
BLOCK_DESCRIPTION=$OPTARG
;;
n)
BLOCK_NAME=$OPTARG
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
__update_block
# echo -e "new block template follows use: block-add >\n" $BLOCK
# echo ----
echo $([ -e "$BLOCK_FILE" ] || echo "Warning: No output file set yet, use block-set -f <path>" && echo "block will be placed in $BLOCK_FILE")
# echo -------
}
function add_block () {
block_remove_content
# only add if it doesn't exist, existance is only based on block begin
local ADD
ADD=$(echo "${BLOCK}" | sed 's/\//\\\//g')
if [ -e "$BLOCK_FILE" ]; then
sed -n '/^[ \t]*'"${BLOCK_BEGIN}"'/{q50}' "${BLOCK_FILE}"
[ $? == 50 ] && return
WHERE=${1-${BLOCK_BEGIN}} # default is end
# echo Where $WHERE
sed -i '/'"${WHERE}"'/{s/.*/&\n'"${ADD}"'/;h};${x;/^$/{s//'"$ADD"'/;H};x}' "${BLOCK_FILE}"
[[ $1 ]] && block_add_line $1
else
echo "no file set in which to add block, use: ${RED_COLOR}set-block -f <file path>"
fi
}
list_block () {
if [[ $1 =~ -?r(aw)? ]]; then
echo $BLOCK
else
local block
block=${BLOCK##'\n'}'\n'
printf "$block"
fi
}
get_block_file () {
[ $BLOCK_FILE ] && echo ${BLOCK_FILE}
[[ $1 = -v ]] && echo "no output file set, use set_block -f <file-path>"
}
modify_block () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${BLOCK_BEGIN}"'/b
/'"${BLOCK_END}"'/b
s/'"${1}"'/'"${2}"'/
}' "${BLOCK_FILE}"
}
block_remove_leading () {
printf "%s\n" "$(tac $BLOCK_FILE \
| sed '/'"${BLOCK_BEGIN}"'/,/^./{ /^[ \t]*$/d}' | tac)" > $BLOCK_FILE
}
remove_block () {
[[ "$BLOCK_DESCRIPTION" ]] || sed -ni '/'"${BLOCK_BEGIN}"'/{x;d;};1h;1!{x;p;};${x;p;}' "${BLOCK_FILE}"
block_remove_leading
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
d
}' "${BLOCK_FILE}"
}
block_remove_content () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${BLOCK_BEGIN}"'/b
/'"${BLOCK_END}"'/b
d
}' "${BLOCK_FILE}"
}
block_add_line () {
# echo argument count $#, $1, $2, $3
local ADD
ADD="/^${BLOCK_BEGIN}/a\\${1}"
if [ $# -gt 1 ]; then
if [ "$1" == "--end" ];then
ADD="/${BLOCK_END}/i\\${2}"
else
if [ "$1" == "--before" ];then
ADD="/${2}/i\\${3}"
else
ADD="/${1}/a\\${2}"
fi
fi
fi
# echo $ADD
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
'"${ADD}"'
}' "${BLOCK_FILE}"
}
block_remove_line () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${1}"'/d
}' "${BLOCK_FILE}"
}
block_change_line () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${1}"'/c\'"${2}"'
}' "${BLOCK_FILE}"
}
block_modify_line () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${BLOCK_BEGIN}"'/b
/'"${BLOCK_END}"'/b
/'"${1}"'/{s/'"${2}"'/'"${3}"'/}
}' "${BLOCK_FILE}"
}