add stuffz

This commit is contained in:
dtb 2023-07-03 09:02:46 +00:00
parent dadfad0650
commit e7fb0c055e

104
en
View File

@ -1,63 +1,71 @@
#!/bin/sh -e
# Copyright (c) 2023 Emma Tebibyte
# Copyright (c) 2023 Emma Tebibyte, dtb
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved. This file is offered as-is, without any warranty.
# notice are preserved. This file is offered as-is, without any warranty.'
#
# dtb forfeits any and all copyright to their respective changes to this document.
argv0="$0"
exec >&2 </dev/tty
if test -z "$1"; then
printf "Usage: %s file...\n" "$argv0"
fi
while test -n "$1"; do
content="$(cat "$1")"
cat "$1"
ingest(){
buffer=''
while true
do
line="$(head -n 1)"
test "$line" = '.' \
&& break
buffer="$(printf '%s%s\n' "$buffer" "$line")"
done
printf '%s' "$buffer"
}
len(){ printf "%s\n" "$1" | wc -c; }
en(){
while true; do
printf ">> "
read -r command args
line="$(head -n 1)"
dot="(printf '%s\n' "$line" | sed 's/[[^[:digit:]]*?$//g')"
if printf '%s\n' "$dot" | grep ',' >/dev/null 2>&1
then
dot_start="$(printf '%s\n' "$dot" | cut -d ',' -f 1)"
dot_end="$(printf '%s\n' "$dot" | cut -d ',' -f 2)"
else
dot_start="$dot"
dot_end="$dot"
fi
case $command in
"c")
clear
;;
"d")
printf "%s: d: Command not implemented.\n" "$argv0"
;;
"i")
printf "%s: i: Command not implemented.\n" "$argv0"
;;
"p")
printf "%s\n" "$content"
;;
"q")
if test -n "$(printf "%s\n" "$content" | diff "$1" -)"
then
printf "%s: %s: Unsaved changes in the buffer. Quit anyway? [y/N] " \
"$argv0" \
"$1"
read quit
if [ "$quit" = "y" ]; then
break
else
continue
fi
else
break
fi
;;
"s")
content="$(printf "%s" "$content" | sed "$args")"
;;
"w")
printf "%s\n" "$content" > "$1"
;;
command="$(printf '%s\n' "$line" | sed s/"$dot"//)"
case "$command" in
# argless
a* | d* | i* | p* | q*)
! test "$(len "$command")" = 1 \
&& printf "%s: Extraneous input.\n" "$(printf '%s\n' "$command" | head -c 1)" \
&& continue
case "$command" in
a*)
sed -i "$f" "$dot_end"a\
"$(ingest)" ;;
d*) sed -i "$f" "$dot_start,$dot_end"d ;;
i*) sed -i "$f" "$dot_start"i\
"$(ingest)" ;;
p*) <"$f" sed "$dot_start,$dot_end"p ;;
q*) return 0 ;;
esac
# argful
s*) printf "%s: Command not implemented.\n" s ;;
w*) printf "%s: Command not implemented.\n" w ;;
*) printf "?\n" ;;
esac
done
}
shift
done
test -n "$1" \
|| en
while test -n "$1"
do file="$1" en && shift || break
done