pyformat icon indicating copy to clipboard operation
pyformat copied to clipboard

Support formatting code provided via stdin

Open vlcinsky opened this issue 8 years ago • 3 comments

yapf has very similar command line interface as pyformat but provides one additional one: pass a code via stdin and print result on stdout.

Such a feature makes it very easy for integrating e.g. into vim editor by adding following line into .vimrc file:

autocmd FileType python setlocal equalprg=yapf

and user may format the code of whole file:

gg=G

or in visual mode (multiple lines selected):

=

pyformat does not accept file on stdin thus it is not possible to use it in the same style. Adding such feature to pyformat would make use simpler.

vlcinsky avatar Feb 14 '17 15:02 vlcinsky

@vlcinsky you can use something like this:

  1. Create a file called my-equalprg and give execution status to it (chmod +x my-equalprg):
#!/usr/bin/env bash

TMPFILE=$(mktemp).py

cat - > $TMPFILE \
    && pyformat --in-place $TMPFILE
cat $TMPFILE
  1. Inside your .vimrc file:
autocmd FileType python setlocal equalprg=my-equalprg

The gotcha is that equalprg setting writes to stdout and expects result in stdin.

viniciusban avatar Jul 26 '18 05:07 viniciusban

@viniciusban thanks for the workaround.

What gotcha do you mean? Encoding troubles?

vlcinsky avatar Jul 26 '18 07:07 vlcinsky

Sorry, I didn't express myself correctly.

It's not a "gotcha", but a characteristic. As equalprg works with stdin and stdout we can write our own filter and do whatever we need.

Actually my current filter runs some utilities in a pipeline. The last one is yapf, btw. ;-)

viniciusban avatar Jul 26 '18 15:07 viniciusban