Added tab-completion and cli-arguments
This commit is contained in:
@@ -2,32 +2,59 @@ package org.cametendo;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.impl.completer.FileNameCompleter;
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
|
||||
public class FilePathAdd {
|
||||
|
||||
public static String ImagePath = "";
|
||||
|
||||
protected static String filePath(Scanner UserInput) {
|
||||
protected static String filePath() throws IOException {
|
||||
fileQuestion();
|
||||
|
||||
Terminal terminal = TerminalBuilder.terminal();
|
||||
LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(new FileNameCompleter()).build();
|
||||
|
||||
while (true) {
|
||||
ImagePath = UserInput.nextLine();
|
||||
ImagePath = reader.readLine("Path: ").trim();
|
||||
|
||||
if (ImagePath.isBlank()) {
|
||||
System.out.println("Oops.. You didn't specify a file, did you missclick?");
|
||||
System.out.println("Oops... You didn't specify a file!");
|
||||
continue;
|
||||
}
|
||||
|
||||
Path path = Path.of(ImagePath);
|
||||
try {
|
||||
Files.readAttributes(path, "basic:size");
|
||||
System.out.println("Using File: " + ImagePath);
|
||||
return ImagePath;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to access file, invalid path or no access to file! Please try again.");
|
||||
|
||||
if (!Files.exists(path) || !Files.isRegularFile(path)) {
|
||||
System.out.println("Invalid file! Please ensure the path points to an ISO / image file.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("Using File: " + ImagePath);
|
||||
return ImagePath;
|
||||
}
|
||||
}
|
||||
|
||||
public static String validateAndGetFile(String ImagePath) {
|
||||
try {
|
||||
Path path = Path.of(ImagePath);
|
||||
if (Files.exists(path) && Files.isRegularFile(path)) {
|
||||
return path.toRealPath().toString();
|
||||
} else {
|
||||
System.out.println("Invalid file! Please ensure the path points to an ISO / image file.");
|
||||
return null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("File not found! Invalid Path or no access.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected static void fileQuestion() {
|
||||
System.out.println("Please enter the FULL Path of your ISO / Image. (No tab-complete)");
|
||||
System.out.println("Please enter the FULL Path of your ISO / Image. (Tab-completion supported)");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user