module Exodus::TextFormatter

Public Instance Methods

super_print(paragraphes, space_number = 50, title = true) click to toggle source

Prints a paragraphes

# File lib/exodus/helpers/text_formatter.rb, line 5
def super_print(paragraphes, space_number = 50, title = true)
  puts format_paragraph(space_number, title, *paragraphes)
end

Private Instance Methods

format_paragraph(space_number, title, *paragraphes) click to toggle source

Transforms an array of paragraphes to a String using lines and columns Each paragraph is actually an Array of string, where each string is a sentence of a given column if the sentence contains to much caractere the sentence will be splitted (using whitespaces) and written on several lines e,g considering paragraphes = [[“id”, “type”][“id_1”, “test”]] format_paragraph will print: id type id_1 test

# File lib/exodus/helpers/text_formatter.rb, line 18
def format_paragraph(space_number, title, *paragraphes)
  column_size = paragraphes.max_by{|paragraph| paragraph.size}.size
  @full_text = Hash[*column_size.times.map {|i| [i,[]]}.flatten(1)]

  paragraphes.each_with_index do |sentences, paragraph_number|
    sentences.each_with_index do |sentence, column|
      sentence = sentence.to_s
      words = sentence.gsub('=>', ' => ').split(' ') || ''
      
      if sentence.size > space_number && (words).size > 1
        new_sentence = ""
        words.each_with_index do |word, nb_word| 
          if new_sentence.size + word.size  < space_number
            new_sentence << word << ' '
          else
            insert_line(column, new_sentence) unless new_sentence.empty?
            new_sentence = word << ' '
          end
        end

        insert_line(column, new_sentence) unless new_sentence == @full_text[column].last
      else 
        insert_line(column, sentence) 
      end
    end

    @full_text.each {|column, lines| (@max_lines - lines.size).times { lines << '' }}
    space = paragraph_number == 0 && title ? "/nbspace" : ""
    @full_text.each {|column, lines| lines << space}
  end

  stringify_paragraph
end
insert_line(column, line) click to toggle source

Inserts a line at the correcponding column and re-sets the number of maximum line if the maximum has been increased

# File lib/exodus/helpers/text_formatter.rb, line 72
def insert_line(column, line)
  @max_lines ||= 0
  @full_text[column] << line
  @max_lines = @full_text[column].size if @max_lines < @full_text[column].size
end
stringify_paragraph() click to toggle source

Creates a String from a Hash of the following format {column_number => [lines]} “/nbspace” is used to define a border

# File lib/exodus/helpers/text_formatter.rb, line 54
def stringify_paragraph
  ordered_lines = {}

  spaces = @full_text.map {|column, lines| lines.max_by {|sentence| sentence.size}.size}
  @full_text.each_with_index do |(column, lines), i| 
    lines.each_with_index do |line, line_number|
      if line == "/nbspace"
        (ordered_lines[line_number] ||= "") << line.gsub("/nbspace", "-" * (spaces[i] + 4))
      else
        (ordered_lines[line_number] ||= "") << line.to_s.ljust(spaces[i] + 4)
      end
    end
  end

  ordered_lines.values.join("\n")
end