Get Size of Terminal Window (Rows/Columns)

Get size of terminal window (rows/columns)

For Unix(-based), use ioctl(2) and TIOCGWINSZ:


#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
// ...

struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);

/* size.ws_row is the number of rows, size.ws_col is the number of columns. */

// ...

Also, while I haven't touched Windows in the last five years, GetConsoleScreenBufferInfo() should help you get the console window size.

Getting terminal size in c for windows?

(Partial answer)

This code:

CONSOLE_SCREEN_BUFFER_INFO csbi;
int ret;
ret = GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi);
if(ret)
{
printf("Console Buffer Width: %d\n", csbi.dwSize.X);
printf("Console Buffer Height: %d\n", csbi.dwSize.Y);
}

Gives the size of the buffer. The only problem is that dwSize.Y is not really the size of the screen (300 here instead of 25 lines). But dwSize.X matches the column's number. Needs only windows.h to work.

How do I find the width & height of a terminal window?

  • tput cols tells you the number of columns.
  • tput lines tells you the number of rows.

Getting the amount of available lines in a terminal

Determining by that screen shot, you are on Windows

This is from http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/

from ctypes import windll, create_string_buffer

# stdin handle is -10
# stdout handle is -11
# stderr handle is -12

h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)

if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
else:
sizex, sizey = 80, 25 # can't determine actual size - return default values

print sizex, sizey, curx, cury

That will give you screen size, and the cursor position.

cury is the line, so you can calculate the number of lines left.

However, you may want to re-check the console window size as you progress, as the user may resize the window at any time.

How to get Linux console window width in Python

import os
rows, columns = os.popen('stty size', 'r').read().split()

uses the 'stty size' command which according to a thread on the python mailing list is reasonably universal on linux. It opens the 'stty size' command as a file, 'reads' from it, and uses a simple string split to separate the coordinates.

Unlike the os.environ["COLUMNS"] value (which I can't access in spite of using bash as my standard shell) the data will also be up-to-date whereas I believe the os.environ["COLUMNS"] value would only be valid for the time of the launch of the python interpreter (suppose the user resized the window since then).

(See answer by @GringoSuave on how to do this on python 3.3+)

Get width of terminal in Node.js

console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);

And the output looks like e.g. "Terminal size: 80x24". There is also an event if the console size changes.

It's explained under tty in the docs.

Given column + rows of a terminal / tty, how to calculate min/max number of bytes that can fit

The maximum number depends on the nominal size of the window (rows times columns) as well as the way the character cells are encoded. The node application assumes everything is encoded as UTF-8, so that means each cell could be 4 bytes (see this answer for example).

Besides that, you have to allow for a newline at the end of each row (unless you're relying upon line-wrapping the whole time). A newline is a single byte.

So...

(1 + columns) * rows * 4

as a first approximation.

If you take combining characters into account, that could increase the estimate, but (see this answer) the limit on that is not well defined. In practice, those are rarely used in European characters, but are used in some Asian characters (ymmv).



Related Topics



Leave a reply



Submit