Need help with Bash text filter

88 views
Skip to first unread message

Greg Raven

unread,
Dec 20, 2022, 9:24:00 AM12/20/22
to BBEdit Talk
I have a couple of Bash-language text filters that work great. I just tried to create a new one, which instead of working as expected generates an error message.

Here's the code:

```
#!/bin/sh

## requires macOS CommandLineTools
## requires pip3 install beautysh

python3 ~/Library/Python/3.9/bin/beautysh
```
When I run beautysh from the command line, it seems to work fine, just like my text filter that employs css-beautity, js-beautify, and json.tool and uses similar coding.

Can anyone shed any light on this?

Kevin Shay

unread,
Dec 20, 2022, 10:01:20 AM12/20/22
to bbe...@googlegroups.com
I haven't used beautysh but it looks like it's expecting to be passed the name/path of a file to operate on, and rewrites the file in place?

A text filter, on the other hand, is passed the contents of the file you run it against, in the standard input (sys.stdin in Python).

So you may need to adapt the module interface mentioned in the docs ("You can also call beautysh as a module") in order to use it as a text filter.

--Kevin

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/b58483e7-bf67-4e06-899c-a506a2ed88fcn%40googlegroups.com.

jj

unread,
Dec 20, 2022, 10:26:59 AM12/20/22
to BBEdit Talk
Hi Greg,

If you use your script as a text filter, you should add an hyphen '-' after the beautysh command to have stdin as input to the command.
like so:
    ...
    python3 ~/Library/Python/3.9/bin/beautysh -
    ...

HTH,

Jean Jourdain

Kevin Shay

unread,
Dec 20, 2022, 10:52:27 AM12/20/22
to bbe...@googlegroups.com
Ah, it does look like that will work. It's not mentioned in the README but it is documented in the script:

        parser.add_argument(
            "files",
            metavar="FILE",
            nargs="*",
            help="Files to be beautified. This is mandatory. "
            "If - is provided as filename, then beautysh reads "
            "from stdin and writes on stdout.",
        )

Greg Raven

unread,
Dec 20, 2022, 5:49:13 PM12/20/22
to BBEdit Talk
Jean,

Excellent! That works. Thanks so much.

Greg Raven

unread,
Dec 20, 2022, 5:53:06 PM12/20/22
to BBEdit Talk
Right you are. I never would have thought to look there.

Rick Yentzer

unread,
Dec 28, 2022, 10:58:46 PM12/28/22
to BBEdit Talk
Thank you! I came here wondering why a text filter wasn't working, but it did when run in terminal. Mine was the same issue. I was passing the text contents instead of the file which php-cs-fixer expects. Oh well, guess I'll just keep terminal open when needed.

jj

unread,
Dec 29, 2022, 2:30:43 AM12/29/22
to BBEdit Talk
In case you want to close the terminal and stay in BBEdit, you can create a shell script in ~/Library/Application\ Support/BBEdit/Scripts/php-cs-fixer-example.sh  
(Adapt php-cs-fixer's path as per your configuration) :

    #!/usr/bin/env sh
    /opt/homebrew/bin/php-cs-fixer fix --rules=no_whitespace_in_blank_line -- "$BB_DOC_PATH" ;

And apply it to the front document from the BBEdit's Script menu.

HTH,

Jean Jourdain

Rick Yentzer

unread,
Dec 29, 2022, 11:44:26 AM12/29/22
to BBEdit Talk
Thank you Jean, 
That was very helpful and I was able to get it working with a few adjustments.

Do you have recommendations on learning more about shell scripting and setting up some of these types of command line tools to work with BBEdit?

Reply all
Reply to author
Forward
0 new messages