summaryrefslogtreecommitdiff
path: root/lib/prettyprint.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-10-08 01:58:34 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-10-08 01:58:34 +0000
commite2d1e7cfe4873acb518a8265a222307a90d6298a (patch)
treed8fa13d71b57727c7a229defca40c847fd5fe07e /lib/prettyprint.rb
parentb06720481e8f004565b55d7862a8a653d40f19d8 (diff)
* 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
Diffstat (limited to 'lib/prettyprint.rb')
-rw-r--r--lib/prettyprint.rb55
1 files changed, 52 insertions, 3 deletions
diff --git a/lib/prettyprint.rb b/lib/prettyprint.rb
index e1b7e7c59a..7f1497b541 100644
--- a/lib/prettyprint.rb
+++ b/lib/prettyprint.rb
@@ -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