Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/cli-table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ Table

should add checks so taht every row is of the same size



7
8
9
# File 'lib/cli-table.rb', line 7

def initialize header 
  @header = header
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/cli-table.rb', line 3

def data
  @data
end

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/cli-table.rb', line 3

def header
  @header
end

Instance Method Details

#printTableObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
67
68
69
70
71
# File 'lib/cli-table.rb', line 11

def printTable
  # i should probably check for things 
  #if @data.empty?
  #  raise "Where is the data?"§
  #end
  sdata = [@header.clone]
  sdata += self.datato_s
  # does everything have to be turned into a string?
  # i think so 

  # get longest item in a column
  rpad = []
  nrows = sdata[0].length
  # the issue was, i was going for how many rows, should of gone by rows
  # this is really confussing
  # idk why it really works 
  0.upto(nrows -1).each do |i|
    #p sdata[i]
    rpad << sdata.max{|a, b| a[i].length <=> b[i].length}[i].length() +1
  end
 
  floor = '─'
  wall = '│' 
  tsign  = {:ut => '┴', :dt => '┬', :rt => '├', :lt => '┤', :cross => '┼'}
  corner = {:tr => '┌', :tl => '┐', :br => '┘', :bl => '└'}

  top = corner[:tr]
  mid = tsign[:rt]
  bot = corner[:bl]

  head = "#{wall}"
  (0...sdata[0].length).each do |i|
    temp = " #{sdata[0][i].ljust(rpad[i])}#{wall}"
    top += temp.split("").map{|s| floor}.join("")
    top[-1] = tsign[:dt]

    mid += temp.split("").map{|s| floor}.join("")
    mid[-1] = tsign[:cross]

    bot += temp.split("").map{|s| floor}.join("")
    bot[-1] = tsign[:ut]
    head << temp
  end
  top[-1] = corner[:tl]
  mid[-1] = tsign[:lt]
  bot[-1] = corner[:br]

  puts top 
  puts head
  puts mid
  # we can start printing table 
  sdata[1..-1].each do |l|
    (0...l.length()).each do |i|
      print("#{wall} #{l[i].ljust(rpad[i])}")
    end
    print "#{wall}"
    puts ""
  end

  puts bot 
end