2012-11-29 05:51:08 -05:00
|
|
|
class Pry::TerminalInfo
|
|
|
|
# Return a pair of [rows, columns] which gives the size of the window.
|
|
|
|
#
|
|
|
|
# If the window size cannot be determined, return nil.
|
|
|
|
def self.screen_size
|
2012-11-29 13:23:43 -05:00
|
|
|
rows, cols = actual_screen_size
|
|
|
|
if rows && cols
|
|
|
|
[rows.to_i, cols.to_i]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.actual_screen_size
|
2012-11-29 05:51:08 -05:00
|
|
|
[
|
|
|
|
# Some readlines also provides get_screen_size.
|
|
|
|
# Readline comes before IO#winsize because jruby sometimes defaults winsize to [25, 80]
|
2012-12-10 01:05:49 -05:00
|
|
|
readline_screen_size,
|
2012-11-29 05:51:08 -05:00
|
|
|
|
|
|
|
# io/console adds a winsize method to IO streams.
|
|
|
|
# rescue nil for jruby 1.7.0 [jruby/jruby#354]
|
|
|
|
$stdout.tty? && $stdout.respond_to?(:winsize) && ($stdout.winsize rescue nil),
|
|
|
|
|
|
|
|
# Otherwise try to use the environment (this may be out of date due
|
|
|
|
# to window resizing, but it's better than nothing).
|
|
|
|
[ENV["ROWS"], ENV["COLUMNS"]],
|
|
|
|
|
|
|
|
# If the user is running within ansicon, then use the screen size
|
|
|
|
# that it reports (same caveats apply as with ROWS and COLUMNS)
|
2012-11-29 13:23:43 -05:00
|
|
|
ENV['ANSICON'] =~ /\((.*)x(.*)\)/ && [$2, $1],
|
2012-11-29 05:51:08 -05:00
|
|
|
].detect do |(_, cols)|
|
|
|
|
cols.to_i > 0
|
2012-11-29 13:23:43 -05:00
|
|
|
end
|
2012-11-29 05:51:08 -05:00
|
|
|
end
|
2012-12-10 01:05:49 -05:00
|
|
|
|
|
|
|
def self.readline_screen_size
|
2012-12-20 09:28:27 -05:00
|
|
|
Readline.get_screen_size if Readline.respond_to?(:get_screen_size)
|
|
|
|
rescue Java::JavaLang::NullPointerException
|
|
|
|
# This rescue won't happen on jrubies later than:
|
|
|
|
# https://github.com/jruby/jruby/pull/436
|
|
|
|
nil
|
2012-12-10 01:05:49 -05:00
|
|
|
end
|
2012-11-29 05:51:08 -05:00
|
|
|
end
|