Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine size of ANSI terminal?

Standard input and output are connected to a terminal that implements ANSI escape sequences, but is of unknown dimensions.

I need to know how big the terminal so to facilitate drawing a full-screen text UI on it. How can I get the size?

The correct size is not loaded into environment variables. I cannot use TIOCGETS; the the call would return success but the values are not correct -- the kernel doesn't know the size either.

There are lots and lots of answers searching stackoverflow, but they all depend on the OS providing the answer one way or anther; but this time that is not true.

The best clue I can find is the DSR command which returns the current cursor position; but there's no move to bottom/right command.

like image 622
Joshua Avatar asked Feb 28 '16 21:02

Joshua


People also ask

What is ANSI in terminal?

ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII escape character and a bracket character, are embedded into text.

Do ANSI escape sequences work on Windows?

The advantage of using ANSI escape codes is that, today, these are available on most operating systems, including Windows, and you don't need to install third party libraries. These are well suited for simple command line applications. If you need to do complex text graphics check the ncurses library.

What is a terminal control sequence?

A Terminal Control Code, AKA terminal escape sequence, AKA terminal control sequecence, is an in-band sequence of bytes that may be interpreted by a character imaging device such as a terminal.


1 Answers

The resize program does this by moving the cursor to a very large column and row; the terminal moves as far as it can, e.g.,

CUP 999 999

Then resize asks where the cursor is:

DSR 6

The terminal replies with the actual cursor position (i.e., the cursor position report CPR), from which resize knows the terminal's size: the cursor is on the lower-right corner.

That's all done using standard (ECMA-48 / VT100) escape sequences. In XTerm Control Sequences (which should apply to your "ANSI" terminal)

CSI Ps n  Device Status Report (DSR).
            Ps = 6  -> Report Cursor Position (CPR) [row;column].
          Result is CSI r ; c R
like image 146
Thomas Dickey Avatar answered Sep 24 '22 00:09

Thomas Dickey