ANSI escape sequence save/restore cursor position support - terminal

Are there any known terminals which support the ANSI escape sequences for cursor keys up, down, forward and back:
CSI n A Cursor Up
CSI n B Cursor Down
CSI n C Cursor Forward
CSI n D Cursor Back
but don't support the save-cursor-position and restore-cursor-position escape sequences?
CSI s Save Cursor Position
CSI u Restore Cursor Position

A genuine VT100 (or VT220 for that matter) did not recognize these control sequences for save/restore cursor position. They use (as noted in xterm's control sequences list)
ESC 7 Save Cursor (DECSC).
ESC 8 Restore Cursor (DECRC).
Those are the sc and rc capabilities in terminfo; you may see these listed using
infocmp vt100
infocmp vt220
The origin of CSI s and CSI u likely is from ansi.sys (see terminal database entry for ansi.sys-old), though an older source is always possible.

Related

Obtain cursor color in terminal

Is there any way to determine what the color of the cursor is in a terminal running something like vim? I know that you can use tput cols and tput rows to determine the height and width of the terminal, are there similar tools for cursor color/obtaining the ansi standard foreground/background color of any character location in the current terminal?
The answer to your question is "No, there is no standard way to do that."
Consider that your terminal is modelled after ancient text terminals (like DEC VT100 and the like) which communicated with a server over a serial port or modem. And those terminals were in turn modelled after TeleTYpe (tty) devices which were connected to computers back in the 1960s.
Teletype machines ("dumb" terminals) provided no data back to the server that was not typed in to the keyboard. Devices like the VT100 ("smart" terminals) provided VERY LITTLE back to the server, but the list of what's available hasn't changed in many years.
Other resources:
The full list of reports available in VT220 emulation.
Another useful unofficial resource with slightly easier to follow wording.
Note that not all terminals are VT100/VT220, and your system may have locak extensions that provide what you need in a non-standard way.
For extra reading, have a look at man termcap and man terminfo. Check out the references in the "SEE ALSO" section of those pages.
Short answer: no
Long answer: the feature, if widely available would be another capability of tput, which allows you to retrieve any of the terminal capabilities for scripting. Those are documented in the terminfo manual page. None of those deal with cursor color, only with the (rather vague) cvvis (very visible), cnorm (normal) and civis (invisible) cursor attributes.
That is, most terminals do not
provide a way to set the cursor color or
provide a way to retrieve the cursor color
xterm is a rare exception, providing both. But the feature is not often supported in terminals imitating xterm. It is documented in XTerm Control Sequences as part of the dynamic colors feature:
OSC Ps ; Pt ST
OSC Ps ; Pt BEL
Set Text Parameters. For colors and font, if Pt is a "?", the
control sequence elicits a response which consists of the con-
trol sequence which would set the corresponding value. The
dtterm control sequences allow you to determine the icon name
and window title.
The 10 colors (below) which may be set or queried using 1 0
through 1 9 are denoted dynamic colors, since the correspond-
ing control sequences were the first means for setting xterm's
colors dynamically, i.e., after it was started. They are not
the same as the ANSI colors. These controls may be disabled
using the allowColorOps resource. At least one parameter is
expected for Pt. Each successive parameter changes the next
color in the list. The value of Ps tells the starting point
in the list. The colors are specified by name or RGB specifi-
cation as per XParseColor.
If a "?" is given rather than a name or RGB specification,
xterm replies with a control sequence of the same form which
can be used to set the corresponding dynamic color. Because
more than one pair of color number and specification can be
given in one control sequence, xterm can make more than one
reply.
Ps = 1 2 -> Change text cursor color to Pt.
The command-line program xtermcontrol uses these escape sequences to set and get the cursor color:
--cursor=COLOR
Set cursor color to COLOR.
--get-cursor
Report cursor color.
For example
$ xtermcontrol --get-cursor
rgb:0000/0000/0000
$ xtermcontrol --cursor limegreen
$ xtermcontrol --get-cursor
rgb:3232/cdcd/3232
For what it's worth, it is supported by VTE (e.g., gnome-terminal).

Terminal - clear half screen

I know that using cmd + K can clear the entire screen buffer.
I am wondering is there a way to just clear half of the screen buffer?
cmd+K maps to the menu item for clearing the visible screen (the screen buffer). That does not include the scrollback (it is a similar question, but different).
iTerm2's preferences and menu do not show any direct way to clear half of the screen.
However, iTerm2 emulates (like Terminal.app) most of the VT100 control sequences. You could add a binding in your shell which (given a suitable key of your choice) tells the shell to echo a control sequence which clears from the current cursor position (a) until the beginning of the screen or (b) to the end of the screen. Since it honors the save/restore cursor controls, you could even make it clear exactly half of the screen if you know the size, e.g.,
save the cursor position
jump to the middle of the screen
clear (which half?) half of the screen
restore the cursor position
Since it would use your shell, I called that indirect.
As noted, Terminal.app implements most of the VT100 control sequences. In ncurses, the appropriate TERM would be nsterm which is known to provide the VT100-style save/restore cursor. You can use tput to extract the corresponding strings for sc, rc and cup from the terminal database. However (if you wanted to erase to the beginning of the screen), you would provide your own string, since that particular flavor is not part of termcap/terminfo.
You can find the relevant VT100 control sequences documented in XTerm Control Sequences:
ESC 7 Save Cursor (DECSC).
ESC 8 Restore Cursor (DECRC).
CSI Ps ; Ps H
Cursor Position [row;column] (default = [1,1]) (CUP).
CSI Ps J Erase in Display (ED).
Ps = 0 -> Erase Below (default).
Ps = 1 -> Erase Above.
Ps = 2 -> Erase All.
Ps = 3 -> Erase Saved Lines (xterm).
Refering answer
# Get ceiling eg: 7/2 = 4
ceiling_divide() {
ceiling_result=$((($1+$2-1)/$2))
}
clear_rows() {
POS=$1
# Insert Empty Rows to push & preserve the content of screen
for i in {1..$((LINES-POS-1))}; echo
# Move to POS, after clearing content from POS to end of screen
tput cup $((POS-1)) 0
}
# Clear quarter
alias ptop='ceiling_divide $LINES 4; clear_rows $ceiling_result'
# Clear half
alias pmid='ceiling_divide $LINES 2; clear_rows $ceiling_result'
# Clear 3/4th
alias pdown='ceiling_divide $((3*LINES)) 4; clear_rows $ceiling_result'

how to get current terminal color pair in bash

I would like to query and store the current terminal color pair in BASH e.g.:
#!/bin/bash
#some ANSI colour escape sequences
red="\033[0;31m"
grn="\033[0;32m"
blu="\033[0;34m"
def="\033[0;00m" # default
echo -e "Change to ${red} red to ${def} default to ${blu} blue."
# now store the current color (which happens to be blue) e.g.:
cur=????
echo -e "Change to ${grn} green and back to what I had before ${cur}"
echo -e "This would be in blue if variable cur contained e.g.: 0;34m."
echo -e "Back to default${def}"
exit 0
The answer that eludes me is how to capture the current color
cur=????
The question was about the current color, not the cursor position.
Both are "nonstandard" (though the latter, cursor position report is implemented by anything which has a valid claim to "VT100 emulator").
However, xterm implements a set of escape sequences referred to as dynamic colors, which predate the ANSI color functionality. Those set the working colors including text foreground and background. I modified this in 2002 to allow an application to send the sequence with a "?" rather than a color to tell xterm to return the color value, e.g.,
OSC 1 1 ? ST
using the notation given in XTerm Control Sequences
You can't; there is no standard control sequence to report the current cursor attributes.
What does exist, however, is a sequence to save and restore the current cursor position and attributes:
\e7 (DECSC) will save the cursor position and attributes.
\e8 (DECRC) will restore the saved cursor position and attributes.
There is no standard way to restore only the cursor attributes; however, as rici mentioned, you can get a report of the current position using \e[6n (DSR), then use the response to manually "un-restore" the cursor position after restoring its position and attributes.
Again, though, it's probably easier (and better) to just keep track of the colors in your application, rather than making the terminal responsible for that.
It's important to understand that the terminal state has nothing to do with bash. Bash doesn't care. It simply reads from stdin and writes to stdout and stderr. (See Note 1)
All terminal effects are implemented by the terminal emulator you happen to be using, of which there are many. In a graphical environment, you might be using, for example, xterm or konsole. You'll need to search the documentation for those emulators for specific terminal control codes which they interpret.
As far as I know, there is no standard code to get a report of the current terminal state, other than the cursor position (ESC[6n).
So your best bet is to remember the changes you made when you make them.
You can find a list of the standard codes implemented by the Linux console using man console_codes (although few people use the Linux console these days); most of those are also interpreted by xterm and other graphical consoles. There's an list of xterm sequences in Thomas Dickey's xterm site; it's a more or less de facto standard for terminal emulators but, as I said, you'll need to search in each emulator's documentation for idiosyncratic control sequences.
Notes
In interactive mode, bash uses a library called readline to help it handle some terminal effects. In particular, readline tries to maintain the current console cursor position, although it is easy to fool it. In PS1 you need to surround console control sequences with \[ and \] precisely because readline does not know that they are control sequences.
What I understood, is that u are asking to to get the default profile color of the user, of which u have change color of!
Eg: User is using 'Bright Green font color on Black background', you change it to 'Red font color on White background'. Now how to get/know the default colors and set them back to it!
You can use the sequence \033[0m in bash [also for command prompt (batch scripting) / powershell (ps scripting)]

Terminfo smkx and Application Cursor Keys vs Application keypad

XTerm Control Sequences specifies the following key sequences:
CSI ? 1 h → Application Cursor Keys (DECCKM)
CSI ? 1 l → Normal Cursor Mode (DECOM)
CSI ? 66 h → Application keypad (DECNKM)
CSI ? 66 l → Numeric keypad (DECNKM)
and the Terminfo Source Format has the following entry:
Variable: keypad_xmit
Capname: smkx
Termcap: ks
Description: Put terminal in "keypad-transmit" mode
But the terminfo for xterm actually says smkx=\E[?1h\E=, which seems mixed up to me (smkx should affect the keypad, not the cursor keys, right?). What am I missing here?
The line
CSI ? 1 h → Normal Cursor Mode (DECOM)
probably should read
CSI ? 1 l → Normal Cursor Keys (DECCKM)
There are two escape sequences in each of smkx and rmkx because these capabilities are used in curses, i.e., for the keypad function.
The manual page for ncurses (like others) does not say so explicitly, but the function applies to all of the special keys on the keyboard which have the ability to switch between normal and application mode. This is a well-known feature of smkx and rmkx, as for example in the xterm FAQ Why can't I use the cursor keys in (whatever) shell?.
Special keys on the keyboard may include these (depending on the type of terminal):
numeric keypad (on the extreme right of a PC keyboard)
editing keypad (6 keys between the main keyboard and the numeric keyboard)
cursor keys
function keys (commonly at the top of a PC keyboard)
The VT100 (which does not have function keys or editing keypad) has separate escape sequences for the other two categories. In VT220 (emulated by xterm), the editing keypad's normal/application mode is an extension of the VT100 DECCKM, (documented in XTerm Control Sequences). DEC did not define a corresponding feature for function keys; however if there were some terminal which supported this capability it would probably be used in smkx and rmkx.
It looks like you are missing the \E= at the end of smkx.
ESC = Application Keypad (DECKPAM).
ESC > Normal Keypad (DECKPNM).
are listed on the XTerm Control Sequences page as well and you would expect them to affect the keypad.
I found a related question here: keyboard transmit mode in vt100 terminal emulator.

Cursor keys leave insert mode in irb in vi mode

I am using vi-mode in irb by having a ~/.inputrc stating:
set editing-mode vi
Since I changed to ruby 1.9.3 and 2.0.0, IRB and the rails console started behaving differently.
Before I could use the left and right cursor arrow keys in insert mode and the cursor would move accordingly, staying in insert mode (like standard in vim).
Typing arrow left now deletes all characters till the end of the line and takes me right out of insert mode.
I am lost, why is this happening?
UPDATE:
How can I get irb to interpret the arrow keys as such and not as escape sequences?
This is because Readline is interpreting the escape characters for the cursor keys. Left cursor is ESC[D which leaves insert mode and then deletes the rest of the line. Similarly cursor right will leave insert mode and Change the rest of the line, up will Append to the rest of the line and down will go Back a word.
I believe this could be the same thing as bug #7859.

Resources