diff --git a/build.gradle b/build.gradle index 3e7c874..ecd8f8e 100644 --- a/build.gradle +++ b/build.gradle @@ -12,10 +12,3 @@ repositories { dependencies { compile (['jline:jline:2.13']) } - -sourceSets { - main { - java.srcDirs = ['src'] - resources.srcDirs = ['resources'] - } -} diff --git a/src/ru/dmitriymx/shell/CommandCompleter.java b/src/main/java/ru/dmitriymx/shell/CommandCompleter.java similarity index 97% rename from src/ru/dmitriymx/shell/CommandCompleter.java rename to src/main/java/ru/dmitriymx/shell/CommandCompleter.java index 2251b11..7943a58 100644 --- a/src/ru/dmitriymx/shell/CommandCompleter.java +++ b/src/main/java/ru/dmitriymx/shell/CommandCompleter.java @@ -1,33 +1,33 @@ -package ru.dmitriymx.shell; - -import jline.console.completer.ArgumentCompleter; -import jline.console.completer.Completer; -import jline.console.completer.StringsCompleter; - -import java.util.List; -import java.util.Set; - -public class CommandCompleter implements Completer { - private ArgumentCompleter.ArgumentDelimiter delimiter = new ArgumentCompleter.WhitespaceArgumentDelimiter(); - private Completer commandNamesCompleter; - - public CommandCompleter(Set commandList) { - commandNamesCompleter = new StringsCompleter(commandList); - } - - @Override - public int complete(String buffer, int cursor, List candidates) { - ArgumentCompleter.ArgumentList parseCommandLine = parseLine(buffer, cursor); - int cursorArgument = parseCommandLine.getCursorArgumentIndex(); - - if (cursorArgument < 0) { - return -1; - } else { - return commandNamesCompleter.complete(buffer, cursor, candidates); - } - } - - public ArgumentCompleter.ArgumentList parseLine(String buffer, int cursor) { - return delimiter.delimit(buffer, cursor); - } +package ru.dmitriymx.shell; + +import jline.console.completer.ArgumentCompleter; +import jline.console.completer.Completer; +import jline.console.completer.StringsCompleter; + +import java.util.List; +import java.util.Set; + +public class CommandCompleter implements Completer { + private ArgumentCompleter.ArgumentDelimiter delimiter = new ArgumentCompleter.WhitespaceArgumentDelimiter(); + private Completer commandNamesCompleter; + + public CommandCompleter(Set commandList) { + commandNamesCompleter = new StringsCompleter(commandList); + } + + @Override + public int complete(String buffer, int cursor, List candidates) { + ArgumentCompleter.ArgumentList parseCommandLine = parseLine(buffer, cursor); + int cursorArgument = parseCommandLine.getCursorArgumentIndex(); + + if (cursorArgument < 0) { + return -1; + } else { + return commandNamesCompleter.complete(buffer, cursor, candidates); + } + } + + public ArgumentCompleter.ArgumentList parseLine(String buffer, int cursor) { + return delimiter.delimit(buffer, cursor); + } } \ No newline at end of file diff --git a/src/ru/dmitriymx/shell/IShellCommand.java b/src/main/java/ru/dmitriymx/shell/IShellCommand.java similarity index 94% rename from src/ru/dmitriymx/shell/IShellCommand.java rename to src/main/java/ru/dmitriymx/shell/IShellCommand.java index 0c102dc..7187ffa 100644 --- a/src/ru/dmitriymx/shell/IShellCommand.java +++ b/src/main/java/ru/dmitriymx/shell/IShellCommand.java @@ -1,8 +1,8 @@ -package ru.dmitriymx.shell; - -public interface IShellCommand { - - public String getName(); - - public void execute(final String[] args); -} +package ru.dmitriymx.shell; + +public interface IShellCommand { + + public String getName(); + + public void execute(final String[] args); +} diff --git a/src/ru/dmitriymx/shell/Shell.java b/src/main/java/ru/dmitriymx/shell/Shell.java similarity index 96% rename from src/ru/dmitriymx/shell/Shell.java rename to src/main/java/ru/dmitriymx/shell/Shell.java index 0100360..9be3f84 100644 --- a/src/ru/dmitriymx/shell/Shell.java +++ b/src/main/java/ru/dmitriymx/shell/Shell.java @@ -1,124 +1,124 @@ -package ru.dmitriymx.shell; - -import jline.console.ConsoleReader; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - * Командная оболочка - */ -public class Shell implements Runnable { - private Thread shellThread; - private String prompt; - private Map commandList = new HashMap<>(); - private CommandCompleter commandCompleter; - private final String[] emptyArray = new String[0]; - protected ConsoleReader cReader; - protected boolean runned = false; - - /** - * Создание командной оболочки - * @throws IOException - */ - public Shell() throws IOException { - cReader = new ConsoleReader(System.in, System.out); - cReader.setExpandEvents(false); - } - - /** - * Установить текст приглашения - * @param prompt - */ - public void setPrompt(String prompt) { - this.prompt = prompt; - } - - /** - * Добавить команду - * @param commandName имя команды - * @param command команда - */ - public void addCommand(String commandName, IShellCommand command) { - commandList.put(commandName.toLowerCase(), command); - } - - /** - * Удаление команды - * @param command удаляемая команда - */ - public void removeCommand(String commandName, IShellCommand command) { - commandList.remove(commandName); - } - - /** - * Запуск командной оболочки - */ - public void start() { - shellThread = new Thread(this, "Shell Thread"); - try { - runned = true; - shellThread.join(); - shellThread.start(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - /** - * Остановка командной оболочки - */ - public void stop() { - shellThread.interrupt(); - } - - /** - * Обработчик входящих комманд - */ - @Override - public void run() { - cReader.setPrompt(prompt); - commandCompleter = new CommandCompleter(commandList.keySet()); - cReader.addCompleter(commandCompleter); - Thread currentThread = Thread.currentThread(); - - String line; - try { - while (!currentThread.isInterrupted() && (line = cReader.readLine()) != null) { - String[] parseLine = commandCompleter.parseLine(line, cReader.getCursorBuffer().cursor).getArguments(); - if (parseLine.length == 0) { - continue; - } - - String commandName = parseLine[0].toLowerCase(); - if (commandList.containsKey(commandName)) { - IShellCommand command = commandList.get(commandName); - - if (parseLine.length == 1) { - command.execute(emptyArray); - } else { - String[] args = new String[parseLine.length - 1]; - System.arraycopy(parseLine, 1, args, 0, args.length); - command.execute(args); - } - - continue; - } - - System.err.println(String.format("Unknow command \"%s\"", commandName)); - - try { - Thread.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } catch (IOException e) { - System.err.println("Shell exception"); - e.printStackTrace(); - } - - cReader.removeCompleter(commandCompleter); - } -} +package ru.dmitriymx.shell; + +import jline.console.ConsoleReader; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * Командная оболочка + */ +public class Shell implements Runnable { + private Thread shellThread; + private String prompt; + private Map commandList = new HashMap<>(); + private CommandCompleter commandCompleter; + private final String[] emptyArray = new String[0]; + protected ConsoleReader cReader; + protected boolean runned = false; + + /** + * Создание командной оболочки + * @throws IOException + */ + public Shell() throws IOException { + cReader = new ConsoleReader(System.in, System.out); + cReader.setExpandEvents(false); + } + + /** + * Установить текст приглашения + * @param prompt + */ + public void setPrompt(String prompt) { + this.prompt = prompt; + } + + /** + * Добавить команду + * @param commandName имя команды + * @param command команда + */ + public void addCommand(String commandName, IShellCommand command) { + commandList.put(commandName.toLowerCase(), command); + } + + /** + * Удаление команды + * @param command удаляемая команда + */ + public void removeCommand(String commandName, IShellCommand command) { + commandList.remove(commandName); + } + + /** + * Запуск командной оболочки + */ + public void start() { + shellThread = new Thread(this, "Shell Thread"); + try { + runned = true; + shellThread.join(); + shellThread.start(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + /** + * Остановка командной оболочки + */ + public void stop() { + shellThread.interrupt(); + } + + /** + * Обработчик входящих комманд + */ + @Override + public void run() { + cReader.setPrompt(prompt); + commandCompleter = new CommandCompleter(commandList.keySet()); + cReader.addCompleter(commandCompleter); + Thread currentThread = Thread.currentThread(); + + String line; + try { + while (!currentThread.isInterrupted() && (line = cReader.readLine()) != null) { + String[] parseLine = commandCompleter.parseLine(line, cReader.getCursorBuffer().cursor).getArguments(); + if (parseLine.length == 0) { + continue; + } + + String commandName = parseLine[0].toLowerCase(); + if (commandList.containsKey(commandName)) { + IShellCommand command = commandList.get(commandName); + + if (parseLine.length == 1) { + command.execute(emptyArray); + } else { + String[] args = new String[parseLine.length - 1]; + System.arraycopy(parseLine, 1, args, 0, args.length); + command.execute(args); + } + + continue; + } + + System.err.println(String.format("Unknow command \"%s\"", commandName)); + + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } catch (IOException e) { + System.err.println("Shell exception"); + e.printStackTrace(); + } + + cReader.removeCompleter(commandCompleter); + } +}