https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112016-11-09T16:10:55ZRuby Issue Tracking SystemRuby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=614112016-11-09T16:10:55Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>I think the columns obtained by IO#winsize of io/console is better default now.</p>
<p>I'm not sure you are satisfied with it, though.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=614132016-11-09T22:52:28Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Akr-san, thank you for your reply.</p>
<p>I agree that it is a better default than the fixed 79.</p>
<p>However, I'd still like to have <code>PP.default_maxwidth=</code> too.<br>
My motivation is to show abstract syntax tree (in <a href="http://ascii.jp/elem/000/001/255/1255878/" class="external">my article</a>).<br>
In such a case, I think that "break as far as possible" would be useful.</p>
<p>Thank you for your consideration,</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=614282016-11-11T02:04:16Zakr (Akira Tanaka)akr@fsij.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Yusuke Endoh wrote:</p>
<blockquote>
<p>In such a case, I think that "break as far as possible" would be useful.</p>
</blockquote>
<p>"break as far as possible" is opposite to pp.rb's intent:<br>
"don't break as far as possible (in screen width limitation)".</p>
<p>I think a global configuration to choose different intent for single API is not a good idea.</p>
<p>pp (including pretty_inspect and PP.pp) method is used in many gems.<br>
I think the intent of most of them is "don't break as far as possible".<br>
PP.default_maxwidth = 1 breaks the intent.</p>
<p>I recommend that you define a method to call PP.pp with width = 1.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=614302016-11-11T03:31:39Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Akira Tanaka wrote:</p>
<blockquote>
<p>pp (including pretty_inspect and PP.pp) method is used in many gems.</p>
</blockquote>
<p>Note that my proposal is only for PP; the scope does not include pretty_inspect.</p>
<p>PP is a debugging-purpose API, so it is rarely used in a released code. So, we don't have to consider the case where PP is used in multiple modules simultaneously, I think. Also, because of debugging-purpose, it would be good to prioritize usability than safety.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=621642016-12-21T07:15:50Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>I think pp is not only for debug.<br>
I guess someone use it for logging, for example.</p>
<p>So, I think global configuration is dangerous,</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=621732016-12-21T09:06:36Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul><p>Okay, I respect your decision. Thank you for your time.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=702192018-02-06T04:47:15Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>What about this?</p>
<ul>
<li>affects <code>PP.pp</code> and <code>Kernel#pp</code> only</li>
<li>try console window size, <code>COLUMNS</code> environment variable, then old good 80</li>
</ul>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/lib/pp.rb b/lib/pp.rb
</span><span class="p">indiff --git a/lib/pp.rb b/lib/pp.rb
</span><span class="gh">index 85401c8aa6..a364d0fc2a 100644
</span><span class="gd">--- a/lib/pp.rb
</span><span class="gi">+++ b/lib/pp.rb
</span><span class="p">@@ -68,7 +68,7 @@</span>
# If +width+ is omitted, 79 is assumed.
#
# PP.pp returns +out+.
<span class="gd">- def PP.pp(obj, out=$>, width=79)
</span><span class="gi">+ def PP.pp(obj, out=$>, width=PP.width_for(out))
</span> q = PP.new(out, width)
q.guard_inspect_key {q.pp obj}
q.flush
<span class="p">@@ -91,6 +91,15 @@</span>
def PP.mcall(obj, mod, meth, *args, &block)
mod.instance_method(meth).bind(obj).call(*args, &block)
end
<span class="gi">+
+ 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
</span> # :startdoc:
@sharing_detection = false
<span class="p">@@ -549,9 +558,9 @@</span>
# prints arguments in pretty form.
#
# pp returns argument(s).
<span class="gd">- def pp(*objs)
</span><span class="gi">+ def pp(*objs, out: $>, width: PP.width_for(out))
</span> objs.each {|obj|
<span class="gd">- PP.pp(obj)
</span><span class="gi">+ PP.pp(obj, out, width)
</span> }
objs.size <= 1 ? objs.first : objs
end
<span class="err">
</span></code></pre> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=702212018-02-06T05:30:16Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>What about this?</p>
<ul>
<li>affects <code>PP.pp</code> and <code>Kernel#pp</code> only</li>
<li>try console window size, <code>COLUMNS</code> environment variable, then old good 80</li>
</ul>
</blockquote>
<p>It seems fine.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=921562021-05-25T04:44:07Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Assigned</i></li><li><strong>Assignee</strong> changed from <i>akr (Akira Tanaka)</i> to <i>nobu (Nobuyoshi Nakada)</i></li></ul><p>akr (Akira Tanaka) wrote in <a href="#note-8">#note-8</a>:</p>
<blockquote>
<p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>What about this?</p>
<ul>
<li>affects <code>PP.pp</code> and <code>Kernel#pp</code> only</li>
<li>try console window size, <code>COLUMNS</code> environment variable, then old good 80</li>
</ul>
</blockquote>
<p>It seems fine.</p>
</blockquote>
<p>I prefer @nobu's patch to mine. Since it was fine to <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/271">@akr (Akira Tanaka)</a>, how about committing it?</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=921972021-05-26T13:00:12Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p><a href="https://github.com/nobu/ruby/runs/2672425246?check_suite_focus=true#step:15:239" class="external">https://github.com/nobu/ruby/runs/2672425246?check_suite_focus=true#step:15:239</a><br>
A test in rbs passes keyword arguments to <code>pp</code>.<br>
Also <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a> says he uses keywords arguments with <code>pp</code> very often.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=923642021-06-07T07:16:48Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>So, some people (including <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a>) have a custom to pass keyword arguments to Kernel#pp:</p>
<pre><code>foo = 42
bar = 43
pp(foo: foo, bar: bar) #=> {:foo=>42, :bar=>43}
</code></pre>
<p>Unfortunately, introducing keyword arguments to <code>Kernel#pp</code> breaks their custom.</p>
<p>How about introducing <code>pp_out</code> and <code>pp_width</code> keywords? It is a bit dirty, but good enoguh.</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/lib/pp.rb b/lib/pp.rb
index 72480e5304..b090d2bdeb 100644
</span><span class="gd">--- a/lib/pp.rb
</span><span class="gi">+++ b/lib/pp.rb
</span><span class="p">@@ -91,6 +91,15 @@</span> def PP.singleline_pp(obj, out=$>)
def PP.mcall(obj, mod, meth, *args, &block)
mod.instance_method(meth).bind_call(obj, *args, &block)
end
<span class="gi">+
+ 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
</span> # :startdoc:
<span class="err">
</span> if defined? ::Ractor
<span class="p">@@ -596,12 +605,21 @@</span> def pretty_inspect
PP.pp(self, ''.dup)
end
<span class="err">
</span><span class="gd">- # prints arguments in pretty form.
</span><span class="gi">+ # Prints arguments in pretty form.
</span> #
# pp returns argument(s).
<span class="gd">- def pp(*objs)
</span><span class="gi">+ #
+ # It prints to +$>+ by default. A keyword argument +pp_out+ is given,
+ # it is used as the output target.
+ #
+ # The output is fitted to the width of the console by default.
+ # A keyword +pp_width+ is used if given.
+ def pp(*objs, **kw)
+ out = kw.delete(:pp_out) || $>
+ width = kw.delete(:pp_width) || PP.width_for(out)
+ objs << kw unless kw.empty?
</span> objs.each {|obj|
<span class="gd">- PP.pp(obj)
</span><span class="gi">+ PP.pp(obj, out, width)
</span> }
objs.size <= 1 ? objs.first : objs
end
<span class="gh">diff --git a/prelude.rb b/prelude.rb
index b1e477a3ea..b8c564e84e 100644
</span><span class="gd">--- a/prelude.rb
</span><span class="gi">+++ b/prelude.rb
</span><span class="p">@@ -10,9 +10,9 @@</span> def irb
end
<span class="err">
</span> module Kernel
<span class="gd">- def pp(*objs)
</span><span class="gi">+ def pp(...)
</span> require 'pp'
<span class="gd">- pp(*objs)
</span><span class="gi">+ pp(...)
</span> end
<span class="err">
</span> # suppress redefinition warning
</code></pre> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=925582021-06-17T09:25:07Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>We discussed this issue in today's dev-meeting. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/271">@akr (Akira Tanaka)</a> suggested that</p>
<ol>
<li>To make the default size to be io/console's winsize (width)</li>
<li>Instead of changing <code>Kernel#pp</code>, extend <code>PP.pp</code> to accept keywords <code>out</code> and <code>width</code>
</li>
<li>If we want <code>width=1</code>, define another new method like <code>PP.fully_expanded_pp(obj)</code> or something (Note that there is already <code>PP.singleline_pp(obj)</code>)</li>
</ol>
<p>and we agreed with them. I'll commit a change for 1 later.</p>
<p>I am happy to create another patch for 2 and 3. Anyone have an idea about the name of <code>fully_expanded_pp</code>?</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=925672021-06-17T14:51:27Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>mame (Yusuke Endoh) wrote in <a href="#note-12">#note-12</a>:</p>
<blockquote>
<p>We discussed this issue in today's dev-meeting. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/271">@akr (Akira Tanaka)</a> suggested that</p>
<ol>
<li>To make the default size to be io/console's winsize (width)</li>
<li>Instead of changing <code>Kernel#pp</code>, extend <code>PP.pp</code> to accept keywords <code>out</code> and <code>width</code>
</li>
<li>If we want <code>width=1</code>, define another new method like <code>PP.fully_expanded_pp(obj)</code> or something (Note that there is already <code>PP.singleline_pp(obj)</code>)</li>
</ol>
<p>and we agreed with them. I'll commit a change for 1 later.</p>
<p>I am happy to create another patch for 2 and 3. Anyone have an idea about the name of <code>fully_expanded_pp</code>?</p>
</blockquote>
<p>In keeping with the spirit that <code>Kernel#pp</code> is more expanded than <code>Kernel#p</code>, I suggest <code>PP.ppp</code>. :)</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=949482021-11-30T02:44:25Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="lib/pp.rb (PP.pp): Use io/console's winsize by default [Feature #12913]" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/eac347fdb04023e1a365d84a8c163263cc7a5904">git|eac347fdb04023e1a365d84a8c163263cc7a5904</a>.</p>
<hr>
<p>lib/pp.rb (PP.pp): Use io/console's winsize by default</p>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: A way to configure the default maximum width of pp (Closed)" href="https://bugs.ruby-lang.org/issues/12913">#12913</a>]</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=949502021-11-30T04:14:25Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul><p>I have committed the change for 1.</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=951882021-12-07T03:08:58Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="NEWS.md: Describe the change of the default width of `pp` [Feature #12913]" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/dfd9728c87640d9699b1e7c63a6fefb6b2797fbc">git|dfd9728c87640d9699b1e7c63a6fefb6b2797fbc</a>.</p>
<hr>
<p>NEWS.md: Describe the change of the default width of <code>pp</code></p>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: A way to configure the default maximum width of pp (Closed)" href="https://bugs.ruby-lang.org/issues/12913">#12913</a>]</p> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=951962021-12-07T07:44:19Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul> Ruby master - Feature #12913: A way to configure the default maximum width of pphttps://bugs.ruby-lang.org/issues/12913?journal_id=954772021-12-23T09:03:13Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="[ruby/pp] [DOC] Update for PP.width_for [Feature #12913] https://github.com/ruby/pp/commit/cad3c..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/c6cf19340a116f21d71ecc218a2e8c31fe2f1d85">git|c6cf19340a116f21d71ecc218a2e8c31fe2f1d85</a>.</p>
<hr>
<p>[ruby/pp] [DOC] Update for PP.width_for [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: A way to configure the default maximum width of pp (Closed)" href="https://bugs.ruby-lang.org/issues/12913">#12913</a>]</p>
<p><a href="https://github.com/ruby/pp/commit/cad3cc762c" class="external">https://github.com/ruby/pp/commit/cad3cc762c</a></p>