Реорганизация кода добавления комманд
This commit is contained in:
@@ -4,23 +4,15 @@ import jline.console.completer.ArgumentCompleter;
|
|||||||
import jline.console.completer.Completer;
|
import jline.console.completer.Completer;
|
||||||
import jline.console.completer.StringsCompleter;
|
import jline.console.completer.StringsCompleter;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Set;
|
||||||
|
|
||||||
public class CommandCompleter implements Completer {
|
public class CommandCompleter implements Completer {
|
||||||
private ArgumentCompleter.ArgumentDelimiter delimiter = new ArgumentCompleter.WhitespaceArgumentDelimiter();
|
private ArgumentCompleter.ArgumentDelimiter delimiter = new ArgumentCompleter.WhitespaceArgumentDelimiter();
|
||||||
private Map<String, IShellCommand> commandMap = new HashMap<>();
|
|
||||||
private Completer commandNamesCompleter;
|
private Completer commandNamesCompleter;
|
||||||
|
|
||||||
public CommandCompleter(List<IShellCommand> commandList) {
|
public CommandCompleter(Set<String> commandList) {
|
||||||
for (IShellCommand command : commandList) {
|
commandNamesCompleter = new StringsCompleter(commandList);
|
||||||
commandMap.put(command.getName(), command);
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] commandNames = new String[commandMap.size()];
|
|
||||||
commandNames = commandMap.keySet().toArray(commandNames);
|
|
||||||
commandNamesCompleter = new StringsCompleter(commandNames);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ package ru.dmitriymx.shell;
|
|||||||
import jline.console.ConsoleReader;
|
import jline.console.ConsoleReader;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Командная оболочка
|
* Командная оболочка
|
||||||
@@ -11,8 +12,9 @@ import java.util.LinkedList;
|
|||||||
public class Shell implements Runnable {
|
public class Shell implements Runnable {
|
||||||
private Thread shellThread;
|
private Thread shellThread;
|
||||||
private String prompt;
|
private String prompt;
|
||||||
private LinkedList<IShellCommand> commandList = new LinkedList<>();
|
private Map<String, IShellCommand> commandList = new HashMap<>();
|
||||||
private CommandCompleter commandCompleter;
|
private CommandCompleter commandCompleter;
|
||||||
|
private final String[] emptyArray = new String[0];
|
||||||
protected ConsoleReader cReader;
|
protected ConsoleReader cReader;
|
||||||
protected boolean runned = false;
|
protected boolean runned = false;
|
||||||
|
|
||||||
@@ -35,37 +37,19 @@ public class Shell implements Runnable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Добавить команду
|
* Добавить команду
|
||||||
|
* @param commandName имя команды
|
||||||
* @param command команда
|
* @param command команда
|
||||||
*/
|
*/
|
||||||
public void addCommand(IShellCommand command) {
|
public void addCommand(String commandName, IShellCommand command) {
|
||||||
commandList.add(command);
|
commandList.put(commandName.toLowerCase(), command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Удаление команды
|
* Удаление команды
|
||||||
* @param command удаляемая команда
|
* @param command удаляемая команда
|
||||||
*/
|
*/
|
||||||
public void removeCommand(IShellCommand command) {
|
public void removeCommand(String commandName, IShellCommand command) {
|
||||||
commandList.remove(command);
|
commandList.remove(commandName);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Удаление команды по её имени
|
|
||||||
* @param commandName имя команды
|
|
||||||
*/
|
|
||||||
public void removeCommand(String commandName) {
|
|
||||||
IShellCommand foundCmd = null;
|
|
||||||
|
|
||||||
for (IShellCommand command : commandList) {
|
|
||||||
if(command.getName().equalsIgnoreCase(commandName)) {
|
|
||||||
foundCmd = command;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (foundCmd != null) {
|
|
||||||
commandList.remove(foundCmd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,33 +79,35 @@ public class Shell implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
cReader.setPrompt(prompt);
|
cReader.setPrompt(prompt);
|
||||||
commandCompleter = new CommandCompleter(commandList);
|
commandCompleter = new CommandCompleter(commandList.keySet());
|
||||||
cReader.addCompleter(commandCompleter);
|
cReader.addCompleter(commandCompleter);
|
||||||
Thread currentThread = Thread.currentThread();
|
Thread currentThread = Thread.currentThread();
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
try {
|
try {
|
||||||
readerLoop:
|
|
||||||
while (!currentThread.isInterrupted() && (line = cReader.readLine()) != null) {
|
while (!currentThread.isInterrupted() && (line = cReader.readLine()) != null) {
|
||||||
String[] parseLine = commandCompleter.parseLine(line, cReader.getCursorBuffer().cursor).getArguments();
|
String[] parseLine = commandCompleter.parseLine(line, cReader.getCursorBuffer().cursor).getArguments();
|
||||||
if (parseLine.length == 0) {
|
if (parseLine.length == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String commandName = parseLine[0];
|
String commandName = parseLine[0].toLowerCase();
|
||||||
for (IShellCommand command : commandList) {
|
if (commandList.containsKey(commandName)) {
|
||||||
if (command.getName().equalsIgnoreCase(commandName)) {
|
IShellCommand command = commandList.get(commandName);
|
||||||
|
|
||||||
if (parseLine.length == 1) {
|
if (parseLine.length == 1) {
|
||||||
command.execute(null);
|
command.execute(emptyArray);
|
||||||
} else {
|
} else {
|
||||||
String[] args = new String[parseLine.length - 1];
|
String[] args = new String[parseLine.length - 1];
|
||||||
System.arraycopy(parseLine, 1, args, 0, args.length);
|
System.arraycopy(parseLine, 1, args, 0, args.length);
|
||||||
command.execute(args);
|
command.execute(args);
|
||||||
}
|
}
|
||||||
continue readerLoop;
|
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(String.format("Unknow command \"%s\"", commandName));
|
System.err.println(String.format("Unknow command \"%s\"", commandName));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1);
|
Thread.sleep(1);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user