prettyprint 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +24 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/prettyprint.rb +556 -0
- data/prettyprint.gemspec +22 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0ae9c29bdf4d1698ed37bd984cb88cc2185d3adbcb167ce7041862a0852c4e66
|
4
|
+
data.tar.gz: e9959e096d793bfea23236de42c3a8fe9f882ba60783af4d3cbde9f3e00cf443
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f926e5ae6da0608c6ca997b7c3209da719eb33acd70cf636e491d1e51e4d1a23c80324cbfaf52d3eb4af2a921eb9ee4b1996ba37e1799639900045589c3fa77
|
7
|
+
data.tar.gz: 64ef90afb2f0f900e56f805c3e4ae1e54b1e1d9dfdf32190ad9db5bf1e5aa527f5ff45a42b1e165f449c79d6145f74257c53d5026d52cad4dd2a5804d06c0849
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: test
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
name: build (${{ matrix.ruby }} / ${{ matrix.os }})
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ 2.7, 2.6, 2.5, head ]
|
11
|
+
os: [ ubuntu-latest, macos-latest ]
|
12
|
+
runs-on: ${{ matrix.os }}
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@master
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
- name: Install dependencies
|
20
|
+
run: |
|
21
|
+
gem install bundler --no-document
|
22
|
+
bundle install
|
23
|
+
- name: Run test
|
24
|
+
run: rake test
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Prettyprint
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/prettyprint`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'prettyprint'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install prettyprint
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hsbt/prettyprint.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "prettyprint"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/prettyprint.rb
ADDED
@@ -0,0 +1,556 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# This class implements a pretty printing algorithm. It finds line breaks and
|
4
|
+
# nice indentations for grouped structure.
|
5
|
+
#
|
6
|
+
# By default, the class assumes that primitive elements are strings and each
|
7
|
+
# byte in the strings have single column in width. But it can be used for
|
8
|
+
# other situations by giving suitable arguments for some methods:
|
9
|
+
# * newline object and space generation block for PrettyPrint.new
|
10
|
+
# * optional width argument for PrettyPrint#text
|
11
|
+
# * PrettyPrint#breakable
|
12
|
+
#
|
13
|
+
# There are several candidate uses:
|
14
|
+
# * text formatting using proportional fonts
|
15
|
+
# * multibyte characters which has columns different to number of bytes
|
16
|
+
# * non-string formatting
|
17
|
+
#
|
18
|
+
# == Bugs
|
19
|
+
# * Box based formatting?
|
20
|
+
# * Other (better) model/algorithm?
|
21
|
+
#
|
22
|
+
# Report any bugs at http://bugs.ruby-lang.org
|
23
|
+
#
|
24
|
+
# == References
|
25
|
+
# Christian Lindig, Strictly Pretty, March 2000,
|
26
|
+
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
|
27
|
+
#
|
28
|
+
# Philip Wadler, A prettier printer, March 1998,
|
29
|
+
# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
|
30
|
+
#
|
31
|
+
# == Author
|
32
|
+
# Tanaka Akira <akr@fsij.org>
|
33
|
+
#
|
34
|
+
class PrettyPrint
|
35
|
+
|
36
|
+
# This is a convenience method which is same as follows:
|
37
|
+
#
|
38
|
+
# begin
|
39
|
+
# q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
40
|
+
# ...
|
41
|
+
# q.flush
|
42
|
+
# output
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
def PrettyPrint.format(output=''.dup, maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
|
46
|
+
q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
47
|
+
yield q
|
48
|
+
q.flush
|
49
|
+
output
|
50
|
+
end
|
51
|
+
|
52
|
+
# This is similar to PrettyPrint::format but the result has no breaks.
|
53
|
+
#
|
54
|
+
# +maxwidth+, +newline+ and +genspace+ are ignored.
|
55
|
+
#
|
56
|
+
# The invocation of +breakable+ in the block doesn't break a line and is
|
57
|
+
# treated as just an invocation of +text+.
|
58
|
+
#
|
59
|
+
def PrettyPrint.singleline_format(output=''.dup, maxwidth=nil, newline=nil, genspace=nil)
|
60
|
+
q = SingleLine.new(output)
|
61
|
+
yield q
|
62
|
+
output
|
63
|
+
end
|
64
|
+
|
65
|
+
# Creates a buffer for pretty printing.
|
66
|
+
#
|
67
|
+
# +output+ is an output target. If it is not specified, '' is assumed. It
|
68
|
+
# should have a << method which accepts the first argument +obj+ of
|
69
|
+
# PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
|
70
|
+
# first argument +newline+ of PrettyPrint.new, and the result of a given
|
71
|
+
# block for PrettyPrint.new.
|
72
|
+
#
|
73
|
+
# +maxwidth+ specifies maximum line length. If it is not specified, 79 is
|
74
|
+
# assumed. However actual outputs may overflow +maxwidth+ if long
|
75
|
+
# non-breakable texts are provided.
|
76
|
+
#
|
77
|
+
# +newline+ is used for line breaks. "\n" is used if it is not specified.
|
78
|
+
#
|
79
|
+
# The block is used to generate spaces. {|width| ' ' * width} is used if it
|
80
|
+
# is not given.
|
81
|
+
#
|
82
|
+
def initialize(output=''.dup, maxwidth=79, newline="\n", &genspace)
|
83
|
+
@output = output
|
84
|
+
@maxwidth = maxwidth
|
85
|
+
@newline = newline
|
86
|
+
@genspace = genspace || lambda {|n| ' ' * n}
|
87
|
+
|
88
|
+
@output_width = 0
|
89
|
+
@buffer_width = 0
|
90
|
+
@buffer = []
|
91
|
+
|
92
|
+
root_group = Group.new(0)
|
93
|
+
@group_stack = [root_group]
|
94
|
+
@group_queue = GroupQueue.new(root_group)
|
95
|
+
@indent = 0
|
96
|
+
end
|
97
|
+
|
98
|
+
# The output object.
|
99
|
+
#
|
100
|
+
# This defaults to '', and should accept the << method
|
101
|
+
attr_reader :output
|
102
|
+
|
103
|
+
# The maximum width of a line, before it is separated in to a newline
|
104
|
+
#
|
105
|
+
# This defaults to 79, and should be a Fixnum
|
106
|
+
attr_reader :maxwidth
|
107
|
+
|
108
|
+
# The value that is appended to +output+ to add a new line.
|
109
|
+
#
|
110
|
+
# This defaults to "\n", and should be String
|
111
|
+
attr_reader :newline
|
112
|
+
|
113
|
+
# A lambda or Proc, that takes one argument, of a Fixnum, and returns
|
114
|
+
# the corresponding number of spaces.
|
115
|
+
#
|
116
|
+
# By default this is:
|
117
|
+
# lambda {|n| ' ' * n}
|
118
|
+
attr_reader :genspace
|
119
|
+
|
120
|
+
# The number of spaces to be indented
|
121
|
+
attr_reader :indent
|
122
|
+
|
123
|
+
# The PrettyPrint::GroupQueue of groups in stack to be pretty printed
|
124
|
+
attr_reader :group_queue
|
125
|
+
|
126
|
+
# Returns the group most recently added to the stack.
|
127
|
+
#
|
128
|
+
# Contrived example:
|
129
|
+
# out = ""
|
130
|
+
# => ""
|
131
|
+
# q = PrettyPrint.new(out)
|
132
|
+
# => #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0>
|
133
|
+
# q.group {
|
134
|
+
# q.text q.current_group.inspect
|
135
|
+
# q.text q.newline
|
136
|
+
# q.group(q.current_group.depth + 1) {
|
137
|
+
# q.text q.current_group.inspect
|
138
|
+
# q.text q.newline
|
139
|
+
# q.group(q.current_group.depth + 1) {
|
140
|
+
# q.text q.current_group.inspect
|
141
|
+
# q.text q.newline
|
142
|
+
# q.group(q.current_group.depth + 1) {
|
143
|
+
# q.text q.current_group.inspect
|
144
|
+
# q.text q.newline
|
145
|
+
# }
|
146
|
+
# }
|
147
|
+
# }
|
148
|
+
# }
|
149
|
+
# => 284
|
150
|
+
# puts out
|
151
|
+
# #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
|
152
|
+
# #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
|
153
|
+
# #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
|
154
|
+
# #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
|
155
|
+
def current_group
|
156
|
+
@group_stack.last
|
157
|
+
end
|
158
|
+
|
159
|
+
# Breaks the buffer into lines that are shorter than #maxwidth
|
160
|
+
def break_outmost_groups
|
161
|
+
while @maxwidth < @output_width + @buffer_width
|
162
|
+
return unless group = @group_queue.deq
|
163
|
+
until group.breakables.empty?
|
164
|
+
data = @buffer.shift
|
165
|
+
@output_width = data.output(@output, @output_width)
|
166
|
+
@buffer_width -= data.width
|
167
|
+
end
|
168
|
+
while !@buffer.empty? && Text === @buffer.first
|
169
|
+
text = @buffer.shift
|
170
|
+
@output_width = text.output(@output, @output_width)
|
171
|
+
@buffer_width -= text.width
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# This adds +obj+ as a text of +width+ columns in width.
|
177
|
+
#
|
178
|
+
# If +width+ is not specified, obj.length is used.
|
179
|
+
#
|
180
|
+
def text(obj, width=obj.length)
|
181
|
+
if @buffer.empty?
|
182
|
+
@output << obj
|
183
|
+
@output_width += width
|
184
|
+
else
|
185
|
+
text = @buffer.last
|
186
|
+
unless Text === text
|
187
|
+
text = Text.new
|
188
|
+
@buffer << text
|
189
|
+
end
|
190
|
+
text.add(obj, width)
|
191
|
+
@buffer_width += width
|
192
|
+
break_outmost_groups
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# This is similar to #breakable except
|
197
|
+
# the decision to break or not is determined individually.
|
198
|
+
#
|
199
|
+
# Two #fill_breakable under a group may cause 4 results:
|
200
|
+
# (break,break), (break,non-break), (non-break,break), (non-break,non-break).
|
201
|
+
# This is different to #breakable because two #breakable under a group
|
202
|
+
# may cause 2 results:
|
203
|
+
# (break,break), (non-break,non-break).
|
204
|
+
#
|
205
|
+
# The text +sep+ is inserted if a line is not broken at this point.
|
206
|
+
#
|
207
|
+
# If +sep+ is not specified, " " is used.
|
208
|
+
#
|
209
|
+
# If +width+ is not specified, +sep.length+ is used. You will have to
|
210
|
+
# specify this when +sep+ is a multibyte character, for example.
|
211
|
+
#
|
212
|
+
def fill_breakable(sep=' ', width=sep.length)
|
213
|
+
group { breakable sep, width }
|
214
|
+
end
|
215
|
+
|
216
|
+
# This says "you can break a line here if necessary", and a +width+\-column
|
217
|
+
# text +sep+ is inserted if a line is not broken at the point.
|
218
|
+
#
|
219
|
+
# If +sep+ is not specified, " " is used.
|
220
|
+
#
|
221
|
+
# If +width+ is not specified, +sep.length+ is used. You will have to
|
222
|
+
# specify this when +sep+ is a multibyte character, for example.
|
223
|
+
#
|
224
|
+
def breakable(sep=' ', width=sep.length)
|
225
|
+
group = @group_stack.last
|
226
|
+
if group.break?
|
227
|
+
flush
|
228
|
+
@output << @newline
|
229
|
+
@output << @genspace.call(@indent)
|
230
|
+
@output_width = @indent
|
231
|
+
@buffer_width = 0
|
232
|
+
else
|
233
|
+
@buffer << Breakable.new(sep, width, self)
|
234
|
+
@buffer_width += width
|
235
|
+
break_outmost_groups
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
# Groups line break hints added in the block. The line break hints are all
|
240
|
+
# to be used or not.
|
241
|
+
#
|
242
|
+
# If +indent+ is specified, the method call is regarded as nested by
|
243
|
+
# nest(indent) { ... }.
|
244
|
+
#
|
245
|
+
# If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
|
246
|
+
# before grouping. If +close_obj+ is specified, <tt>text close_obj,
|
247
|
+
# close_width</tt> is called after grouping.
|
248
|
+
#
|
249
|
+
def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
|
250
|
+
text open_obj, open_width
|
251
|
+
group_sub {
|
252
|
+
nest(indent) {
|
253
|
+
yield
|
254
|
+
}
|
255
|
+
}
|
256
|
+
text close_obj, close_width
|
257
|
+
end
|
258
|
+
|
259
|
+
# Takes a block and queues a new group that is indented 1 level further.
|
260
|
+
def group_sub
|
261
|
+
group = Group.new(@group_stack.last.depth + 1)
|
262
|
+
@group_stack.push group
|
263
|
+
@group_queue.enq group
|
264
|
+
begin
|
265
|
+
yield
|
266
|
+
ensure
|
267
|
+
@group_stack.pop
|
268
|
+
if group.breakables.empty?
|
269
|
+
@group_queue.delete group
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# Increases left margin after newline with +indent+ for line breaks added in
|
275
|
+
# the block.
|
276
|
+
#
|
277
|
+
def nest(indent)
|
278
|
+
@indent += indent
|
279
|
+
begin
|
280
|
+
yield
|
281
|
+
ensure
|
282
|
+
@indent -= indent
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
# outputs buffered data.
|
287
|
+
#
|
288
|
+
def flush
|
289
|
+
@buffer.each {|data|
|
290
|
+
@output_width = data.output(@output, @output_width)
|
291
|
+
}
|
292
|
+
@buffer.clear
|
293
|
+
@buffer_width = 0
|
294
|
+
end
|
295
|
+
|
296
|
+
# The Text class is the means by which to collect strings from objects.
|
297
|
+
#
|
298
|
+
# This class is intended for internal use of the PrettyPrint buffers.
|
299
|
+
class Text # :nodoc:
|
300
|
+
|
301
|
+
# Creates a new text object.
|
302
|
+
#
|
303
|
+
# This constructor takes no arguments.
|
304
|
+
#
|
305
|
+
# The workflow is to append a PrettyPrint::Text object to the buffer, and
|
306
|
+
# being able to call the buffer.last() to reference it.
|
307
|
+
#
|
308
|
+
# As there are objects, use PrettyPrint::Text#add to include the objects
|
309
|
+
# and the width to utilized by the String version of this object.
|
310
|
+
def initialize
|
311
|
+
@objs = []
|
312
|
+
@width = 0
|
313
|
+
end
|
314
|
+
|
315
|
+
# The total width of the objects included in this Text object.
|
316
|
+
attr_reader :width
|
317
|
+
|
318
|
+
# Render the String text of the objects that have been added to this Text object.
|
319
|
+
#
|
320
|
+
# Output the text to +out+, and increment the width to +output_width+
|
321
|
+
def output(out, output_width)
|
322
|
+
@objs.each {|obj| out << obj}
|
323
|
+
output_width + @width
|
324
|
+
end
|
325
|
+
|
326
|
+
# Include +obj+ in the objects to be pretty printed, and increment
|
327
|
+
# this Text object's total width by +width+
|
328
|
+
def add(obj, width)
|
329
|
+
@objs << obj
|
330
|
+
@width += width
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
# The Breakable class is used for breaking up object information
|
335
|
+
#
|
336
|
+
# This class is intended for internal use of the PrettyPrint buffers.
|
337
|
+
class Breakable # :nodoc:
|
338
|
+
|
339
|
+
# Create a new Breakable object.
|
340
|
+
#
|
341
|
+
# Arguments:
|
342
|
+
# * +sep+ String of the separator
|
343
|
+
# * +width+ Fixnum width of the +sep+
|
344
|
+
# * +q+ parent PrettyPrint object, to base from
|
345
|
+
def initialize(sep, width, q)
|
346
|
+
@obj = sep
|
347
|
+
@width = width
|
348
|
+
@pp = q
|
349
|
+
@indent = q.indent
|
350
|
+
@group = q.current_group
|
351
|
+
@group.breakables.push self
|
352
|
+
end
|
353
|
+
|
354
|
+
# Holds the separator String
|
355
|
+
#
|
356
|
+
# The +sep+ argument from ::new
|
357
|
+
attr_reader :obj
|
358
|
+
|
359
|
+
# The width of +obj+ / +sep+
|
360
|
+
attr_reader :width
|
361
|
+
|
362
|
+
# The number of spaces to indent.
|
363
|
+
#
|
364
|
+
# This is inferred from +q+ within PrettyPrint, passed in ::new
|
365
|
+
attr_reader :indent
|
366
|
+
|
367
|
+
# Render the String text of the objects that have been added to this
|
368
|
+
# Breakable object.
|
369
|
+
#
|
370
|
+
# Output the text to +out+, and increment the width to +output_width+
|
371
|
+
def output(out, output_width)
|
372
|
+
@group.breakables.shift
|
373
|
+
if @group.break?
|
374
|
+
out << @pp.newline
|
375
|
+
out << @pp.genspace.call(@indent)
|
376
|
+
@indent
|
377
|
+
else
|
378
|
+
@pp.group_queue.delete @group if @group.breakables.empty?
|
379
|
+
out << @obj
|
380
|
+
output_width + @width
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
# The Group class is used for making indentation easier.
|
386
|
+
#
|
387
|
+
# While this class does neither the breaking into newlines nor indentation,
|
388
|
+
# it is used in a stack (as well as a queue) within PrettyPrint, to group
|
389
|
+
# objects.
|
390
|
+
#
|
391
|
+
# For information on using groups, see PrettyPrint#group
|
392
|
+
#
|
393
|
+
# This class is intended for internal use of the PrettyPrint buffers.
|
394
|
+
class Group # :nodoc:
|
395
|
+
# Create a Group object
|
396
|
+
#
|
397
|
+
# Arguments:
|
398
|
+
# * +depth+ - this group's relation to previous groups
|
399
|
+
def initialize(depth)
|
400
|
+
@depth = depth
|
401
|
+
@breakables = []
|
402
|
+
@break = false
|
403
|
+
end
|
404
|
+
|
405
|
+
# This group's relation to previous groups
|
406
|
+
attr_reader :depth
|
407
|
+
|
408
|
+
# Array to hold the Breakable objects for this Group
|
409
|
+
attr_reader :breakables
|
410
|
+
|
411
|
+
# Makes a break for this Group, and returns true
|
412
|
+
def break
|
413
|
+
@break = true
|
414
|
+
end
|
415
|
+
|
416
|
+
# Boolean of whether this Group has made a break
|
417
|
+
def break?
|
418
|
+
@break
|
419
|
+
end
|
420
|
+
|
421
|
+
# Boolean of whether this Group has been queried for being first
|
422
|
+
#
|
423
|
+
# This is used as a predicate, and ought to be called first.
|
424
|
+
def first?
|
425
|
+
if defined? @first
|
426
|
+
false
|
427
|
+
else
|
428
|
+
@first = false
|
429
|
+
true
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
# The GroupQueue class is used for managing the queue of Group to be pretty
|
435
|
+
# printed.
|
436
|
+
#
|
437
|
+
# This queue groups the Group objects, based on their depth.
|
438
|
+
#
|
439
|
+
# This class is intended for internal use of the PrettyPrint buffers.
|
440
|
+
class GroupQueue # :nodoc:
|
441
|
+
# Create a GroupQueue object
|
442
|
+
#
|
443
|
+
# Arguments:
|
444
|
+
# * +groups+ - one or more PrettyPrint::Group objects
|
445
|
+
def initialize(*groups)
|
446
|
+
@queue = []
|
447
|
+
groups.each {|g| enq g}
|
448
|
+
end
|
449
|
+
|
450
|
+
# Enqueue +group+
|
451
|
+
#
|
452
|
+
# This does not strictly append the group to the end of the queue,
|
453
|
+
# but instead adds it in line, base on the +group.depth+
|
454
|
+
def enq(group)
|
455
|
+
depth = group.depth
|
456
|
+
@queue << [] until depth < @queue.length
|
457
|
+
@queue[depth] << group
|
458
|
+
end
|
459
|
+
|
460
|
+
# Returns the outer group of the queue
|
461
|
+
def deq
|
462
|
+
@queue.each {|gs|
|
463
|
+
(gs.length-1).downto(0) {|i|
|
464
|
+
unless gs[i].breakables.empty?
|
465
|
+
group = gs.slice!(i, 1).first
|
466
|
+
group.break
|
467
|
+
return group
|
468
|
+
end
|
469
|
+
}
|
470
|
+
gs.each {|group| group.break}
|
471
|
+
gs.clear
|
472
|
+
}
|
473
|
+
return nil
|
474
|
+
end
|
475
|
+
|
476
|
+
# Remote +group+ from this queue
|
477
|
+
def delete(group)
|
478
|
+
@queue[group.depth].delete(group)
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
# PrettyPrint::SingleLine is used by PrettyPrint.singleline_format
|
483
|
+
#
|
484
|
+
# It is passed to be similar to a PrettyPrint object itself, by responding to:
|
485
|
+
# * #text
|
486
|
+
# * #breakable
|
487
|
+
# * #nest
|
488
|
+
# * #group
|
489
|
+
# * #flush
|
490
|
+
# * #first?
|
491
|
+
#
|
492
|
+
# but instead, the output has no line breaks
|
493
|
+
#
|
494
|
+
class SingleLine
|
495
|
+
# Create a PrettyPrint::SingleLine object
|
496
|
+
#
|
497
|
+
# Arguments:
|
498
|
+
# * +output+ - String (or similar) to store rendered text. Needs to respond to '<<'
|
499
|
+
# * +maxwidth+ - Argument position expected to be here for compatibility.
|
500
|
+
# This argument is a noop.
|
501
|
+
# * +newline+ - Argument position expected to be here for compatibility.
|
502
|
+
# This argument is a noop.
|
503
|
+
def initialize(output, maxwidth=nil, newline=nil)
|
504
|
+
@output = output
|
505
|
+
@first = [true]
|
506
|
+
end
|
507
|
+
|
508
|
+
# Add +obj+ to the text to be output.
|
509
|
+
#
|
510
|
+
# +width+ argument is here for compatibility. It is a noop argument.
|
511
|
+
def text(obj, width=nil)
|
512
|
+
@output << obj
|
513
|
+
end
|
514
|
+
|
515
|
+
# Appends +sep+ to the text to be output. By default +sep+ is ' '
|
516
|
+
#
|
517
|
+
# +width+ argument is here for compatibility. It is a noop argument.
|
518
|
+
def breakable(sep=' ', width=nil)
|
519
|
+
@output << sep
|
520
|
+
end
|
521
|
+
|
522
|
+
# Takes +indent+ arg, but does nothing with it.
|
523
|
+
#
|
524
|
+
# Yields to a block.
|
525
|
+
def nest(indent) # :nodoc:
|
526
|
+
yield
|
527
|
+
end
|
528
|
+
|
529
|
+
# Opens a block for grouping objects to be pretty printed.
|
530
|
+
#
|
531
|
+
# Arguments:
|
532
|
+
# * +indent+ - noop argument. Present for compatibility.
|
533
|
+
# * +open_obj+ - text appended before the &blok. Default is ''
|
534
|
+
# * +close_obj+ - text appended after the &blok. Default is ''
|
535
|
+
# * +open_width+ - noop argument. Present for compatibility.
|
536
|
+
# * +close_width+ - noop argument. Present for compatibility.
|
537
|
+
def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
|
538
|
+
@first.push true
|
539
|
+
@output << open_obj
|
540
|
+
yield
|
541
|
+
@output << close_obj
|
542
|
+
@first.pop
|
543
|
+
end
|
544
|
+
|
545
|
+
# Method present for compatibility, but is a noop
|
546
|
+
def flush # :nodoc:
|
547
|
+
end
|
548
|
+
|
549
|
+
# This is used as a predicate, and ought to be called first.
|
550
|
+
def first?
|
551
|
+
result = @first[-1]
|
552
|
+
@first[-1] = false
|
553
|
+
result
|
554
|
+
end
|
555
|
+
end
|
556
|
+
end
|
data/prettyprint.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "prettyprint"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Tanaka Akira"]
|
5
|
+
spec.email = ["akr@fsij.org"]
|
6
|
+
|
7
|
+
spec.summary = %q{Implements a pretty printing algorithm for readable structure.}
|
8
|
+
spec.description = %q{Implements a pretty printing algorithm for readable structure.}
|
9
|
+
spec.homepage = "https://github.com/ruby/prettyprint"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prettyprint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tanaka Akira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Implements a pretty printing algorithm for readable structure.
|
14
|
+
email:
|
15
|
+
- akr@fsij.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/test.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/prettyprint.rb
|
29
|
+
- prettyprint.gemspec
|
30
|
+
homepage: https://github.com/ruby/prettyprint
|
31
|
+
licenses:
|
32
|
+
- Ruby
|
33
|
+
- BSD-2-Clause
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/ruby/prettyprint
|
36
|
+
source_code_uri: https://github.com/ruby/prettyprint
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.2.0.rc.1
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Implements a pretty printing algorithm for readable structure.
|
56
|
+
test_files: []
|