Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[loaders] improve fixed-width saver #2257

Merged
merged 3 commits into from
Feb 5, 2024

Conversation

daviewales
Copy link
Contributor

@daviewales daviewales commented Jan 15, 2024

Don't add so many spaces between columns when saving.
Use the max width of the data, rather than the max width of the window when saving.
Don't include the separating space as data when loading. (Prevents extra space being added each round trip, as the saver also adds a single space between columns.)

Fixes #2255.

@@ -38,7 +38,7 @@ def columnize(rows):
# collapse fields
for i in allNonspaces:
if i > prev+1:
yield colstart, i
yield colstart, prev+1 #2255
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be:

            yield colstart, i-1 #2255

prev+1 collapses multiple spaces between columns.
i-1 assumes only a single space is the column separator, and keeps the remaining spaces as data.

For example, given the following fixed width data with three spaces between the columns:

column_1    column_2
one         two

prev+1 would be saved as the following CSV. (Separating spaces are removed, but padding spaces are retained):

column_1,column_2
one     ,two

i-1 would be saved as the following CSV. (One separating space is removed. All others are retained.):

column_1  ,column_2
one       ,two

The current version of the code with prev+1 is my personal preference, but open to guidance if the i-1 version better matches the intent of 'fixed width'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, it must be i-1 rather than i to avoid adding an extra space between the columns each round trip.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I prefer prev+1 too. Thanks for the explanation.

@daviewales daviewales marked this pull request as ready for review January 23, 2024 05:15
@daviewales
Copy link
Contributor Author

I think this is ready for initial review. Let me know if you're happy with the suggested changes, and I'll update the tests accordingly. (They are currently broken due to these changes.)

@daviewales daviewales changed the title Improve fixed-width saver [loaders] improve fixed-width saver Jan 23, 2024
@daviewales
Copy link
Contributor Author

Note: Something which should be considered is a file like the following:

colours shades               counts 
red     light                3      
red     dark                 4      
green   very light           5      
green   very very very light 5      
blue    dark                 8      

VisiData reads this as four columns rather than three, because it splits columns based on unbroken vertical space, rather than header position.

Screen Shot 2024-01-23 at 4 23 01 pm

This behaviour remains unchanged by this PR.

visidata/column.py Outdated Show resolved Hide resolved
Copy link
Owner

@saulpw saulpw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your attention on this, @daviewales! If you wouldn't mind updating the tests too, we'd much appreciate it.

daviewales and others added 3 commits February 4, 2024 17:36
Don't add so many spaces between columns.
Use the max width of the data, rather than the max width of the window.
@anjakefala
Copy link
Collaborator

Hey @daviewales!

I went ahead and made the requested changes, and updated the tests. =) Thanks for taking care of this.

@anjakefala anjakefala merged commit a44c9ff into saulpw:develop Feb 5, 2024
13 checks passed
@daviewales
Copy link
Contributor Author

Thanks @anjakefala, much appreciated!

@daviewales daviewales deleted the fixed-width-saver branch February 5, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fixed width saver and loader don't round trip. (Columns expand with increasing number of spaces)
3 participants