Skip to content

Commit 0a8110f

Browse files
committed
Added JsonParsing, FileSharing, Renaming, Changed Errormessaging
1 parent cb2af2e commit 0a8110f

19 files changed

+506
-20
lines changed

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ shadowJar {
2525

2626
repositories {
2727
mavenCentral()
28+
maven { url 'https://jitpack.io' }
2829
}
2930

3031
dependencies {
3132
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
3233
implementation group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
33-
34+
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
35+
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
36+
3437
implementation 'com.google.code.gson:gson:2.10.1'
3538
implementation group: 'net.sf.jopt-simple', name: 'jopt-simple', version: '4.7'
3639
implementation 'org.reflections:reflections:0.10.2'
40+
41+
implementation 'com.google.guava:guava:31.1-jre'
3742
}

src/main/java/de/jo/ConsoleTools.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package de.jo;
22

33
import de.jo.modules.Module;
4-
import de.jo.modules.ModuleInfo;
54
import de.jo.modules.ModuleManager;
65
import de.jo.modules.impl.other.ModuleHelp;
7-
import de.jo.options.Option;
86
import de.jo.options.Options;
97
import de.jo.util.ConsoleColors;
108
import de.jo.util.Files;
11-
import de.jo.util.PackageScanner;
129
import de.jo.util.Strings;
10+
import de.jo.web.FileServer;
1311
import joptsimple.OptionSet;
14-
import joptsimple.OptionSpec;
1512

1613
import java.io.File;
17-
import java.io.IOException;
1814
import java.io.PrintStream;
1915
import java.util.*;
2016
import java.util.regex.Matcher;
@@ -33,7 +29,7 @@ public class ConsoleTools {
3329

3430
public File currentDirectory = new File(System.getProperty("user.dir"));
3531

36-
public ConsoleTools(String[] args) {
32+
public ConsoleTools(String[] args) throws Exception{
3733
ConsoleTools.instance = this;
3834
this.options = new Options();
3935
this.manager = new ModuleManager();

src/main/java/de/jo/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import joptsimple.OptionSet;
1111
import org.apache.log4j.BasicConfigurator;
1212

13+
import java.io.IOException;
1314
import java.io.PrintStream;
1415
import java.util.Arrays;
1516

@@ -19,7 +20,7 @@
1920
*/
2021
public class Main {
2122

22-
public static void main(String[] args) {
23+
public static void main(String[] args) throws Exception {
2324
BasicConfigurator.configure();
2425
new ConsoleTools(args);
2526
}

src/main/java/de/jo/modules/impl/files/ModuleChangeDirectory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ public class ModuleChangeDirectory implements Module {
1818
public void run(String... args) throws Exception {
1919
if(args.length == 0) {
2020
System.out.println(ConsoleColors.YELLOW_BRIGHT+"> "+ConsoleColors.YELLOW+ConsoleTools.instance().currentDirectory.getCanonicalFile());
21-
} else {
21+
} else if(args.length == 1){
2222
String fileName = args[0];
2323
// D: --> absolut path
2424
File newDir = fileName.charAt(1) == ':' ? new File(fileName) : new File(ConsoleTools.instance().currentDirectory, fileName);
2525
if(!newDir.exists() || !newDir.isDirectory()) {
2626
Strings.error("Invalid destination: "+newDir.getCanonicalFile());
27+
Strings.moduleError(this, 27);
2728
return;
2829
}
2930
ConsoleTools.instance().currentDirectory = newDir.getCanonicalFile();
3031
System.out.println(ConsoleColors.YELLOW_BRIGHT+"> "+ConsoleColors.YELLOW+newDir.getCanonicalFile());
32+
}else {
33+
Strings.error("Invalid usage");
3134
}
3235
}
3336
}

src/main/java/de/jo/modules/impl/files/ModuleConcatenate.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void run(String... args) {
2121
String filename = Files.parseFile(args[0]);
2222
if(!new File(filename).exists()) {
2323
Strings.error("File: \""+filename+"\" not found");
24-
Strings.error(new StackTraceElement("ModuleCat", "run", "ModuleCat.java", 24).toString());
24+
Strings.moduleError(this, 24);
2525
return;
2626
}
2727
List<String> lines = Files.lines(filename);
@@ -35,13 +35,13 @@ public void run(String... args) {
3535
System.out.println(out);
3636
}
3737
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
38-
}else if(args.length > 1){
38+
}else if(args.length == 2){
3939
try {
4040
String filename = Files.parseFile(args[0]);
4141
Integer line = Integer.parseInt(args[1]);
4242
if(!new File(filename).exists()) {
4343
Strings.error("File: \""+filename+"\" not found");
44-
Strings.error(new StackTraceElement("ModuleCat", "run", "ModuleCat.java", 24).toString());
44+
Strings.moduleError(this, 44);
4545
return;
4646
}
4747
List<String> lines = Files.lines(filename);
@@ -50,7 +50,10 @@ public void run(String... args) {
5050
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
5151
} catch(Exception ex) {
5252
Strings.error("Invalid line number");
53+
Strings.moduleError(this, 53);
5354
}
55+
}else {
56+
Strings.error("Invalid usage");
5457
}
5558
}
5659
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package de.jo.modules.impl.files;
2+
3+
import de.jo.modules.Module;
4+
import de.jo.modules.ModuleInfo;
5+
import de.jo.modules.impl.other.ModuleHelp;
6+
import de.jo.util.ConsoleColors;
7+
import de.jo.util.Files;
8+
import de.jo.util.Strings;
9+
10+
import java.io.File;
11+
12+
/**
13+
* @author Johannes Hans 29.10.2023
14+
* @Project ConsoleTools
15+
*/
16+
@ModuleInfo(name = "json", description = "Prints beautified json to the console", syntax = "<--f|--s> <File|String>")
17+
public class ModuleJson implements Module {
18+
@Override
19+
public void run(String... args) throws Exception {
20+
if(args.length == 2) {
21+
if(args[0].equals("--f")) {
22+
File file = new File(Files.parseFile(args[1]));
23+
if(!file.exists()) {
24+
Strings.error("File: \""+file.getCanonicalPath()+"\" not found");
25+
Strings.error(Strings.errorMessage(this, "run", 44));
26+
return;
27+
}
28+
String json = Strings.lines(Files.lines(file));
29+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
30+
System.out.println(Strings.json(json));
31+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
32+
}else if(args[0].equals("--t")) {
33+
String json = args[1];
34+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
35+
System.out.println(Strings.json(json));
36+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
37+
}else {
38+
Strings.error("Invalid argument");
39+
Strings.error(ModuleHelp.moduleInfo(this));
40+
}
41+
}else {
42+
Strings.error("Invalid usage");
43+
}
44+
}
45+
}

src/main/java/de/jo/modules/impl/files/ModuleList.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void run(String... args) throws Exception {
2121
File dir = new File(Files.parseFile(args[0]));
2222
if(!dir.exists() || !dir.isDirectory()) {
2323
Strings.error("File needs to be a directory");
24+
Strings.moduleError(this, 24);
2425
return;
2526
}
2627
int longestLength = dir.getCanonicalPath().length();
@@ -35,6 +36,8 @@ public void run(String... args) throws Exception {
3536
int spaces = longestLength-file.getName().length()+5;
3637
System.out.println(ConsoleColors.YELLOW_BRIGHT+" L___ "+ConsoleColors.YELLOW+file.getName()+Strings.repeat(spaces, " ")+ConsoleColors.YELLOW_BRIGHT+"| "+ConsoleColors.RED_BRIGHT+filesize);
3738
}
39+
}else {
40+
Strings.error("Invalid usage");
3841
}
3942
}
4043

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.jo.modules.impl.files;
2+
3+
import de.jo.modules.Module;
4+
import de.jo.modules.ModuleInfo;
5+
import de.jo.util.Files;
6+
import de.jo.util.Strings;
7+
8+
import java.io.File;
9+
10+
/**
11+
* @author Johannes Hans 29.10.2023
12+
* @Project ConsoleTools
13+
*/
14+
@ModuleInfo(name = "rename", description = "Renames a file", aliases = "rn", syntax = "<File> <Name>")
15+
public class ModuleRename implements Module {
16+
@Override
17+
public void run(String... args) throws Exception {
18+
if(args.length == 2) {
19+
File file = new File(Files.parseFile(args[0]));
20+
if(!file.exists()) {
21+
Strings.error("Invalid file: "+file.getCanonicalFile());
22+
Strings.moduleError(this, 22);
23+
return;
24+
}
25+
String name = args[1];
26+
if(!Files.isValidFileName(name)) {
27+
Strings.error("Invalid filename: "+name);
28+
Strings.moduleError(this, 28);
29+
return;
30+
}
31+
file.renameTo(new File(file.getParent(), name));
32+
}else {
33+
Strings.error("Invalid usage");
34+
}
35+
}
36+
}

src/main/java/de/jo/modules/impl/files/ModuleScan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void run(String... args) throws IOException, InterruptedException {
2323
String searchterm = args[1];
2424
if(!new File(filename).exists()) {
2525
Strings.error("File: \""+filename+"\" not found");
26-
Strings.error(new StackTraceElement("ModuleCat", "run", "ModuleCat.java", 24).toString());
26+
Strings.moduleError(this, 26);
2727
return;
2828
}
2929
List<String> lines = Files.lines(filename);

src/main/java/de/jo/modules/impl/other/ModuleClearScreen.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
public class ModuleClearScreen implements Module {
1616
@Override
1717
public void run(String... args) throws IOException, InterruptedException {
18-
for (int i = 0; i < 2; i++) {
19-
Strings.clearScreen();
18+
if(args.length == 0) {
19+
for (int i = 0; i < 2; i++) {
20+
Strings.clearScreen();
21+
}
22+
ConsoleTools.instance().logo();
23+
}else {
24+
Strings.error("Invalid usage");
2025
}
21-
ConsoleTools.instance().logo();
2226
}
2327
}

src/main/java/de/jo/modules/impl/other/ModuleExit.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import de.jo.modules.Module;
44
import de.jo.modules.ModuleInfo;
5+
import de.jo.util.Strings;
56

67
/**
78
* @author Johannes Hans 27.10.2023
@@ -15,12 +16,14 @@ public class ModuleExit implements Module {
1516
public void run(String... args) {
1617
if(args.length == 0) {
1718
System.exit(0);
18-
}else {
19+
}else if(args.length == 1){
1920
try {
2021
System.exit(Integer.parseInt(args[0]));
2122
} catch(Exception ex) {
2223
System.exit(0);
2324
}
25+
}else {
26+
Strings.error("Invalid usage");
2427
}
2528
}
2629
}

src/main/java/de/jo/modules/impl/other/ModuleHelp.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,30 @@ public static void printModuleInfo(Module module) {
4242
System.out.println(ConsoleColors.YELLOW_BOLD_BRIGHT + syntax);
4343
}
4444
}
45+
46+
public static String moduleInfo(Module module) {
47+
ModuleInfo info = ConsoleTools.instance().manager().moduleInfo(module);
48+
49+
String out = "";
50+
51+
out = ConsoleColors.YELLOW_BOLD_BRIGHT + info.name() + ConsoleColors.YELLOW + " - " + info.description() + "\n";
52+
//print aliases
53+
if (info.aliases().length > 0) {
54+
StringBuilder aliases = new StringBuilder(ConsoleColors.YELLOW_BOLD_BRIGHT + " aliases: " + ConsoleColors.YELLOW);
55+
for (int i = 0; i < info.aliases().length; i++) {
56+
if (i > 0) {
57+
aliases.append(", ").append("\"").append(info.aliases()[i]).append("\"");
58+
} else {
59+
aliases.append("\"").append(info.aliases()[i]).append("\"");
60+
}
61+
}
62+
out += ConsoleColors.YELLOW_BOLD_BRIGHT + aliases + "\n";
63+
}
64+
//print syntax
65+
if (!info.syntax().isEmpty()) {
66+
StringBuilder syntax = new StringBuilder(ConsoleColors.YELLOW_BOLD_BRIGHT + " usage: " + info.name() + " " + info.syntax() + ConsoleColors.YELLOW);
67+
out += ConsoleColors.YELLOW_BOLD_BRIGHT + syntax + "\n";
68+
}
69+
return out;
70+
}
4571
}

0 commit comments

Comments
 (0)