Skip to content

Commit 69968e1

Browse files
committed
Added ModuleCd, Changed behaviour of file paths, changed logo rendering
1 parent 56bcd07 commit 69968e1

File tree

7 files changed

+50
-7
lines changed

7 files changed

+50
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import joptsimple.OptionSet;
1313
import joptsimple.OptionSpec;
1414

15+
import java.io.File;
1516
import java.io.IOException;
1617
import java.io.PrintStream;
1718
import java.util.*;
@@ -29,6 +30,8 @@ public class ConsoleTools {
2930

3031
private static ConsoleTools instance;
3132

33+
public File currentDirectory = new File(System.getProperty("user.dir"));
34+
3235
public ConsoleTools(String[] args) {
3336
ConsoleTools.instance = this;
3437
this.options = new Options();
@@ -63,8 +66,10 @@ public ConsoleTools(String[] args) {
6366

6467
public void logo() {
6568
System.out.println(ConsoleColors.BLACK_BACKGROUND+" "+ConsoleColors.RESET);
69+
boolean b = false;
6670
for(String s : Files.sysLines("logo.txt")) {
67-
System.out.println(ConsoleColors.BLACK_BACKGROUND+ConsoleColors.BLUE_BOLD_BRIGHT+s+ConsoleColors.RESET);
71+
System.out.println((b ? ConsoleColors.YELLOW : ConsoleColors.YELLOW_BRIGHT)+ConsoleColors.BLACK_BACKGROUND+s+ConsoleColors.RESET);
72+
b = !b;
6873
}
6974
System.out.println(ConsoleColors.BLACK_BACKGROUND+" "+ConsoleColors.RESET);
7075
ConsoleColors.reset();

src/main/java/de/jo/modules/impl/ModuleCat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
* @author Johannes Hans 27.10.2023
1414
* @Project ConsoleTools
1515
*/
16-
@ModuleInfo(name = "cat", description = "Prints the contents of a file to the console", syntax = "<File> <Line(Optional)>")
16+
@ModuleInfo(name = "cat", description = "Prints the contents of a file to the console", syntax = "($/)<File> <Line(Optional)>")
1717
public class ModuleCat implements Module {
1818
@Override
1919
public void run(String... args) {
2020
if(args.length == 1) {
21-
String filename = args[0];
21+
String filename = Files.parseFile(args[0]);
2222
if(!new File(filename).exists()) {
2323
Strings.error("File: \""+filename+"\" not found");
2424
Strings.error(new StackTraceElement("ModuleCat", "run", "ModuleCat.java", 24).toString());
@@ -37,7 +37,7 @@ public void run(String... args) {
3737
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
3838
}else if(args.length > 1){
3939
try {
40-
String filename = args[0];
40+
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");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.jo.modules.impl;
2+
3+
import de.jo.ConsoleTools;
4+
import de.jo.modules.Module;
5+
import de.jo.modules.ModuleInfo;
6+
import de.jo.util.ConsoleColors;
7+
import de.jo.util.Strings;
8+
9+
import java.io.File;
10+
11+
/**
12+
* @author Johannes Hans 28.10.2023
13+
* @Project ConsoleTools
14+
*/
15+
@ModuleInfo(name = "changedirectory", description = "Changes the current directory ($/) to another", aliases = {"changedir", "cd"}, syntax = "<Path>")
16+
public class ModuleCd implements Module {
17+
@Override
18+
public void run(String... args) throws Exception {
19+
if(args.length > 0) {
20+
String fileName = args[0];
21+
// D: --> absolut path
22+
File newDir = fileName.charAt(1) == ':' ? new File(fileName) : new File(ConsoleTools.instance().currentDirectory, fileName);
23+
if(!newDir.exists() || !newDir.isDirectory()) {
24+
Strings.error("Invalid destination: "+newDir.getCanonicalFile());
25+
return;
26+
}
27+
ConsoleTools.instance().currentDirectory = newDir.getCanonicalFile();
28+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"> "+ConsoleColors.YELLOW+newDir.getCanonicalFile());
29+
}
30+
}
31+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* @author Johannes Hans 28.10.2023
1515
* @Project ConsoleTools
1616
*/
17-
@ModuleInfo(name = "scan", description = "Scans a file for a certain word", aliases = {"scn"}, syntax = "<File> <Term>")
17+
@ModuleInfo(name = "scan", description = "Scans a file for a certain word", aliases = {"scn"}, syntax = "($/)<File> <Term>")
1818
public class ModuleScan implements Module {
1919
@Override
2020
public void run(String... args) throws IOException, InterruptedException {
2121
if(args.length > 1){
22-
String filename = args[0];
22+
String filename = Files.parseFile(args[0]);
2323
String searchterm = args[1];
2424
if(!new File(filename).exists()) {
2525
Strings.error("File: \""+filename+"\" not found");

src/main/java/de/jo/modules/impl/ModuleTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ public class ModuleTest implements Module {
1616

1717
@Override
1818
public void run(String... args) throws IOException, InterruptedException {
19-
System.out.println(Arrays.asList(args));
2019
}
2120
}

src/main/java/de/jo/util/Files.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.jo.util;
22

3+
import de.jo.ConsoleTools;
4+
35
import java.io.*;
46
import java.nio.charset.StandardCharsets;
57
import java.nio.file.Paths;
@@ -62,4 +64,9 @@ public static boolean write(List<String> lines, String file) {
6264
return write(lines, new File(file));
6365
}
6466

67+
public static String parseFile(String arg) {
68+
arg = arg.replace("$", ConsoleTools.instance().currentDirectory.getAbsolutePath());
69+
return arg;
70+
}
71+
6572
}

src/main/resources/logo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| |
33
| |
44
| |
5+
| |
56
| ________ _________ ________ ________ ___ ________ |
67
| |\ ____\ |\___ ___\ |\ __ \ |\ __ \ |\ \ |\ ____\ |
78
| \ \ \___| ____________ \|___ \ \_| \ \ \|\ \ \ \ \|\ \ \ \ \ \ \ \___|_ |

0 commit comments

Comments
 (0)