Created
March 30, 2011 14:08
-
-
Save zeero/894454 to your computer and use it in GitHub Desktop.
detect tty width & height
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
# Determines if a shell command exists by searching for it in ENV['PATH']. | |
def command_exists?(command) | |
ENV['PATH'].split(File::PATH_SEPARATOR).any? { | |
|d| File.exists? File.join(d, command) | |
} | |
end | |
# Returns [width, height] of terminal when detected, nil if not detected. | |
def detect_terminal_size | |
@log.debug_var binding, "ENV['COLUMNS']", "ENV['LINES']" | |
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) | |
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i] | |
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput') | |
[`tput cols`.to_i, `tput lines`.to_i] | |
elsif STDIN.tty? && command_exists?('stty') | |
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse | |
else | |
[0, 0] | |
end | |
rescue | |
[0, 0] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unix - How to get the width of terminal window in Ruby - Stack Overflow