1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Tweak and Refactor Pry::Terminal

The biggest behavior change is to favor `io/console` (but still not when
on jruby). The previous favorite was `Readline.get_screen_size`, but
that thing is stale just like the env. The only reason it gets updated
is if you put `Pry.auto_resize!` in your `~/.pryrc`

The next change is more error handling in `Pry.auto_resize!`

Otherwise I renamed `TerminalInfo` to `Terminal`, which is something
andrewvos and I discussed way back when `TerminalInfo` first came about.

The rest was a little Extract Method stuff, no biggie.
This commit is contained in:
☈king 2013-01-30 21:14:06 -06:00 committed by rkingpa@sharpsaw.org
parent b928092abe
commit fc522f837a
7 changed files with 80 additions and 63 deletions

View file

@ -267,6 +267,6 @@ require 'pry/pry_class'
require 'pry/pry_instance'
require 'pry/cli'
require 'pry/pager'
require 'pry/terminal_info'
require 'pry/terminal'
require 'pry/editor'
require 'pry/rubygem'

View file

@ -3,7 +3,7 @@ class Pry
def self.tablify_or_one_line(heading, things)
plain_heading = Pry::Helpers::Text.strip_color(heading)
attempt = Table.new(things, :column_count => things.size)
if attempt.fits_on_line?(TerminalInfo.width! - plain_heading.size - 2)
if attempt.fits_on_line?(Terminal.width! - plain_heading.size - 2)
"#{heading}: #{attempt}\n"
else
"#{heading}: \n#{tablify_to_screen_width(things, :indent => ' ')}\n"
@ -13,10 +13,10 @@ class Pry
def self.tablify_to_screen_width(things, options = {})
things = things.compact
if indent = options[:indent]
usable_width = TerminalInfo.width! - indent.size
usable_width = Terminal.width! - indent.size
tablify(things, usable_width).to_s.gsub(/^/, indent)
else
tablify(things, TerminalInfo.width!).to_s
tablify(things, Terminal.width!).to_s
end
end

View file

@ -1,12 +1,6 @@
require 'coderay'
class Pry
# Load io-console if possible, so that we can use $stdout.winsize.
begin
require 'io/console'
rescue LoadError
end
##
# Pry::Indent is a class that can be used to indent a number of lines
# containing Ruby code similar as to how IRB does it (but better). The class
@ -392,7 +386,7 @@ class Pry
full_line = prompt + code
whitespace = ' ' * overhang
_, cols = TerminalInfo.screen_size
_, cols = Terminal.screen_size
cols = cols.to_i
lines = cols != 0 ? (full_line.length / cols + 1) : 1

View file

@ -27,7 +27,7 @@ class Pry::Pager
end
def self.page_size
rows = Pry::TerminalInfo.screen_size
rows = Pry::Terminal.screen_size
@page_size ||= (rows && rows.first || 27)
end

View file

@ -247,9 +247,16 @@ class Pry
def self.auto_resize!
trap :WINCH do
size = `stty size`.split(/\s+/).map &:to_i
Readline.set_screen_size *size
Readline.refresh_line
begin
Readline.set_screen_size *Terminal.size!
rescue => e
warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
end
begin
Readline.refresh_line
rescue => e
warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}"
end
end
end

64
lib/pry/terminal.rb Normal file
View file

@ -0,0 +1,64 @@
class Pry::Terminal
class << self
# Return a pair of [rows, columns] which gives the size of the window.
#
# If the window size cannot be determined, return nil.
def screen_size
rows, cols = actual_screen_size
if rows && cols
[rows.to_i, cols.to_i]
else
nil
end
end
# Return a screen size or a default if that fails.
def size! default = [25, 80]
screen_size || default
end
# Return a screen width or the default if that fails.
def width! default = 80
size![1]
end
def actual_screen_size
# The best way, if possible (requires non-jruby ≥1.9 or io-console gem)
screen_size_according_to_io_console or
# Fall back to the old standby, though it might be stale:
screen_size_according_to_env or
# Fall further back, though this one is also out of date without something
# calling Readline.set_screen_size
screen_size_according_to_readline or
# Windows users can otherwise run ansicon and get a decent answer:
screen_size_according_to_ansicon_env
end
def screen_size_according_to_io_console
return if Pry::Helpers::BaseHelpers.jruby?
require 'io/console'
$stdout.winsize if $stdout.tty? and $stdout.respond_to?(:winsize)
rescue LoadError
# They're probably on 1.8 without the io-console gem. We'll keep trying.
end
def screen_size_according_to_env
size = [ENV['LINES'] || ENV['ROWS'], ENV['COLUMNS']]
size if size[1].to_i > 0
end
def screen_size_according_to_readline
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
end
def screen_size_according_to_ansicon_env
return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
size = [$2, $1]
size if size[1].to_i > 0
end
end
end

View file

@ -1,48 +0,0 @@
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
rows, cols = actual_screen_size
if rows && cols
[rows.to_i, cols.to_i]
else
nil
end
end
# Return a screen width or a default if it fails.
def self.width! default = 80
(screen_size || [nil, default])[1]
end
def self.actual_screen_size
[
# Some readlines also provides get_screen_size.
# Readline comes before IO#winsize because jruby sometimes defaults winsize to [25, 80]
readline_screen_size,
# 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["LINES"] || 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)
ENV['ANSICON'] =~ /\((.*)x(.*)\)/ && [$2, $1],
].detect do |(_, cols)|
cols.to_i > 0
end
end
def self.readline_screen_size
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
end
end