Skip to content

Simple example

Ståle W. Pedersen edited this page Aug 24, 2017 · 6 revisions

A very simple example of how to use Æsh-Readline on a local terminal

In this example we'll just print out the input we get from the user and exit when the user types exit

import org.aesh.readline.Readline;
import org.aesh.readline.ReadlineBuilder;
import org.aesh.readline.tty.terminal.TerminalConnection;
import org.aesh.terminal.Connection;

import java.io.IOException;
import java.util.function.Consumer;

public class SimpleExample implements Consumer<Connection> {

    public static void main(String... args) throws IOException {
        //we're setting up readline to read when connection receives any input
        new TerminalConnection(new SimpleExample());
    }

    @Override
    public void accept(Connection connection) {
        read(connection, ReadlineBuilder.builder().enableHistory(false).build(), "[@aesh]$ ");
        //lets open the connection to the terminal using this thread
        connection.openBlocking();
    }

    private void read(Connection connection, Readline readline, String prompt) {
        readline.readline(connection, prompt, input -> {
            //we specify a simple lambda consumer to read the 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);
            }
        });
    }
}
Clone this wiki locally