Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users of picocli-shell-jline3 to use clear command and a nicer help command #1265

Merged
merged 9 commits into from
Dec 14, 2020
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package picocli.shell.jline3;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -13,19 +21,18 @@
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.utils.AttributedString;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.OptionSpec;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Compiles SystemCompleter for command completion and implements a method commandDescription() that provides command descriptions
* for JLine TailTipWidgets to be displayed in terminal status bar.
Expand All @@ -35,6 +42,26 @@
* @since 4.1.2
*/
public class PicocliCommands implements CommandRegistry {

/**
* Command that clears the screen.
sualeh marked this conversation as resolved.
Show resolved Hide resolved
*/
sualeh marked this conversation as resolved.
Show resolved Hide resolved
@Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true,
description = "Clears the screen", version = "1.0")
static class ClearScreen implements Callable<Void> {

private final LineReaderImpl reader;

ClearScreen(LineReaderImpl reader) {
this.reader = reader;
}

public Void call() throws IOException {
remkop marked this conversation as resolved.
Show resolved Hide resolved
reader.clearScreen();
return null;
}
}

private final Supplier<Path> workDir;
private final CommandLine cmd;
private final Set<String> commands;
Expand All @@ -47,14 +74,25 @@ public PicocliCommands(Path workDir, CommandLine cmd) {
public PicocliCommands(Supplier<Path> workDir, CommandLine cmd) {
this.workDir = workDir;
this.cmd = cmd;
commands = cmd.getCommandSpec().subcommands().keySet();
commands = new HashSet<>(cmd.getCommandSpec().subcommands().keySet());
sualeh marked this conversation as resolved.
Show resolved Hide resolved
for (String c: commands) {
for (String a: cmd.getSubcommands().get(c).getCommandSpec().aliases()) {
aliasCommand.put(a, c);
}
}
}

public void includeClearScreenCommand(LineReader reader) {
if (reader == null) return;
ClearScreen clearScreen = new ClearScreen((LineReaderImpl) reader);
cmd.addSubcommand(clearScreen);

commands.add("clear");

aliasCommand.put("clear", "clear");
aliasCommand.put("cls", "clear");
}

sualeh marked this conversation as resolved.
Show resolved Hide resolved
/**
*
* @param command
Expand All @@ -74,9 +112,9 @@ public SystemCompleter compileCompleters() {
return out;
}

private class PicocliCompleter implements Completer {
private class PicocliCompleter extends ArgumentCompleter implements Completer {
sualeh marked this conversation as resolved.
Show resolved Hide resolved

public PicocliCompleter() {}
public PicocliCompleter() { super(NullCompleter.INSTANCE); }

@Override
public void complete(LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,12 @@ public class Example {
""},
footer = {"", "Press Ctl-D to exit."},
subcommands = {
MyCommand.class, ClearScreen.class, CommandLine.HelpCommand.class})
MyCommand.class, CommandLine.HelpCommand.class})
static class CliCommands implements Runnable {
LineReaderImpl reader;
PrintWriter out;

CliCommands() {}

public void setReader(LineReader reader){
this.reader = (LineReaderImpl)reader;
out = reader.getTerminal().writer();
}

public void run() {
out.println(new CommandLine(this).getUsageMessage());
}
Expand Down Expand Up @@ -128,21 +122,6 @@ public void subtract(@Option(names = {"-l", "--left"}, required = true) int left
}
}

/**
* Command that clears the screen.
*/
@Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true,
description = "Clears the screen", version = "1.0")
static class ClearScreen implements Callable<Void> {

@ParentCommand CliCommands parent;

public Void call() throws IOException {
parent.reader.clearScreen();
return null;
}
}

private static Path workDir() {
return Paths.get(System.getProperty("user.dir"));
}
Expand All @@ -164,6 +143,7 @@ public static void main(String[] args) {
try (Terminal terminal = TerminalBuilder.builder().build()) {
SystemRegistry systemRegistry = new SystemRegistryImpl(parser, terminal, Example::workDir, null);
systemRegistry.setCommandRegistries(builtins, picocliCommands);
systemRegistry.register("help", picocliCommands);
remkop marked this conversation as resolved.
Show resolved Hide resolved

LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
Expand All @@ -172,7 +152,7 @@ public static void main(String[] args) {
.variable(LineReader.LIST_MAX, 50) // max tab completion candidates
.build();
builtins.setLineReader(reader);
commands.setReader(reader);
picocliCommands.includeClearScreenCommand(reader);
sualeh marked this conversation as resolved.
Show resolved Hide resolved
TailTipWidgets widgets = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
widgets.enable();
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
Expand Down