mirror of
https://github.com/nmap/nmap.git
synced 2026-01-04 13:49:03 +00:00
Add an append_raw method to the Table class to add an unformatted string
to a table. This is going to be for script output.
This commit is contained in:
20
ndiff/ndiff
20
ndiff/ndiff
@@ -739,6 +739,10 @@ class Table(object):
|
||||
strings.append(s)
|
||||
self.rows.append(strings)
|
||||
|
||||
def append_raw(self, s):
|
||||
"""Append a raw string for a row that is not formatted into columns."""
|
||||
self.rows.append(s)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.rows)
|
||||
|
||||
@@ -747,12 +751,16 @@ class Table(object):
|
||||
for row in self.rows:
|
||||
parts = [self.prefix]
|
||||
i = 0
|
||||
while i < len(row):
|
||||
parts.append(row[i].ljust(self.widths[i]))
|
||||
if i < len(self.padding):
|
||||
parts.append(self.padding[i])
|
||||
i += 1
|
||||
lines.append(u"".join(parts).rstrip())
|
||||
if isinstance(row, basestring):
|
||||
# A raw string.
|
||||
lines.append(row)
|
||||
else:
|
||||
while i < len(row):
|
||||
parts.append(row[i].ljust(self.widths[i]))
|
||||
if i < len(self.padding):
|
||||
parts.append(self.padding[i])
|
||||
i += 1
|
||||
lines.append(u"".join(parts).rstrip())
|
||||
return u"\n".join(lines)
|
||||
|
||||
def parse_port_list(port_list):
|
||||
|
||||
@@ -641,6 +641,15 @@ class table_test(unittest.TestCase):
|
||||
t.append(("a", "b", "c", "d"))
|
||||
self.assertEqual(str(t), "<<<a>>>b!!!cd")
|
||||
|
||||
def test_append_raw(self):
|
||||
"""Test the append_raw method that inserts an unformatted row."""
|
||||
t = Table("<* * *>")
|
||||
t.append(("1", "2", "3"))
|
||||
t.append_raw(" row ")
|
||||
self.assertEqual(str(t), "<1 2 3>\n row ")
|
||||
t.append(("4", "5", "6"))
|
||||
self.assertEqual(str(t), "<1 2 3>\n row \n<4 5 6>")
|
||||
|
||||
def test_strip(self):
|
||||
"""Test that trailing whitespace is stripped."""
|
||||
t = Table("* * * ")
|
||||
|
||||
Reference in New Issue
Block a user