Using Black as text filter

198 views
Skip to first unread message

Maxime Pigeon

unread,
Sep 4, 2021, 6:02:36 PM9/4/21
to BBEdit Talk
Hi,

I'm trying to create a very simple text filter that uses Black to format Python code. According to their documentation, Black supports formatting code via stdin, with the result being printed to stdout. So my text filter is simply:

#!/bin/bash

black -

Script output says that the reformat was successful, yet my file doesn't change. Anyone has experience with using Black in BBEdit?

Maxime Pigeon

unread,
Sep 4, 2021, 7:25:20 PM9/4/21
to BBEdit Talk
For reference, I ended up using Black's (private) method format_str() like so:

#!/usr/bin/env python3

import sys
from black import format_str
from black.mode import Mode

stdin = sys.stdin.read()
stdout = format_str(stdin, mode=Mode())

print(stdout, end="")

Still, it would be nice to know what's wrong with the previous code.

jj

unread,
Sep 5, 2021, 1:44:01 PM9/5/21
to BBEdit Talk
Hi Maxime,

Passing the --quiet flag seems to do the trick.

#!/bin/bash

black -q -

HTH

Jean Jourdain

Maxime Pigeon

unread,
Sep 5, 2021, 4:04:31 PM9/5/21
to BBEdit Talk
Indeed it does! Thanks Jean. 
Reply all
Reply to author
Forward
0 new messages