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

[Feature Request] Journal Prettifier #232

Open
the-solipsist opened this issue Sep 5, 2022 · 5 comments
Open

[Feature Request] Journal Prettifier #232

the-solipsist opened this issue Sep 5, 2022 · 5 comments

Comments

@the-solipsist
Copy link
Contributor

One of the features that the Vim/Emacs ledger-language support add-ons have is reformatting of the transactions to make them look prettier (e.g., with transaction amounts aligned neatly, etc.)

While this is not a very important feature (since one can run hledger print to obtain somewhat similar results, though it is lossy since it doesn't preserve inter-transaction comments, etc.), it would be good to have.

@mhansen
Copy link
Owner

mhansen commented Sep 5, 2022 via email

@simonmichael
Copy link

simonmichael commented Sep 5, 2022 via email

@the-solipsist
Copy link
Contributor Author

What's wrong with piping through hledger print (and handling inter transaction content separately) ?

Nothing at all. It's just that doing so requires a separate script[1]. So it's just a matter of laziness vs. convenience.

At one point, I was using the vim-ledger plugin solely for reformatting of my file, since it made it very easy. Now that I've moved to vscode, I was hoping I could use hledger-vscode for the same purpose.

[1]: Here's a script that does the job for me, at least wrt to the include and default commands I use at the beginning of a file:

#!/bin/bash
sed '/^20[0-9][0-9]/Q' $1 > /tmp/j
tail -n +2 $1 | hledger -f- print -x -I >> /tmp/j
mv $1{,.bak}
mv /tmp/j $1

@mhansen
Copy link
Owner

mhansen commented Sep 6, 2022

What's wrong with piping through hledger print (and handling inter transaction content
separately) ?

I suppose I'd want to be very careful not to lose data. So I'd want to be very confident that I'm not going to lose data before releasing anything like this.

I just tried hledger print on a file, and it complained about a balance assertion, which I could silence with -I. So that's one problem fixed.

I assumed hledger print wouldn't show comments inside transactions, or comments at the end of a transaction line. But it appears it does?

Let's try a test for a few language features:

$ cat test.hledger
; Comment
alias X = Y
commodity 1000.00 AUD
include /dev/null

; Comment after newline
2021-01-01 Description ; end of line comment
  ; Inside transaction comment
  Expenses:Post  200 AUD = 300 AUD ; End of post comment
  Assets:Bank  ; end of bank comment
  ; End of transaction comment
; Post transaction comment

Let's see how much it preserves:

$ hledger print -f test.hledger -I
2021-01-01 Description  ; end of line comment
    ; Inside transaction comment
    Expenses:Post      200.00 AUD = 300 AUD  ; End of post comment
    Assets:Bank                    ; end of bank comment
    ; End of transaction comment

It's good that some of the comments are preserved! But statements for alias, commodity, include, and inter-transaction comments have all gone missing. And I imagine the include will pull other files in during autoformatting. There's probably a bunch of other more esoteric language features that I don't use (timedot?) that I'd want to be confident in handling correctly before offering an autoformatter. I don't really feel confident in building a parser that will mirror what hledger does precisely (and as hledger changes in future too).

I believe these autoformatters best live alongside the canonical parser: that is, in hledger core, so that the autoformatter parser evolves along with the autoformatter (cf gofmt, rustfmt, clang-format).

@the-solipsist
Copy link
Contributor Author

Ah. I misunderstood what @simonmichael meant. I mistakenly thought his question was aimed at me, rather than at @mhansen.

The things that hledger print (currently) doesn't handle are clearly defined:

  1. Directives (like alias, commodity, include, account, etc.)
  2. Inter-transaction comments

So intra-transaction comments (those attached to transactions and postings) are handled just fine. What aren't handled are inter-transaction comments.

And I imagine the include will pull other files in during autoformatting

Yes, hledger print respects include directives. That's why in my short script, I omit the include and default directives (which are in the top 2 lines of my file) before passing it on to hledger: tail -n +2 $1 | hledger -f- print -x -I.

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

No branches or pull requests

3 participants