111 lines
2.6 KiB
Bash
Executable File
111 lines
2.6 KiB
Bash
Executable File
#!/bin/sh -e
|
||
|
||
# Copyright (c) 2023-2024 Emma Tebibyte <emma@tebibyte.media>
|
||
# 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.
|
||
|
||
if command -v highlight >/dev/null 2>&1
|
||
then
|
||
HIGHLIGHT_OPTIONS='--out-format=ansi'; export HIGHLIGHT_OPTIONS
|
||
else
|
||
alias highlight=cat
|
||
fi
|
||
|
||
if test -z "$1"; then
|
||
set -- /dev/stdin
|
||
HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS --syntax=none"
|
||
export HIGHLIGHT_OPTIONS
|
||
fi
|
||
|
||
help="$(printf "Commands:\n\
|
||
/ – display lines containing a string\n\
|
||
? – display this help message\n\
|
||
a – append a line to the current buffer\n\
|
||
c – clear the screen\n\
|
||
d – delete a line\n\
|
||
e – evaluate a command in the shell environment\n\
|
||
h – change syntax highlighting language\n\
|
||
i – insert a line into the buffer at position line\n\
|
||
p – print the current buffer\n\
|
||
q – terminate editing\n\
|
||
r – apply a regular expression to the current buffer\n\
|
||
x – exit the program without writing output\n\
|
||
\n\
|
||
en(1) Copyright (c) 2023–2024 Emma Tebibyte\n")"
|
||
|
||
while test -n "$1"; do
|
||
content="$(cat "$1")"
|
||
|
||
while true; do
|
||
printf ">> " >/dev/tty
|
||
read -r command args
|
||
|
||
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
|
||
printf "%s: %s\n" "$0" "$help"
|
||
else
|
||
printf "%s: %s: %s invalid argument\n" "$0" "$command" "$args" 1>&2
|
||
continue
|
||
fi
|
||
;;
|
||
"a")
|
||
read -r append
|
||
if test -z "$content"; then
|
||
content="$append"
|
||
else
|
||
content="$(printf "%s\n%s" "$content" "$append")"
|
||
fi
|
||
;;
|
||
"c") clear ;;
|
||
"d")
|
||
if test -z "$args"; then
|
||
printf "%s: %s: Missing argument.\n" "$0" "$command" 1>&2
|
||
else
|
||
for line in $(printf "%s\n" "$args" | sed 's/,/\n/g')
|
||
do
|
||
content="$(printf "%s\n" "$content" |\
|
||
awk -v l="$line" 'NR == l {next} {print}')"
|
||
done
|
||
fi
|
||
;;
|
||
"e")
|
||
eval "$args" || true
|
||
;;
|
||
"h")
|
||
HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS --syntax=$args"
|
||
export HIGHLIGHT_OPTIONS
|
||
;;
|
||
"i")
|
||
printf "%s: i: Command not implemented.\n" "$0" 1>&2
|
||
;;
|
||
"p")
|
||
printf "%s\n" "$content" | highlight | nl -ba >/dev/tty
|
||
;;
|
||
"q") break ;;
|
||
"r")
|
||
content="$(printf "%s" "$content" | sed -e "$args")"
|
||
;;
|
||
"x") exit ;;
|
||
"") ;;
|
||
*)
|
||
printf "%s: %s: unrecognized command\n" "$0" "$command"
|
||
;;
|
||
esac
|
||
done
|
||
|
||
printf '%s\n' "$content"
|
||
|
||
shift
|
||
done
|