设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1494|回复: 2
打印 上一主题 下一主题

[已经解决] 请问如何让控制台自动排版

[复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
130 小时
注册时间
2013-6-30
帖子
92
跳转到指定楼层
1
发表于 2015-6-15 16:16:40 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
就拿原版脚本举个例子。在地图中,通过事件让电脑执行p $game_player这条命令,会显示出$game_player的详细内容。但这些内容完全是杂糅在一起的,很杂很乱。
可不可以通过改变脚本来实现“让这些自动排版”的效果?比如每个逗号后自动提一行之类的。

Lv1.梦旅人

梦石
0
星屑
50
在线时间
130 小时
注册时间
2013-6-30
帖子
92
3
 楼主| 发表于 2015-6-15 21:02:58 | 只看该作者
taroxd 发表于 2015-6-15 17:24
参考Ruby标准库的pp(pretty print)。没记错的话是纯Ruby实现,应该可以直接拿来用 ...

我倒是找到prettyprint.rb了,但对其脚本中的方法没什么头绪。希望前辈能再指点指点
  1. # prettyprint.rb
  2. # This class implements a pretty printing algorithm. It finds line breaks and
  3. # nice indentations for grouped structure.
  4. #
  5. # By default, the class assumes that primitive elements are strings and each
  6. # byte in the strings have single column in width. But it can be used for
  7. # other situations by giving suitable arguments for some methods:
  8. # * newline object and space generation block for PrettyPrint.new
  9. # * optional width argument for PrettyPrint#text
  10. # * PrettyPrint#breakable
  11. #
  12. # There are several candidate uses:
  13. # * text formatting using proportional fonts
  14. # * multibyte characters which has columns different to number of bytes
  15. # * non-string formatting
  16. #
  17. # == Bugs
  18. # * Box based formatting?
  19. # * Other (better) model/algorithm?
  20. #
  21. # Report any bugs at http://bugs.ruby-lang.org
  22. #
  23. # == References
  24. # Christian Lindig, Strictly Pretty, March 2000,
  25. # http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
  26. #
  27. # Philip Wadler, A prettier printer, March 1998,
  28. # http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
  29. #
  30. # == Author
  31. # Tanaka Akira <[email protected]>
  32. #
  33. class PrettyPrint
  34.   # This is a convenience method which is same as follows:
  35.   #
  36.   #   begin
  37.   #     q = PrettyPrint.new(output, maxwidth, newline, &genspace)
  38.   #     ...
  39.   #     q.flush
  40.   #     output
  41.   #   end
  42.   #
  43.   def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
  44.     q = PrettyPrint.new(output, maxwidth, newline, &genspace)
  45.     yield q
  46.     q.flush
  47.     output
  48.   end
  49.   # This is similar to PrettyPrint::format but the result has no breaks.
  50.   #
  51.   # +maxwidth+, +newline+ and +genspace+ are ignored.
  52.   #
  53.   # The invocation of +breakable+ in the block doesn't break a line and is
  54.   # treated as just an invocation of +text+.
  55.   #
  56.   def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
  57.     q = SingleLine.new(output)
  58.     yield q
  59.     output
  60.   end
  61.   # Creates a buffer for pretty printing.
  62.   #
  63.   # +output+ is an output target. If it is not specified, '' is assumed. It
  64.   # should have a << method which accepts the first argument +obj+ of
  65.   # PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
  66.   # first argument +newline+ of PrettyPrint.new, and the result of a given
  67.   # block for PrettyPrint.new.
  68.   #
  69.   # +maxwidth+ specifies maximum line length. If it is not specified, 79 is
  70.   # assumed. However actual outputs may overflow +maxwidth+ if long
  71.   # non-breakable texts are provided.
  72.   #
  73.   # +newline+ is used for line breaks. "\n" is used if it is not specified.
  74.   #
  75.   # The block is used to generate spaces. {|width| ' ' * width} is used if it
  76.   # is not given.
  77.   #
  78.   def initialize(output='', maxwidth=79, newline="\n", &genspace)
  79.     @output = output
  80.     @maxwidth = maxwidth
  81.     @newline = newline
  82.     @genspace = genspace || lambda {|n| ' ' * n}
  83.     @output_width = 0
  84.     @buffer_width = 0
  85.     @buffer = []
  86.     root_group = Group.new(0)
  87.     @group_stack = [root_group]
  88.     @group_queue = GroupQueue.new(root_group)
  89.     @indent = 0
  90.   end
  91.   # The output object.
  92.   #
  93.   # This defaults to '', and should accept the << method
  94.   attr_reader :output
  95.   # The maximum width of a line, before it is separated in to a newline
  96.   #
  97.   # This defaults to 79, and should be a Fixnum
  98.   attr_reader :maxwidth
  99.   # The value that is appended to +output+ to add a new line.
  100.   #
  101.   # This defaults to "\n", and should be String
  102.   attr_reader :newline
  103.   # A lambda or Proc, that takes one argument, of a Fixnum, and returns
  104.   # the corresponding number of spaces.
  105.   #
  106.   # By default this is:
  107.   #   lambda {|n| ' ' * n}
  108.   attr_reader :genspace
  109.   # The number of spaces to be indented
  110.   attr_reader :indent
  111.   # The PrettyPrint::GroupQueue of groups in stack to be pretty printed
  112.   attr_reader :group_queue
  113.   # Returns the group most recently added to the stack.
  114.   #
  115.   # Contrived example:
  116.   #   out = ""
  117.   #   => ""
  118.   #   q = PrettyPrint.new(out)
  119.   #   => #<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>
  120.   #   q.group {
  121.   #     q.text q.current_group.inspect
  122.   #     q.text q.newline
  123.   #     q.group(q.current_group.depth + 1) {
  124.   #       q.text q.current_group.inspect
  125.   #       q.text q.newline
  126.   #       q.group(q.current_group.depth + 1) {
  127.   #         q.text q.current_group.inspect
  128.   #         q.text q.newline
  129.   #         q.group(q.current_group.depth + 1) {
  130.   #           q.text q.current_group.inspect
  131.   #           q.text q.newline
  132.   #         }
  133.   #       }
  134.   #     }
  135.   #   }
  136.   #   => 284
  137.   #    puts out
  138.   #   #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
  139.   #   #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
  140.   #   #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
  141.   #   #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
  142.   def current_group
  143.     @group_stack.last
  144.   end
  145.   # first? is a predicate to test the call is a first call to first? with
  146.   # current group.
  147.   #
  148.   # It is useful to format comma separated values as:
  149.   #
  150.   #   q.group(1, '[', ']') {
  151.   #     xxx.each {|yyy|
  152.   #       unless q.first?
  153.   #         q.text ','
  154.   #         q.breakable
  155.   #       end
  156.   #       ... pretty printing yyy ...
  157.   #     }
  158.   #   }
  159.   #
  160.   # first? is obsoleted in 1.8.2.
  161.   #
  162.   def first?
  163.     warn "PrettyPrint#first? is obsoleted at 1.8.2."
  164.     current_group.first?
  165.   end
  166.   # Breaks the buffer into lines that are shorter than #maxwidth
  167.   def break_outmost_groups
  168.     while @maxwidth < @output_width + @buffer_width
  169.       return unless group = @group_queue.deq
  170.       until group.breakables.empty?
  171.         data = @buffer.shift
  172.         @output_width = data.output(@output, @output_width)
  173.         @buffer_width -= data.width
  174.       end
  175.       while [email protected]? && Text === @buffer.first
  176.         text = @buffer.shift
  177.         @output_width = text.output(@output, @output_width)
  178.         @buffer_width -= text.width
  179.       end
  180.     end
  181.   end
  182.   # This adds +obj+ as a text of +width+ columns in width.
  183.   #
  184.   # If +width+ is not specified, obj.length is used.
  185.   #
  186.   def text(obj, width=obj.length)
  187.     if @buffer.empty?
  188.       @output << obj
  189.       @output_width += width
  190.     else
  191.       text = @buffer.last
  192.       unless Text === text
  193.         text = Text.new
  194.         @buffer << text
  195.       end
  196.       text.add(obj, width)
  197.       @buffer_width += width
  198.       break_outmost_groups
  199.     end
  200.   end
  201.   # This is similar to #breakable except
  202.   # the decision to break or not is determined individually.
  203.   #
  204.   # Two #fill_breakable under a group may cause 4 results:
  205.   # (break,break), (break,non-break), (non-break,break), (non-break,non-break).
  206.   # This is different to #breakable because two #breakable under a group
  207.   # may cause 2 results:
  208.   # (break,break), (non-break,non-break).
  209.   #
  210.   # The text +sep+ is inserted if a line is not broken at this point.
  211.   #
  212.   # If +sep+ is not specified, " " is used.
  213.   #
  214.   # If +width+ is not specified, +sep.length+ is used. You will have to
  215.   # specify this when +sep+ is a multibyte character, for example.
  216.   #
  217.   def fill_breakable(sep=' ', width=sep.length)
  218.     group { breakable sep, width }
  219.   end
  220.   # This says "you can break a line here if necessary", and a +width+\-column
  221.   # text +sep+ is inserted if a line is not broken at the point.
  222.   #
  223.   # If +sep+ is not specified, " " is used.
  224.   #
  225.   # If +width+ is not specified, +sep.length+ is used. You will have to
  226.   # specify this when +sep+ is a multibyte character, for example.
  227.   #
  228.   def breakable(sep=' ', width=sep.length)
  229.     group = @group_stack.last
  230.     if group.break?
  231.       flush
  232.       @output << @newline
  233.       @output << @genspace.call(@indent)
  234.       @output_width = @indent
  235.       @buffer_width = 0
  236.     else
  237.       @buffer << Breakable.new(sep, width, self)
  238.       @buffer_width += width
  239.       break_outmost_groups
  240.     end
  241.   end
  242.   # Groups line break hints added in the block. The line break hints are all
  243.   # to be used or not.
  244.   #
  245.   # If +indent+ is specified, the method call is regarded as nested by
  246.   # nest(indent) { ... }.
  247.   #
  248.   # If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
  249.   # before grouping. If +close_obj+ is specified, <tt>text close_obj,
  250.   # close_width</tt> is called after grouping.
  251.   #
  252.   def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
  253.     text open_obj, open_width
  254.     group_sub {
  255.       nest(indent) {
  256.         yield
  257.       }
  258.     }
  259.     text close_obj, close_width
  260.   end
  261.   # Takes a block and queues a new group that is indented 1 level further.
  262.   def group_sub
  263.     group = Group.new(@group_stack.last.depth + 1)
  264.     @group_stack.push group
  265.     @group_queue.enq group
  266.     begin
  267.       yield
  268.     ensure
  269.       @group_stack.pop
  270.       if group.breakables.empty?
  271.         @group_queue.delete group
  272.       end
  273.     end
  274.   end
  275.   # Increases left margin after newline with +indent+ for line breaks added in
  276.   # the block.
  277.   #
  278.   def nest(indent)
  279.     @indent += indent
  280.     begin
  281.       yield
  282.     ensure
  283.       @indent -= indent
  284.     end
  285.   end
  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.   # The Text class is the means by which to collect strings from objects.
  296.   #
  297.   # This class is intended for internal use of the PrettyPrint buffers.
  298.   class Text # :nodoc:
  299.     # Creates a new text object.
  300.     #
  301.     # This constructor takes no arguments.
  302.     #
  303.     # The workflow is to append a PrettyPrint::Text object to the buffer, and
  304.     # being able to call the buffer.last() to reference it.
  305.     #
  306.     # As there are objects, use PrettyPrint::Text#add to include the objects
  307.     # and the width to utilized by the String version of this object.
  308.     def initialize
  309.       @objs = []
  310.       @width = 0
  311.     end
  312.     # The total width of the objects included in this Text object.
  313.     attr_reader :width
  314.     # Render the String text of the objects that have been added to this Text object.
  315.     #
  316.     # Output the text to +out+, and increment the width to +output_width+
  317.     def output(out, output_width)
  318.       @objs.each {|obj| out << obj}
  319.       output_width + @width
  320.     end
  321.     # Include +obj+ in the objects to be pretty printed, and increment
  322.     # this Text object's total width by +width+
  323.     def add(obj, width)
  324.       @objs << obj
  325.       @width += width
  326.     end
  327.   end
  328.   # The Breakable class is used for breaking up object information
  329.   #
  330.   # This class is intended for internal use of the PrettyPrint buffers.
  331.   class Breakable # :nodoc:
  332.     # Create a new Breakable object.
  333.     #
  334.     # Arguments:
  335.     # * +sep+ String of the separator
  336.     # * +width+ Fixnum width of the +sep+
  337.     # * +q+ parent PrettyPrint object, to base from
  338.     def initialize(sep, width, q)
  339.       @obj = sep
  340.       @width = width
  341.       @pp = q
  342.       @indent = q.indent
  343.       @group = q.current_group
  344.       @group.breakables.push self
  345.     end
  346.     # Holds the separator String
  347.     #
  348.     # The +sep+ argument from ::new
  349.     attr_reader :obj
  350.     # The width of +obj+ / +sep+
  351.     attr_reader :width
  352.     # The number of spaces to indent.
  353.     #
  354.     # This is inferred from +q+ within PrettyPrint, passed in ::new
  355.     attr_reader :indent
  356.     # Render the String text of the objects that have been added to this
  357.     # Breakable object.
  358.     #
  359.     # Output the text to +out+, and increment the width to +output_width+
  360.     def output(out, output_width)
  361.       @group.breakables.shift
  362.       if @group.break?
  363.         out << @pp.newline
  364.         out << @pp.genspace.call(@indent)
  365.         @indent
  366.       else
  367.         @pp.group_queue.delete @group if @group.breakables.empty?
  368.         out << @obj
  369.         output_width + @width
  370.       end
  371.     end
  372.   end
  373.   # The Group class is used for making indentation easier.
  374.   #
  375.   # While this class does neither the breaking into newlines nor indentation,
  376.   # it is used in a stack (as well as a queue) within PrettyPrint, to group
  377.   # objects.
  378.   #
  379.   # For information on using groups, see PrettyPrint#group
  380.   #
  381.   # This class is intended for internal use of the PrettyPrint buffers.
  382.   class Group # :nodoc:
  383.     # Create a Group object
  384.     #
  385.     # Arguments:
  386.     # * +depth+ - this group's relation to previous groups
  387.     def initialize(depth)
  388.       @depth = depth
  389.       @breakables = []
  390.       @break = false
  391.     end
  392.     # This group's relation to previous groups
  393.     attr_reader :depth
  394.     # Array to hold the Breakable objects for this Group
  395.     attr_reader :breakables
  396.     # Makes a break for this Group, and returns true
  397.     def break
  398.       @break = true
  399.     end
  400.     # Boolean of whether this Group has made a break
  401.     def break?
  402.       @break
  403.     end
  404.     # Boolean of whether this Group has been queried for being first
  405.     #
  406.     # This is used as a predicate, and ought to be called first.
  407.     def first?
  408.       if defined? @first
  409.         false
  410.       else
  411.         @first = false
  412.         true
  413.       end
  414.     end
  415.   end
  416.   # The GroupQueue class is used for managing the queue of Group to be pretty
  417.   # printed.
  418.   #
  419.   # This queue groups the Group objects, based on their depth.
  420.   #
  421.   # This class is intended for internal use of the PrettyPrint buffers.
  422.   class GroupQueue # :nodoc:
  423.     # Create a GroupQueue object
  424.     #
  425.     # Arguments:
  426.     # * +groups+ - one or more PrettyPrint::Group objects
  427.     def initialize(*groups)
  428.       @queue = []
  429.       groups.each {|g| enq g}
  430.     end
  431.     # Enqueue +group+
  432.     #
  433.     # This does not strictly append the group to the end of the queue,
  434.     # but instead adds it in line, base on the +group.depth+
  435.     def enq(group)
  436.       depth = group.depth
  437.       @queue << [] until depth < @queue.length
  438.       @queue[depth] << group
  439.     end
  440.     # Returns the outer group of the queue
  441.     def deq
  442.       @queue.each {|gs|
  443.         (gs.length-1).downto(0) {|i|
  444.           unless gs[i].breakables.empty?
  445.             group = gs.slice!(i, 1).first
  446.             group.break
  447.             return group
  448.           end
  449.         }
  450.         gs.each {|group| group.break}
  451.         gs.clear
  452.       }
  453.       return nil
  454.     end
  455.     # Remote +group+ from this queue
  456.     def delete(group)
  457.       @queue[group.depth].delete(group)
  458.     end
  459.   end
  460.   # PrettyPrint::SingleLine is used by PrettyPrint.singleline_format
  461.   #
  462.   # It is passed to be similar to a PrettyPrint object itself, by responding to:
  463.   # * #text
  464.   # * #breakable
  465.   # * #nest
  466.   # * #group
  467.   # * #flush
  468.   # * #first?
  469.   #
  470.   # but instead, the output has no line breaks
  471.   #
  472.   class SingleLine
  473.     # Create a PrettyPrint::SingleLine object
  474.     #
  475.     # Arguments:
  476.     # * +output+ - String (or similar) to store rendered text. Needs to respond to '<<'
  477.     # * +maxwidth+ - Argument position expected to be here for compatibility.
  478.     #                This argument is a noop.
  479.     # * +newline+ - Argument position expected to be here for compatibility.
  480.     #               This argument is a noop.
  481.     def initialize(output, maxwidth=nil, newline=nil)
  482.       @output = output
  483.       @first = [true]
  484.     end
  485.     # Add +obj+ to the text to be output.
  486.     #
  487.     # +width+ argument is here for compatibility. It is a noop argument.
  488.     def text(obj, width=nil)
  489.       @output << obj
  490.     end
  491.     # Appends +sep+ to the text to be output. By default +sep+ is ' '
  492.     #
  493.     # +width+ argument is here for compatibility. It is a noop argument.
  494.     def breakable(sep=' ', width=nil)
  495.       @output << sep
  496.     end
  497.     # Takes +indent+ arg, but does nothing with it.
  498.     #
  499.     # Yields to a block.
  500.     def nest(indent) # :nodoc:
  501.       yield
  502.     end
  503.     # Opens a block for grouping objects to be pretty printed.
  504.     #
  505.     # Arguments:
  506.     # * +indent+ - noop argument. Present for compatibility.
  507.     # * +open_obj+ - text appended before the &blok. Default is ''
  508.     # * +close_obj+ - text appended after the &blok. Default is ''
  509.     # * +open_width+ - noop argument. Present for compatibility.
  510.     # * +close_width+ - noop argument. Present for compatibility.
  511.     def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
  512.       @first.push true
  513.       @output << open_obj
  514.       yield
  515.       @output << close_obj
  516.       @first.pop
  517.     end
  518.     # Method present for compatibility, but is a noop
  519.     def flush # :nodoc:
  520.     end
  521.     # This is used as a predicate, and ought to be called first.
  522.     def first?
  523.       result = @first[-1]
  524.       @first[-1] = false
  525.       result
  526.     end
  527.   end
  528. end
复制代码

点评

噢,是的,看到了。能用了。非常感谢  发表于 2015-6-16 10:17
pp.rb 应该也在一起的  发表于 2015-6-16 07:16
还有一个 pp。用 pp 代替 p 就可以了  发表于 2015-6-16 07:14
回复 支持 反对

使用道具 举报

Lv3.寻梦者 (版主)

…あたしは天使なんかじゃないわ

梦石
0
星屑
2208
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

2
发表于 2015-6-15 17:24:47 | 只看该作者
参考Ruby标准库的pp(pretty print)。没记错的话是纯Ruby实现,应该可以直接拿来用

评分

参与人数 1星屑 +150 收起 理由
VIPArcher + 150 标准库的屁屁(つд⊂)~

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-11-16 08:55

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表