Skip to content

Commit d97ad5f

Browse files
author
Brent Cook
committed
support more consistent table output formatting
from @sempervictus
1 parent 8a0bb67 commit d97ad5f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lib/rex/ui/text/table.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,17 @@ def columns_to_s # :nodoc:
323323
last_idx = nil
324324
columns.each_with_index { |col,idx|
325325
if (last_col)
326-
nameline << pad(' ', last_col, last_idx)
327-
328-
remainder = colprops[last_idx]['MaxWidth'] - last_col.length
326+
# This produces clean to_s output without truncation
327+
# Preserves full string in cells for to_csv output
328+
padding = pad(' ', last_col, last_idx)
329+
nameline << padding
330+
remainder = padding.length - cellpad
329331
if (remainder < 0)
330332
remainder = 0
331333
end
332334
barline << (' ' * (cellpad + remainder))
333335
end
336+
334337
nameline << col
335338
barline << ('-' * col.length)
336339

@@ -359,7 +362,6 @@ def row_to_s(row) # :nodoc:
359362
if (idx != 0)
360363
line << pad(' ', last_cell.to_s, last_idx)
361364
end
362-
# line << pad(' ', cell.to_s, idx)
363365
# Limit wide cells
364366
if colprops[idx]['MaxChar']
365367
last_cell = cell.to_s[0..colprops[idx]['MaxChar'].to_i]
@@ -379,8 +381,12 @@ def row_to_s(row) # :nodoc:
379381
# some text and a column index.
380382
#
381383
def pad(chr, buf, colidx, use_cell_pad = true) # :nodoc:
382-
remainder = colprops[colidx]['MaxWidth'] - buf.length
383-
val = chr * remainder;
384+
# Ensure we pad the minimum required amount
385+
max = colprops[colidx]['MaxChar'] || colprops[colidx]['MaxWidth']
386+
max = colprops[colidx]['MaxWidth'] if max.to_i > colprops[colidx]['MaxWidth'].to_i
387+
remainder = max - buf.length
388+
remainder = 0 if remainder < 0
389+
val = chr * remainder
384390

385391
if (use_cell_pad)
386392
val << ' ' * cellpad

0 commit comments

Comments
 (0)