Archived
0

Zond: первые наметки коммандной оболочки

This commit is contained in:
2017-06-15 15:05:40 +03:00
parent 82fcdcdfe8
commit d8d80cf6dc
8 changed files with 221 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys' group = 'asys'
version = '0.6.1-SNAPSHOT' version = '0.7.1-SNAPSHOT'
apply plugin: 'application' apply plugin: 'application'
@@ -7,25 +7,11 @@ mainClassName = "asys.zond.Main"
configurations { configurations {
included included
includedEx
compile.extendsFrom included compile.extendsFrom included
compile.extendsFrom includedEx
}
compileJava {
dependsOn ':bridge-protocol:compileJava'
}
def zp(FileTree ft) {
return ft.matching {
exclude 'org/apache/commons/exec/StreamPumper.class'
}
} }
jar { jar {
dependsOn ':bridge-protocol:jar'
dependsOn configurations.included dependsOn configurations.included
dependsOn configurations.includedEx
manifest { manifest {
attributes 'Implementation-Title': 'ASys Zond', attributes 'Implementation-Title': 'ASys Zond',
'Implementation-Version': version, 'Implementation-Version': version,
@@ -33,18 +19,8 @@ jar {
} }
baseName = project.group + '.' + project.name baseName = project.group + '.' + project.name
from { configurations.included.collect { it.isDirectory() ? it : zipTree(it) } } from { configurations.included.collect { it.isDirectory() ? it : zipTree(it) } }
from { configurations.includedEx.collect { it.isDirectory() ? it : zp(zipTree(it)) } }
}
ext {
nettyVersion = '4.0.23.Final'
} }
dependencies { dependencies {
included files(project(':bridge-protocol').sourceSets.main.output.classesDir) included group: 'jline', name: 'jline', version: '2.14.3'
included group: 'org.fusesource.jansi', name: 'jansi', version: '1.11'
includedEx group: 'org.apache.commons', name: 'commons-exec', version: '1.3'
included group: 'io.netty', name: 'netty-codec', version: nettyVersion
included group: 'com.google.guava', name: 'guava', version: '21.0'
included group: 'jline', name: 'jline', version: '2.13'
} }

View File

@@ -1,12 +1,25 @@
/* /*
* DmitriyMX <dimon550@gmail.com> * DmitriyMX <dimon550@gmail.com>
* 2017-06-07 * 2017-06-15
* Idea by Daniil on 2017-06-07 * Idea by Daniil on 2017-06-07
*/ */
package asys.zond; package asys.zond;
public class Main { import asys.zond.shell.Shell;
public static void main(String[] args) {
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new Main().start();
}
private void start() {
Shell shell = Shell.getInstance();
try {
shell.start(System.in);
} catch (Exception e) {
e.printStackTrace();
}
} }
} }

View File

@@ -0,0 +1,9 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
public interface CommandHandler {
void handle(String commandLine);
}

View File

@@ -0,0 +1,35 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
import jline.console.ConsoleReader;
import java.io.IOException;
public class CommandLooper implements Runnable {
private ConsoleReader consoleReader;
private CommandHandler commandHandler;
CommandLooper(ConsoleReader consoleReader, CommandHandler commandHandler) {
this.consoleReader = consoleReader;
this.commandHandler = commandHandler;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
String line;
try {
line = consoleReader.readLine();
} catch (IOException e) {
break;
}
if (line == null) break;
if (line.trim().isEmpty()) continue;
commandHandler.handle(line);
}
}
}

View File

@@ -0,0 +1,12 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
public class EchoCommandHandler implements CommandHandler {
@Override
public void handle(String commandLine) {
Shell.getInstance().getOutput().println(commandLine);
}
}

View File

@@ -0,0 +1,62 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
import jline.console.ConsoleReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public class Shell {
private static final String DEFAULT_PROMPT = ":";
private static Shell instance;
private ConsoleReader consoleReader;
private Thread threadCommandLoop;
private CommandHandler commandHandler;
private ShellStdOut shellStdOut;
public static Shell getInstance() {
if (instance == null) instance = new Shell();
return instance;
}
private Shell() {
}
public void start(InputStream inputStream) throws IOException {
consoleReader = new ConsoleReader(inputStream, (shellStdOut = new ShellStdOut()));
shellStdOut.setConsoleReader(consoleReader);
consoleReader.setPrompt(DEFAULT_PROMPT);
if (commandHandler == null) commandHandler = new EchoCommandHandler();
threadCommandLoop = new Thread(
new CommandLooper(consoleReader, commandHandler),
"Zond shell looper");
threadCommandLoop.start();
}
public void shutdown() {
threadCommandLoop.interrupt();
consoleReader.close();
}
public void setPrompt(String value) {
consoleReader.setPrompt(value);
}
public void resetPrompt() {
consoleReader.setPrompt(DEFAULT_PROMPT);
}
public PrintStream getOutput() {
return shellStdOut;
}
public void setOutputHook(ShellOutputHook outputHook) {
shellStdOut.setOutputHook(outputHook);
}
}

View File

@@ -0,0 +1,9 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
public interface ShellOutputHook {
String handle(String line);
}

View File

@@ -0,0 +1,75 @@
/*
* DmitriyMX <d.mihailov@samson-rus.com>
* 2017-06-15
*/
package asys.zond.shell;
import jline.console.ConsoleReader;
import java.io.PrintStream;
import java.io.PrintWriter;
public class ShellStdOut extends PrintStream {
private ConsoleReader consoleReader;
private PrintWriter writer;
private ShellOutputHook shellOutputHook;
ShellStdOut() {
super(System.out, true);
}
void setConsoleReader(ConsoleReader consoleReader) {
this.consoleReader = consoleReader;
this.writer = new PrintWriter(consoleReader.getOutput());
}
private void _print(String line) {
String newLine;
if (shellOutputHook != null) {
try {
newLine = shellOutputHook.handle(line);
if (newLine == null) return;
else line = newLine;
} catch (Exception ignore) {
}
}
writer.print(ConsoleReader.RESET_LINE);
writer.print(line);
cleanTrashLine(line);
writer.println();
writer.flush();
}
/**
* Очистка печатной строки от мусора
*/
private void cleanTrashLine(String string) {
// очищает полностью строку
if (consoleReader.getCursorBuffer().buffer.length() + consoleReader.getPrompt().length() > string.length()) {
for (int i = string.length(); i <= consoleReader.getCursorBuffer().buffer.length() + 2; i++) {
writer.print(' ');
}
}
}
void setOutputHook(ShellOutputHook shellOutputHook) {
this.shellOutputHook = shellOutputHook;
}
@Override
public void write(byte[] buf, int off, int len) {
if ((char)buf[len-1] == '\n') len--;
_print(new String(buf, off, len));
}
@Override
public void print(String s) {
_print(s);
}
@Override
public void println(String x) {
_print(x);
}
}