• Stars
    star
    444
  • Rank 98,300 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bash beautifier for the masses.

Beautysh CI

This program takes upon itself the hard task of beautifying Bash scripts (yeesh). Processing Bash scripts is not trivial, they aren't like C or Java programs β€” they have a lot of ambiguous syntax, and (shudder) you can use keywords as variables. Years ago, while testing the first version of this program, I encountered this example:

done=0;while (( $done <= 10 ));do echo done=$done;done=$((done+1));done

Same name, but three distinct meanings (sigh). The Bash interpreter can sort out this perversity, but I decided not to try to recreate the Bash interpreter to beautify a script. This means there will be some border cases this Python program won't be able to process. But in tests with large Linux system Bash scripts, its error-free score was ~99%.

Installation

If you have pip set up you can do

pip install beautysh

or clone the repo and install:

git clone https://github.com/lovesegfault/beautysh
cd beautysh
poetry install

Usage

You can call Beautysh from the command line such as

beautysh file1.sh file2.sh file3.sh

in which case it will beautify each one of the files.

Available flags are:

  --indent-size INDENT_SIZE, -i INDENT_SIZE
                        Sets the number of spaces to be used in indentation.
  --backup, -b          Beautysh will create a backup file in the same path as
                        the original.
  --check, -c           Beautysh will just check the files without doing any
                        in-place beautify.
  --tab, -t             Sets indentation to tabs instead of spaces.
  --force-function-style FORCE_FUNCTION_STYLE, -s FORCE_FUNCTION_STYLE
                        Force a specific Bash function formatting. See below
                        for more info.
  --version, -v         Prints the version and exits.
  --help, -h            Print this help message.

Bash function styles that can be specified via --force-function-style are:
  fnpar: function keyword, open/closed parentheses, e.g.      function foo()
  fnonly: function keyword, no open/closed parentheses, e.g.  function foo
  paronly: no function keyword, open/closed parentheses, e.g. foo()

You can also call beautysh as a module:

from beautysh import Beautify

source = "my_string"

result, error = Beautify().beautify_string(source)

As written, beautysh can beautify large numbers of Bash scripts when called from a variety of means,including a Bash script:

#!/bin/sh

for path in `find /path -name '*.sh'`
do
   beautysh $path
done

As well as the more obvious example:

$ beautysh *.sh

CAUTION: Because Beautysh overwrites all the files submitted to it, this could have disastrous consequences if the files include some of the increasingly common Bash scripts that have appended binary content (a regime where Beautysh has undefined behaviour ). So please β€” back up your files, and don't treat Beautysh as a harmless utility. Even if that is true most of the time.

Beautysh handles Bash here-docs with care(and there are probably some border cases it doesn't handle). The basic idea is that the originator knew what format he wanted in the here-doc, and a beautifier shouldn't try to outguess him. So Beautysh does all it can to pass along the here-doc content unchanged:

if true
then

   echo "Before here-doc"

   # Insert 2 lines in file, then save.
   #--------Begin here document-----------#
vi $TARGETFILE <<x23LimitStringx23
i
This is line 1 of the example file.
This is line 2 of the example file.
^[
ZZ
x23LimitStringx23
   #----------End here document-----------#

   echo "After here-doc"

fi

Special comments @formatter:off and @formatter:on are available to disable formatting around a block of statements.

# @formatter:off
command \
    --option1 \
        --option2 \
            --option3 \
# @formatter:on

This takes inspiration from the Eclipse feature.

Contributing

Contributions are welcome and appreciated, however test cases must be added to prevent regression. Adding a test case is easy, and involves the following:

  1. Create a file tests/fixtures/my_test_name_raw.sh containing the unformatted version of your test case.
  2. Create a file tests/fixtures/my_test_name_formatted.sh containing the formatted version of your test case.
  3. Register your test case in tests/test_integration.py, It should look something like this:
def test_my_test_name(self):
    self.assert_formatting("my_test_name")

Originally written by Paul Lutus

More Repositories

1

nix-config

My personal NixOS config
Nix
340
star
2

cache-size

A Rust library to quickly get the size and line size of your CPU caches.
Rust
26
star
3

daedalos

A fully (dis)functional OS!
Rust
20
star
4

nix-workshop

Nix Workshop materials
Nix
18
star
5

nspire-kernel

Linux Kernel Configs for the TI Nspire CX and CX CAS
8
star
6

genesis

Rust
4
star
7

voronix

Voron + Klipper setup with Nix
Nix
4
star
8

musync

A fast music syncing software.
Rust
3
star
9

http-spi-bridge

Rust
3
star
10

chromaprint-rs

Safe Rust bindings to Chromaprint
Rust
3
star
11

hemoglobin

A Cellular Automaton written in Rust
Rust
3
star
12

cv

my cv
TeX
3
star
13

nix-rustc

Nix flake for rustc development
Nix
3
star
14

zuul

A dead simple test framework for C
C
2
star
15

fpga-challenges

HTML
2
star
16

ream

A Scheme interpreter written in Rust
Rust
2
star
17

dotfiles

My personal dotfiles and aux files
Shell
2
star
18

exercism

my solutions to exercism problems
Rust
2
star
19

p-challenges

A set of programming challenges, solved
C
2
star
20

MidiView

A Python based MIDI visualizer
Python
2
star
21

multi-ackermann

Fast, multithreaded implementation of the Ackermann Function in C++
C++
2
star
22

c-ht

A blazing fast hashtable implemented in C
C
2
star
23

nixpkgs-kernel-redux

Nix
2
star
24

hyperpixel-init

Code to configure and initialize the HyperPixel 4 display
Rust
2
star
25

nix-ffmpeg-opt

Nix
1
star
26

edconfig-parser

A parser for Editor Config http://editorconfig.org
Rust
1
star
27

lovesegfault-overlay

My Gentoo Overlay
Shell
1
star
28

backhand-async

Rust
1
star
29

leetcode

Rust
1
star
30

crane-cross-example

Nix
1
star
31

arduino-watch

An Arduino based watch with an OLED display
Arduino
1
star
32

rust-ml

Rust
1
star
33

advent-of-code

1
star
34

os-vcs

A list of OS's and their respective version control system
1
star
35

blog

personal blog
SCSS
1
star
36

cracking-the-coding-interview

Rust
1
star
37

sbcc

Notes from classes at SBCC
TeX
1
star
38

hard-calc

A set of hard Calculus and Real Analysis problems
TeX
1
star
39

collatz

A C++ Calculator of Collatz Conjecture
C++
1
star
40

slides

TeX
1
star
41

chat

A simple chat server
Rust
1
star
42

chirp

A CHIP-8 emulator written in Rust
Rust
1
star
43

mobius

A fast Rust implementation of the Mobius function
Shell
1
star
44

buildkit

Rust
1
star
45

transcode-rs

Rust utility to transcode media in bulk
Rust
1
star