Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've found that on Ubuntu, none of the other methods specified here (<code>ENV['COLUMNS']</code>, <code>tput columns</code> or hirb) give the correct result if the terminal is resized while the Ruby application is running. This is not an issue for scripts, but it is an issue for interactive consoles, such as irb.</p> <p>The <a href="https://github.com/genki/ruby-terminfo" rel="noreferrer">ruby-terminfo gem</a> is the best solution I've find so far to give the correct dimensions after a resize, but it requires that you install an additional gem, and is unix-specific.</p> <p>The gem's usage is simple:</p> <pre><code>require 'terminfo' p TermInfo.screen_size # [lines, columns] </code></pre> <p>TermInfo internally uses TIOCGWINSZ ioctl for the screen size.</p> <p>Alternatively, as mentioned by user83510, highline's system_extensions also works:</p> <pre><code>require 'highline' HighLine::SystemExtensions.terminal_size # [columns, lines] </code></pre> <p>Interally, highline uses <code>stty size</code> on Unix, and other implementations for ncurses and Windows.</p> <p>To listen for changes to the terminal size (instead of polling), we can trap the SIGWINCH signal:</p> <pre><code>require 'terminfo' Signal.trap('SIGWINCH', proc { puts TermInfo.screen_size.inspect }) </code></pre> <p>This is specifically useful for applications using Readline, such as irb:</p> <pre><code>Signal.trap('SIGWINCH', proc { Readline.set_screen_size(TermInfo.screen_size[0], TermInfo.screen_size[1]) }) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload