summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-05-26 10:42:52 +0300
committerKyrylo Silin <silin@kyrylo.org>2019-05-26 10:42:52 +0300
commit5166b1a259bb5e31e21d0164596afe80346f864d (patch)
treed706de278ce6f448e0caaab6b0ade900569e9085
parent7ca5facc1cf6b9021928ff4fcb7f6874cce904c5 (diff)
downloadpry-1991-output-winsize-fix.tar.gz
-rw-r--r--lib/pry.rb1
-rw-r--r--lib/pry/color_printer.rb2
-rw-r--r--lib/pry/commands/ls/formatter.rb2
-rw-r--r--lib/pry/helpers/table.rb23
-rw-r--r--lib/pry/indent.rb5
-rw-r--r--lib/pry/output.rb85
-rw-r--r--lib/pry/pager.rb4
-rw-r--r--lib/pry/pry_class.rb2
-rw-r--r--lib/pry/pry_instance.rb2
-rw-r--r--lib/pry/repl.rb2
-rw-r--r--spec/indent_spec.rb2
11 files changed, 108 insertions, 22 deletions
diff --git a/lib/pry.rb b/lib/pry.rb
index c33bc0cb..1be39d92 100644
--- a/lib/pry.rb
+++ b/lib/pry.rb
@@ -58,7 +58,6 @@ require 'pry/pry_class'
require 'pry/pry_instance'
require 'pry/inspector'
require 'pry/pager'
-require 'pry/terminal'
require 'pry/indent'
require 'pry/object_path'
require 'pry/output'
diff --git a/lib/pry/color_printer.rb b/lib/pry/color_printer.rb
index ee6c2979..8e7a14d0 100644
--- a/lib/pry/color_printer.rb
+++ b/lib/pry/color_printer.rb
@@ -11,7 +11,7 @@ class Pry
def self.default(_output, value, pry_instance)
pry_instance.pager.open do |pager|
pager.print pry_instance.config.output_prefix
- pp(value, pager, Pry::Terminal.width - 1)
+ pp(value, pager, pry_instance.output.width - 1)
end
end
diff --git a/lib/pry/commands/ls/formatter.rb b/lib/pry/commands/ls/formatter.rb
index ddd17842..04baee60 100644
--- a/lib/pry/commands/ls/formatter.rb
+++ b/lib/pry/commands/ls/formatter.rb
@@ -31,7 +31,7 @@ class Pry
return '' if body.compact.empty?
fancy_heading = Pry::Helpers::Text.bold(color(:heading, heading))
- Pry::Helpers.tablify_or_one_line(fancy_heading, body, @pry_instance.config)
+ Pry::Helpers.tablify_or_one_line(fancy_heading, body, @pry_instance)
end
def format_value(value)
diff --git a/lib/pry/helpers/table.rb b/lib/pry/helpers/table.rb
index f7bd40d0..23f98d05 100644
--- a/lib/pry/helpers/table.rb
+++ b/lib/pry/helpers/table.rb
@@ -2,29 +2,30 @@
class Pry
module Helpers
- def self.tablify_or_one_line(heading, things, config = Pry.config)
+ def self.tablify_or_one_line(heading, things, pry_instance = Pry.new)
plain_heading = Pry::Helpers::Text.strip_color(heading)
attempt = Table.new(things, column_count: things.size)
- if attempt.fits_on_line?(Terminal.width - plain_heading.size - 2)
+ if attempt.fits_on_line?(pry_instance.output.width - plain_heading.size - 2)
"#{heading}: #{attempt}\n"
else
- "#{heading}: \n#{tablify_to_screen_width(things, { indent: ' ' }, config)}\n"
+ content = tablify_to_screen_width(things, { indent: ' ' }, pry_instance)
+ "#{heading}: \n#{content}\n"
end
end
- def self.tablify_to_screen_width(things, options, config = Pry.config)
+ def self.tablify_to_screen_width(things, options, pry_instance = Pry.new)
options ||= {}
things = things.compact
if (indent = options[:indent])
- usable_width = Terminal.width - indent.size
- tablify(things, usable_width, config).to_s.gsub(/^/, indent)
+ usable_width = pry_instance.output.width - indent.size
+ tablify(things, usable_width, pry_instance).to_s.gsub(/^/, indent)
else
- tablify(things, Terminal.width, config).to_s
+ tablify(things, pry_instance.output.width, pry_instance).to_s
end
end
- def self.tablify(things, line_length, config = Pry.config)
- table = Table.new(things, { column_count: things.size }, config)
+ def self.tablify(things, line_length, pry_instance = Pry.new)
+ table = Table.new(things, { column_count: things.size }, pry_instance)
until (table.column_count == 1) || table.fits_on_line?(line_length)
table.column_count -= 1
end
@@ -33,9 +34,9 @@ class Pry
class Table
attr_reader :items, :column_count
- def initialize(items, args, config = Pry.config)
+ def initialize(items, args, pry_instance = Pry.new)
@column_count = args[:column_count]
- @config = config
+ @config = pry_instance.config
self.items = items
end
diff --git a/lib/pry/indent.rb b/lib/pry/indent.rb
index e6e96dc3..963758a6 100644
--- a/lib/pry/indent.rb
+++ b/lib/pry/indent.rb
@@ -101,7 +101,8 @@ class Pry
indent.module_nesting
end
- def initialize
+ def initialize(pry_instance = Pry.new)
+ @pry_instance = pry_instance
reset
end
@@ -394,7 +395,7 @@ class Pry
line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
whitespace = ' ' * overhang
- cols = Terminal.width
+ cols = @pry_instance.output.width
lines = cols == 0 ? 1 : (line_to_measure.length / cols + 1).to_i
if Helpers::Platform.windows_ansi?
diff --git a/lib/pry/output.rb b/lib/pry/output.rb
index 6ecd8752..454f3cec 100644
--- a/lib/pry/output.rb
+++ b/lib/pry/output.rb
@@ -54,5 +54,90 @@ class Pry
Pry::Helpers::Text.strip_color str
end
end
+
+ # 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
+ [rows.to_i, cols.to_i] if rows.to_i != 0 && cols.to_i != 0
+ end
+
+ # Return a screen size or a default if that fails.
+ def size(default = [27, 80])
+ screen_size || default
+ end
+
+ # Return a screen width or the default if that fails.
+ def width
+ size[1]
+ end
+
+ # Return a screen height or the default if that fails.
+ def height
+ size[0]
+ end
+
+ private
+
+ def actual_screen_size
+ # The best way, if possible (requires non-jruby >=1.9 or io-console gem)
+ screen_size_according_to_io_console ||
+ # Fall back to the old standby, though it might be stale:
+ screen_size_according_to_env ||
+ # Fall further back, though this one is also out of date without something
+ # calling Readline.set_screen_size
+ screen_size_according_to_readline ||
+ # 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::Platform.jruby?
+
+ begin
+ require 'io/console'
+
+ begin
+ if @boxed_io.respond_to?(:tty?) &&
+ @boxed_io.tty? &&
+ @boxed_io.respond_to?(:winsize)
+ @boxed_io.winsize
+ end
+ rescue Errno::EOPNOTSUPP # rubocop:disable Lint/HandleExceptions
+ # @boxed_io is probably a socket, which doesn't support #winsize.
+ end
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
+ # They probably don't have the io/console stdlib or the io-console gem.
+ # We'll keep trying.
+ end
+ end
+
+ def screen_size_according_to_env
+ size = [ENV['LINES'] || ENV['ROWS'], ENV['COLUMNS']]
+ size if nonzero_column?(size)
+ end
+
+ def screen_size_according_to_readline
+ if defined?(Readline) && Readline.respond_to?(:get_screen_size)
+ size = Readline.get_screen_size
+ size if nonzero_column?(size)
+ end
+ 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 = [Regexp.last_match(2), Regexp.last_match(1)]
+ size if nonzero_column?(size)
+ end
+
+ def nonzero_column?(size)
+ size[1].to_i > 0
+ end
end
end
diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb
index 7296510a..828735dd 100644
--- a/lib/pry/pager.rb
+++ b/lib/pry/pager.rb
@@ -88,11 +88,11 @@ class Pry
private
def height
- @height ||= Pry::Terminal.height
+ @height ||= @out.height
end
def width
- @width ||= Pry::Terminal.width
+ @width ||= @out.width
end
end
diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb
index 6db46b14..4f3e2326 100644
--- a/lib/pry/pry_class.rb
+++ b/lib/pry/pry_class.rb
@@ -300,7 +300,7 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
trap :WINCH do
begin
- Readline.set_screen_size(*Terminal.size)
+ Readline.set_screen_size(*output.size)
rescue StandardError => e
warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
end
diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb
index ef963a07..fd1fc800 100644
--- a/lib/pry/pry_instance.rb
+++ b/lib/pry/pry_instance.rb
@@ -80,7 +80,7 @@ class Pry
# The initial context for this session.
def initialize(options = {})
@binding_stack = []
- @indent = Pry::Indent.new
+ @indent = Pry::Indent.new(self)
@eval_string = ''.dup
@backtrace = options.delete(:backtrace) || caller
target = options.delete(:target)
diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb
index d226641b..d603e8de 100644
--- a/lib/pry/repl.rb
+++ b/lib/pry/repl.rb
@@ -21,7 +21,7 @@ class Pry
# @option options [Object] :target The initial target of the session.
def initialize(pry, options = {})
@pry = pry
- @indent = Pry::Indent.new
+ @indent = Pry::Indent.new(pry)
@readline_output = nil
diff --git a/spec/indent_spec.rb b/spec/indent_spec.rb
index aa48379f..29c07a5f 100644
--- a/spec/indent_spec.rb
+++ b/spec/indent_spec.rb
@@ -5,7 +5,7 @@
# lines.
describe Pry::Indent do
before do
- @indent = Pry::Indent.new
+ @indent = Pry::Indent.new(Pry.new)
end
it 'should indent an array' do