
javarepl.console.commands.ShowHistory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javarepl Show documentation
Show all versions of javarepl Show documentation
Java REPL is a simple Read-Eval-Print-Loop for Java language.
package javarepl.console.commands;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import com.googlecode.totallylazy.numbers.Numbers;
import javarepl.completion.CommandCompleter;
import javarepl.console.ConsoleHistory;
import javarepl.console.ConsoleLogger;
import static com.googlecode.totallylazy.Pair.functions.values;
import static com.googlecode.totallylazy.Strings.replace;
import static com.googlecode.totallylazy.Strings.startsWith;
import static javarepl.Utils.listValues;
public final class ShowHistory extends Command {
private static final String COMMAND = ":hist";
private final ConsoleLogger logger;
private final ConsoleHistory history;
public ShowHistory(ConsoleLogger logger, ConsoleHistory history) {
super(COMMAND + " - shows the history (optional 'num' is number of evaluations to show)",
startsWith(COMMAND), new CommandCompleter(COMMAND));
this.logger = logger;
this.history = history;
}
public void execute(String expression) {
Integer limit = parseNumericCommand(expression).second().getOrElse(history.items().size());
Sequence numberedHistory = numberedHistory(history).reverse().take(limit).reverse();
if (!numberedHistory.isEmpty()) {
logger.success(listValues("History", numberedHistory));
} else {
logger.success("No history.");
}
}
public static Sequence numberedHistory(ConsoleHistory consoleHistory) {
return Numbers.range(1)
.zip(consoleHistory.items().map(replace("\n", "\n ")))
.map(values().then(Sequences.toString(" ")));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy