Search code examples
perlterminalcygwinansi-escapemintty

Set size of a cygwin terminal window


I have a small perl script, which is executed in a cygwin terminal and prints out a formatted table. On Default window size, cygwin will insert an line break if the text gets too long and thereby destroy the format of my table. Is there a way from my perl script to set the cygwin window to a bigger size to avoid that kind of problem?


Solution

  • If you are using mintty as your terminal emulator (it has been the default terminal emulator for Cygwin for the past couple of years), you can use ANSI escape codes to manipulate the terminal.

    You can test this by running the following snippet of Perl code to change the size of your terminal emulator window:

    # If terminal supports ANSI escape sequences
    $lines = 80;
    $columns = 100;
    print "\e[8;$lines;${columns}t";
    

    Note: This doesn't work if run while in a screen window and I don't know why. According to the screen man page, this escape sequence should be supported.

    Explanation

    The syntax of ANSI escape sequences isn’t the easiest to read but here’s the documentation that provides the basis of the above sequence.

    The \e prints an Escape character which begins the ANSI escape sequence. This is also known as the Control Sequence Introducer (CSI).

    The specific sequence ending with t comes from this List of xterm control sequences

    CSI Ps ; Ps ; Ps t
              Window manipulation (from dtterm, as well as extensions).
              These controls may be disabled using the allowWindowOps
              resource.  Valid values for the first (and any additional
              parameters) are:
    …
    Ps = 8  ;  height ;  width -> Resize the text area to given
              height and width in characters.  Omitted parameters reuse the
              current height or width.  Zero parameters use the display's
              height or width.