Skip to content

Commit 56bcd07

Browse files
committed
Added ScannerModule, Changed Exception for Module.run
1 parent 09f31cc commit 56bcd07

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ TO BE ADDED
1919
UCat - prints url contents
2020
CLS - clears the screen
2121
Exit - exits the program
22-
Help - prints the helpscreen
22+
Help - prints the helpscreen
23+
Scan - scans a file for a keyword

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ConsoleTools(String[] args) {
4343
if(arg.toString().equals("help") || arg.toString().equals("h") || arg.toString().equals("?")) {
4444
try {
4545
this.manager.module("help").run("");
46-
} catch (IOException | InterruptedException e) {
46+
} catch (Exception e) {
4747
throw new RuntimeException(e);
4848
}
4949
}

src/main/java/de/jo/modules/Module.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
*/
99
public interface Module {
1010

11-
public void run(String... args) throws IOException, InterruptedException;
11+
public void run(String... args) throws Exception;
1212

1313
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.jo.modules.impl;
2+
3+
import de.jo.modules.Module;
4+
import de.jo.modules.ModuleInfo;
5+
import de.jo.util.ConsoleColors;
6+
import de.jo.util.Files;
7+
import de.jo.util.Strings;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
/**
14+
* @author Johannes Hans 28.10.2023
15+
* @Project ConsoleTools
16+
*/
17+
@ModuleInfo(name = "scan", description = "Scans a file for a certain word", aliases = {"scn"}, syntax = "<File> <Term>")
18+
public class ModuleScan implements Module {
19+
@Override
20+
public void run(String... args) throws IOException, InterruptedException {
21+
if(args.length > 1){
22+
String filename = args[0];
23+
String searchterm = args[1];
24+
if(!new File(filename).exists()) {
25+
Strings.error("File: \""+filename+"\" not found");
26+
Strings.error(new StackTraceElement("ModuleCat", "run", "ModuleCat.java", 24).toString());
27+
return;
28+
}
29+
List<String> lines = Files.lines(filename);
30+
long startTime = System.currentTimeMillis();
31+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
32+
for(int i = 0; i < lines.size(); i++) {
33+
String s = lines.get(i);
34+
if(s.contains(searchterm.trim())) {
35+
long time = (System.currentTimeMillis()-startTime);
36+
System.out.println(ConsoleColors.YELLOW+"Found keyword in line: "+ConsoleColors.YELLOW_BRIGHT+i+ConsoleColors.YELLOW+" in "+ConsoleColors.YELLOW_BRIGHT+(time <= 100 ? time+"ms" : (time/1000F)+"s"));
37+
}
38+
}
39+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
40+
}else {
41+
Strings.error("Invalid usage");
42+
}
43+
}
44+
}

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

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

3+
import java.util.List;
4+
35
/**
46
* @author Johannes Hans 27.10.2023
57
* @Project ConsoleTools
@@ -149,4 +151,15 @@ public static void error(String string) {
149151
ConsoleColors.reset();
150152
}
151153

154+
public static String lines(List<String> lines) {
155+
StringBuilder sb = new StringBuilder();
156+
157+
for(String s : lines) {
158+
sb.append(s);
159+
sb.append("\n");
160+
}
161+
162+
return sb.toString();
163+
}
164+
152165
}

0 commit comments

Comments
 (0)