Sha256: 39bdbaa145152219802eb5558c8b14adc34e8321d93907b5c15fce64b52a5f11

Contents?: true

Size: 1.45 KB

Versions: 69

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8

# String
class String
  # Justify pipe using table format
  #
  # before justify
  #
  #   |* first name|* family name|
  #   |eiichiro|oda|
  #   |akira|toriyama|
  #   |yusei|matsui|
  #
  # after justify
  #
  #   |* first name|* family name|
  #   |eiichiro    |oda          |
  #   |akira       |toriyama     |
  #   |yusei       |matsui       |
  def justify_table
    return self if self.empty?
    max_sizes = get_column_maxes
    return self if max_sizes.nil?
    justify_lines max_sizes
  end

  private

  def get_column_maxes
    max_sizes = []
    each_line do |line|
      return nil unless table? line
      columns = get_columuns(line)
      max_sizes = get_column_max(columns, max_sizes)
    end
    max_sizes
  end

  def justify_lines(max_sizes)
    ret = []
    each_line do |line|
      columns = get_columuns(line)
      line_ret = []
      columns.each_with_index do |column, cnt|
        line_ret << column.ljust(max_sizes[cnt])
      end
      ret << "|#{line_ret.join('|')}|"
    end
    ret.join("\n") + "\n"
  end

  def get_columuns(line)
    line.split('|')[1..-2]
  end

  def get_column_max(columns, max_sizes)
    columns.each_with_index do |column, index|
      current_size = column.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

  def table?(text)
    text.count('|') > 0
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.75 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.74 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.73 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.72 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.71 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.70 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.69 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.68 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.67 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.66 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.65 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.64 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.63 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.62 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.61 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.60 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.59 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.58 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.57 lib/open_classes/string/justify_table.rb
tbpgr_utils-0.0.56 lib/open_classes/string/justify_table.rb