en/en
2023-07-02 23:52:22 -06:00

114 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh -e
# Copyright (c) 2023 Emma Tebibyte
# 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.
argv0="$0"
if test -z "$1"; then
printf "Usage: %s file...\n" "$argv0"
fi
help="Commands:\n\
a appends a line to the current buffer.\n\
c clears the screen.\n\
d deletes a line.\n\
e evaluates a command in the shell environment.\n\
h displays this help message.\n\
i inserts a line into the buffer at position line.\n\
p prints the current buffer.\n\
q terminates editing.\n\
r applies a regular expression to the current buffer.\n\
w writes the current buffer to the file.\n\
\n\
To learn more about a command verb, type \`h verb'.
"
while test -n "$1"; do
content="$(cat "$1")"
cat "$1"
while true; do
printf ">> "
read -r command args
case $command in
"a")
read -r append
content="$(printf "%s\n%s" "$content" "$append")"
;;
"c")
clear
;;
"d")
printf "%s: d: Command not implemented.\n" "$argv0"
;;
"e")
eval "$args"
;;
"h")
if test -z "$args"; then
printf "$help\n"
else
printf "%s: Command-specific help not implemented.\n" "$argv0"
continue
case $args in
"c")
;;
"d")
;;
"e")
;;
"h")
;;
"i")
;;
"p")
;;
"q")
;;
"r")
;;
"w")
;;
esac
fi
;;
"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
;;
"r")
content="$(printf "%s" "$content" | sed "$args")"
;;
"w")
printf "%s\n" "$content" > "$1"
;;
esac
done
shift
done