org.aesh.readline.Prompt Java Examples

The following examples show how to use org.aesh.readline.Prompt. 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 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 #2
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 #3
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private String promptFromNonStartedConsole(Prompt prompt, Completion completer) throws InterruptedException, IOException {
    initializeConnection();
    LOG.trace("Not started");
    String[] out = new String[1];
    if (connection.suspended()) {
        connection.awake();
    }
    List<Completion> lst = null;
    if (completer != null) {
        lst = new ArrayList<>();
        lst.add(completer);
    }
    getReadLine().readline(connection, prompt, newLine -> {
        out[0] = newLine;
        LOG.trace("Got some input");

        // We must call stopReading to stop reading from terminal
        // and release this thread.
        connection.stopReading();
    }, lst, null, null, null, READLINE_FLAGS);
    connection.openBlockingInterruptable();
    LOG.trace("Done for prompt");
    return out[0];
}
 
Example #4
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String readLine(Prompt prompt, Completion completer) throws InterruptedException, IOException {
    if (started) {
        // If there are some output collected, flush it.
        printCollectedOutput();
        pagingSupport.reset();
    }

    // Keep a reference on the caller thread in case Ctrl-C is pressed
    // and thread needs to be interrupted.
    readingThread = Thread.currentThread();
    try {
        if (!started) {
            // That is the case of the CLI connecting prior to start the terminal.
            // No Terminal waiting in Main thread yet.
            // We are opening the connection to the terminal until we have read
            // something from prompt.
            return promptFromNonStartedConsole(prompt, completer);
        } else {
            return promptFromStartedConsole(prompt, completer, null);
        }
    } finally {
        readingThread = null;
    }
}
 
Example #5
Source File: InteractiveSecurityBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private String buildDN() throws InterruptedException {
    String dnString = null;
    boolean correct = false;
    while (!correct) {
        name = prompt("What is your first and last name?", name);
        orgUnit = prompt("What is the name of your organizational unit?", orgUnit);
        org = prompt("What is the name of your organization?", org);
        city = prompt("What is the name of your City or Locality?", city);
        state = prompt("What is the name of your State or Province?", state);
        countryCode = prompt("What is the two-letter country code for this unit?", countryCode);
        dnString = buildDNString();
        String res = commandInvocation.inputLine(new Prompt("Is " + dnString + " correct y/n [y]?"));
        if (res != null && res.equals("y")) {
            correct = true;
            break;
        } else if (res != null && res.equals("n")) {
            correct = false;
        } else if (res != null && res.length() == 0) {
            correct = true;
            break;
        } else {
            correct = false;
        }
    }
    return dnString;
}
 
Example #6
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 #7
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 #8
Source File: CLICommandInvocationImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Prompt getPrompt() {
    if (console != null) {
        return console.getPrompt();
    }
    return null;
}
 
Example #9
Source File: PmSession.java    From galleon with Apache License 2.0 5 votes vote down vote up
public Prompt buildPrompt() {
    ToolModes.Mode mode = toolModes.getActiveMode();
    Prompt prompt = null;
    switch (mode) {
        case NOMINAL: {
            prompt = buildPrompt(ctx.getCurrentWorkingDirectory().getName());
            break;
        }
        case EDIT: {
            prompt = buildPrompt("");
            break;
        }
    }
    return prompt;
}
 
Example #10
Source File: CLICommandInvocationImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public String inputLine(Prompt prompt, Completion completer) throws InterruptedException, IOException {
    if (console != null) {
        return console.readLine(prompt, completer);
    }
    return null;
}
 
Example #11
Source File: CliShell.java    From galleon with Apache License 2.0 5 votes vote down vote up
@Override
public String readLine(Prompt prompt) throws InterruptedException {
    final String[] out = {null};
    readline.readline(connection, prompt, event -> {
        out[0] = event;
        connection.stopReading();
    });
    try {
        connection.openBlocking();
    } finally {
        connection.setStdinHandler(null);
    }
    return out[0];
}
 
Example #12
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String promptFromStartedConsole(Prompt prompt, Completion completer, History history) throws InterruptedException, IOException {
    initializeConnection();
    String[] out = new String[1];
    // We must be called from another Thread. connection is reading in Main thread.
    // calling readline will wakeup the Main thread that will execute
    // the Prompt handling.
    // We can safely wait.
    if (readingThread == startThread) {
        throw new RuntimeException("Can't prompt from the Thread that is "
                + "reading terminal input");
    }
    List<Completion> lst = null;
    if (completer != null) {
        lst = new ArrayList<>();
        lst.add(completer);
    }
    CountDownLatch latch = new CountDownLatch(1);
    // We need to set the interrupt SignalHandler to interrupt the current thread.
    Consumer<Signal> prevHandler = connection.getSignalHandler();
    connection.setSignalHandler(interruptHandler);
    readline.readline(connection, prompt, newLine -> {
        out[0] = newLine;
        LOG.trace("Got some input");
        latch.countDown();
    }, lst, null, history, null, READLINE_FLAGS);
    try {
        latch.await();
    } finally {
        connection.setSignalHandler(prevHandler);
    }
    LOG.trace("Done for prompt");
    return out[0];
}
 
Example #13
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setPrompt(String prompt) {
    if (prompt.contains("\u001B[")) {
        this.prompt = new Prompt(Parser.stripAwayAnsiCodes(prompt), prompt);
    } else {
        this.prompt = new Prompt(prompt);
    }
}
 
Example #14
Source File: CLICommandInvocationBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public String readLine(Prompt prompt) throws InterruptedException {
    try {
        return ctx.input(prompt);
    } catch (CommandLineException | IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #15
Source File: CommandContextImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String input(Prompt prompt) throws CommandLineException, InterruptedException, IOException {
    // Only fail an interact if we're not in interactive.
    if (!INTERACT && ERROR_ON_INTERACT) {
        interactionDisabled();
    }

    if (console == null) {
        initBasicConsole(null, false);
    }

    return console.readLine(prompt);
}
 
Example #16
Source File: CLICommandInvocationBuilder.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public Key read(Prompt prompt) throws InterruptedException {
    //TODO
    return null;
}
 
Example #17
Source File: CLICommandInvocationImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void setPrompt(Prompt prompt) {
    if (console != null) {
        console.setPrompt(prompt);
    }
}
 
Example #18
Source File: CLICommandInvocationImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public String inputLine(Prompt prompt) throws InterruptedException {
    return shell.readLine(prompt);
}
 
Example #19
Source File: CLICommandInvocationImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public String inputLine() throws InterruptedException {
    return inputLine(new Prompt(""));
}
 
Example #20
Source File: OutputPmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public Prompt getPrompt() {
    return delegate.getPrompt();
}
 
Example #21
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String readLine(Prompt prompt) throws InterruptedException, IOException {
    return readLine(prompt, null);
}
 
Example #22
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void setPrompt(Prompt prompt) {
    this.prompt = prompt;
}
 
Example #23
Source File: ReadlineConsole.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Prompt getPrompt() {
    return prompt;
}
 
Example #24
Source File: CliShellPmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public Prompt getPrompt() {
    return delegate.getPrompt();
}
 
Example #25
Source File: OutputPmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public void setPrompt(Prompt prompt) {
    delegate.setPrompt(prompt);
}
 
Example #26
Source File: OutputPmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public String inputLine(Prompt prompt) throws InterruptedException {
    return delegate.inputLine(prompt);
}
 
Example #27
Source File: InteractivePmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public void setPrompt(Prompt prompt) {
    delegate.setPrompt(prompt);
}
 
Example #28
Source File: InteractivePmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public Prompt getPrompt() {
    return delegate.getPrompt();
}
 
Example #29
Source File: InteractivePmCommandInvocation.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public String inputLine(Prompt prompt) throws InterruptedException {
    return delegate.inputLine(prompt);
}
 
Example #30
Source File: CliShell.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public String readLine() throws InterruptedException {
    return readLine(new Prompt(""));
}