Created
September 6, 2010 14:17
-
-
Save xoebus/567081 to your computer and use it in GitHub Desktop.
A snippet of code from HIRB to detect the terminal width.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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