Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support formatting code provided via stdin #7

Open
vlcinsky opened this issue Feb 14, 2017 · 3 comments · May be fixed by #11
Open

Support formatting code provided via stdin #7

vlcinsky opened this issue Feb 14, 2017 · 3 comments · May be fixed by #11

Comments

@vlcinsky
Copy link

vlcinsky commented Feb 14, 2017

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.

@viniciusban
Copy link

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

@vlcinsky
Copy link
Author

@viniciusban thanks for the workaround.

What gotcha do you mean? Encoding troubles?

@viniciusban
Copy link

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

@askurihin askurihin linked a pull request Oct 23, 2021 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants