module Columnist

Constants

DEFAULTS

Attributes

formatter[R]

Public Instance Methods

aligned(text, options = {}) click to toggle source
# File lib/columnist.rb, line 96
def aligned(text, options = {})
    validate_options(options, :align, :width, :color, :bold)

    align = options[:align] || DEFAULTS[:align]
    width = options[:width] || DEFAULTS[:width]
    color = options[:color]
    bold  = options[:bold] || false

    line = case align
               when 'left'
                   text
               when 'right'
                   text.rjust(width)
               when 'center'
                   text.rjust((width - text.size)/2 + text.size)
               else
                   raise ArgumentError
           end

    line = line.send(color) if color
    line = line.send('bold') if bold

    puts line
end
capture_output() click to toggle source
# File lib/columnist.rb, line 22
def capture_output
    $stdout.rewind
    $stdout.read
ensure
    $stdout = STDOUT
end
column(text, options = {}) click to toggle source
# File lib/columnist.rb, line 134
def column(text, options = {})
    col = Columnist::Column.new(text, options)
    @row.add(col)
end
datetime(options = {}) click to toggle source
# File lib/columnist.rb, line 82
def datetime(options = {})
    validate_options(options, :align, :width, :format, :color, :bold)

    format = options[:format] || '%Y-%m-%d - %l:%M:%S%p'
    align  = options[:align] || DEFAULTS[:align]
    width  = options[:width] || DEFAULTS[:width]

    text = Time.now.strftime(format)

    raise Exception if text.size > width

    aligned(text, :align => align, :width => width, :color => options[:color], :bold => options[:bold])
end
formatter=(type = 'nested') click to toggle source
# File lib/columnist.rb, line 37
def formatter=(type = 'nested')
    name  = type.capitalize + 'Formatter'
    klass = %W{Columnist #{name}}.inject(Kernel) { |s, c| s.const_get(c) }

    # Each formatter is a singleton that responds to #instance
    @formatter = klass.instance
rescue
    raise ArgumentError, 'Invalid formatter specified'
end
header(options = {}) click to toggle source
# File lib/columnist.rb, line 47
def header(options = {})
    section(:header, options)
end
horizontal_rule(options = {}) click to toggle source
# File lib/columnist.rb, line 64
def horizontal_rule(options = {})
    validate_options(options, :char, :width, :color, :bold)

    # Got unicode?
    use_char = "\u2501" == 'u2501' ? '-' : "\u2501"

    char  = options[:char].is_a?(String) ? options[:char] : use_char
    width = options[:width] || DEFAULTS[:width]

    aligned(char * width, :width => width, :color => options[:color], :bold => options[:bold])
end
progress(override = nil) click to toggle source
# File lib/columnist.rb, line 56
def progress(override = nil)
    self.formatter.progress(override)
end
report(options = {}, &block) click to toggle source
# File lib/columnist.rb, line 51
def report(options = {}, &block)
    self.formatter ||= DEFAULTS[:formatter]
    self.formatter.format(options, block)
end
restore_output() click to toggle source
# File lib/columnist.rb, line 33
def restore_output
    $stdout = STDOUT
end
row(options = {}) { || ... } click to toggle source
# File lib/columnist.rb, line 127
def row(options = {})
    options[:encoding] ||= @table.encoding
    @row               = Columnist::Row.new(options)
    yield
    @table.add(@row)
end
suppress_output() click to toggle source
# File lib/columnist.rb, line 29
def suppress_output
    $stdout = StringIO.new
end
table(options = {}) { || ... } click to toggle source
# File lib/columnist.rb, line 121
def table(options = {})
    @table = Columnist::Table.new(options)
    yield
    @table.output
end
vertical_spacing(lines = 1) click to toggle source
# File lib/columnist.rb, line 76
def vertical_spacing(lines = 1)
    puts "\n" * lines
rescue
    raise ArgumentError
end

Private Instance Methods

assign_section_properties(options) click to toggle source
# File lib/columnist.rb, line 161
def assign_section_properties options
    validate_options(options, :title, :width, :align, :spacing, :timestamp, :rule, :color, :bold)

    title = options[:title] || 'Report'
    width = options[:width] || DEFAULTS[:width]
    align = options[:align] || DEFAULTS[:align]
    lines = options[:spacing] || 1
    color = options[:color]
    bold  = options[:bold] || false

    return [title, width, align, lines, color, bold]
end
section(type, options) click to toggle source
# File lib/columnist.rb, line 141
def section(type, options)
    title, width, align, lines, color, bold = assign_section_properties(options)

    # This also ensures that width is a Fixnum
    raise ArgumentError if title.size > width

    if type == :footer
        vertical_spacing(lines)
        horizontal_rule(:char => options[:rule], :width => width, :color => color, :bold => bold) if options[:rule]
    end

    aligned(title, :align => align, :width => width, :color => color, :bold => bold)
    datetime(:align => align, :width => width, :color => color, :bold => bold) if options[:timestamp]

    if type == :header
        horizontal_rule(:char => options[:rule], :width => width, :color => color, :bold => bold) if options[:rule]
        vertical_spacing(lines)
    end
end