Parent

Files

Class/Module Index [+]

Quicksearch

Formatador

Public Class Methods

new() click to toggle source
# File lib/formatador.rb, line 59
def initialize
  @indent = 1
end

Public Instance Methods

display(string = '') click to toggle source
# File lib/formatador.rb, line 63
def display(string = '')
  print(parse("[indent]#{string}"))
  STDOUT.flush
  nil
end
display_compact_table(hashes, keys = nil, &block) click to toggle source
# File lib/formatador/table.rb, line 10
def display_compact_table(hashes, keys = nil, &block)
  headers = keys || []
  widths = {}
  if hashes.empty? && keys
    for key in keys
      widths[key] = key.to_s.length
    end
  else
    for hash in hashes
      next unless hash.respond_to?(:keys)

      for key in hash.keys
        unless keys
          headers << key
        end
        widths[key] = [ length(key), widths[key] || 0, hash[key] && length(hash[key]) || 0].max
      end
      headers = headers.uniq
    end
  end

  if block_given?
    headers = headers.sort(&block)
  elsif !keys
    headers = headers.sort {|x,y| x.to_s <=> y.to_s}
  end

  split = "+"
  if headers.empty?
    split << '--+'
  else
    for header in headers
      widths[header] ||= length(header)
      split << ('-' * (widths[header] + 2)) << '+'
    end
  end

  display_line(split)
  columns = []
  for header in headers
    columns << "[bold]#{header}[/]#{' ' * (widths[header] - header.to_s.length)}"
  end
  display_line("| #{columns.join(' | ')} |")
  display_line(split)

  for hash in hashes
    if hash.respond_to? :keys
      columns = []
      for header in headers
        datum = hash[header] || ''
        columns << "#{datum}#{' ' * (widths[header] - length(datum))}"
      end
      display_line("| #{columns.join(' | ')} |")
    else
      if hash == :split
        display_line(split)
      end 
    end
    nil
  end
  display_line(split)
end
display_line(string = '') click to toggle source
# File lib/formatador.rb, line 69
def display_line(string = '')
  display(string)
  new_line
  nil
end
display_lines(lines = []) click to toggle source
# File lib/formatador.rb, line 75
def display_lines(lines = [])
  for line in [*lines]
    display_line(line)
  end
  nil
end
display_table(hashes, keys = nil, &block) click to toggle source
# File lib/formatador/table.rb, line 2
def display_table(hashes, keys = nil, &block)
  new_hashes = hashes.inject([]) do |accum,item|
    accum << :split unless accum.empty?
    accum << item
  end
  display_compact_table(new_hashes, keys, &block)
end
indent(&block) click to toggle source
# File lib/formatador.rb, line 90
def indent(&block)
  @indent += 1
  yield
  @indent -= 1
end
indentation() click to toggle source
# File lib/formatador.rb, line 96
def indentation
  '  ' * @indent
end
new_line() click to toggle source
# File lib/formatador.rb, line 112
def new_line
  print("\n")
  nil
end
parse(string) click to toggle source
# File lib/formatador.rb, line 82
def parse(string)
  if STDOUT.tty?
    string.gsub(PARSE_REGEX) { "\e[#{STYLES[$1.to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
  else
    strip(string)
  end
end
redisplay(string = '', width = 120) click to toggle source
# File lib/formatador.rb, line 100
def redisplay(string = '', width = 120)
  print("\r#{' ' * width}\r")
  display("#{string}")
  nil
end
redisplay_line(string = '', width = 120) click to toggle source
# File lib/formatador.rb, line 106
def redisplay_line(string = '', width = 120)
  redisplay(string, width)
  new_line
  nil
end
redisplay_progressbar(current, total, options = {}) click to toggle source
# File lib/formatador/progressbar.rb, line 3
def redisplay_progressbar(current, total, options = {})
  options = { :color => 'white', :width => 50, :new_line => true }.merge!(options)
  data = progressbar(current, total, options)
  if current < total
    redisplay(data)
  else
    redisplay("#{data}")
    if options[:new_line]
      new_line
    end
    @progressbar_started_at = nil
  end
end
strip(string) click to toggle source
# File lib/formatador.rb, line 117
def strip(string)
  string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation }
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.