Class: Terminal::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal/table.rb,
lib/terminal/table/version.rb

Constant Summary collapse

VERSION =
'0.0.10'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, options = {}) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/terminal/table.rb', line 77

def initialize(object = nil, options = {})
  @rows = []
  @headings = []
  @column_widths = []

  if options[:use_new_line_symbol]
    @new_line_symbol = '⏎'
  else
    @new_line_symbol = ' '
  end

  if object
    if object.is_a?(Hash)
      add_hash(object, options)
    elsif object.respond_to?(:each)
      object.each { |o| add_object(o, options) }
    else
      add_object(object, options)
    end
  end

  yield self if block_given?
  recalculate_column_widths!
end

Instance Attribute Details

#column_widthsObject

Returns the value of attribute column_widths.



74
75
76
# File 'lib/terminal/table.rb', line 74

def column_widths
  @column_widths
end

#headingsObject

Returns the value of attribute headings.



73
74
75
# File 'lib/terminal/table.rb', line 73

def headings
  @headings
end

#new_line_symbolObject

Returns the value of attribute new_line_symbol.



75
76
77
# File 'lib/terminal/table.rb', line 75

def new_line_symbol
  @new_line_symbol
end

#rowsObject

Returns the value of attribute rows.



72
73
74
# File 'lib/terminal/table.rb', line 72

def rows
  @rows
end

Class Method Details

.special_tokensObject



168
169
170
# File 'lib/terminal/table.rb', line 168

def special_tokens
  String::CHARS_OF_WIDTH_OF_1 + String::CHARS_OF_WIDTH_OF_0
end

Instance Method Details

#add_hash(hash, options) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/terminal/table.rb', line 110

def add_hash(hash, options)
  if options[:only]
    hash.keep_if { |k, v| options[:only].map(&:to_sym).include?(k) }
  elsif options[:except]
    hash.delete_if { |k, v| options[:except].map(&:to_sym).include?(k) }
  end

  @headings = hash.keys.map(&:to_s)
  @rows << hash.values
end

#add_object(object, options) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/terminal/table.rb', line 102

def add_object(object, options)
  if object.respond_to?(:to_hash)
    add_hash(object.to_hash, options)
  elsif object.respond_to?(:each)
    @rows << object
  end
end

#recalculate_column_widths!Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/terminal/table.rb', line 125

def recalculate_column_widths!
  @rows = rows.map { |row| row.map { |item| item.to_s.gsub("\r\n", @new_line_symbol).gsub("\n", @new_line_symbol).gsub("\r", @new_line_symbol) } }

  if @rows.count > 0
    (0...@rows.first.size).each do |col|
      @column_widths[col] = @rows.map { |row| row[col].to_s.twidth }.max
    end
  end

  if @headings.count > 0
    (0...@headings.size).each do |col|
      @column_widths[col] = [@column_widths[col] || 0, @headings[col].twidth].max
    end
  end
end

#to_sObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/terminal/table.rb', line 141

def to_s
  recalculate_column_widths!

  result = ''

  header_and_footer = '+' + @column_widths.map { |w| '-' * (w + 2) }.join('+') + '+' + "\n"

  if @headings.count > 0
    result += header_and_footer

    content = @headings.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }

    result += '| ' + content.join(' | ') + " |\n"
  end

  result += header_and_footer

  @rows.each do |row|
    content = row.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }

    result += '| ' + content.join(' | ') + " |\n"
  end

  result + header_and_footer
end