stdin support, added optional highlight(1) dependency
This commit is contained in:
parent
4f0788048b
commit
41b0293d42
55
en
55
en
@ -7,15 +7,17 @@
|
|||||||
# 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.
|
||||||
|
|
||||||
if test -z "$1"; then
|
if command -v highlight >/dev/null 2>&1
|
||||||
printf "Usage: %s file...\n" "$0" 1>&2
|
then
|
||||||
exit 64 # sysexits.h(3) EX_USAGE
|
HIGHLIGHT_OPTIONS='--out-format=ansi'; export HIGHLIGHT_OPTIONS
|
||||||
|
else
|
||||||
|
alias highlight=cat
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! command -v mktemp >/dev/null 2>&1
|
if test -z "$1"; then
|
||||||
then
|
set -- /dev/stdin
|
||||||
printf "%s: mktemp(1): missing dependency" "$0"
|
HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS --syntax=none"
|
||||||
exit 69 # sysexits.h(3) EX_UNAVAILABLE
|
export HIGHLIGHT_OPTIONS
|
||||||
fi
|
fi
|
||||||
|
|
||||||
help="$(printf "Commands:\n\
|
help="$(printf "Commands:\n\
|
||||||
@ -25,30 +27,17 @@ a – append a line to the current buffer\n\
|
|||||||
c – clear the screen\n\
|
c – clear the screen\n\
|
||||||
d – delete a line\n\
|
d – delete a line\n\
|
||||||
e – evaluate a command in the shell environment\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\
|
i – insert a line into the buffer at position line\n\
|
||||||
p – print the current buffer\n\
|
p – print the current buffer\n\
|
||||||
q – terminate editing\n\
|
q – terminate editing\n\
|
||||||
r – applie a regular expression to the current buffer\n\
|
r – apply a regular expression to the current buffer\n\
|
||||||
x – exit the program without writing output\n\
|
x – exit the program without writing output\n\
|
||||||
\n\
|
\n\
|
||||||
en(1) Copyright (c) 2023–2024 Emma Tebibyte\n")"
|
en(1) Copyright (c) 2023–2024 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 it’s available, but remove the frills
|
|
||||||
if command -v bat >/dev/null
|
|
||||||
then
|
|
||||||
# make bat use terminal theme
|
|
||||||
BAT_THEME=ansi; export BAT_THEME
|
|
||||||
# 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 ">> " >/dev/tty
|
printf ">> " >/dev/tty
|
||||||
@ -66,7 +55,7 @@ while test -n "$1"; do
|
|||||||
if test -z "$args"; then
|
if test -z "$args"; then
|
||||||
printf "%s: %s\n" "$0" "$help"
|
printf "%s: %s\n" "$0" "$help"
|
||||||
else
|
else
|
||||||
printf "%s: command-specific help not implemented\n" "$0" 1>&2
|
printf "%s: %s: %s invalid argument\n" "$0" "$command" "$args" 1>&2
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
@ -78,9 +67,7 @@ while test -n "$1"; do
|
|||||||
content="$(printf "%s\n%s" "$content" "$append")"
|
content="$(printf "%s\n%s" "$content" "$append")"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"c")
|
"c") clear ;;
|
||||||
clear
|
|
||||||
;;
|
|
||||||
"d")
|
"d")
|
||||||
if test -z "$args"; then
|
if test -z "$args"; then
|
||||||
printf "%s: %s: Missing argument.\n" "$0" "$command" 1>&2
|
printf "%s: %s: Missing argument.\n" "$0" "$command" 1>&2
|
||||||
@ -95,22 +82,22 @@ while test -n "$1"; do
|
|||||||
"e")
|
"e")
|
||||||
eval "$args" || true
|
eval "$args" || true
|
||||||
;;
|
;;
|
||||||
|
"h")
|
||||||
|
HIGHLIGHT_OPTIONS="$HIGHLIGHT_OPTIONS --syntax=$args"
|
||||||
|
export HIGHLIGHT_OPTIONS
|
||||||
|
;;
|
||||||
"i")
|
"i")
|
||||||
printf "%s: i: Command not implemented.\n" "$0" 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 >/dev/tty
|
|
||||||
;;
|
|
||||||
"q")
|
|
||||||
break
|
|
||||||
;;
|
;;
|
||||||
|
"q") break ;;
|
||||||
"r")
|
"r")
|
||||||
content="$(printf "%s" "$content" | sed -e "$args")"
|
content="$(printf "%s" "$content" | sed -e "$args")"
|
||||||
;;
|
;;
|
||||||
"x")
|
"x") exit ;;
|
||||||
exit
|
"") ;;
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
printf "%s: %s: unrecognized command\n" "$0" "$command"
|
printf "%s: %s: unrecognized command\n" "$0" "$command"
|
||||||
;;
|
;;
|
||||||
|
Loading…
Reference in New Issue
Block a user