I'm creating a Telnet CLI application that is controlled with VT100 escape sequences. So to e.g. navigate the cursor left the <ESC>[D escape sequence is sent from the Telnet server to the client, which may be Putty or Gnome-terminal. Unfortunately, with lines that are longer than the Putty line length, the escape sequence above will not permit navigating the cursor to the line above the current line.
An example. Cursor is '|'. Comments are marked with '//'
----------------
>potato| // Now I press left arrow which sends esc sequence to application
----------------
>potat|o // Works as expected. The cursor moved left
----------------
Another example
----------------
>potatopotatopot // This is a long command which goes over two lines
|ato // Now I press left arrow which sends esc sequence to application
----------------
>potatopotatopot // The cursor didn't move, since the escape sequence
|ato // does nothing if the cursor is at the edge
----------------
I have been searching for any other escape sequence that would wrap around when at the edge, but found none. I have neither found any escape sequence that changes the terminal mode to something that allows wrapping.
So how is terminal navigation like this commonly handled?
The bw
capability in a termcap terminal description says whether moving left at the edge of a screen wraps to the previous line. It was present in a PuTTy description I checked (infocmp putty
under ncurses), but not in many others (e.g. not in infocmp gnome
).
You could try to keep track of which column the cursor is in and use movement control sequences when you want to wrap round to the previous line. You would have to know the width of the user's screen, which can be done by them setting the LINES
and COLS
environmental variables.
As noted, the bw
capability could solve part of the problem, but it is rare. In particular, it is not a feature of vt100-compatible programs (such as xterm). The OP mentioned both PuTTY and gnome-terminal. The latter does not use bw
, so a different solution is preferred.
On the other hand, PuTTY does implement the vt100 cursor position report which is used by resize as a fallback when it cannot obtain the screensize using system calls. Quoting from xterm's control sequences document:
CSI Ps n Device Status Report (DSR).
Ps = 5 -> Status Report.
Result (``OK'') is CSI 0 n
Ps = 6 -> Report Cursor Position (CPR) [row;column].
Result is CSI r ; c R
The resize
program uses this by
- sending the cursor to the lower right corner of a "huge" (999 by 999) window
- sending the CPR sequence
- reading the report of the actual cursor position
Knowing the screensize, the server could send the cursor to more useful positions.
来源:https://stackoverflow.com/questions/25038426/vt100-escape-sequences-cursor-movement-wrap-around-to-end-of-line