added syntax highlighting

This commit is contained in:
Emma Tebibyte 2023-07-03 09:40:13 -06:00
parent a61ae5ab77
commit a5075bc9cd
Signed by: emma
GPG Key ID: 6D661C738815E7DD

55
en
View File

@ -10,7 +10,14 @@
argv0="$0"
if test -z "$1"; then
printf "Usage: %s file...\n" "$argv0"
printf "Usage: %s file...\n" "$argv0" 1>&2
exit 1
fi
if ! command -v mktemp >/dev/null 2>&1
then
printf "%s: mktemp(1): Missing dependency." "$argv0"
exit 1
fi
help="Commands:\n\
@ -30,7 +37,18 @@ To learn more about a command verb, type \`h verb'.
while test -n "$1"; do
content="$(cat "$1")"
cat "$1"
file="$(printf "%s\n" "$1" | tr -d '/')"
# use bat for syntax highlighting if its available, but remove the frills
if command -v bat >/dev/null
then
# there has to be a temp file for writing the highlighted text to
syntax_h() {
temp_highlight="$(mktemp -t "XXXXX$file")"
printf "%s\n" "$1" >"$temp_highlight"
bat -pp --color always "$temp_highlight"
}
fi
while true; do
printf ">> "
@ -45,44 +63,25 @@ while test -n "$1"; do
clear
;;
"d")
printf "%s: d: Command not implemented.\n" "$argv0"
printf "%s: d: Command not implemented.\n" "$argv0" 1>&2
;;
"e")
eval "$args"
;;
"h")
if test -z "$args"; then
printf "$help\n"
printf "%s: %s\n" "$argv0" "$help"
else
printf "%s: Command-specific help not implemented.\n" "$argv0"
printf "%s: Command-specific help not implemented.\n" "$argv0" 1>&2
continue
case $args in
"c")
;;
"d")
;;
"e")
;;
"h")
;;
"i")
;;
"p")
;;
"q")
;;
"r")
;;
"w")
;;
esac
fi
;;
"i")
printf "%s: i: Command not implemented.\n" "$argv0"
printf "%s: i: Command not implemented.\n" "$argv0" 1>&2
;;
"p")
printf "%s\n" "$content"
out="$(syntax_h "$content" || printf "%s\n" "$content")"
printf "%s" "$out" | nl -ba
;;
"q")
if test -n "$(printf "%s\n" "$content" | diff "$1" -)"
@ -90,7 +89,7 @@ while test -n "$1"; do
printf "%s: %s: Unsaved changes in the buffer. Quit anyway? [y/N] " \
"$argv0" \
"$1"
read quit
read -r quit
if [ "$quit" = "y" ]; then
break
else