Sha256: 65fc5737b1f66bb18f643ddf174ef74b7cbf715abad0f417a03756ce1269a775

Contents?: true

Size: 1.91 KB

Versions: 30

Compression:

Stored size: 1.91 KB

Contents

# encoding: utf-8
require 'open_classes/string/ascii1_other2_size'

# String
class String
  # Justify string using separator
  #
  # before justify
  #
  #   print 'hoge' # => 'hoge'
  #   print 'hoge' * 2 # => 'hogehoge'
  #   print 'hoge' + 'hige' # => 'hogehige'
  #
  # after justify
  #
  #   print 'hoge'          # => 'hoge'
  #   print 'hoge' * 2      # => 'hogehoge'
  #   print 'hoge' + 'hige' # => 'hogehige'
  #
  def justify_char(separator = '|', position = :left)
    return self if empty?
    return self unless include? separator
    max_sizes = get_column_maxes(separator)
    return self if max_sizes.nil?
    justify_lines max_sizes, position, separator
  end

  private

  def get_column_maxes(separator)
    max_sizes = []
    each_line do |line|
      columns = get_columuns(line, separator)
      max_sizes = get_column_max(columns, max_sizes)
    end
    max_sizes
  end

  def justify_lines(max_sizes, position, separator)
    ret = []
    each_line do |line|
      columns = get_columuns(line, separator)
      line_ret = []
      columns.each_with_index do |column, cnt|
        diff = column.ascii1_other2_size - column.size
        line_ret << justified_column(column, max_sizes[cnt], diff, position)
      end
      ret << line_ret.join(separator).gsub(/ +$/m, '')
    end
    ret.join
  end

  def justified_column(column, max_size, diff, position)
    pos = max_size - diff
    case position
    when :left
      column.ljust(pos)
    when :right
      column.rjust(pos)
    when :center
      column.center(pos)
    end
  end

  def get_columuns(line, separator)
    line.split(separator)
  end

  def get_column_max(columns, max_sizes)
    columns.each_with_index do |column, index|
      current_size = column.ascii1_other2_size
      if max_sizes[index].nil?
        max_sizes << current_size
        next
      end
      max_sizes[index] = current_size if current_size > max_sizes[index]
    end
    max_sizes
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.150 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.149 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.148 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.147 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.146 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.145 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.144 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.143 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.142 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.141 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.140 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.139 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.138 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.137 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.136 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.135 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.134 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.133 lib/open_classes/string/justify_char.rb
tbpgr_utils-0.0.132 lib/open_classes/string/justify_char.rb