Class: Rex::Text::WrappedTable
- Inherits:
-
Object
- Object
- Rex::Text::WrappedTable
- Defined in:
- lib/rex/text/wrapped_table.rb
Overview
Prints text in a tablized format. Pretty lame at the moment, but whatever.
private_constant
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:.
-
#scterm ⇒ Object
:nodoc:.
-
#sort_index ⇒ Object
:nodoc:.
-
#sort_order ⇒ Object
:nodoc:.
-
#width ⇒ Object
:nodoc:.
-
#word_wrap ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.new_from_csv(csv) ⇒ Object
Build table from CSV dump.
Instance Method Summary collapse
-
#<<(fields) ⇒ Object
Adds a row using the supplied fields.
- #[](*col_names) ⇒ Object
-
#add_hr ⇒ Object
Adds a horizontal line.
-
#add_row(fields = []) ⇒ Object
Adds a row with the supplied fields.
-
#drop_left ⇒ Object
Returns new sub-table with headers and rows maching column names submitted.
-
#header_to_s ⇒ Object
Returns the header string.
-
#initialize(opts = {}) ⇒ WrappedTable
constructor
Initializes a text table instance using the supplied properties.
- #ip_cmp(a, b) ⇒ Object
-
#print ⇒ Object
(also: #p)
Prints the contents of the table.
-
#sort_rows(index = sort_index, order = sort_order) ⇒ 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.
- #valid_ip?(value) ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ WrappedTable
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.
ColProps
A hash specifying column Width, Stylers, and Formatters.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rex/text/wrapped_table.rb', line 67 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'] || ::IO.console&.winsize&.[](1) self.width = ::BigDecimal::INFINITY if !self.width || self.width == 0 self.word_wrap = opts.fetch('WordWrap', true) self.indent = opts['Indent'] || 0 self.cellpad = opts['CellPad'] || 2 self.prefix = opts['Prefix'] || '' self.postfix = opts['Postfix'] || '' self.colprops = [] self.scterm = /#{opts['SearchTerm']}/mi if opts['SearchTerm'] self.sort_index = opts['SortIndex'] || 0 self.sort_order = opts['SortOrder'] || :forward # Default column properties self.columns.length.times { |idx| self.colprops[idx] = {} self.colprops[idx]['Width'] = nil self.colprops[idx]['WordWrap'] = true self.colprops[idx]['Strip'] = true self.colprops[idx]['Stylers'] = [] self.colprops[idx]['Formatters'] = [] self.colprops[idx]['ColumnStylers'] = [] } # 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:
334 335 336 |
# File 'lib/rex/text/wrapped_table.rb', line 334 def cellpad @cellpad end |
#colprops ⇒ Object
:nodoc:
333 334 335 |
# File 'lib/rex/text/wrapped_table.rb', line 333 def colprops @colprops end |
#columns ⇒ Object
:nodoc:
333 334 335 |
# File 'lib/rex/text/wrapped_table.rb', line 333 def columns @columns end |
#header ⇒ Object
:nodoc:
332 333 334 |
# File 'lib/rex/text/wrapped_table.rb', line 332 def header @header end |
#headeri ⇒ Object
:nodoc:
332 333 334 |
# File 'lib/rex/text/wrapped_table.rb', line 332 def headeri @headeri end |
#indent ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/rex/text/wrapped_table.rb', line 334 def indent @indent end |
#postfix ⇒ Object
:nodoc:
335 336 337 |
# File 'lib/rex/text/wrapped_table.rb', line 335 def postfix @postfix end |
#prefix ⇒ Object
:nodoc:
335 336 337 |
# File 'lib/rex/text/wrapped_table.rb', line 335 def prefix @prefix end |
#rows ⇒ Object
:nodoc:
333 334 335 |
# File 'lib/rex/text/wrapped_table.rb', line 333 def rows @rows end |
#scterm ⇒ Object
:nodoc:
336 337 338 |
# File 'lib/rex/text/wrapped_table.rb', line 336 def scterm @scterm end |
#sort_index ⇒ Object
:nodoc:
336 337 338 |
# File 'lib/rex/text/wrapped_table.rb', line 336 def sort_index @sort_index end |
#sort_order ⇒ Object
:nodoc:
336 337 338 |
# File 'lib/rex/text/wrapped_table.rb', line 336 def sort_order @sort_order end |
#width ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/rex/text/wrapped_table.rb', line 334 def width @width end |
#word_wrap ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/rex/text/wrapped_table.rb', line 334 def word_wrap @word_wrap end |
Class Method Details
.new_from_csv(csv) ⇒ Object
Build table from CSV dump
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/rex/text/wrapped_table.rb', line 283 def self.new_from_csv(csv) # Read in or keep data, get CSV or die if csv.is_a?(String) csv = File.file?(csv) ? CSV.read(csv) : CSV.parse(csv) end # Adjust for skew if csv.first == ["Keys", "Values"] csv.shift # drop marker cols = [] rows = [] csv.each do |row| cols << row.shift rows << row end tbl = self.new('Columns' => cols) rows.in_groups_of(cols.count) {|r| tbl << r.flatten} else tbl = self.new('Columns' => csv.shift) while !csv.empty? do tbl << csv.shift end end return tbl end |
Instance Method Details
#<<(fields) ⇒ Object
Adds a row using the supplied fields.
188 189 190 |
# File 'lib/rex/text/wrapped_table.rb', line 188 def <<(fields) add_row(fields) end |
#[](*col_names) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/rex/text/wrapped_table.rb', line 308 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 ⇒ Object
Adds a horizontal line.
247 248 249 |
# File 'lib/rex/text/wrapped_table.rb', line 247 def add_hr rows << '__hr__' end |
#add_row(fields = []) ⇒ Object
Adds a row with the supplied fields.
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/rex/text/wrapped_table.rb', line 195 def add_row(fields = []) if fields.length != self.columns.length raise RuntimeError, 'Invalid number of columns!' end formatted_fields = fields.map.with_index { |field, idx| field = format_table_field(field, idx) field } rows << formatted_fields end |
#drop_left ⇒ Object
Returns new sub-table with headers and rows maching column names submitted
Flips table 90 degrees left
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/rex/text/wrapped_table.rb', line 257 def drop_left tbl = self.class.new( 'Columns' => Array.new(self.rows.count+1,' '), 'Header' => self.header, 'Indent' => self.indent) (self.columns.count+1).times do |ti| row = self.rows.map {|r| r[ti]}.unshift(self.columns[ti]).flatten # insert our col|row break. kind of hackish row[1] = "| #{row[1]}" unless row.all? {|e| e.nil? || e.empty?} tbl << row end return tbl end |
#header_to_s ⇒ Object
Returns the header string.
168 169 170 171 172 173 174 175 176 |
# File 'lib/rex/text/wrapped_table.rb', line 168 def header_to_s # :nodoc: if (header) pad = " " * headeri return pad + header + "\n" + pad + "=" * header.length + "\n\n" end return '' end |
#ip_cmp(a, b) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rex/text/wrapped_table.rb', line 208 def ip_cmp(a, b) begin a = IPAddr.new(a.to_s) b = IPAddr.new(b.to_s) return 1 if a.ipv6? && b.ipv4? return -1 if a.ipv4? && b.ipv6? a <=> b rescue IPAddr::Error nil end end |
#print ⇒ Object Also known as: p
Prints the contents of the table.
181 182 183 |
# File 'lib/rex/text/wrapped_table.rb', line 181 def print puts to_s end |
#sort_rows(index = sort_index, order = sort_order) ⇒ 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.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rex/text/wrapped_table.rb', line 225 def sort_rows(index = sort_index, order = sort_order) return if index == -1 return unless rows rows.sort! do |a,b| if a[index].nil? cmp = -1 elsif b[index].nil? cmp = 1 elsif a[index] =~ /^[0-9]+$/ and b[index] =~ /^[0-9]+$/ cmp = a[index].to_i <=> b[index].to_i elsif (cmp = ip_cmp(a[index], b[index])) != nil else cmp = a[index] <=> b[index] # assumes otherwise comparable. end cmp ||= 0 order == :forward ? cmp : -cmp end end |
#to_csv ⇒ Object
Converts table contents to a csv
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rex/text/wrapped_table.rb', line 149 def to_csv sort_rows str = '' str << ( columns.join(",") + "\n" ) rows.each { |row| next if is_hr(row) || !row_visible(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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rex/text/wrapped_table.rb', line 118 def to_s sort_rows # Loop over and style columns styled_columns = columns.map.with_index { |col, idx| style_table_column_headers(col, idx) } # Loop over and style rows that are visible to the user styled_rows = rows.select { |row| row_visible(row) } .map! { |row| row.map.with_index { |cell, index| style_table_field(cell, index) } } optimal_widths = calculate_optimal_widths(styled_columns, styled_rows) str = prefix.dup str << header_to_s || '' str << columns_to_s(styled_columns, optimal_widths) || '' str << hr_to_s || '' styled_rows.each { |row| if is_hr(row) str << hr_to_s else str << row_to_s(row, optimal_widths) end } str << postfix return str end |
#valid_ip?(value) ⇒ Boolean
271 272 273 274 275 276 277 278 |
# File 'lib/rex/text/wrapped_table.rb', line 271 def valid_ip?(value) begin IPAddr.new value true rescue IPAddr::Error false end end |