Skip to content

Commit

Permalink
Update JLine to version 2.14.6.
Browse files Browse the repository at this point in the history
  • Loading branch information
archiecobbs committed Apr 10, 2018
1 parent e2e4e7e commit 939d63a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 56 deletions.
Expand Up @@ -28,16 +28,17 @@
* <p>
* This class can be used to implement an embedded CLI console within an application, accessed via telnet. For example:
* <pre>
* // Accept a new connection
* final Socket socket = ...
* // Accept a new connection and setup telnet CLI console
* try (final Socket socket = /* accept new TCP connection *&#47;;
* final TelnetConsole console = TelnetConsole.create(database, socket)) {
*
* // Set up telnet CLI console
* final TelnetConsole console = TelnetConsole.create(database, socket);
* console.getSession().loadFunctionsFromClasspath();
* console.getSession().loadCommandsFromClasspath();
* // Install commands and functions
* console.getSession().loadFunctionsFromClasspath();
* console.getSession().loadCommandsFromClasspath();
*
* // Run the console
* console.run();
* // Run the console
* console.run();
* }
* </pre>
*/
public final class TelnetConsole extends Console {
Expand Down Expand Up @@ -223,6 +224,16 @@ public void setEchoEnabled(boolean enabled) {
public String getOutputEncoding() {
return this.terminal.getOutputEncoding();
}

@Override
public void enableInterruptCharacter() {
this.terminal.enableInterruptCharacter();
}

@Override
public void disableInterruptCharacter() {
this.terminal.disableInterruptCharacter();
}
}

// TelnetTerminal
Expand Down
11 changes: 9 additions & 2 deletions permazen-cli/src/main/java/io/permazen/cli/Console.java
Expand Up @@ -15,6 +15,7 @@
import io.permazen.parse.ParseException;
import io.permazen.util.ParseContext;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -41,7 +42,7 @@
/**
* CLI console.
*/
public class Console {
public class Console implements Closeable {

protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected final ConsoleReader console;
Expand Down Expand Up @@ -345,7 +346,6 @@ public void run() throws IOException {
if (this.history != null)
this.history.flush();
this.console.flush();
this.console.shutdown();
}
}

Expand Down Expand Up @@ -402,6 +402,13 @@ public static Terminal getTerminal() throws IOException {
return TerminalFactory.get();
}

// Closeable

@Override
public void close() {
this.console.close();
}

// ConsoleCompleter

private class ConsoleCompleter implements Completer {
Expand Down
94 changes: 49 additions & 45 deletions permazen-cliapp/src/main/java/io/permazen/cli/app/Main.java
Expand Up @@ -144,69 +144,73 @@ public int run(String[] args) throws Exception {
}

// Set up console
final Console console;
final Console console0;
switch (this.mode) {
case KEY_VALUE:
console = new Console(db.getKVDatabase(), new FileInputStream(FileDescriptor.in), System.out);
console0 = new Console(db.getKVDatabase(), new FileInputStream(FileDescriptor.in), System.out);
break;
case CORE_API:
console = new Console(db, new FileInputStream(FileDescriptor.in), System.out);
console0 = new Console(db, new FileInputStream(FileDescriptor.in), System.out);
break;
case PERMAZEN:
console = new Console(jdb, new FileInputStream(FileDescriptor.in), System.out);
console0 = new Console(jdb, new FileInputStream(FileDescriptor.in), System.out);
break;
default:
console = null;
console0 = null;
assert false;
break;
}
if (this.keyboardInput && !this.batchMode)
console.setHistoryFile(this.historyFile);

// Set up CLI session
final CliSession session = console.getSession();
session.setDatabaseDescription(this.getDatabaseDescription());
session.setReadOnly(this.readOnly);
session.setVerbose(this.verbose);
session.setSchemaModel(schemaModel);
session.setSchemaVersion(this.schemaVersion);
session.setAllowNewSchema(this.allowNewSchema);
session.loadFunctionsFromClasspath();
session.loadCommandsFromClasspath();

// Handle file input
for (String filename : this.execFiles) {
final File file = new File(filename);
try (final InputStream input = new FileInputStream(file)) {
if (!this.parseAndExecuteCommands(console, new InputStreamReader(input), file.getName()))
try (final Console console = console0) {

// Configure history
if (this.keyboardInput && !this.batchMode)
console.setHistoryFile(this.historyFile);

// Set up CLI session
final CliSession session = console.getSession();
session.setDatabaseDescription(this.getDatabaseDescription());
session.setReadOnly(this.readOnly);
session.setVerbose(this.verbose);
session.setSchemaModel(schemaModel);
session.setSchemaVersion(this.schemaVersion);
session.setAllowNewSchema(this.allowNewSchema);
session.loadFunctionsFromClasspath();
session.loadCommandsFromClasspath();

// Handle file input
for (String filename : this.execFiles) {
final File file = new File(filename);
try (final InputStream input = new FileInputStream(file)) {
if (!this.parseAndExecuteCommands(console, new InputStreamReader(input), file.getName()))
return 1;
} catch (IOException e) {
session.getWriter().println("Error: error opening " + file.getName() + ": " + e);
return 1;
} catch (IOException e) {
session.getWriter().println("Error: error opening " + file.getName() + ": " + e);
return 1;
}
}
}

// Handle command-line commands
for (String command : this.execCommands) {
if (!this.parseAndExecuteCommands(console, new StringReader(command), null))
return 1;
}
// Handle command-line commands
for (String command : this.execCommands) {
if (!this.parseAndExecuteCommands(console, new StringReader(command), null))
return 1;
}

// Handle standard input
if (!this.keyboardInput) {
if (!this.parseAndExecuteCommands(console, new InputStreamReader(System.in), "(stdin)"))
return 1;
}
// Handle standard input
if (!this.keyboardInput) {
if (!this.parseAndExecuteCommands(console, new InputStreamReader(System.in), "(stdin)"))
return 1;
}

// Run console if not in batch mode
if (this.keyboardInput && !this.batchMode)
console.run();
// Run console if not in batch mode
if (this.keyboardInput && !this.batchMode)
console.run();

// Shut down KV database
this.shutdownKVDatabase();
// Shut down KV database
this.shutdownKVDatabase();

// Done
return 0;
// Done
return 0;
}
}

private boolean parseAndExecuteCommands(Console console, Reader input, String inputDescription) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -100,7 +100,7 @@
<javax.el.version>3.0.1-b09</javax.el.version>
<javax.mail.version>1.6.1</javax.mail.version>
<jetty.version>9.2.24.v20180105</jetty.version>
<jline.version>2.14.2</jline.version>
<jline.version>2.14.6</jline.version>
<leveldb.version>0.9</leveldb.version>
<log4j.version>1.2.17</log4j.version>
<maven.artifact.version>3.5.3</maven.artifact.version>
Expand Down

0 comments on commit 939d63a

Please sign in to comment.