Compare commits

..

8 Commits

Author SHA1 Message Date
0568b2a8f0 minor nitpicks 2024-06-26 18:41:48 -06:00
c0608baa7b fixed syntax highlighting 2024-06-26 18:37:03 -06:00
41b0293d42 stdin support, added optional highlight(1) dependency 2024-04-18 11:50:20 -06:00
4f0788048b made en print to stdout by default 2024-04-18 10:10:22 -06:00
5bbbd60cfc make bat(1) use terminal colors 2023-08-28 15:43:44 -06:00
4a66427179 removed test file 2023-07-17 13:47:00 -06:00
7ea9657d83 fixed appending 2023-07-14 16:32:37 -06:00
bda7e9eace added string search 2023-07-13 15:58:19 -06:00

125
en
View File

@@ -1,126 +1,115 @@
#!/bin/sh -e #!/bin/sh
# Copyright (c) 2023 Emma Tebibyte # Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
# SPDX-License-Identifier: FSFAP # SPDX-License-Identifier: FSFAP
# #
# Copying and distribution of this file, with or without modification, are # Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this # 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.
argv0="$0" set -e
exec >&2 </dev/tty
if test -z "$1"; then if test -n "$DEBUG"; then
printf "Usage: %s file...\n" "$argv0" 1>&2 set -x
exit 64 # sysexits.h(3) EX_USAGE
fi fi
if ! command -v mktemp >/dev/null 2>&1 if command -v highlight >/dev/null 2>&1
then then
printf "%s: mktemp(1): Missing dependency." "$argv0" export HIGHLIGHT_OPTIONS='--out-format=ansi'
exit 69 # sysexits.h(3) EX_UNAVAILABLE else
alias highlight=cat
fi
if test -z "$1"; then
set -- /dev/stdin
export HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS -S none"
else
export HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS -S \
$(printf '%s\n' "$1" | sed 's/^.*\.//g')"
fi fi
help="$(printf "Commands:\n\ help="$(printf "Commands:\n\
a appends a line to the current buffer.\n\ / display lines containing a string\n\
c clears the screen.\n\ ? display this help message\n\
d deletes a line.\n\ a append a line to the current buffer\n\
e evaluates a command in the shell environment.\n\ c clear the screen\n\
? displays this help message.\n\ d delete a line\n\
i inserts a line into the buffer at position line.\n\ e evaluate a command in the shell environment\n\
p prints the current buffer.\n\ h change syntax highlighting language\n\
q terminates editing.\n\ i insert a line into the buffer at position line\n\
r applies a regular expression to the current buffer.\n\ p print the current buffer\n\
w writes the current buffer to the file.\n\ q terminate editing\n\
r apply a regular expression to the current buffer\n\
x exit the program without writing output\n\
\n\ \n\
To learn more about a command verb, type \`? verb'.\n\ en(1) Copyright (c) 20232024 Emma Tebibyte\n")"
en(1) Copyright (c) 2023 Emma Tebibyte\n")"
while test -n "$1"; do while test -n "$1"; do
content="$(cat "$1")" content="$(cat "$1")"
file="$(printf "%s\n" "$1" | tr -d '/')"
temp_highlight="$(mktemp -t "XXXXX$file")"
# 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() {
printf "%s\n" "$1" >"$temp_highlight"
bat -pp --color always "$temp_highlight"
}
fi
while true; do while true; do
printf ">> " printf ">> " >/dev/tty
read -r command args read -r command args
case $command in case $command in
"/")
if test -z "$args"; then
printf "%s: %s: missing argument\n" "$0" "$args"
else
printf "%s" "$content" | nl | sed -n "/$args/p"
fi
;;
"?") "?")
if test -z "$args"; then if test -z "$args"; then
printf "%s: %s\n" "$argv0" "$help" printf "%s: %s\n" "$0" "$help"
else else
printf "%s: Command-specific help not implemented.\n" "$argv0" 1>&2 printf "%s: %s: %s invalid argument\n" "$0" "$command" "$args" 1>&2
continue continue
fi fi
;; ;;
"a") "a")
read -r append read -r append
content="$content$append" if test -z "$content"; then
;; content="$append"
"c") else
clear content="$(printf "%s\n%s" "$content" "$append")"
fi
;; ;;
"c") clear ;;
"d") "d")
if test -z "$args"; then if test -z "$args"; then
printf "%s: %s: Missing argument.\n" "$argv0" "$command" 1>&2 printf "%s: %s: missing argument.\n" "$0" "$command" 1>&2
else else
for line in $(printf "%s\n" "$args" | sed 's/,/\n/g') for line in $(printf "%s\n" "$args" | sed 's/,/\n/g')
do do
content="$(printf "%s\n" "$content" |\ content="$(printf "%s\n" "$content" |\
awk -v l="$line" 'NR == l {next} {print}')" awk -v l="$line" 'NR == l {next} {print}')"
#del="$(printf "%s\n" "$content" | head -n "$line" | tail -n 1)"
#content="$(printf "%s\n" "$content" | sed -n "/$del/!p")"
done done
fi fi
;; ;;
"e") "e")
eval "$args" || true eval "$args" || true
;; ;;
"h") export HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS -S $args" ;;
"i") "i")
printf "%s: i: Command not implemented.\n" "$argv0" 1>&2 printf "%s: i: command not implemented.\n" "$0" 1>&2
;; ;;
"p") "p")
out="$(syntax_h "$content" 2>/dev/null || printf "%s\n" "$content")" printf "%s\n" "$content" | highlight | nl -ba >/dev/tty
printf "%s\n" "$out" | nl -ba
;;
"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 -r quit
if [ "$quit" = "y" ]; then
break
else
continue
fi
else
break
fi
;; ;;
"q") break ;;
"r") "r")
content="$(printf "%s" "$content" | sed "$args")" content="$(printf "%s" "$content" | sed -e "$args")"
;;
"w")
printf "%s\n" "$content" > "$1"
;; ;;
"x") exit ;;
"") ;;
*) *)
printf "%s: %s: No such command.\n" "$argv0" "$command" printf "%s: %s: unrecognized command\n%s\n" "$0" "$command" "$help"
;; ;;
esac esac
done done
printf '%s\n' "$content"
shift shift
done done