Skip to content

Instantly share code, notes, and snippets.

@j4james
Created March 12, 2023 17:48
Show Gist options
  • Save j4james/570a9b7dd28dbdd8b626be2770567210 to your computer and use it in GitHub Desktop.
Save j4james/570a9b7dd28dbdd8b626be2770567210 to your computer and use it in GitHub Desktop.
Testing Cursor Information Reports
#!/bin/bash
CSI=$'\e[' # Control Sequence Introducer
DCS=$'\eP' # Device Control String
ST=$'\e\\' # String Terminator
ESC=$'\e'
get_cursor_info() {
IFS="\e" read -a REPLY -t 1 -s -p ${CSI}'1$w' -r -d '\'
echo "${REPLY[0]:1:-1}"
}
set_cursor_info() {
echo -n ${DCS}'1$t'${1}${ST}
}
get_xcpr() {
IFS="\e" read -a REPLY -t 1 -s -p ${CSI}'?6n' -r -d 'R'
echo "${REPLY[0]:3}"
}
get_sgr() {
IFS="\e" read -a REPLY -t 1 -s -p ${DCS}'$qm'${ST} -r -d '\'
echo "${REPLY[0]:5:-2}"
}
get_decsca() {
IFS="\e" read -a REPLY -t 1 -s -p ${DCS}'$q"q'${ST} -r -d '\'
echo "${REPLY[0]:5:-3}"
}
get_decom() {
IFS="\e" read -a REPLY -t 1 -s -p ${CSI}'?6$p' -r -d 'y'
echo "${REPLY[0]:2:-1}"
}
reset_state() {
echo -n ${CSI}'!p' # Soft reset
echo -n ${CSI}'H' # Home cursor
echo -n ${CSI}' P' # Page 1
echo -n ${CSI}'?7h' # Auto wrap
echo -n ${DCS}'0!u%5'${ST} # UPSS
echo -n ${CSI}'r' # No margins
}
# Start with default values
reset_state
initial=$(get_cursor_info)
# Cursor position
echo -n ${CSI}'3;4H'
cursor=$(get_cursor_info)
# Page number
echo -n ${CSI}'2 P'
page=$(get_cursor_info)
# Bold
echo -n ${CSI}'1m'
bold=$(get_cursor_info)
# Underline
echo -n ${CSI}'4m'
underline=$(get_cursor_info)
# Blinking
echo -n ${CSI}'5m'
blinking=$(get_cursor_info)
# Negative
echo -n ${CSI}'7m'
negative=$(get_cursor_info)
# Invisible
echo -n ${CSI}'8m'
invisible=$(get_cursor_info)
# ANSI colors
echo -n ${CSI}'33;44m'
colors=$(get_cursor_info)
# Selective erase
echo -n ${CSI}'1"q'
decsca=$(get_cursor_info)
# Origin mode
echo -n ${CSI}'?6h' # Set mode
echo -n ${CSI}'10;20r' # Set margins
echo -n ${CSI}'5;6H' # Set cursor
decom=$(get_cursor_info)
# Single shift 2
echo -n ${ESC}'N'
ss2=$(get_cursor_info)
# Single shift 3
echo -n ${ESC}'O'
ss3=$(get_cursor_info)
# Pending wrap
echo -n ${CSI}'1;9999H*'
wrap=$(get_cursor_info)
# Invoke G1 into GL (LS1)
echo -en '\x0E'
gl=$(get_cursor_info)
# Invoke G3 into GR (LS3R)
echo -n ${ESC}'|'
gr=$(get_cursor_info)
# G0 as Supplemental (%5)
echo -n ${ESC}'(%5'
g0_94=$(get_cursor_info)
# G1 as UPSS (<)
echo -n ${ESC}')<'
g1_94=$(get_cursor_info)
# G2 as DEC Graphics (0)
echo -n ${ESC}'*0'
g2_94=$(get_cursor_info)
# G3 as DEC Graphics (0)
echo -n ${ESC}'+0'
g3_94=$(get_cursor_info)
# G1 as Latin-1 (A)
echo -n ${ESC}'-A'
g1_96=$(get_cursor_info)
# G2 as Latin-1 (A)
echo -n ${ESC}'.A'
g2_96=$(get_cursor_info)
# G3 as Latin-1 (A)
echo -n ${ESC}'/A'
g3_96=$(get_cursor_info)
reset_state
# Cursor position
set_cursor_info '3;4;1;@;@;@;0;2;@;BBBB'
cursor_set=$(get_xcpr)
# Page number
set_cursor_info '1;1;2;@;@;@;0;2;@;BBBB'
page_set=$(get_xcpr)
# Bold
set_cursor_info '1;1;1;A;@;@;0;2;@;BBBB'
bold_set=$(get_sgr)
# Underline
set_cursor_info '1;1;1;B;@;@;0;2;@;BBBB'
underline_set=$(get_sgr)
# Blinking
set_cursor_info '1;1;1;D;@;@;0;2;@;BBBB'
blinking_set=$(get_sgr)
# Negative
set_cursor_info '1;1;1;H;@;@;0;2;@;BBBB'
negative_set=$(get_sgr)
# Invisible
set_cursor_info '1;1;1;P;@;@;0;2;@;BBBB'
invisible_set=$(get_sgr)
# Selective erase
set_cursor_info '1;1;1;@;A;@;0;2;@;BBBB'
decsca_set=$(get_decsca)
# Origin mode
echo -n ${CSI}'10;20r' # Set margins
set_cursor_info '5;6;1;@;@;A;0;2;@;BBBB'
decom_set="$(get_decom) $(get_xcpr)"
echo -n ${CSI}'?6l' # Disable DECOM
echo -n ${CSI}'r' # Clear margins
# Pending wrap
set_cursor_info '1;80;1;@;@;H;0;2;@;BBBB'
echo -n '*'
pending_set=$(get_xcpr)
reset_state
echo -n ${CSI}'J'
COL65=$'\r'${CSI}'64C'
echo "Initial state: ${initial}"
echo "Cursor position: ${cursor}" ${COL65}"${cursor_set}"
echo "Page number: ${page}" ${COL65}"${page_set}"
echo "Bold rendition: ${bold}" ${COL65}"${bold_set}"
echo "Underline rendition: ${underline}" ${COL65}"${underline_set}"
echo "Blinking rendition: ${blinking}" ${COL65}"${blinking_set}"
echo "Negative rendition: ${negative}" ${COL65}"${negative_set}"
echo "Invisible rendition: ${invisible}" ${COL65}"${invisible_set}"
echo "Color rendition: ${colors}"
echo "Selective erase: ${decsca}" ${COL65}"${decsca_set}"
echo "Origin mode: ${decom}" ${COL65}"${decom_set}"
echo "Pending SS2: ${ss2}"
echo "Pending SS3: ${ss3}"
echo "Pending wrap: ${wrap}" ${COL65}"${pending_set}"
echo "GL mapped to G1: ${gl}"
echo "GR mapped to G3: ${gr}"
echo "G0 as Supplemental: ${g0_94}"
echo "G1 as UPSS: ${g1_94}"
echo "G2 as DEC Graphics: ${g2_94}"
echo "G3 as DEC Graphics: ${g3_94}"
echo "G1 as Latin-1: ${g1_96}"
echo "G2 as Latin-1: ${g2_96}"
echo "G3 as Latin-1: ${g3_96}"
# Single shift 2
echo -n ${ESC}'*0' # Make sure G2 is 0
set_cursor_info '12;65;1;@;@;B;0;2;@;BB0B'
echo -n 'aa'
echo -n ${CSI}'!p' # Soft reset
# Single shift 3
echo -n ${ESC}'+0' # Make sure G3 is 0
set_cursor_info '13;65;1;@;@;D;0;2;@;BBB0'
echo -n 'aa'
echo -n ${CSI}'!p' # Soft reset
# Invoke G1 into GL
echo -n ${ESC}')>' # Make sure G1 is >
set_cursor_info '15;65;1;@;@;@;1;2;@;B>BB'
echo -n 'abcde'
echo -n ${CSI}'!p' # Soft reset
# Invoke G3 into GR
echo -n ${ESC}'%@' # Make sure ISO2022 coding is enabled on modern terminals
echo -n ${ESC}'+>' # Make sure G3 is >
set_cursor_info '16;65;1;@;@;@;0;3;@;BBB>'
echo -n $'\xE1\xE2\xE3\xE4\xE5'
echo -n ${CSI}'!p' # Soft reset
# G0 as Supplemental (%5)
echo -en '\x0F' # Make sure G0 is in GL
set_cursor_info '17;65;1;@;@;@;0;2;@;%5BBB'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G1 as UPSS (<)
echo -en '\x0E' # Make sure G1 is in GL
set_cursor_info '18;65;1;@;@;@;1;2;@;B<BB'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G2 as DEC Graphics (0)
echo -n ${ESC}'n' # Make sure G2 is in GL
set_cursor_info '19;65;1;@;@;@;2;2;@;BB0B'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G3 as DEC Graphics (0)
echo -n ${ESC}'o' # Make sure G3 is in GL
set_cursor_info '20;65;1;@;@;@;3;2;@;BBB0'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G1 as Latin-1 (A)
echo -en '\x0E' # Make sure G1 is in GL
set_cursor_info '21;65;1;@;@;@;1;2;B;BABB'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G2 as Latin-1 (A)
echo -n ${ESC}'n' # Make sure G2 is in GL
set_cursor_info '22;65;1;@;@;@;2;2;D;BBAB'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# G3 as Latin-1 (A)
echo -n ${ESC}'o' # Make sure G3 is in GL
set_cursor_info '23;65;1;@;@;@;3;2;H;BBBA'
echo -n 'abcd^'
echo -n ${CSI}'!p' # Soft reset
# Restore position
echo -n ${CSI}'24H'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment