File: common/pretty-print.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Umu#4
  module: PrettyPrint#6
has properties
function: __parse_opt_params__ / 1 #24
function: group / 3 #59
function: group_for_enum / 4 #79

Code

   1  # coding: utf-8
   2  # frozen_string_literal: true
   3 
   4  module Umu
   5 
   6  module PrettyPrint
   7 
   8  =begin
   9  Option Parameter
  10  * bb:   Begining symbol of grouping (Begin Braket)
  11  * eb:   Stopping symbol of grouping (End Braket)
  12  * sep:  Separator symbol between all elements
  13  * join: Join symbol between repeated elements
  14 
  15  How to Print
  16  * N==0: <bb> <sep> <eb>
  17  * N==1: <bb> <sep> elem <sep> <eb>
  18  * N>=2: <bb> <sep> elem { <join> <sep> elem } <sep> <eb>
  19  * where N is number of elements
  20  =end
  21 
  22  module_function
  23 
  24      def __parse_opt_params__(opts)
  25          ASSERT.kind_of opts, ::Hash
  26 
  27          bb   = nil
  28          eb   = nil
  29          sep  = nil
  30          join = nil
  31 
  32          opts.each do |key, val|
  33              case key
  34              when :bb
  35                  ASSERT.kind_of val, ::String
  36 
  37                  bb = val
  38              when :eb
  39                  ASSERT.kind_of val, ::String
  40 
  41                  eb = val
  42              when :sep
  43                  ASSERT.kind_of val, ::String
  44 
  45                  sep = val
  46              when :join
  47                  ASSERT.kind_of val, ::String
  48 
  49                  join = val
  50              else
  51                  ASSERT.abort format("Unknown option: %s", key.to_s)
  52              end
  53          end
  54 
  55          [bb, eb, sep, sep ? sep : '', join]
  56      end
  57 
  58 
  59      def group(q, opts = {}, &_block)
  60          ASSERT.kind_of q,      ::PrettyPrint
  61          ASSERT.kind_of opts,   ::Hash
  62 
  63          bb, eb, _sep, sep_str, _join = __parse_opt_params__ opts
  64 
  65          q.text bb if bb
  66 
  67          q.group(PP_INDENT_WIDTH) do
  68              q.breakable sep_str
  69 
  70              yield
  71          end
  72 
  73          q.breakable sep_str
  74 
  75          q.text eb if eb
  76      end
  77 
  78 
  79      def group_for_enum(q, elems, opts = {}, &_block)
  80          ASSERT.kind_of q,      ::PrettyPrint
  81          ASSERT.assert elems.respond_to? :each
  82          ASSERT.kind_of opts,   ::Hash
  83 
  84          bb, eb, sep, sep_str, join = __parse_opt_params__ opts
  85 
  86          q.text bb if bb
  87 
  88          if elems.count == 0
  89              q.text sep_str if sep
  90              q.text eb if eb
  91          else
  92              q.group(PP_INDENT_WIDTH) do
  93                  q.breakable sep_str
  94 
  95                  if block_given?
  96                      yield elems.first
  97                  else
  98                      q.pp elems.first
  99                  end
 100 
 101                  elems.drop(1).each do |elem|
 102                      q.text join if join
 103 
 104                      q.breakable sep_str
 105 
 106                      if block_given?
 107                          yield elem
 108                      else
 109                          q.pp elem
 110                      end
 111                  end
 112              end
 113 
 114              if eb
 115                  q.breakable sep_str
 116 
 117                  q.text eb
 118              end
 119          end
 120      end
 121  end # Umu::PrettyPrint
 122 
 123  end # Umu