Class: Columnist::Row

Inherits:
Object
  • Object
show all
Includes:
OptionsValidator
Defined in:
lib/columnist/row.rb

Constant Summary collapse

VALID_OPTIONS =
[:header, :color, :border_color, :bold, :encoding]

Instance Method Summary collapse

Methods included from OptionsValidator

#validate_options

Constructor Details

#initialize(options = {}) ⇒ Row

Returns a new instance of Row.



8
9
10
11
12
13
14
15
16
17
# File 'lib/columnist/row.rb', line 8

def initialize(options = {})
    self.validate_options(options, *VALID_OPTIONS)
    self.columns      = []
    self.border       = false
    self.header       = options[:header] || false
    self.color        = options[:color]
    self.border_color = options[:border_color]
    self.bold         = options[:bold] || false
    self.encoding     = options[:encoding] || :unicode
end

Instance Method Details

#add(column) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/columnist/row.rb', line 19

def add(column)
    if column.color.nil? && self.color
        column.color = self.color
    end

    if self.bold || self.header
        column.bold = true
    end

    self.columns << column
end

#outputObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/columnist/row.rb', line 31

def output
    screen_count.times do |sr|
        border_char = use_utf8? ? "\u2503" : '|'
        border_char = colorize(border_char, self.border_color)
        line        = (self.border) ? "#{border_char} " : ''
        self.columns.size.times do |mc|
            col = self.columns[mc]
            # Account for the fact that some columns will have more screen rows than their
            # counterparts in the row.  An example being:
            # c1 = Column.new('x' * 50, :width => 10)
            # c2 = Column.new('x' * 20, :width => 10)
            #
            # c1.screen_rows.size == 5
            # c2.screen_rows.size == 2
            #
            # So when we don't have a screen row for c2 we need to fill the screen with the
            # proper number of blanks so the layout looks like (parenthesis on the right just
            # indicate screen row index)
            #
            # +-------------+------------+
            # | xxxxxxxxxxx | xxxxxxxxxx | (0)
            # | xxxxxxxxxxx | xxxxxxxxxx | (1)
            # | xxxxxxxxxxx |            | (2)
            # | xxxxxxxxxxx |            | (3)
            # | xxxxxxxxxxx |            | (4)
            # +-------------+------------+
            if col.screen_rows[sr].nil?
                line << ' ' * col.width
            else
                line << self.columns[mc].screen_rows[sr]
            end
            line << ' ' + ((self.border) ? "#{border_char} " : '')
        end
        puts line
    end
end