ruby-winsize Build Status

winsize is a small library that lets you get and set the winsize of a tty.


winsize adds 2 methods: winsize and winsize= to IO that allows you to control the size of a tty window. Mainly used with pty to deal with executables that word wrap (i.e. top). If you are using ruby 1.9.3 this gem is not needed. require "io/console" provides the same functionality.


add this line your application’s Gemfile:

gem "winsize"

And then execute:

$ bundle install

Or install it yourself as:

$ gem install winsize


Get the size of the terminal

“by require “winsize”

rows, cols = $stdout.winsize puts “Your terminal is #colsx#rows”

Set the size of a pty terminal

“by require “pty” require “winsize”

size =, 180) input, output, pid = PTY.spawn output.winsize = size

Reacting to a resized terminal

You may want to combine winsize and catching the SIGWINCH signal so you can react when the terminal has been resized.

“by require “winsize”

puts “Terminal size is #$stdout$stdout.winsize$stdout.winsize.colsx#$stdout$stdout.winsize$stdout.winsize.rows”

Signal.trap(:WINCH) do puts “Terminal resized to #$stdout$stdout.winsize$stdout.winsize.colsx#$stdout$stdout.winsize$stdout.winsize.rows” end

loop { sleep(10) }

io/console API compatible

“by require “winsize”

$stdout.winsize.to_ary # => [32, 180] $stdout.winsize = [32, 180]


Fork, branch & pull request.