Skip to content

Commit

Permalink
[loaders] improve fixed-width saver saulpw#2255
Browse files Browse the repository at this point in the history
Don't add so many spaces between columns.
Use the max width of the data, rather than the max width of the window.
  • Loading branch information
daviewales authored and anjakefala committed Feb 5, 2024
1 parent 6424f63 commit 18db9d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 0 additions & 1 deletion visidata/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ def getMaxWidth(self, rows):
return w



# ---- basic Columns

class AttrColumn(Column):
Expand Down
20 changes: 18 additions & 2 deletions visidata/loaders/fixed_width.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from visidata import VisiData, vd, Sheet, Column, Progress, SequenceSheet
from visidata import VisiData, vd, Sheet, Column, Progress, SequenceSheet, Column, dispwidth


vd.option('fixed_rows', 1000, 'number of rows to check for fixed width columns')
Expand All @@ -9,6 +9,22 @@
def open_fixed(vd, p):
return FixedWidthColumnsSheet(p.base_stem, source=p, headerlines=[])

@Column.api
def getMaxDataWidth(col, rows): #2255 need real max width for fixed width saver
'''Return the maximum length of any cell in column or its header,
even if wider than window. (Slow for large cells!)'''

w = 0
nlen = dispwidth(col.name)
if len(rows) > 0:
w_max = 0
for r in rows:
row_w = dispwidth(col.getDisplayValue(r))
if w_max < row_w:
w_max = row_w
w = w_max
return max(w, nlen)

class FixedWidthColumn(Column):
def __init__(self, name, i, j, **kwargs):
super().__init__(name, **kwargs)
Expand Down Expand Up @@ -84,7 +100,7 @@ def save_fixed(vd, p, *vsheets):
widths = {} # Column -> width:int
# headers
for col in Progress(sheet.visibleCols, gerund='sizing'):
widths[col] = col.getMaxWidth(sheet.rows) #1849
widths[col] = col.getMaxDataWidth(sheet.rows) #1849 #2255
fp.write(('{0:%s} ' % widths[col]).format(col.name))
fp.write('\n')

Expand Down

0 comments on commit 18db9d6

Please sign in to comment.