Class: Table
- Inherits:
-
Object
- Object
- Table
- Defined in:
- lib/cli-table.rb
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
-
#initialize(header) ⇒ Table
constructor
A new instance of Table.
- #is_correct? ⇒ Boolean
- #show ⇒ Object
Constructor Details
#initialize(header) ⇒ Table
Returns a new instance of Table.
5 6 7 8 |
# File 'lib/cli-table.rb', line 5 def initialize header @header = header @rows = [] end |
Instance Attribute Details
#header ⇒ Object
Returns the value of attribute header.
3 4 5 |
# File 'lib/cli-table.rb', line 3 def header @header end |
#rows ⇒ Object
Returns the value of attribute rows.
3 4 5 |
# File 'lib/cli-table.rb', line 3 def rows @rows end |
Instance Method Details
#is_correct? ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cli-table.rb', line 72 def is_correct? if @rows == [] return true end t = @rows.map {|l| l.length}.uniq if t.length != 1 return false end if t[0] != @header.length return false end return true end |
#show ⇒ Object
10 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 |
# File 'lib/cli-table.rb', line 10 def show # wish i could explain what line of code its wrong if not is_correct then return 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 |