Class: TermUtils::Tab::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/tab.rb

Overview

Represents a table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Table

Returns a new instance of Table.

Options Hash (opts):

  • :id (Symbol)


11
12
13
14
# File 'lib/term_utils/tab.rb', line 11

def initialize(opts = {})
  @id = opts.fetch(:id, nil)
  @columns = []
end

Instance Attribute Details

#columnsArray<Tab::Column>



8
9
10
# File 'lib/term_utils/tab.rb', line 8

def columns
  @columns
end

#idSymbol



6
7
8
# File 'lib/term_utils/tab.rb', line 6

def id
  @id
end

Instance Method Details

#define_column(id, opts = {}, &block) ⇒ Tab::Column

Defines a column.

Options Hash (opts):

  • :width (Integer)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/term_utils/tab.rb', line 20

def define_column(id, opts = {}, &block)
  col = @columns.find { |c| c.id == id }
  if col
    block.call(col) if block
    col.validate
  else
    opts[:id] = id
    opts[:index] = @columns.length
    col = Column.new(opts)
    block.call(col) if block
    col.validate
    @columns << col
  end
  col
end

#find_column(id) ⇒ Tab::Column?

Finds a column.



38
39
40
# File 'lib/term_utils/tab.rb', line 38

def find_column(id)
  @columns.find { |c| c.id == id }
end

Prints a data row.

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/term_utils/tab.rb', line 88

def print_data(io, values, opts = {})
  vals = values
  if values.is_a? Hash
    vals = []
    @columns.each do |col|
      vals << values[col.id]
    end
  end
  raise "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << col.render_data(vals[col.index])
  end
  io.puts sb.string
end

Prints a header row.

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/term_utils/tab.rb', line 60

def print_header(io, values = nil, opts = {})
  vals = values
  if values.nil?
    vals = @columns.map { |col| col.id.to_s }
  elsif values.is_a? Hash
    vals = []
    @columns.each do |col|
      vals << values[col.id]
    end
  end
  raise "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << col.render_header(vals[col.index])
  end
  io.puts sb.string
end

Prints a separator row.

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/term_utils/tab.rb', line 113

def print_separator(io, opts = {})
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << "-" * col.width
  end
  io.puts sb.string
end

#printer(io, opts = {}, &block) ⇒ Tab::Printer

Creates a new table printer.

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)


48
49
50
51
52
# File 'lib/term_utils/tab.rb', line 48

def printer(io, opts = {}, &block)
  ptr = Printer.new(self, io, opts)
  block.call(ptr) if block
  ptr
end

#titlesHash<Symbol, String>

Returns column titles.



126
127
128
129
130
131
132
# File 'lib/term_utils/tab.rb', line 126

def titles
  h = {}
  @columns.each do |col|
    h[col.id] = col.id.to_s
  end
  h
end