Skip to content

Commit

Permalink
Include ClearScreen command declaratively
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh authored and remkop committed Dec 14, 2020
1 parent 7ff98c6 commit 9ba72e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand Down Expand Up @@ -48,19 +47,31 @@ public class PicocliCommands implements CommandRegistry {
*/
@Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true,
description = "Clears the screen", version = "1.0")
static class ClearScreen implements Callable<Void> {
public static class ClearScreen implements Callable<Void> {

private final LineReaderImpl reader;

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

public Void call() throws IOException {
reader.clearScreen();
if (reader != null) { reader.clearScreen(); }
return null;
}
}

public static class PicocliCommandsFactory implements CommandLine.IFactory {
private LineReader reader;

@SuppressWarnings("unchecked")
public <K> K create(Class<K> clazz) throws Exception {
if (ClearScreen.class == clazz) { return (K) new ClearScreen(reader); }
return CommandLine.defaultFactory().create(clazz);
}

public void setLineReader(LineReader reader) {
this.reader = reader;
}
}

private final Supplier<Path> workDir;
private final CommandLine cmd;
Expand All @@ -74,25 +85,14 @@ public PicocliCommands(Path workDir, CommandLine cmd) {
public PicocliCommands(Supplier<Path> workDir, CommandLine cmd) {
this.workDir = workDir;
this.cmd = cmd;
commands = new HashSet<>(cmd.getCommandSpec().subcommands().keySet());
commands = cmd.getCommandSpec().subcommands().keySet();
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");
}

/**
*
* @param command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.shell.jline3.PicocliCommands;
import picocli.shell.jline3.PicocliCommands.PicocliCommandsFactory;

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -42,7 +43,7 @@ public class Example {
""},
footer = {"", "Press Ctl-D to exit."},
subcommands = {
MyCommand.class, CommandLine.HelpCommand.class})
MyCommand.class, PicocliCommands.ClearScreen.class, CommandLine.HelpCommand.class})
static class CliCommands implements Runnable {
PrintWriter out;

Expand Down Expand Up @@ -136,7 +137,13 @@ public static void main(String[] args) {
builtins.alias("bindkey", "keymap");
// set up picocli commands
CliCommands commands = new CliCommands();
CommandLine cmd = new CommandLine(commands);

PicocliCommandsFactory factory = new PicocliCommandsFactory();
// Or, if you have your own factory, you can chain them like this:
// MyCustomFactory customFactory = createCustomFactory(); // your application custom factory
// PicocliCommands.Factory factory = new PicocliCommands.Factory(customFactory); // chain the factories

CommandLine cmd = new CommandLine(commands, factory);
PicocliCommands picocliCommands = new PicocliCommands(Example::workDir, cmd);

Parser parser = new DefaultParser();
Expand All @@ -152,7 +159,7 @@ public static void main(String[] args) {
.variable(LineReader.LIST_MAX, 50) // max tab completion candidates
.build();
builtins.setLineReader(reader);
picocliCommands.includeClearScreenCommand(reader);
factory.setLineReader(reader);
TailTipWidgets widgets = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
widgets.enable();
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
Expand Down

0 comments on commit 9ba72e6

Please sign in to comment.