Java Code Examples for org.aesh.readline.Readline#readline()

The following examples show how to use org.aesh.readline.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: 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 4
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 5
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());
}