Skip to content

Commit

Permalink
Allow a built-in clear command to be included with the rest of the pi…
Browse files Browse the repository at this point in the history
…cocli commands
  • Loading branch information
sualeh authored and remkop committed Dec 14, 2020
1 parent a610c7c commit 103ad14
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
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,16 @@
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.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 +40,26 @@
* @since 4.1.2
*/
public class PicocliCommands implements CommandRegistry {

/**
* 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> {

private final LineReaderImpl reader;

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

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

private final Supplier<Path> workDir;
private final CommandLine cmd;
private final Set<String> commands;
Expand All @@ -47,14 +72,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());
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 @@ -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 Down Expand Up @@ -172,7 +151,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);
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 103ad14

Please sign in to comment.