Class: Rex::Ui::Text::Table
- Inherits:
-
Object
- Object
- Rex::Ui::Text::Table
- Defined in:
- lib/rex/ui/text/table.rb
Overview
Prints text in a tablized format. Pretty lame at the moment, but whatever.
Defined Under Namespace
Classes: UnitTest
Instance Attribute Summary collapse
-
#cellpad ⇒ Object
:nodoc:.
-
#colprops ⇒ Object
:nodoc:.
-
#columns ⇒ Object
:nodoc:.
-
#header ⇒ Object
:nodoc:.
-
#headeri ⇒ Object
:nodoc:.
-
#indent ⇒ Object
:nodoc:.
-
#postfix ⇒ Object
:nodoc:.
-
#prefix ⇒ Object
:nodoc:.
-
#rows ⇒ Object
:nodoc:.
-
#sort_index ⇒ Object
:nodoc:.
-
#width ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#<<(fields) ⇒ Object
Adds a row using the supplied fields.
-
#add_hr ⇒ Object
Adds a horizontal line.
-
#add_row(fields = []) ⇒ Object
Adds a row with the supplied fields.
-
#header_to_s ⇒ Object
Returns the header string.
-
#initialize(opts = {}) ⇒ Table
constructor
Initializes a text table instance using the supplied properties.
-
#print ⇒ Object
(also: #p)
Prints the contents of the table.
-
#sort_rows(index = sort_index) ⇒ Object
Sorts the rows based on the supplied index of sub-arrays If the supplied index is an IPv4 address, handle it differently, but avoid actually resolving domain names.
-
#to_csv ⇒ Object
Converts table contents to a csv.
-
#to_s ⇒ Object
Converts table contents to a string.
Constructor Details
#initialize(opts = {}) ⇒ Table
Initializes a text table instance using the supplied properties. The Table class supports the following hash attributes:
Header
The string to display as a heading above the table. If none is specified, no header will be displayed.
HeaderIndent
The amount of space to indent the header. The default is zero.
Columns
The array of columns that will exist within the table.
Rows
The array of rows that will exist.
Width
The maximum width of the table in characters.
Indent
The number of characters to indent the table.
CellPad
The number of characters to put between each horizontal cell.
Prefix
The text to prefix before the table.
Postfix
The text to affix to the end of the table.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rex/ui/text/table.rb', line 58 def initialize(opts = {}) self.header = opts['Header'] self.headeri = opts['HeaderIndent'] || 0 self.columns = opts['Columns'] || [] # updated below if we got a "Rows" option self.rows = [] self.width = opts['Width'] || 80 self.indent = opts['Indent'] || 0 self.cellpad = opts['CellPad'] || 2 self.prefix = opts['Prefix'] || '' self.postfix = opts['Postfix'] || '' self.colprops = [] self.sort_index = opts['SortIndex'] || 0 # Default column properties self.columns.length.times { |idx| self.colprops[idx] = {} self.colprops[idx]['MaxWidth'] = self.columns[idx].length } # ensure all our internal state gets updated with the given rows by # using add_row instead of just adding them to self.rows. See #3825. opts['Rows'].each { |row| add_row(row) } if opts['Rows'] # Merge in options if (opts['ColProps']) opts['ColProps'].each_key { |col| idx = self.columns.index(col) if (idx) self.colprops[idx].merge!(opts['ColProps'][col]) end } end end |
Instance Attribute Details
#cellpad ⇒ Object
:nodoc:
214 215 216 |
# File 'lib/rex/ui/text/table.rb', line 214 def cellpad @cellpad end |
#colprops ⇒ Object
:nodoc:
213 214 215 |
# File 'lib/rex/ui/text/table.rb', line 213 def colprops @colprops end |
#columns ⇒ Object
:nodoc:
213 214 215 |
# File 'lib/rex/ui/text/table.rb', line 213 def columns @columns end |
#header ⇒ Object
:nodoc:
212 213 214 |
# File 'lib/rex/ui/text/table.rb', line 212 def header @header end |
#headeri ⇒ Object
:nodoc:
212 213 214 |
# File 'lib/rex/ui/text/table.rb', line 212 def headeri @headeri end |
#indent ⇒ Object
:nodoc:
214 215 216 |
# File 'lib/rex/ui/text/table.rb', line 214 def indent @indent end |
#postfix ⇒ Object
:nodoc:
215 216 217 |
# File 'lib/rex/ui/text/table.rb', line 215 def postfix @postfix end |
#prefix ⇒ Object
:nodoc:
215 216 217 |
# File 'lib/rex/ui/text/table.rb', line 215 def prefix @prefix end |
#rows ⇒ Object
:nodoc:
213 214 215 |
# File 'lib/rex/ui/text/table.rb', line 213 def rows @rows end |
#sort_index ⇒ Object
:nodoc:
216 217 218 |
# File 'lib/rex/ui/text/table.rb', line 216 def sort_index @sort_index end |
#width ⇒ Object
:nodoc:
214 215 216 |
# File 'lib/rex/ui/text/table.rb', line 214 def width @width end |
Instance Method Details
#<<(fields) ⇒ Object
Adds a row using the supplied fields.
161 162 163 |
# File 'lib/rex/ui/text/table.rb', line 161 def <<(fields) add_row(fields) end |
#add_hr ⇒ Object
Adds a horizontal line.
206 207 208 |
# File 'lib/rex/ui/text/table.rb', line 206 def add_hr rows << '__hr__' end |
#add_row(fields = []) ⇒ Object
Adds a row with the supplied fields.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rex/ui/text/table.rb', line 168 def add_row(fields = []) if fields.length != self.columns.length raise RuntimeError, 'Invalid number of columns!' end fields.each_with_index { |field, idx| if (colprops[idx]['MaxWidth'] < field.to_s.length) colprops[idx]['MaxWidth'] = field.to_s.length end } rows << fields end |
#header_to_s ⇒ Object
Returns the header string.
141 142 143 144 145 146 147 148 149 |
# File 'lib/rex/ui/text/table.rb', line 141 def header_to_s # :nodoc: if (header) pad = " " * headeri return pad + header + "\n" + pad + "=" * header.length + "\n\n" end return '' end |
#print ⇒ Object Also known as: p
Prints the contents of the table.
154 155 156 |
# File 'lib/rex/ui/text/table.rb', line 154 def print puts to_s end |
#sort_rows(index = sort_index) ⇒ Object
Sorts the rows based on the supplied index of sub-arrays If the supplied index is an IPv4 address, handle it differently, but avoid actually resolving domain names.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rex/ui/text/table.rb', line 186 def sort_rows(index=sort_index) return unless rows rows.sort! do |a,b| if a[index].nil? -1 elsif b[index].nil? 1 elsif Rex::Socket.dotted_ip?(a[index]) and Rex::Socket.dotted_ip?(b[index]) Rex::Socket::addr_atoi(a[index]) <=> Rex::Socket::addr_atoi(b[index]) elsif a[index] =~ /^[0-9]+$/ and b[index] =~ /^[0-9]+$/ a[index].to_i <=> b[index].to_i else a[index] <=> b[index] # assumes otherwise comparable. end end end |
#to_csv ⇒ Object
Converts table contents to a csv
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rex/ui/text/table.rb', line 123 def to_csv str = '' str << ( columns.join(",") + "\n" ) rows.each { |row| next if is_hr(row) str << ( row.map{|x| x = x.to_s x.gsub(/[\r\n]/, ' ').gsub(/\s+/, ' ').gsub('"', '""') }.map{|x| "\"#{x}\"" }.join(",") + "\n" ) } str end |
#to_s ⇒ Object
Converts table contents to a string.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rex/ui/text/table.rb', line 100 def to_s str = prefix.dup str << header_to_s || '' str << columns_to_s || '' str << hr_to_s || '' sort_rows rows.each { |row| if (is_hr(row)) str << hr_to_s else str << row_to_s(row) end } str << postfix return str end |