Added java-beautifier

This commit is contained in:
ByteDream 2021-06-03 01:21:17 +02:00
parent ba1cf70160
commit a89aaec1fb
3 changed files with 71 additions and 0 deletions

View File

@ -9,6 +9,7 @@ This repository contains some (linux) scripts I am using to simplify my daily wo
- [Merge pdf files together](/merge-pdf) - [Merge pdf files together](/merge-pdf)
- [Rename files random](/randomize-filename) - [Rename files random](/randomize-filename)
- [Rename files based on their MD5 hashsum](/hashify-filename) - [Rename files based on their MD5 hashsum](/hashify-filename)
- [Beautify java source code](/java-beautifier)
If you want to use any of the (bash!) scripts globaly, see [pathify](/pathify) If you want to use any of the (bash!) scripts globaly, see [pathify](/pathify)

40
java-beautifier/README.md Normal file
View File

@ -0,0 +1,40 @@
## Java beautifier
Makes java code look like it should.
#### Usage
Beautifies a java file.
`./java-beautifier.py <a java file>`
#### Example
1. Create `Main.java` with the content
```java
public class Main {
public static void main(String[] args) {
System.out.println("I'm so beautiful");
}
public static class UnnecessarySubClass {
public static void unnecessaryVoid() {
System.out.println("Hello, i'm unnecessary");
}
}
}
```
2. Beautify the file: `./java-beautifier.py Main.java`
3. Look at the beautiful output
```java
public class Main {
public static void main(String[] args) {
System.out.println("I'm so beautiful"); }
public static class UnnecessarySubClass {
public static void unnecessaryVoid() {
System.out.println("Hello, i'm unnecessary"); }}}
```

View File

@ -0,0 +1,30 @@
#!/usr/bin/python3
import sys
if __name__ == '__main__':
file = sys.argv[-1]
lenght = 0
content = []
new_content = []
for line in open(file, 'r'):
line = line.strip()
if line:
if len(line) > lenght:
lenght = len(line)
content.append(line)
lenght = lenght + 2
for i, line in enumerate(content):
if line and line[-1] in ('{', '}'):
if len(line.strip()) == 1 and i != 0:
new_content[-1] += ' ' * (lenght - len(new_content[-1]) - 1) + line[-1]
else:
new_content.append(line[:-1] + (' ' * (lenght - len(line))) + line[-1])
else:
new_content.append(line)
sys.stdout.write('\n'.join(new_content))