summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-11-30 11:42:06 +0900
committerYusuke Endoh <mame@ruby-lang.org>2021-11-30 11:43:54 +0900
commiteac347fdb04023e1a365d84a8c163263cc7a5904 (patch)
treef4a15e8b2a8f08547b105099fdb3bb36c5e419f2
parentf379748e80dacc208dbe23a2db508aead8b79978 (diff)
lib/pp.rb (PP.pp): Use io/console's winsize by default
[Feature #12913]
-rw-r--r--lib/pp.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/pp.rb b/lib/pp.rb
index 72480e5304..c164110508 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -61,6 +61,15 @@ require 'prettyprint'
# Tanaka Akira <akr@fsij.org>
class PP < PrettyPrint
+ def PP.width_for(out)
+ begin
+ require 'io/console'
+ _, width = out.winsize
+ rescue LoadError, NoMethodError, Errno::ENOTTY
+ end
+ (width || ENV['COLUMNS']&.to_i&.nonzero? || 80) - 1
+ end
+
# Outputs +obj+ to +out+ in pretty printed format of
# +width+ columns in width.
#
@@ -68,7 +77,7 @@ class PP < PrettyPrint
# If +width+ is omitted, 79 is assumed.
#
# PP.pp returns +out+.
- def PP.pp(obj, out=$>, width=79)
+ def PP.pp(obj, out=$>, width=width_for(out))
q = PP.new(out, width)
q.guard_inspect_key {q.pp obj}
q.flush