* 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
@ -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>
|
Mon Oct 7 16:43:07 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* bignum.c (bigdivrem): bignum zero's len should not be 0.
|
* 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.pp returns ((|out|)).
|
||||||
|
|
||||||
--- PP.sharing_detection
|
--- 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.
|
It is false by default.
|
||||||
|
|
||||||
--- PP.sharing_detection = boolean_value
|
--- PP.sharing_detection = boolean_value
|
||||||
|
@ -21,7 +21,7 @@ non-string formatting, etc.
|
|||||||
--- PrettyPrint.new([output[, maxwidth[, newline]]]) [{|width| ...}]
|
--- PrettyPrint.new([output[, maxwidth[, newline]]]) [{|width| ...}]
|
||||||
creates a buffer for pretty printing.
|
creates a buffer for pretty printing.
|
||||||
|
|
||||||
((|output|)) is a output target.
|
((|output|)) is an output target.
|
||||||
If it is not specified, (({''})) is assumed.
|
If it is not specified, (({''})) is assumed.
|
||||||
It should have a (({<<})) method which accepts
|
It should have a (({<<})) method which accepts
|
||||||
the first argument ((|obj|)) of (({PrettyPrint#text})),
|
the first argument ((|obj|)) of (({PrettyPrint#text})),
|
||||||
@ -45,12 +45,19 @@ non-string formatting, etc.
|
|||||||
is a convenience method which is same as follows:
|
is a convenience method which is same as follows:
|
||||||
|
|
||||||
begin
|
begin
|
||||||
pp = PrettyPrint.format(output, maxwidth, newline, &genspace)
|
pp = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
||||||
...
|
...
|
||||||
pp.flush
|
pp.flush
|
||||||
output
|
output
|
||||||
end
|
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
|
== methods
|
||||||
--- text(obj[, width])
|
--- text(obj[, width])
|
||||||
adds ((|obj|)) as a text of ((|width|)) columns in 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>))
|
((<URL:http://www.gaertner.de/~lindig/papers/strictly-pretty.html>))
|
||||||
|
|
||||||
Philip Wadler, A prettier printer, March 1998,
|
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
|
=end
|
||||||
|
|
||||||
@ -122,6 +129,12 @@ class PrettyPrint
|
|||||||
output
|
output
|
||||||
end
|
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)
|
def initialize(output='', maxwidth=79, newline="\n", &genspace)
|
||||||
@output = output
|
@output = output
|
||||||
@maxwidth = maxwidth
|
@maxwidth = maxwidth
|
||||||
@ -340,6 +353,42 @@ class PrettyPrint
|
|||||||
@queue[group.depth].delete(group)
|
@queue[group.depth].delete(group)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
if __FILE__ == $0
|
if __FILE__ == $0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user