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

feat: cli: Add tsv output (#869) #2108

Merged
merged 1 commit into from
Nov 7, 2023
Merged

Conversation

psagers
Copy link
Contributor

@psagers psagers commented Nov 5, 2023

I've been writing some add-on scripts to analyze my hledger data and frequently wishing I had tsv output. csv is just complicated enough to require a bit of parsing--usually through some external dependency--whereas tsv is trivial enough to decode directly in essentially any environment. (It's also the native and strongly preferred format of visidata.)

I see there's an old issue requesting this feature (#869). There also seems to be an old pull request that circled around it a bit, but never turned into anything.

I believe the implementation is in fact pretty trivial. The data is identical to csv output, so it should just be a matter of writing printTSV based on printCSV and hooking it up.

A few notes:

  • This creates a small amount of code duplication, which could be resolved if it's worthwhile.
  • There was one csv test that I couldn't duplicate, as it has output lines that begin with <. Perhaps someone familiar with shelltest could suggest a workaround, if desired.
  • To be safe, I'm replacing all tabs, carriage returns, and newlines in field data with spaces. I'm assuming these characters are rare to nonexistent in output data to begin with.

Copy link
Owner

@simonmichael simonmichael left a comment

Choose a reason for hiding this comment

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

Looks good !

hledger/hledger.txt Outdated Show resolved Hide resolved
@simonmichael
Copy link
Owner

For test data lines beginning with <, maybe shelltestrunner's format 2 helps (triple
character delimiters)

All commands that suport csv output now also support tsv output. The
data is identical, but the fields are separated by tab characters and
there is no quoting or escaping. Tab, carriage return, and newline
characters in data are converted to spaces (this should rarely if ever
happen in practice).
@simonmichael
Copy link
Owner

Looks good to me. I appreciate that you added the new output type to all commands. Thank you!

@simonmichael simonmichael merged commit efcea06 into simonmichael:master Nov 7, 2023
1 check passed
@psagers
Copy link
Contributor Author

psagers commented Nov 7, 2023 via email

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.

2 participants