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

Option for reindentation #565

Closed
rgarrigue opened this issue May 14, 2019 · 6 comments
Closed

Option for reindentation #565

rgarrigue opened this issue May 14, 2019 · 6 comments
Labels

Comments

@rgarrigue
Copy link

Hi

Just tried out bat. Fell in love. I wish you'ld get it included in Ubuntu / Debian / CentOS repositories but that's not my topic right now.

I'm screwing up with Jenkins atm, it look like this

image

I wish there was an option to add line break & indent this properly. Make it pretty basically. I did it adding a | xmllint --format - |, but maybe you can consider this for bat

Keep up the rocking on !

@sharkdp
Copy link
Owner

sharkdp commented May 14, 2019

Thank you for the feedback.

We have discussed pretty-printing in the past (see e.g. #55). I'd rather not implement this in bat as I'm afraid that it would lead to code/feature bloat. I think it is better left to external tools for the specific languages.

I think the best way to do this is to pipe into bat, just like you did (see also the json_pp example in the How-To section of the README).

@sharkdp
Copy link
Owner

sharkdp commented May 14, 2019

Thinking of it, maybe we could provide a wrapper script in this repository that would:

  1. match on certain file types (xml, json, js, …)
  2. call the appropriate pretty-printer (xmllint, json_pp, etc.)
  3. pipe the output to bat and set the correct --language

Something like:

#!/bin/bash

for file in "$@"; do
    extension="${file##*.}"
    case "$extension" in
        xml)
            xmllint --format "$file" | bat -lxml
            ;;
        json)
            json_pp < "$file" | bat -ljson
            ;;
        *)
            bat "$file"
            ;;
    esac
done

@keith-hall
Copy link
Collaborator

I like the idea, but I would guess that it is a little out of scope for this project.
The syntect library, which bat uses to power the syntax highlighting functionality, relies on sublime-syntax grammars, which were invented for the popular Sublime Text editor.
Sublime Text has auto indentation support using legacy tmPreferences files, but note that these don't support re-formatting the document by adding line breaks etc., they literally only deal with indentation.
Supporting proper pretty formatting for any language isn't, afaik, a solved problem ™️ that any single library can perform. I don't doubt that something could be built on top of sublime-syntax files to achieve this, but it would be a lot of work.

Perhaps a possible solution could be to store some preferences as to what external command to execute for each file type to prettify them. But I guess many people will be using bat expecting the output to represent the exact original file contents - like cat - and wouldn't desire such functionality. So maybe the best option is documentation on a commonly useful wrapper script to invoke such auto formatting tools?

Just my 2 cents :)

@sharkdp
Copy link
Owner

sharkdp commented May 14, 2019

But I guess many people will be using bat expecting the output to represent the exact original file contents - like cat - and wouldn't desire such functionality.

Absolutely. Even if we were to implement this in bat directly, it would be behind a flag.

I don't doubt that something could be built on top of sublime-syntax files to achieve this, but it would be a lot of work.

Hm, I don't believe that sublime syntax files have enough information to implement pretty printing (for any non-trivial syntax). I think that most pretty printers are based on actual parsers for the given language. The pretty-printing part is then "just" a way of formatting the AST.

So maybe the best option is documentation on a commonly useful wrapper script to invoke such auto formatting tools?

👍

@keith-hall
Copy link
Collaborator

oops, sorry - I didn't see your replies before I wrote mine @sharkdp, so I like the idea you came up with is so similar to what I was imagining ;)

you're right - my thinking was that if one wants to build a general purpose prettifier, each sublime-syntax could have a corresponding file detailing how to build an AST-like structure from the tokens parsed, and some rules or something else to operate on that to insert line-breaks and change indentation.

@sharkdp sharkdp added good first issue Good for newcomers help wanted Extra attention is needed labels Aug 28, 2019
@sharkdp
Copy link
Owner

sharkdp commented Sep 25, 2019

I think we can close this now that we have prettybat by @eth-p: https://github.com/sharkdp/bat#prettier--shfmt--rustfmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants