Module: Columnist

Includes:
OptionsValidator
Defined in:
lib/columnist.rb,
lib/columnist/row.rb,
lib/columnist/table.rb,
lib/columnist/column.rb,
lib/columnist/formatter/nested.rb,
lib/columnist/formatter/progress.rb

Defined Under Namespace

Classes: Column, NestedFormatter, ProgressFormatter, Row, Table

Constant Summary collapse

DEFAULTS =
{
    :width     => 100,
    :align     => 'left',
    :formatter => 'nested',
    :encoding  => :unicode,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsValidator

#validate_options

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



13
14
15
# File 'lib/columnist.rb', line 13

def formatter
  @formatter
end

Instance Method Details

#aligned(text, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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_outputObject



22
23
24
25
26
27
# File 'lib/columnist.rb', line 22

def capture_output
    $stdout.rewind
    $stdout.read
ensure
    $stdout = STDOUT
end

#column(text, options = {}) ⇒ Object



134
135
136
137
# File 'lib/columnist.rb', line 134

def column(text, options = {})
    col = Columnist::Column.new(text, options)
    @row.add(col)
end

#datetime(options = {}) ⇒ Object

Raises:

  • (Exception)


82
83
84
85
86
87
88
89
90
91
92
93
94
# 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


60
61
62
# File 'lib/columnist.rb', line 60

def footer(options = {})
    section(:footer, options)
end

#header(options = {}) ⇒ Object



47
48
49
# File 'lib/columnist.rb', line 47

def header(options = {})
    section(:header, options)
end

#horizontal_rule(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



56
57
58
# File 'lib/columnist.rb', line 56

def progress(override = nil)
    self.formatter.progress(override)
end

#report(options = {}, &block) ⇒ Object



51
52
53
54
# File 'lib/columnist.rb', line 51

def report(options = {}, &block)
    self.formatter ||= DEFAULTS[:formatter]
    self.formatter.format(options, block)
end

#restore_outputObject



33
34
35
# File 'lib/columnist.rb', line 33

def restore_output
    $stdout = STDOUT
end

#row(options = {}) ⇒ Object



127
128
129
130
131
132
# File 'lib/columnist.rb', line 127

def row(options = {})
    options[:encoding] ||= @table.encoding
    @row               = Columnist::Row.new(options)
    yield
    @table.add(@row)
end

#suppress_outputObject



29
30
31
# File 'lib/columnist.rb', line 29

def suppress_output
    $stdout = StringIO.new
end

#table(options = {}) ⇒ Object



121
122
123
124
125
# File 'lib/columnist.rb', line 121

def table(options = {})
    @table = Columnist::Table.new(options)
    yield
    @table.output
end

#vertical_spacing(lines = 1) ⇒ Object



76
77
78
79
80
# File 'lib/columnist.rb', line 76

def vertical_spacing(lines = 1)
    puts "\n" * lines
rescue
    raise ArgumentError
end