Created
December 11, 2018 19:53
-
-
Save BasPH/5e665273d5e4cb8a8eefb6f9d43b0b6d to your computer and use it in GitHub Desktop.
Black on selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -x | |
black=$1 | |
input_file=$2 | |
start_line=$3 | |
end_line=$4 | |
# Read selected lines and write to tmpfile | |
selection=$(sed -n "$start_line, $end_line p; $(($end_line+1)) q" < $input_file) | |
tmpfile=$(mktemp) | |
echo "$selection" > "$tmpfile" | |
# Apply Black formatting to tmpfile | |
$black $tmpfile | |
# Delete original lines from file | |
sed -i "" "$start_line,$end_line d" $input_file | |
# And insert newly formatted lines | |
sed -i "" "$(($start_line-1)) r $tmpfile" $input_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any idea about this? @BasPH