Black: [Question] Is it possible to get formatted file to stdout without side-effect of file modification?

Created on 1 Aug 2018  路  3Comments  路  Source: psf/black

As a user unfamiliar with Black I would like to customize the view of formatting results as in
black filename --diff .
If only I could pipe results as an input for a tool of choice to

  • review diff side-by-side
  • highlight it with green/red colors with some other tool

Two options I could find:

  1. $ black filename --diff -q - to use this output as an input I should parse it by +/- appearance (nontrivial)
  2. $ black filename -q ; git diff filename:
  3. side-effect of changing file
  4. the need to reset changes
  5. if working directory wasn't clear before - we should select formatted file for git diff explicitly
  6. what if user doesn't have git?

PS I played around with piping between git diff --color-words or git diff --word-diff=color; diff -y (side-by-side view) and black filename -q | some-unix-command - for redirect... no luck

Most helpful comment

FWIW, abusing cat | like that is inefficient. Use file redirection instead: black -q - < $filename

All 3 comments

Try cat filename | black -

wow, thanks @zsol !

For those who come after:
To see changes side-by-side.
$ cat filename.py | black -q - | diff -y --color filename.py -
Only new or deleted lines are highlighted. Changed - not.

Side-by-side, colored. Requires colordiff tool
$ cat filename.py | black -q - | diff -y filename.py - | colordiff

Version for git diff because it has some nice options too
$ cat filename.py | black -q - | git diff --color-words --no-index -- filename.py -

FWIW, abusing cat | like that is inefficient. Use file redirection instead: black -q - < $filename

Was this page helpful?
0 / 5 - 0 ratings

Related issues

feluxe picture feluxe  路  3Comments

dgnsrekt picture dgnsrekt  路  3Comments

JelleZijlstra picture JelleZijlstra  路  3Comments

asottile picture asottile  路  3Comments

Curly-Mo picture Curly-Mo  路  3Comments