@@ -323,14 +323,17 @@ def columns_to_s # :nodoc:
323
323
last_idx = nil
324
324
columns . each_with_index { |col , idx |
325
325
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
329
331
if ( remainder < 0 )
330
332
remainder = 0
331
333
end
332
334
barline << ( ' ' * ( cellpad + remainder ) )
333
335
end
336
+
334
337
nameline << col
335
338
barline << ( '-' * col . length )
336
339
@@ -359,7 +362,6 @@ def row_to_s(row) # :nodoc:
359
362
if ( idx != 0 )
360
363
line << pad ( ' ' , last_cell . to_s , last_idx )
361
364
end
362
- # line << pad(' ', cell.to_s, idx)
363
365
# Limit wide cells
364
366
if colprops [ idx ] [ 'MaxChar' ]
365
367
last_cell = cell . to_s [ 0 ..colprops [ idx ] [ 'MaxChar' ] . to_i ]
@@ -379,8 +381,12 @@ def row_to_s(row) # :nodoc:
379
381
# some text and a column index.
380
382
#
381
383
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
384
390
385
391
if ( use_cell_pad )
386
392
val << ' ' * cellpad
0 commit comments