class Rex::Ui::Text::Table
Prints text in a tablized format. Pretty lame at the moment, but whatever.
Public Class Methods
new(opts = {})
click to toggle source
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.
Sortindex
The column to sort the table on, -1 disables sorting.
# File lib/rex/ui/text/table.rb, line 61 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
Public Instance Methods
<<(fields)
click to toggle source
Adds a row using the supplied fields.
# File lib/rex/ui/text/table.rb, line 164 def <<(fields) add_row(fields) end
[](*col_names)
click to toggle source
Returns new sub-table with headers and rows maching column names submitted
# File lib/rex/ui/text/table.rb, line 217 def [](*col_names) tbl = self.class.new('Indent' => self.indent, 'Header' => self.header, 'Columns' => col_names) indexes = [] col_names.each do |col_name| index = self.columns.index(col_name) raise RuntimeError, "Invalid column name #{col_name}" if index.nil? indexes << index end self.rows.each do |old_row| new_row = [] indexes.map {|i| new_row << old_row[i]} tbl << new_row end return tbl end
add_hr()
click to toggle source
Adds a horizontal line.
# File lib/rex/ui/text/table.rb, line 210 def add_hr rows << '__hr__' end
add_row(fields = [])
click to toggle source
Adds a row with the supplied fields.
# File lib/rex/ui/text/table.rb, line 171 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
print()
click to toggle source
Prints the contents of the table.
# File lib/rex/ui/text/table.rb, line 157 def print puts to_s end
Also aliased as: p
sort_rows(index=sort_index)
click to toggle source
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.
# File lib/rex/ui/text/table.rb, line 189 def sort_rows(index=sort_index) return if index == -1 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()
click to toggle source
Converts table contents to a csv
# File lib/rex/ui/text/table.rb, line 126 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()
click to toggle source
Converts table contents to a string.
# File lib/rex/ui/text/table.rb, line 103 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