track escape sequences

This commit is contained in:
Dylan Araps committed 2020-08-04 15:54:39 +03:00
1 parent 916e88c51e
commit 00b4f74bfb
1 file changed
+60 -17
+60 -17
View File
@@ -1,15 +1,37 @@
#!/bin/sh #!/bin/sh
esc() {
case $1 in
cursorhome) printf '%s[%s;%sH' "$esc_char" "$2" "$3" ;; # VT100
ED[0-2]) printf '%s[%sJ' "$esc_char" "${1#ED}" ;; # VT100
EL[0-2]) printf '%s[%sK' "$esc_char" "${1#EL}" ;; # VT100
DECSTBM) printf '%s[%s;%sr' "$esc_char" "$2" "$3" ;; # VT100
DECAWM) printf '%s[?7%s' "$esc_char" "$2" ;; # VT100
DECTCEM) printf '%s[?25%s' "$esc_char" "$2" ;; # VT500?
screen_alt) printf '%s[?1049%s' "$esc_char" "$2" ;; # xterm
CUU) printf '%s[%sA' "$esc_char" "$2" ;; # VT100
CUD) printf '%s[%sB' "$esc_char" "$2" ;; # VT100
IL) printf '%s[%sL' "$esc_char" "$2" ;; # VT102?
SGR) printf '%s[%s;%sm' "$esc_char" "$2" "$3" ;; # VT100
DECSC) printf '%s%s' "$esc_char" "$2" ;; # VT100
esac
}
term_setup() { term_setup() {
stty=$(stty -g) stty=$(stty -g)
stty -icanon -echo stty -icanon -echo
printf '\033[?7l\033[?25l\033[2J\033[1;%sr' \ esc DECAWM l
"$((LINES - 2))" esc DECTCEM l
esc ED2
esc DECSTBM 1 "$((LINES - 2))"
} }
term_reset() { term_reset() {
printf '\033[?7h\033[?25h\033[2J\033[;r' esc DECAWM h
esc DECTCEM h
esc ED2
esc DECSTBM
stty "$stty" stty "$stty"
} }
@@ -29,9 +51,11 @@ term_resize() {
} }
cmd_run() { cmd_run() {
printf '\033[?1049h\033[?25h' esc screen_alt h
esc DECTCEM h
"$@" "$@"
printf '\033[?25l\033[?1049l' esc screen_alt l
esc DECTCEM l
} }
file_escape() { file_escape() {
@@ -68,7 +92,8 @@ hist_search() {
} }
list_print() { list_print() {
printf '\033[2J\033[H' esc ED2
esc cursorhome
i=1 i=1
end=$((bottom + 1)) end=$((bottom + 1))
@@ -89,17 +114,22 @@ list_print() {
file_escape "$file" file_escape "$file"
case $i in case $i in
"$y2") printf '\033[34;7m' "$y2") esc SGR 34 7
esac esac
case $((i - end)) in case $((i - end)) in
-*) printf '%s\033[m\033[B\r' "$safe" -*)
printf %s "$safe"
esc SGR
esc CUD
printf '\r'
;;
esac esac
i=$((i + 1)) i=$((i + 1))
done done
printf '\033[%sH' "$((y > bottom ? y2 : y))" esc cursorhome "$((y > bottom ? y2 : y))"
} }
redraw() { redraw() {
@@ -108,12 +138,20 @@ redraw() {
} }
status_line() { status_line() {
printf '\0337\033[%sH\033[31;7m%-*s\033[m\0338' \ esc DECSC 7
"$LINES" "$COLUMNS" "$1" esc cursorhome "$LINES"
esc SGR 31 7
printf '%-*s' "$COLUMNS" "$1"
esc SGR
esc DECSC 8
} }
prompt() { prompt() {
printf '\0337\033[%sH%s\033[?25h\033[K' "$LINES" "$1" esc DECSC 7
esc cursorhome "$LINES"
printf %s "$1"
esc DECTCEM h
esc EL0
case $2 in case $2 in
r) r)
@@ -123,7 +161,8 @@ prompt() {
;; ;;
esac esac
printf '\0338\033[?25l' esc DECSC 8
esc DECTCEM l
status_line "($y/$#) $PWD" status_line "($y/$#) $PWD"
} }
@@ -131,12 +170,16 @@ print_line() {
offset=$1 offset=$1
case $offset in case $offset in
"$y") printf '\033[34;7m' "$y") esc SGR 34 7
esac esac
shift "$offset" shift "$offset"
file_escape "$1" file_escape "$1"
printf '\033[K%s\033[m\r' "$safe"
esc EL0
printf %s "$safe"
esc SGR
printf '\r'
case $offset in case $offset in
"$y") cur=$1 "$y") cur=$1
@@ -182,11 +225,11 @@ main() {
case $y2 in case $y2 in
1) 1)
printf '\033[L' esc IL
;; ;;
*) *)
printf '\033[A' esc CUU
y2=$((y2 > 1 ? y2 - 1 : 1)) y2=$((y2 > 1 ? y2 - 1 : 1))
;; ;;
esac esac