Package javarepl.console.commands

Source Code of javarepl.console.commands.EvaluateFromHistory

package javarepl.console.commands;

import com.googlecode.totallylazy.Option;
import javarepl.completion.CommandCompleter;
import javarepl.console.Console;
import javarepl.console.ConsoleHistory;
import javarepl.console.ConsoleLogger;

import static com.googlecode.totallylazy.Strings.startsWith;

public final class EvaluateFromHistory extends Command {
    private static final String COMMAND = ":h!";
    private final Console console;
    private final ConsoleHistory history;
    private final ConsoleLogger logger;

    public EvaluateFromHistory(Console console, ConsoleHistory history, ConsoleLogger logger) {
        super(COMMAND + " num - evaluate expression from history)", startsWith(COMMAND), new CommandCompleter(COMMAND));
        this.console = console;
        this.history = history;
        this.logger = logger;
    }

    public void execute(String expression) {
        Integer historyItem = parseNumericCommand(expression).second().getOrElse(history.items().size());
        Option<String> fromHistory = history.items().drop(historyItem - 1).headOption();

        if (!fromHistory.isEmpty()) {
            console.execute(fromHistory.get());
        } else {
            logger.error("Expression not found.\n");
        }
    }
}
TOP

Related Classes of javarepl.console.commands.EvaluateFromHistory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.