Java Code Examples for jline.console.ConsoleReader#println()

The following examples show how to use jline.console.ConsoleReader#println() . 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: SparqlPrompt.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<String> getSparqlWithResults() throws IOException {
    final ConsoleReader reader = getReader();
    reader.setCopyPasteDetection(true); // disable tab completion from activating
    reader.setHistoryEnabled(false);    // don't store SPARQL fragments in the command history
    try {
        reader.println("To see more results press [Enter].");
        reader.println("Type '" + STOP_COMMAND + "' to stop showing results.");
        reader.flush();

        final String line = reader.readLine("> ");
        if(line.endsWith(STOP_COMMAND)) {
            return Optional.of(STOP_COMMAND);
        }
        return Optional.absent();
    } finally {
        reader.setHistoryEnabled(true);      // restore the ConsoleReader's settings
        reader.setCopyPasteDetection(false); // restore tab completion
    }
}
 
Example 2
Source File: InstallPrompt.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Prompt the user asking them if they are sure they would like to do the
 * install.
 *
 * @param instanceName - The Rya instance name. (not null)
 * @param installConfig - The configuration that will be presented to the user. (not null)
 * @return The value they entered.
 * @throws IOException There was a problem reading the value.
 */
private boolean promptMongoVerified(final String instanceName, final InstallConfiguration installConfig)  throws IOException {
    requireNonNull(instanceName);
    requireNonNull(installConfig);

    final ConsoleReader reader = getReader();
    reader.println();
    reader.println("A Rya instance will be installed using the following values:");
    reader.println("   Instance Name: " + instanceName);
    reader.println("   Use Free Text Indexing: " + installConfig.isFreeTextIndexEnabled());
    reader.println("   Use Temporal Indexing: " + installConfig.isTemporalIndexEnabled());
    reader.println("   Use PCJ Indexing: " + installConfig.isPcjIndexEnabled());
    reader.println("");

    return promptBoolean("Continue with the install? (y/n) ", Optional.absent());
}
 
Example 3
Source File: JLineCompletionHandler.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
/**
 * Prints out the candidates. If the size of the candidates is greater than the
 * {@link ConsoleReader#getAutoprintThreshold}, a warning is printed
 *
 * @param candidates the list of candidates to print
 */
protected void printCandidates(final ConsoleReader reader, List<CharSequence> candidates) throws IOException {

    Set<CharSequence> distinctCandidates = new HashSet<CharSequence>(candidates);
    if (distinctCandidates.size() > reader.getAutoprintThreshold()) {
        reader.println();
        reader.print(Messages.DISPLAY_CANDIDATES.format(candidates.size()));
        reader.flush();

        char[] allowed = {'y', 'n'};
        int c;
        while ((c = reader.readCharacter(allowed)) != -1) {
            if (c=='n') {
                reader.println();
                return;
            }
            if (c=='y') {
                break;
            }
            reader.beep();
        }
    }

    reader.println();
    reader.printColumns(sortCandidates(distinctCandidates));
}
 
Example 4
Source File: ConsoleUtil.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void printLine(ConsoleReader console, String author, String message) {
    try {
        CursorBuffer stashed = stashLine(console);
        console.println(author + " > " + message);
        unstashLine(console, stashed);
        console.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: SparqlPrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<String> getSparql() throws IOException {
    final ConsoleReader reader = getReader();
    reader.setCopyPasteDetection(true); // disable tab completion from activating
    reader.setHistoryEnabled(false);    // don't store SPARQL fragments in the command history
    try {
        reader.println("Enter a SPARQL Query.");
        reader.println("Type '" + EXECUTE_COMMAND + "' to execute the current query.");
        reader.println("Type '" + CLEAR_COMMAND + "' to clear the current query.");
        reader.flush();

        final StringBuilder sb = new StringBuilder();
        String line = reader.readLine("SPARQL> ");
        while (!line.endsWith(CLEAR_COMMAND) && !line.endsWith(EXECUTE_COMMAND)) {
            sb.append(line).append("\n");
            line = reader.readLine("     -> ");
        }

        if (line.endsWith(EXECUTE_COMMAND)) {
            sb.append(line.substring(0, line.length() - EXECUTE_COMMAND.length()));
            return Optional.of(sb.toString());
        }
        return Optional.absent();
    } finally {
        reader.setHistoryEnabled(true);      // restore the ConsoleReader's settings
        reader.setCopyPasteDetection(false); // restore tab completion
    }
}
 
Example 6
Source File: JLinePrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Prompts a user for a boolean value. The prompt will be repeated until
 * a value true/false string has been submitted. If a default value is
 * provided, then it will be used if the user doens't enter anything.
 *
 * @param prompt - The prompt for the input. (not null)
 * @param defaultValue - The default value for the input if one is provided. (not null)
 * @return The value the user entered.
 * @throws IOException There was a problem reading values from the user.
 */
protected boolean promptBoolean(final String prompt, final Optional<Boolean> defaultValue) throws IOException {
    requireNonNull(prompt);
    requireNonNull(defaultValue);

    final ConsoleReader reader = getReader();
    reader.setPrompt(prompt);

    Boolean value = null;
    boolean prompting = true;

    while(prompting) {
        // An empty input means to use the default value.
        final String input = reader.readLine();
        if(input.isEmpty() && defaultValue.isPresent()) {
            value = defaultValue.get();
            prompting = false;
        }

        // Check if it is one of the affirmative answers.
        if(isAffirmative(input)) {
            value = true;
            prompting = false;
        }

        // Check if it is one of the negative answers.
        if(isNegative(input)) {
            value = false;
            prompting = false;
        }

        // If we are still prompting, the input was invalid.
        if(prompting) {
            reader.println("Invalid response (true/false)");
        }
    }

    return value;
}
 
Example 7
Source File: JLinePrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Prompts a user for a String value. The prompt will be repeated until a
 * value has been submitted. If a default value is provided, then it will be
 * used if the user doesn't enter anything.
 *
 * @param prompt - The prompt for the input. (not null)
 * @param defaultValue - The default value for the input if one is provided. (not null)
 * @return The value the user entered.
 * @throws IOException There was a problem reading values from the user.
 */
protected String promptString(final String prompt, final Optional<String> defaultValue) throws IOException {
    requireNonNull(prompt);
    requireNonNull(defaultValue);

    final ConsoleReader reader = getReader();
    reader.setPrompt(prompt);

    String value = null;
    boolean prompting = true;

    while(prompting) {
        // Read a line of input.
        final String input = reader.readLine();

        if(!input.isEmpty()) {
            // If a value was provided, return it.
            value = input;
            prompting = false;
        } else {
            // Otherwise, if a default value was provided, return it;
            if(defaultValue.isPresent()) {
                value = defaultValue.get();
                prompting = false;
            } else {
                // Otherwise, the user must provide a value.
                reader.println("Invalid response. Must provide a value.");
            }
        }
    }

    return value;
}
 
Example 8
Source File: InstallPrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Prompt the user asking them if they are sure they would like to do the
 * install.
 *
 * @param instanceName - The Rya instance name. (not null)
 * @param installConfig - The configuration that will be presented to the user. (not null)
 * @return The value they entered.
 * @throws IOException There was a problem reading the value.
 */
private boolean promptAccumuloVerified(final String instanceName, final InstallConfiguration installConfig)  throws IOException {
    requireNonNull(instanceName);
    requireNonNull(installConfig);

    final ConsoleReader reader = getReader();
    reader.println();
    reader.println("A Rya instance will be installed using the following values:");
    reader.println("   Instance Name: " + instanceName);
    reader.println("   Use Shard Balancing: " + installConfig.isTableHashPrefixEnabled());
    reader.println("   Use Entity Centric Indexing: " + installConfig.isEntityCentrixIndexEnabled());
    reader.println("   Use Free Text Indexing: " + installConfig.isFreeTextIndexEnabled());
    // RYA-215            reader.println("   Use Geospatial Indexing: " + installConfig.isGeoIndexEnabled());
    reader.println("   Use Temporal Indexing: " + installConfig.isTemporalIndexEnabled());
    reader.println("   Use Precomputed Join Indexing: " + installConfig.isPcjIndexEnabled());
    if(installConfig.isPcjIndexEnabled()) {
        if(installConfig.getFluoPcjAppName().isPresent()) {
            reader.println("   PCJ Updater Fluo Application Name: " + installConfig.getFluoPcjAppName().get());
        } else {
            reader.println("   Not using a PCJ Updater Fluo Application");
        }
    }

    reader.println("");

    return promptBoolean("Continue with the install? (y/n) ", Optional.absent());
}
 
Example 9
Source File: ConsoleShellFactory.java    From Bukkit-SSHD with Apache License 2.0 5 votes vote down vote up
private void printPreamble(ConsoleReader consoleReader) throws IOException {
    consoleReader.println("  _____ _____ _    _ _____" + "\r");
    consoleReader.println(" / ____/ ____| |  | |  __ \\" + "\r");
    consoleReader.println("| (___| (___ | |__| | |  | |" + "\r");
    consoleReader.println(" \\___ \\\\___ \\|  __  | |  | |" + "\r");
    consoleReader.println(" ____) |___) | |  | | |__| |" + "\r");
    consoleReader.println("|_____/_____/|_|  |_|_____/" + "\r");
    consoleReader.println("Connected to: " + Bukkit.getServer().getName() + "\r");
    consoleReader.println("- " + Bukkit.getServer().getMotd() + "\r");
    consoleReader.println("\r");
    consoleReader.println("Type 'exit' to exit the shell." + "\r");
    consoleReader.println("===============================================" + "\r");
}
 
Example 10
Source File: ConsoleUtil.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void printLine(ConsoleReader console, String author, String message) throws IOException {
    CursorBuffer stashed = stashLine(console);
    console.println(author + " > " + message);
    unstashLine(console, stashed);
    console.flush();
}
 
Example 11
Source File: ChatClient.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    String author = args.length == 0 ? "Random_Stranger" : args[0];

    // Connect to the sever
    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", PORT).usePlaintext().build();
    RxChatGrpc.RxChatStub stub = RxChatGrpc.newRxStub(channel);

    CountDownLatch done = new CountDownLatch(1);
    ConsoleReader console = new ConsoleReader();
    console.println("Type /quit to exit");



    /* ******************************
     * Subscribe to incoming messages
     * ******************************/
    disposables.add(Single.just(Empty.getDefaultInstance())
            .as(stub::getMessages)
            .filter(message -> !message.getAuthor().equals(author))
            .subscribe(message -> printLine(console, message.getAuthor(), message.getMessage())));



    /* *************************
     * Publish outgoing messages
     * *************************/
    disposables.add(Observable
            // Send connection message
            .just(author + " joined.")
            // Send user input
            .concatWith(Observable.fromIterable(new ConsoleIterator(console, author + " > ")))
            // Send disconnect message
            .concatWith(Single.just(author + " left."))
            .map(msg -> toMessage(author, msg))
            .flatMapSingle(stub::postMessage)
            .subscribe(
                ChatClient::doNothing,
                throwable -> printLine(console, "ERROR", throwable.getMessage()),
                done::countDown
            ));



    // Wait for a signal to exit, then clean up
    done.await();
    disposables.dispose();
    channel.shutdown();
    channel.awaitTermination(1, TimeUnit.SECONDS);
    console.getTerminal().restore();
}
 
Example 12
Source File: ReactorChatClient.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Connect to the sever
    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", PORT).usePlaintext().build();
    ReactorChatGrpc.ReactorChatStub stub = ReactorChatGrpc.newReactorStub(channel);

    CountDownLatch done = new CountDownLatch(1);
    ConsoleReader console = new ConsoleReader();

    // Prompt the user for their name
    console.println("Press ctrl+D to quit");
    String author = console.readLine("Who are you? > ");
    stub.postMessage(toMessage(author, author + " joined.")).subscribe();

    // Subscribe to incoming messages
    Disposable chatSubscription = stub.getMessages(Mono.just(Empty.getDefaultInstance())).subscribe(
        message -> {
            // Don't re-print our own messages
            if (!message.getAuthor().equals(author)) {
                printLine(console, message.getAuthor(), message.getMessage());
            }
        },
        throwable -> {
            printLine(console, "ERROR", throwable.getMessage());
            done.countDown();
        },
        done::countDown
    );

    // Publish outgoing messages
    Flux.fromIterable(new ConsoleIterator(console, author + " > "))
        .map(msg -> toMessage(author, msg))
        .flatMap(stub::postMessage)
        .subscribe(
            empty -> { },
            throwable -> {
                printLine(console, "ERROR", throwable.getMessage());
                done.countDown();
            },
            done::countDown
        );

    // Wait for a signal to exit, then clean up
    done.await();
    stub.postMessage(toMessage(author, author + " left.")).subscribe();
    chatSubscription.dispose();
    channel.shutdown();
    channel.awaitTermination(1, TimeUnit.SECONDS);
    console.getTerminal().restore();
}
 
Example 13
Source File: CliSession.java    From actframework with Apache License 2.0 4 votes vote down vote up
private static void printBanner(String banner, ConsoleReader console) throws IOException {
    String[] lines = banner.split("[\n\r]");
    for (String line : lines) {
        console.println(line);
    }
}
 
Example 14
Source File: Main.java    From java-repl with Apache License 2.0 4 votes vote down vote up
public static void printCandidates(final ConsoleReader reader, Collection<CharSequence> candidates) throws
        IOException {
    Set<CharSequence> distinct = new HashSet<CharSequence>(candidates);

    if (distinct.size() > reader.getAutoprintThreshold()) {
        //noinspection StringConcatenation
        reader.print(Messages.DISPLAY_CANDIDATES.format(candidates.size()));
        reader.flush();

        int c;

        String noOpt = Messages.DISPLAY_CANDIDATES_NO.format();
        String yesOpt = Messages.DISPLAY_CANDIDATES_YES.format();
        char[] allowed = {yesOpt.charAt(0), noOpt.charAt(0)};

        while ((c = reader.readCharacter(allowed)) != -1) {
            String tmp = new String(new char[]{(char) c});

            if (noOpt.startsWith(tmp)) {
                reader.println();
                return;
            } else if (yesOpt.startsWith(tmp)) {
                break;
            } else {
                reader.beep();
            }
        }
    }

    // copy the values and make them distinct, without otherwise affecting the ordering. Only do it if the sizes differ.
    if (distinct.size() != candidates.size()) {
        Collection<CharSequence> copy = new ArrayList<CharSequence>();

        for (CharSequence next : candidates) {
            if (!copy.contains(next)) {
                copy.add(next);
            }
        }

        candidates = copy;
    }

    reader.println();
    reader.printColumns(candidates);
}