Class: Rex::Ui::Text::Table

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



56
57
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
# File 'lib/rex/ui/text/table.rb', line 56

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 = []

	# 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

#cellpadObject

:nodoc:



185
186
187
# File 'lib/rex/ui/text/table.rb', line 185

def cellpad
  @cellpad
end

#colpropsObject

:nodoc:



184
185
186
# File 'lib/rex/ui/text/table.rb', line 184

def colprops
  @colprops
end

#columnsObject

:nodoc:



184
185
186
# File 'lib/rex/ui/text/table.rb', line 184

def columns
  @columns
end

#headerObject

:nodoc:



183
184
185
# File 'lib/rex/ui/text/table.rb', line 183

def header
  @header
end

#headeriObject

:nodoc:



183
184
185
# File 'lib/rex/ui/text/table.rb', line 183

def headeri
  @headeri
end

#indentObject

:nodoc:



185
186
187
# File 'lib/rex/ui/text/table.rb', line 185

def indent
  @indent
end

#postfixObject

:nodoc:



186
187
188
# File 'lib/rex/ui/text/table.rb', line 186

def postfix
  @postfix
end

#prefixObject

:nodoc:



186
187
188
# File 'lib/rex/ui/text/table.rb', line 186

def prefix
  @prefix
end

#rowsObject

:nodoc:



184
185
186
# File 'lib/rex/ui/text/table.rb', line 184

def rows
  @rows
end

#widthObject

:nodoc:



185
186
187
# File 'lib/rex/ui/text/table.rb', line 185

def width
  @width
end

Instance Method Details

#<<(fields) ⇒ Object

Adds a row using the supplied fields.



154
155
156
# File 'lib/rex/ui/text/table.rb', line 154

def <<(fields)
	add_row(fields)
end

#add_hrObject

Adds a horizontal line.



177
178
179
# File 'lib/rex/ui/text/table.rb', line 177

def add_hr
	rows << '__hr__'
end

#add_row(fields = []) ⇒ Object

Adds a row with the supplied fields.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rex/ui/text/table.rb', line 161

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_sObject

Returns the header string.



134
135
136
137
138
139
140
141
142
# File 'lib/rex/ui/text/table.rb', line 134

def header_to_s # :nodoc:
	if (header)
		pad = " " * headeri

		return pad + header + "\n" + pad + "=" * header.length + "\n\n"
	end

	return ''
end

Prints the contents of the table.



147
148
149
# File 'lib/rex/ui/text/table.rb', line 147

def print
	puts to_s
end

#to_csvObject

Converts table contents to a csv



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rex/ui/text/table.rb', line 118

def to_csv
	str = ''
	str << ( columns.join(",") + "\n" )
	rows.each { |row|
		next if is_hr(row)
		str << ( row.map{|x| 
			x.gsub(/[\r\n]/, ' ').gsub(/\s+/, ' ').gsub('"', '""')
		}.map{|x| "\"#{x}\"" }.join(",") + "\n" )
	}
	str
end

#to_sObject

Converts table contents to a string.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rex/ui/text/table.rb', line 96

def to_s
	str  = prefix
	str << header_to_s || ''
	str << columns_to_s || ''
	str << hr_to_s || ''
	
	rows.each { |row|
		if (is_hr(row))
			str << hr_to_s
		else
			str << row_to_s(row)
		end
	}

	str << postfix

	return str
end