org.aesh.readline.Readline Java Examples

The following examples show how to use org.aesh.readline.Readline. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestTerminalConnection.java    From aesh-readline with Apache License 2.0 6 votes vote down vote up
@Test
public void testSignal() throws IOException, InterruptedException {
    PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    TerminalConnection connection = new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out);
    Attributes attributes = new Attributes();
    attributes.setLocalFlag(Attributes.LocalFlag.ECHOCTL, true);
    connection.setAttributes(attributes);

    Readline readline = new Readline();
    readline.readline(connection, new Prompt(""), s -> {  });

    connection.openNonBlocking();
    outputStream.write(("FOO").getBytes());
    outputStream.flush();
    Thread.sleep(100);
    connection.getTerminal().raise(Signal.INT);
    connection.close();

    Assert.assertEquals("FOO^C"+ Config.getLineSeparator(), new String(out.toByteArray()));
}
 
Example #2
Source File: TestTerminalConnection.java    From aesh-readline with Apache License 2.0 6 votes vote down vote up
@Test
public void testSignalEchoCtlFalse() throws IOException, InterruptedException {
    PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    TerminalConnection connection = new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out);

    Readline readline = new Readline();
    readline.readline(connection, new Prompt(""), s -> {  });

    connection.openNonBlocking();
    outputStream.write(("FOO").getBytes());
    outputStream.flush();
    Thread.sleep(100);
    connection.getTerminal().raise(Signal.INT);
    connection.close();

    Assert.assertEquals(new String(out.toByteArray()), "FOO"+ Config.getLineSeparator());
}
 
Example #3
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
ReadlineConsole(Settings settings) throws IOException {
    this.settings = settings;
    readlineHistory = new FileHistory(settings.getHistoryFile(),
            settings.getHistorySize(), settings.getPermission(), false);
    if (settings.isDisableHistory()) {
        readlineHistory.disable();
    } else {
        readlineHistory.enable();
    }
    if (isTraceEnabled) {
        LOG.tracef("History is enabled? %s", !settings.isDisableHistory());
    }
    aliasManager = new AliasManager(new File(Config.getHomeDir()
            + Config.getPathSeparator() + ".aesh_aliases"), true);
    AliasPreProcessor aliasPreProcessor = new AliasPreProcessor(aliasManager);
    preProcessors.add(aliasPreProcessor);
    completions.add(new AliasCompletion(aliasManager));
    readline = new Readline();
}
 
Example #4
Source File: CliShell.java    From galleon with Apache License 2.0 5 votes vote down vote up
CliShell(TerminalConnection connection) throws IOException {
    this.connection = connection;
    this.terminal = connection.getTerminal();
    if (terminal.getCodePointConsumer() == null) {
        stdOut = new Encoder(Charset.defaultCharset(), this::write);
    } else {
        stdOut = terminal.getCodePointConsumer();
    }
    readline = new Readline(EditModeBuilder.builder().create());
}
 
Example #5
Source File: SimpleTestExample.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
private static void read(TerminalConnection connection, Readline readline, String prompt) {
    readline.readline(connection, prompt, input -> {
        if(input != null && input.equals("exit")) {
            connection.write("we're exiting\n");
            connection.close();
        }
        else {
            connection.write("=====> "+input+"\n");
            //lets read until we get exit
            read(connection, readline, prompt);
        }
    });
}
 
Example #6
Source File: ReadlineAliasTest.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Test
public void alias() throws Exception {
    PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    TerminalConnection connection = new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out);
    Readline readline = new Readline();

    File aliasFile = Config.isOSPOSIXCompatible() ?
            new File("src/test/resources/alias1") : new File("src\\test\\resources\\alias1");
    AliasManager aliasManager = new AliasManager(aliasFile, false);
    AliasPreProcessor aliasPreProcessor = new AliasPreProcessor(aliasManager);
    List<Function<String, Optional<String>>> preProcessors = new ArrayList<>();
    preProcessors.add(aliasPreProcessor);

    readline.readline(connection, new Prompt(""),
            s -> {
                //first check this
                assertEquals("ls -alF", s);
                //then call readline again, to check the next line
                readline.readline(connection, new Prompt(""),
                        t -> assertEquals("grep --color=auto -l", t),
                        null, preProcessors);
            } , null, preProcessors);

    outputStream.write(("ll"+Config.getLineSeparator()).getBytes());
    outputStream.write(("grep -l"+Config.getLineSeparator()).getBytes());
    outputStream.flush();
    connection.openNonBlocking();
    Thread.sleep(200);
}
 
Example #7
Source File: TestTerminalConnection.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSignal() throws IOException, InterruptedException {
    PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    TerminalConnection connection = new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out);
    connection.setSignalHandler( signal -> {
        if(signal == Signal.INT) {
            connection.write("BAR");
            connection.stdoutHandler().accept(Config.CR);
            connection.close();
        }
    });

    Readline readline = new Readline();
    readline.readline(connection, new Prompt(""), s -> {  });

    connection.openNonBlocking();
    outputStream.write(("GAH"+Config.getLineSeparator()).getBytes());
    outputStream.flush();
    Thread.sleep(250);
    assertEquals(new String(out.toByteArray()), "GAH"+Config.getLineSeparator());

    readline.readline(connection, new Prompt(""), s -> {  });
    outputStream.write(("FOO").getBytes());
    outputStream.flush();
    connection.getTerminal().raise(Signal.INT);
    Thread.sleep(250);

    assertEquals(new String(out.toByteArray()), "GAH"+Config.getLineSeparator()+"FOOBAR"+ Config.getLineSeparator());
}
 
Example #8
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * This has the side effect to create the internal readline instance.
 *
 * @param ch The Completion Handler.
 */
public void setCompletionHandler(CompletionHandler<? extends CompleteOperation> ch) {
    readline = new Readline(EditModeBuilder.builder().create(), null, ch);
}