Skip to content

Instantly share code, notes, and snippets.

@xoebus
Created September 6, 2010 14:17
Show Gist options
  • Save xoebus/567081 to your computer and use it in GitHub Desktop.
Save xoebus/567081 to your computer and use it in GitHub Desktop.
A snippet of code from HIRB to detect the terminal width.
def detect_terminal_size
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
elsif (RUBY_PLATFORM =~ /java/ || !STDIN.tty?) && command_exists?('tput')
[`tput cols`.to_i, `tput lines`.to_i]
else
command_exists?('stty') ? `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse : nil
end
rescue
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment