Rex::Text::Ui::Table.new[find_by_colnames]

Add :[] to ...Ui::Table allowing user to pass multiple colnames.
Returns a new table with only those columns and their rows.

Useful when using Rex to filter output, prep CSV, etc.

Testing:
```
t = Rex::Ui::Text::Table.new('Columns' => ['a','b','c'])
t << ['x','y','z']
t << ['p','q','r']
t['a','c']

=> a  c
-  -
p  r
x  z
```
bug/bundler_fix
RageLtMan 2013-03-28 04:02:31 -04:00
parent 79a72a18a9
commit ed3b1cecd4
1 changed files with 18 additions and 0 deletions

View File

@ -212,6 +212,24 @@ class Table
rows << '__hr__' rows << '__hr__'
end end
#
# Returns new sub-table with headers and rows maching column names submitted
#
def [](*colnames)
tbl = self.class.new('Indent' => self.indent,'Header' => self.header,'Columns' => colnames)
idx = []
colnames.each do |colname|
idx << self.columns.index(colname)
end
self.rows.each do |oldrow|
newrow = []
idx.map {|i| newrow << oldrow[i]}
tbl << newrow
end
return tbl
end
alias p print alias p print
attr_accessor :header, :headeri # :nodoc: attr_accessor :header, :headeri # :nodoc: