Bat: Option for reindentation

Created on 14 May 2019  Â·  6Comments  Â·  Source: sharkdp/bat

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 !

documentation good first issue help wanted packagintooling

All 6 comments

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).

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

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 :tm: 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 :)

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?

:+1:

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbsvttr picture tbsvttr  Â·  3Comments

HakubJozak picture HakubJozak  Â·  3Comments

yum-feng picture yum-feng  Â·  3Comments

mjlbach picture mjlbach  Â·  3Comments

lilyball picture lilyball  Â·  3Comments