From a5075bc9cd6c7c25d94febd02ced80e9044c7973 Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 3 Jul 2023 09:40:13 -0600 Subject: [PATCH] added syntax highlighting --- en | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/en b/en index 4938099..c203165 100755 --- a/en +++ b/en @@ -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 it’s 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