Реорганизация проекта
This commit is contained in:
@@ -12,10 +12,3 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
compile (['jline:jline:2.13'])
|
compile (['jline:jline:2.13'])
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets {
|
|
||||||
main {
|
|
||||||
java.srcDirs = ['src']
|
|
||||||
resources.srcDirs = ['resources']
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,33 +1,33 @@
|
|||||||
package ru.dmitriymx.shell;
|
package ru.dmitriymx.shell;
|
||||||
|
|
||||||
import jline.console.completer.ArgumentCompleter;
|
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.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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 Completer commandNamesCompleter;
|
private Completer commandNamesCompleter;
|
||||||
|
|
||||||
public CommandCompleter(Set<String> commandList) {
|
public CommandCompleter(Set<String> commandList) {
|
||||||
commandNamesCompleter = new StringsCompleter(commandList);
|
commandNamesCompleter = new StringsCompleter(commandList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
|
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
|
||||||
ArgumentCompleter.ArgumentList parseCommandLine = parseLine(buffer, cursor);
|
ArgumentCompleter.ArgumentList parseCommandLine = parseLine(buffer, cursor);
|
||||||
int cursorArgument = parseCommandLine.getCursorArgumentIndex();
|
int cursorArgument = parseCommandLine.getCursorArgumentIndex();
|
||||||
|
|
||||||
if (cursorArgument < 0) {
|
if (cursorArgument < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
return commandNamesCompleter.complete(buffer, cursor, candidates);
|
return commandNamesCompleter.complete(buffer, cursor, candidates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArgumentCompleter.ArgumentList parseLine(String buffer, int cursor) {
|
public ArgumentCompleter.ArgumentList parseLine(String buffer, int cursor) {
|
||||||
return delimiter.delimit(buffer, cursor);
|
return delimiter.delimit(buffer, cursor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package ru.dmitriymx.shell;
|
package ru.dmitriymx.shell;
|
||||||
|
|
||||||
public interface IShellCommand {
|
public interface IShellCommand {
|
||||||
|
|
||||||
public String getName();
|
public String getName();
|
||||||
|
|
||||||
public void execute(final String[] args);
|
public void execute(final String[] args);
|
||||||
}
|
}
|
||||||
@@ -1,124 +1,124 @@
|
|||||||
package ru.dmitriymx.shell;
|
package ru.dmitriymx.shell;
|
||||||
|
|
||||||
import jline.console.ConsoleReader;
|
import jline.console.ConsoleReader;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Командная оболочка
|
* Командная оболочка
|
||||||
*/
|
*/
|
||||||
public class Shell implements Runnable {
|
public class Shell implements Runnable {
|
||||||
private Thread shellThread;
|
private Thread shellThread;
|
||||||
private String prompt;
|
private String prompt;
|
||||||
private Map<String, IShellCommand> commandList = new HashMap<>();
|
private Map<String, IShellCommand> commandList = new HashMap<>();
|
||||||
private CommandCompleter commandCompleter;
|
private CommandCompleter commandCompleter;
|
||||||
private final String[] emptyArray = new String[0];
|
private final String[] emptyArray = new String[0];
|
||||||
protected ConsoleReader cReader;
|
protected ConsoleReader cReader;
|
||||||
protected boolean runned = false;
|
protected boolean runned = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Создание командной оболочки
|
* Создание командной оболочки
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public Shell() throws IOException {
|
public Shell() throws IOException {
|
||||||
cReader = new ConsoleReader(System.in, System.out);
|
cReader = new ConsoleReader(System.in, System.out);
|
||||||
cReader.setExpandEvents(false);
|
cReader.setExpandEvents(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Установить текст приглашения
|
* Установить текст приглашения
|
||||||
* @param prompt
|
* @param prompt
|
||||||
*/
|
*/
|
||||||
public void setPrompt(String prompt) {
|
public void setPrompt(String prompt) {
|
||||||
this.prompt = prompt;
|
this.prompt = prompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Добавить команду
|
* Добавить команду
|
||||||
* @param commandName имя команды
|
* @param commandName имя команды
|
||||||
* @param command команда
|
* @param command команда
|
||||||
*/
|
*/
|
||||||
public void addCommand(String commandName, IShellCommand command) {
|
public void addCommand(String commandName, IShellCommand command) {
|
||||||
commandList.put(commandName.toLowerCase(), command);
|
commandList.put(commandName.toLowerCase(), command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Удаление команды
|
* Удаление команды
|
||||||
* @param command удаляемая команда
|
* @param command удаляемая команда
|
||||||
*/
|
*/
|
||||||
public void removeCommand(String commandName, IShellCommand command) {
|
public void removeCommand(String commandName, IShellCommand command) {
|
||||||
commandList.remove(commandName);
|
commandList.remove(commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Запуск командной оболочки
|
* Запуск командной оболочки
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
shellThread = new Thread(this, "Shell Thread");
|
shellThread = new Thread(this, "Shell Thread");
|
||||||
try {
|
try {
|
||||||
runned = true;
|
runned = true;
|
||||||
shellThread.join();
|
shellThread.join();
|
||||||
shellThread.start();
|
shellThread.start();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Остановка командной оболочки
|
* Остановка командной оболочки
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
shellThread.interrupt();
|
shellThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Обработчик входящих комманд
|
* Обработчик входящих комманд
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
cReader.setPrompt(prompt);
|
cReader.setPrompt(prompt);
|
||||||
commandCompleter = new CommandCompleter(commandList.keySet());
|
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 {
|
||||||
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].toLowerCase();
|
String commandName = parseLine[0].toLowerCase();
|
||||||
if (commandList.containsKey(commandName)) {
|
if (commandList.containsKey(commandName)) {
|
||||||
IShellCommand command = commandList.get(commandName);
|
IShellCommand command = commandList.get(commandName);
|
||||||
|
|
||||||
if (parseLine.length == 1) {
|
if (parseLine.length == 1) {
|
||||||
command.execute(emptyArray);
|
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;
|
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) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Shell exception");
|
System.err.println("Shell exception");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
cReader.removeCompleter(commandCompleter);
|
cReader.removeCompleter(commandCompleter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user