-
Notifications
You must be signed in to change notification settings - Fork 0
/
badfmt.py
53 lines (46 loc) · 1.72 KB
/
badfmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""A very bad formatter for any programming language with braces and semicolons (eg. c/c++/java/javascript/rust)"""
import click
@click.command()
@click.argument("filepath", type=click.Path(exists=True))
@click.option(
"-l", "--line-len", type=int, default=95, help="The length you want your lines to be"
)
def bad_fmt(filepath: str = "", line_len: int = 95):
"""A very bad formatter for any programming language with braces and semicolons (eg. c/c++/java/javascript/rust)"""
res = []
llens = []
with open(filepath, "r", encoding="utf-8") as f:
for line in f.readlines():
llens.append(len(line))
if len(line) > line_len:
res.append(line)
elif line.rstrip().endswith(";"):
res.append(
line[: len(line) - 2]
+ (" " * (line_len - len(line) - 1))
+ ";\n"
)
elif line.rstrip().endswith("{"):
res.append(
line[: len(line) - 2]
+ (" " * (line_len - len(line) - 1))
+ "{\n"
)
elif line.rstrip().endswith("}"):
res.append(
line[: len(line) - 2]
+ (" " * (line_len - len(line) - 1))
+ "}\n"
)
elif line.rstrip().endswith("};"):
res.append(
line[: len(line) - 2]
+ (" " * (line_len - len(line) - 2))
+ "};\n"
)
else:
res.append(line)
with open(filepath, "w", encoding="utf-8") as f:
f.writelines(res)
if __name__ == "__main__":
bad_fmt()