mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prettyprint.rb (PrettyPrint.singleline_format): new method.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2944 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b06720481e
commit
e2d1e7cfe4
3 changed files with 57 additions and 4 deletions
|
@ -1,3 +1,7 @@
|
|||
Tue Oct 8 10:55:23 2002 Tanaka Akira <akr@m17n.org>
|
||||
|
||||
* lib/prettyprint.rb (PrettyPrint.singleline_format): new method.
|
||||
|
||||
Mon Oct 7 16:43:07 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* bignum.c (bigdivrem): bignum zero's len should not be 0.
|
||||
|
|
|
@ -79,7 +79,7 @@ PP#pp to print the object.
|
|||
PP.pp returns ((|out|)).
|
||||
|
||||
--- PP.sharing_detection
|
||||
returns the sharing detection flag as boolean value.
|
||||
returns the sharing detection flag as a boolean value.
|
||||
It is false by default.
|
||||
|
||||
--- PP.sharing_detection = boolean_value
|
||||
|
|
|
@ -21,7 +21,7 @@ non-string formatting, etc.
|
|||
--- PrettyPrint.new([output[, maxwidth[, newline]]]) [{|width| ...}]
|
||||
creates a buffer for pretty printing.
|
||||
|
||||
((|output|)) is a output target.
|
||||
((|output|)) is an output target.
|
||||
If it is not specified, (({''})) is assumed.
|
||||
It should have a (({<<})) method which accepts
|
||||
the first argument ((|obj|)) of (({PrettyPrint#text})),
|
||||
|
@ -45,12 +45,19 @@ non-string formatting, etc.
|
|||
is a convenience method which is same as follows:
|
||||
|
||||
begin
|
||||
pp = PrettyPrint.format(output, maxwidth, newline, &genspace)
|
||||
pp = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
||||
...
|
||||
pp.flush
|
||||
output
|
||||
end
|
||||
|
||||
--- PrettyPrint.singleline_format([output[, maxwidth[, newline[, genspace]]]]) {|pp| ...}
|
||||
is similar to (({PrettyPrint.format})) but the result has no breaks.
|
||||
|
||||
((|maxwidth|)), ((|newline|)) and ((|genspace|)) are ignored.
|
||||
The invocation of (({breakable})) in the block doesn't break a line and
|
||||
treated as just an invocation of (({text})).
|
||||
|
||||
== methods
|
||||
--- text(obj[, width])
|
||||
adds ((|obj|)) as a text of ((|width|)) columns in width.
|
||||
|
@ -110,7 +117,7 @@ Christian Lindig, Strictly Pretty, March 2000,
|
|||
((<URL:http://www.gaertner.de/~lindig/papers/strictly-pretty.html>))
|
||||
|
||||
Philip Wadler, A prettier printer, March 1998,
|
||||
((<URL:http://cm.bell-labs.com/cm/cs/who/wadler/topics/recent.html#prettier>))
|
||||
((<URL:http://www.research.avayalabs.com/user/wadler/topics/recent.html#prettier>))
|
||||
|
||||
=end
|
||||
|
||||
|
@ -122,6 +129,12 @@ class PrettyPrint
|
|||
output
|
||||
end
|
||||
|
||||
def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
|
||||
pp = SingleLine.new(output)
|
||||
yield pp
|
||||
output
|
||||
end
|
||||
|
||||
def initialize(output='', maxwidth=79, newline="\n", &genspace)
|
||||
@output = output
|
||||
@maxwidth = maxwidth
|
||||
|
@ -340,6 +353,42 @@ class PrettyPrint
|
|||
@queue[group.depth].delete(group)
|
||||
end
|
||||
end
|
||||
|
||||
class SingleLine
|
||||
def initialize(output, maxwidth=nil, newline=nil)
|
||||
@output = output
|
||||
@first = [true]
|
||||
end
|
||||
|
||||
def text(obj, width=nil)
|
||||
@output << obj
|
||||
end
|
||||
|
||||
def breakable(sep=' ', width=nil)
|
||||
@output << sep
|
||||
end
|
||||
|
||||
def nest(indent)
|
||||
yield
|
||||
end
|
||||
|
||||
def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
|
||||
@first.push true
|
||||
@output << open_obj
|
||||
yield
|
||||
@output << close_obj
|
||||
@first.pop
|
||||
end
|
||||
|
||||
def flush
|
||||
end
|
||||
|
||||
def first?
|
||||
result = @first[-1]
|
||||
@first[-1] = false
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
|
|
Loading…
Add table
Reference in a new issue