PHP 8.2.31
Preview: prettyprint.rb Size: 9.63 KB
//proc/thread-self/root/opt/alt/ruby19/lib64/ruby/1.9.1/prettyprint.rb

# This class implements a pretty printing algorithm. It finds line breaks and
# nice indentations for grouped structure.
#
# By default, the class assumes that primitive elements are strings and each
# byte in the strings have single column in width. But it can be used for
# other situations by giving suitable arguments for some methods:
# * newline object and space generation block for PrettyPrint.new
# * optional width argument for PrettyPrint#text
# * PrettyPrint#breakable
#
# There are several candidate uses:
# * text formatting using proportional fonts
# * multibyte characters which has columns different to number of bytes
# * non-string formatting
#
# == Bugs
# * Box based formatting?
# * Other (better) model/algorithm?
#
# == References
# Christian Lindig, Strictly Pretty, March 2000,
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
#
# Philip Wadler, A prettier printer, March 1998,
# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
#
# == Author
# Tanaka Akira <akr@m17n.org>
#
class PrettyPrint

  # This is a convenience method which is same as follows:
  #
  #   begin
  #     q = PrettyPrint.new(output, maxwidth, newline, &genspace)
  #     ...
  #     q.flush
  #     output
  #   end
  #
  def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
    q = PrettyPrint.new(output, maxwidth, newline, &genspace)
    yield q
    q.flush
    output
  end

  # This 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 is
  # treated as just an invocation of +text+.
  #
  def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
    q = SingleLine.new(output)
    yield q
    output
  end

  # Creates a buffer for pretty printing.
  #
  # +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, the first argument +sep+ of PrettyPrint#breakable, the
  # first argument +newline+ of PrettyPrint.new, and the result of a given
  # block for PrettyPrint.new.
  #
  # +maxwidth+ specifies maximum line length. If it is not specified, 79 is
  # assumed. However actual outputs may overflow +maxwidth+ if long
  # non-breakable texts are provided.
  #
  # +newline+ is used for line breaks. "\n" is used if it is not specified.
  #
  # The block is used to generate spaces. {|width| ' ' * width} is used if it
  # is not given.
  #
  def initialize(output='', maxwidth=79, newline="\n", &genspace)
    @output = output
    @maxwidth = maxwidth
    @newline = newline
    @genspace = genspace || lambda {|n| ' ' * n}

    @output_width = 0
    @buffer_width = 0
    @buffer = []

    root_group = Group.new(0)
    @group_stack = [root_group]
    @group_queue = GroupQueue.new(root_group)
    @indent = 0
  end
  attr_reader :output, :maxwidth, :newline, :genspace
  attr_reader :indent, :group_queue

  # Returns the group most recently added to the stack.
  def current_group
    @group_stack.last
  end

  # first? is a predicate to test the call is a first call to first? with
  # current group.
  #
  # It is useful to format comma separated values as:
  #
  #   q.group(1, '[', ']') {
  #     xxx.each {|yyy|
  #       unless q.first?
  #         q.text ','
  #         q.breakable
  #       end
  #       ... pretty printing yyy ...
  #     }
  #   }
  #
  # first? is obsoleted in 1.8.2.
  #
  def first?
    warn "PrettyPrint#first? is obsoleted at 1.8.2."
    current_group.first?
  end

  # Breaks the buffer into lines that are shorter than #maxwidth
  def break_outmost_groups
    while @maxwidth < @output_width + @buffer_width
      return unless group = @group_queue.deq
      until group.breakables.empty?
        data = @buffer.shift
        @output_width = data.output(@output, @output_width)
        @buffer_width -= data.width
      end
      while !@buffer.empty? && Text === @buffer.first
        text = @buffer.shift
        @output_width = text.output(@output, @output_width)
        @buffer_width -= text.width
      end
    end
  end

  # This adds +obj+ as a text of +width+ columns in width.
  #
  # If +width+ is not specified, obj.length is used.
  #
  def text(obj, width=obj.length)
    if @buffer.empty?
      @output << obj
      @output_width += width
    else
      text = @buffer.last
      unless Text === text
        text = Text.new
        @buffer << text
      end
      text.add(obj, width)
      @buffer_width += width
      break_outmost_groups
    end
  end

  # This is similar to #breakable except
  # the decision to break or not is determined individually.
  #
  # Two #fill_breakable under a group may cause 4 results:
  # (break,break), (break,non-break), (non-break,break), (non-break,non-break).
  # This is different to #breakable because two #breakable under a group
  # may cause 2 results:
  # (break,break), (non-break,non-break).
  #
  # The text sep+ is inserted if a line is not broken at this point.
  #
  # If +sep+ is not specified, " " is used.
  #
  # If +width+ is not specified, +sep.length+ is used. You will have to
  # specify this when +sep+ is a multibyte character, for example.
  #
  def fill_breakable(sep=' ', width=sep.length)
    group { breakable sep, width }
  end

  # This says "you can break a line here if necessary", and a +width+\-column
  # text +sep+ is inserted if a line is not broken at the point.
  #
  # If +sep+ is not specified, " " is used.
  #
  # If +width+ is not specified, +sep.length+ is used. You will have to
  # specify this when +sep+ is a multibyte character, for example.
  #
  def breakable(sep=' ', width=sep.length)
    group = @group_stack.last
    if group.break?
      flush
      @output << @newline
      @output << @genspace.call(@indent)
      @output_width = @indent
      @buffer_width = 0
    else
      @buffer << Breakable.new(sep, width, self)
      @buffer_width += width
      break_outmost_groups
    end
  end

  # Groups line break hints added in the block. The line break hints are all
  # to be used or not.
  #
  # If +indent+ is specified, the method call is regarded as nested by
  # nest(indent) { ... }.
  #
  # If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
  # before grouping. If +close_obj+ is specified, <tt>text close_obj,
  # close_width</tt> is called after grouping.
  #
  def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
    text open_obj, open_width
    group_sub {
      nest(indent) {
        yield
      }
    }
    text close_obj, close_width
  end

  def group_sub
    group = Group.new(@group_stack.last.depth + 1)
    @group_stack.push group
    @group_queue.enq group
    begin
      yield
    ensure
      @group_stack.pop
      if group.breakables.empty?
        @group_queue.delete group
      end
    end
  end

  # Increases left margin after newline with +indent+ for line breaks added in
  # the block.
  #
  def nest(indent)
    @indent += indent
    begin
      yield
    ensure
      @indent -= indent
    end
  end

  # outputs buffered data.
  #
  def flush
    @buffer.each {|data|
      @output_width = data.output(@output, @output_width)
    }
    @buffer.clear
    @buffer_width = 0
  end

  class Text
    def initialize
      @objs = []
      @width = 0
    end
    attr_reader :width

    def output(out, output_width)
      @objs.each {|obj| out << obj}
      output_width + @width
    end

    def add(obj, width)
      @objs << obj
      @width += width
    end
  end

  class Breakable
    def initialize(sep, width, q)
      @obj = sep
      @width = width
      @pp = q
      @indent = q.indent
      @group = q.current_group
      @group.breakables.push self
    end
    attr_reader :obj, :width, :indent

    def output(out, output_width)
      @group.breakables.shift
      if @group.break?
        out << @pp.newline
        out << @pp.genspace.call(@indent)
        @indent
      else
        @pp.group_queue.delete @group if @group.breakables.empty?
        out << @obj
        output_width + @width
      end
    end
  end

  class Group
    def initialize(depth)
      @depth = depth
      @breakables = []
      @break = false
    end
    attr_reader :depth, :breakables

    def break
      @break = true
    end

    def break?
      @break
    end

    def first?
      if defined? @first
        false
      else
        @first = false
        true
      end
    end
  end

  class GroupQueue
    def initialize(*groups)
      @queue = []
      groups.each {|g| enq g}
    end

    def enq(group)
      depth = group.depth
      @queue << [] until depth < @queue.length
      @queue[depth] << group
    end

    def deq
      @queue.each {|gs|
        (gs.length-1).downto(0) {|i|
          unless gs[i].breakables.empty?
            group = gs.slice!(i, 1).first
            group.break
            return group
          end
        }
        gs.each {|group| group.break}
        gs.clear
      }
      return nil
    end

    def delete(group)
      @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

Directory Contents

Dirs: 30 × Files: 78

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
date DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
digest DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
dl DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
drb DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
fiddle DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
irb DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
json DIR
- drwxr-xr-x 2024-03-03 22:48:09
Edit Download
matrix DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
net DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
openssl DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
optparse DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
psych DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
racc DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
rake DIR
- drwxr-xr-x 2024-03-03 22:50:37
Edit Download
rbconfig DIR
- drwxr-xr-x 2024-03-03 22:48:12
Edit Download
rdoc DIR
- drwxr-xr-x 2024-03-03 22:48:11
Edit Download
rexml DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
rinda DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
ripper DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
rss DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
rubygems DIR
- drwxr-xr-x 2024-03-03 22:48:12
Edit Download
shell DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
syck DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
test DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
uri DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
webrick DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
- drwxr-xr-x 2024-03-03 22:48:09
Edit Download
xmlrpc DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
yaml DIR
- drwxr-xr-x 2024-03-03 22:48:05
Edit Download
2.57 KB lrw-r--r-- 2011-05-18 21:19:18
Edit Download
2.63 KB lrw-r--r-- 2009-10-02 10:45:39
Edit Download
18.00 KB lrw-r--r-- 2011-06-28 06:09:46
Edit Download
9.30 KB lrw-r--r-- 2011-05-16 21:21:35
Edit Download
7.22 KB lrw-r--r-- 2011-07-30 14:53:14
Edit Download
380 B lrw-r--r-- 2009-08-16 15:34:35
Edit Download
82.66 KB lrw-r--r-- 2011-07-21 21:40:21
Edit Download
946 B lrw-r--r-- 2011-05-21 12:25:03
Edit Download
23.23 KB lrw-r--r-- 2011-06-28 11:45:50
Edit Download
9.74 KB lrw-r--r-- 2011-07-27 19:56:28
Edit Download
2.24 KB lrw-r--r-- 2011-05-16 20:52:55
Edit Download
176 B lrw-r--r-- 2010-06-12 15:28:47
Edit Download
19 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
3.80 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
5.59 KB lrw-r--r-- 2009-10-02 10:45:39
Edit Download
25.72 KB lrw-r--r-- 2012-12-13 05:24:26
Edit Download
1.33 KB lrw-r--r-- 2011-08-26 23:54:49
Edit Download
928 B lrw-r--r-- 2011-08-16 00:51:58
Edit Download
45.32 KB lrw-r--r-- 2012-02-25 08:11:07
Edit Download
2.03 KB lrw-r--r-- 2011-05-18 21:19:18
Edit Download
7.64 KB lrw-r--r-- 2011-05-22 02:14:57
Edit Download
15.38 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
8.83 KB lrw-r--r-- 2013-10-31 14:40:55
Edit Download
24.92 KB lrw-r--r-- 2013-04-11 02:31:05
Edit Download
8.34 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
1.74 KB lrw-r--r-- 2011-08-30 02:25:32
Edit Download
5.74 KB lrw-r--r-- 2010-12-07 11:47:39
Edit Download
20.85 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
6.52 KB lrw-r--r-- 2011-08-26 23:54:49
Edit Download
47.65 KB lrw-r--r-- 2011-07-09 19:11:06
Edit Download
68.90 KB lrw-r--r-- 2014-02-06 05:20:52
Edit Download
6.94 KB lrw-r--r-- 2011-06-17 22:33:54
Edit Download
1.61 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
5.69 KB lrw-r--r-- 2011-05-16 21:53:12
Edit Download
25.84 KB lrw-r--r-- 2014-02-24 03:43:27
Edit Download
20.64 KB lrw-r--r-- 2011-05-18 21:19:18
Edit Download
547 B lrw-r--r-- 2011-07-25 06:38:44
Edit Download
51.13 KB lrw-r--r-- 2014-02-14 04:52:03
Edit Download
6.49 KB lrw-r--r-- 2012-02-24 14:49:51
Edit Download
14.21 KB lrw-r--r-- 2011-08-26 23:54:49
Edit Download
13.31 KB lrw-r--r-- 2010-08-01 14:09:40
Edit Download
9.63 KB lrw-r--r-- 2011-08-02 15:25:59
Edit Download
13.98 KB lrw-r--r-- 2011-06-01 00:21:46
Edit Download
205 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
1.59 KB lrw-r--r-- 2009-10-02 10:45:39
Edit Download
15.81 KB lrw-r--r-- 2011-10-10 23:14:17
Edit Download
9.82 KB lrw-r--r-- 2012-08-07 20:50:05
Edit Download
2.02 KB lrw-r--r-- 2011-06-29 03:09:34
Edit Download
308 B lrw-r--r-- 2009-09-24 00:42:23
Edit Download
4.29 KB lrw-r--r-- 2013-02-06 08:00:49
Edit Download
1.74 KB lrw-r--r-- 2010-10-29 22:02:39
Edit Download
59.91 KB lrw-r--r-- 2014-02-14 06:56:54
Edit Download
91 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
2.84 KB lrw-r--r-- 2011-05-11 10:22:16
Edit Download
34.13 KB lrw-r--r-- 2013-12-22 04:38:28
Edit Download
23.53 KB lrw-r--r-- 2011-05-20 12:22:26
Edit Download
8.46 KB lrw-r--r-- 2012-05-07 11:25:45
Edit Download
29.91 KB lrw-r--r-- 2012-02-10 08:28:48
Edit Download
5.90 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
3.88 KB lrw-r--r-- 2012-02-12 07:51:05
Edit Download
4.02 KB lrw-r--r-- 2011-05-18 14:09:38
Edit Download
23.22 KB lrw-r--r-- 2014-01-30 12:04:22
Edit Download
13.91 KB lrw-r--r-- 2010-07-19 04:54:06
Edit Download
6.87 KB lrw-r--r-- 2011-06-29 03:09:34
Edit Download
10.42 KB lrw-r--r-- 2013-10-31 14:44:40
Edit Download
6.59 KB lrw-r--r-- 2014-02-14 03:48:42
Edit Download
3.38 KB lrw-r--r-- 2011-06-29 03:09:34
Edit Download
17.03 KB lrw-r--r-- 2013-10-31 14:06:50
Edit Download
3.26 KB lrw-r--r-- 2013-04-02 04:09:07
Edit Download
3.72 KB lrw-r--r-- 2011-05-18 21:19:18
Edit Download
6.63 KB lrw-r--r-- 2011-06-29 13:19:59
Edit Download
6.79 KB lrw-r--r-- 2009-03-06 04:23:05
Edit Download
268 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
8.32 KB lrw-r--r-- 2011-05-19 00:07:25
Edit Download
3.07 KB lrw-r--r-- 2011-05-13 20:03:21
Edit Download
2.29 KB lrw-r--r-- 2011-08-26 23:54:49
Edit Download
6.80 KB lrw-r--r-- 2011-05-10 23:37:43
Edit Download
2.58 KB lrw-r--r-- 2013-05-29 12:00:57
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).