Various shell scripts for use on the RTC server. Not your official RTC stuff!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

175 lines
4.4 KiB

#! /bin/bash
# edlog - an ed(1)/mail(1) inspired shlog reader/editor
# Author: samhunter@rawtext.club
# https://git.rawtext.club/samhunter/rtc-shell-scripts
readonly PROGNAME="${0##*/}"
readonly VERSION="1.3"
declare -i ST=0
declare -i PTR=0
declare -i SCR=$((LINES-1))
declare -a msgs
declare -i MCNT=0
readonly SHLOGDIR="/home/papa/share/plshlog"
readonly USERSHLOG="$HOME/.shlog"
readonly MSGIDPAT='[0-9]{4}'
if (( LINES == 0 )) ; then
if command -v tput > /dev/null ; then
export COLUMNS="$(tput cols)"
export LINES="$(tput lines)"
SCR=$((LINES-1))
else
cat <<-_error_
$PROGNAME encountered a configuration issue:
Export your display variables with:
export COLUMNS LINES
_error_
exit 1
fi
fi
help () {
sed -e 's!_\(.\)_!\x1b[1m\1\x1b[0m!g' <<-_help_
- ? - this help
- g - _g_o to first page
- h - show _h_eaders
- t - _t_ype currently selected post
- . - type currently selected (_._ = "dot") post
- m - (_m_ake a) new post
- n - _n_ext post
- p - _p_revious post
- r - _r_eply
- | - filter ("_|_" = "pipe") post
- ! - execute(_!_) an external program
_ _ !vim \$FILE - open the current post's file in vim
- q - _q_uit
- z - show next page
- msgID - show post (format: 0023)
_help_
}
headers::display () {
local -i START=${1:-1}
local -i END=$((SCR+START))
[ $END -le $MCNT ] || { let END=$MCNT; echo "On last screenful of posts." ; }
for ((i=START;i<END;i++)) {
if (( $i == $PTR )) ; then
MARK="-> "
else
MARK=""
fi
printf "%3s %-70.70s\n" "$MARK" "${msgs[$i]}"
}
}
post::getfilename () {
local POST
if [ -n "$1" ]; then
POST="$1"
else
POST="$(printf "%4.4d" $PTR)"
fi
if [[ $((10#$POST)) -ge $MCNT ]]; then
PTR=$((MCNT-1))
POST=$(printf "%4.4d" $PTR)
fi
local FILE="$(readlink *$POST*)"
echo "$FILE"
}
post::new () {
local AUTHOR="${USER}"
local TITLE
read -e -p "Topic (empty=cancel): " TITLE
[ -n "$TITLE" ] || return
TOPIC="$(date +"%Y-%m-%d")-$(sed -e 's! !_!g;' <<<"$TITLE").txt"
echo "# $TITLE" > "$USERSHLOG/$TOPIC"
#sed -e 's!^!> !' "$FILE" >> "$USERSHLOG/$TOPIC"
#echo -e "\n\nIn reply to: $FILE" >> "$USERSHLOG/$TOPIC"
${EDITOR:-ed} "$USERSHLOG/$TOPIC"
chmod 0644 "$USERSHLOG/$TOPIC"
}
post::extras () {
# XXX to be implemented
local FUN="$1"
FUN="${FUN/:/}"
local FILE="$(post::getfilename)"
echo "$FUN"
}
post::reply () {
local POST="${1:-'0000'}"
FILE="$(post::getfilename "$@")"
local AUTHOR="${FILE##/home/}"
AUTHOR="${AUTHOR%%/*}"
local TITLE="$(sed -e 's!^.*/...........!!;s!_! !g;s!.txt$!!' <<<"$FILE")"
TOPIC="$(date +"%Y-%m-%d")-Re:_$(sed -e 's! !_!g;' <<<"$TITLE").txt"
echo "# Re: $TITLE" > "$USERSHLOG/$TOPIC"
sed -e 's!^!> !' "$FILE" >> "$USERSHLOG/$TOPIC"
echo -e "\n\nIn reply to: $FILE" >> "$USERSHLOG/$TOPIC"
${EDITOR:-ed} "$USERSHLOG/$TOPIC"
chmod 0644 "$USERSHLOG/$TOPIC"
}
post::show () {
local POST="${1:-'0000'}"
FILE="$(post::getfilename "$1")"
local AUTHOR="${FILE##/home/}"
AUTHOR="${AUTHOR%%/*}"
local TITLE="$(sed -e 's!^.*/...........!!;s!_! !g;s!.txt$!!' <<<"$FILE")"
echo -ne "\n\n"
echo -ne "\x1b[1m$TITLE\x1b[0m\n"
echo -ne "\x1b[1mAuthor:\x1b[0m $AUTHOR\n\n"
cat "$FILE"; let PTR=$((10#$POST))
echo -ne "\n\x1b[3mSource: $FILE\x1b[0m\n"
echo -ne "\n------------------------------------\n"
}
post::filter () {
local FILTER="$1"
FILTER="${FILTER/|/}"
local FILE="$(post::getfilename)"
eval "cat "$FILE" | $FILTER"
}
cmd::exec () {
local CMD="$*"
CMD="${CMD/!/}"
local FILE="$(post::getfilename)"
local AUTHOR="${FILE##/home/}"
AUTHOR="${AUTHOR%%/*}"
local TITLE="$(sed -e 's!^.*/...........!!;s!_! !g;s!.txt$!!' <<<"$FILE")"
eval "$CMD"
}
#----
cd $SHLOGDIR
mapfile -t msgs < <(ls -1 *|sed -e 's+^.*/++'|sort -n)
MCNT=$(( ${#msgs[@]}))
echo $MCNT
headers::display $ST
while true ; do
[ $PTR -le $(($MCNT-1)) ] || PTR=$((MCNT-1))
[ $PTR -ge 0 ] || PTR=0
read -e -p "> " CMD
case "$CMD" in
g) let ST=0 ; let PTR=0; headers::display $ST ;;
h) headers::display $ST ;;
m) post::new ;;
n) [ $PTR -lt $((MCNT-1)) ] && ((PTR++)); post::show $(printf "%4.4d" $PTR) ;;
p) [ $PTR -gt 0 ] && ((PTR--)) ; post::show $(printf "%4.4d" $PTR) ;;
r) post::reply $(printf "%4.4d" $PTR) ;;
q) break ;;
z) [[ $ST -le $((MCNT-SCR)) ]] && let ST+=SCR; headers::display $ST ;;
''|'.'|t) post::show $(printf "%4.4d" $PTR) ;;
'?') help ;;
\|*) post::filter "$CMD" ;;
\:*) post::extras "$CMD" ;;
\!*) cmd::exec "$CMD" ;;
*) [[ "$CMD" =~ $MSGIDPAT ]] && post::show ${BASH_REMATCH[0]} || echo -e "?\a" ;;
esac
done