Hello. I wanted to write a console toy in C ++. First of all I decided to set the size of the console window. Google immediately came to the rescue, but the problem is that all the recipes use the windows.h library, and Ubuntu 16.04 is on my machine. I would be grateful for the help.

  • Conceptually, this is not the right decision - your program can request the current size, but not install it. - Vladimir Gamalyan
  • printf '\ e [8; 50; 100t' of sh will set the size of xterm to 50x100 characters (for the "standard" in Ubuntu, gnome-terminal and fce4-terminal also work). - avp
  • In general, the size of the window itself can also be set. For example, this makes the utility wmctrl. You can use it, or see how it does it in the source. - cronfy
  • If the application is a console, the console can be a remote connection and a printer, and much more, which cannot change its size. Therefore, the correct solution would be to write a windowing / GUI application. - Vladimir Gamalyan

1 answer 1

Rare task.

Fortunately, xterm developers, along with the implementation of a huge number of control sequences (Control Sequences) are typical for common text terminals (cursor control, color, character set, insertion, deletion and shifting of text, etc., etc.), We thought about similar sequences for moving the emulator window and changing its size on the screen of the X-windows window system ( in the link above, you can find them by searching for the text Window manipulation ).

To resize a window in characters, use the sequence:

ESC [8; height ; width ; t

Where ESC is a character with code 033 (octal) 27 (decimal) or "\ e" in the string.

In general, in the same document for the ESC character sequence [ term CSI is used (Control Sequince Introducer)
(Other common sequences in terminal control are DCS ESC P (Device Control String), OSC ESC ] (Operating System Command) and ST ESC \ (String Terminator).

Small demo program in C / C ++

 #include <stdio.h> #include <stdlib.h> #define COLS 24 #define ROWS 80 int main (int ac, char *av[]) { int cols = av[1] ? atoi(av[1]) : COLS, rows = av[1] && av[2] ? atoi(av[2]) : ROWS; if (cols < 0) cols = COLS; if (rows < 0) rows = ROWS; printf("\e[8;%d;%d;t", cols, rows); return fflush(stdout) == EOF; } 

Although the document says that if width or height (width and height (number of lines)) are omitted, then their current value is used, and if they are zero, then some default values ​​are taken (display's height or width), but in my experiments with bash script resize.sh

 #!/bin/bash printf "\e[8;$1;$2;t" 

in both cases, the window was changed to a standard size of 24 x 80 characters.

In Ubuntu 16.04.1 LTS, resizing the window works at least in the terminals gnome-terminal and xfce4-terminal (but in the xterm version of XTerm(322) nothing changes (!!!)).