Skip to content

Commit

Permalink
Added CommandRegistry interface, fixes #480
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 5, 2019
1 parent abb6bb8 commit 94b6fcf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
31 changes: 27 additions & 4 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jline.builtins.Commands;
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.TTop;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Widgets.ArgDesc;
import org.jline.builtins.Widgets.CmdDesc;
import org.jline.reader.Completer;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.LineReader;
import org.jline.reader.Widget;
import org.jline.reader.LineReader.Option;
import org.jline.reader.Widget;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
Expand All @@ -48,7 +46,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class Builtins {
public class Builtins implements CommandRegistry {
public enum Command {NANO
, LESS
, HISTORY
Expand All @@ -65,6 +63,7 @@ public enum Command {NANO
private Map<String,Command> nameCommand = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<Command,CommandMethods> commandExecute = new HashMap<>();
private Map<Command,List<String>> commandInfo = new HashMap<>();
private LineReader reader;
private Exception exception;

Expand Down Expand Up @@ -105,6 +104,21 @@ public Builtins(Set<Command> commands, Supplier<Path> workDir, ConfigurationPath
commandExecute.put(Command.TTOP, new CommandMethods(this::ttop, this::ttopCompleter));
}

public Set<String> commandNames() {
return nameCommand.keySet();
}

public Map<String, String> commandAliases() {
return aliasCommand;
}

public List<String> commandInfo(String command) {
if (!commandInfo.containsKey(command(command))) {
commandOptions(command);
}
return commandInfo.get(command(command));
}

private void doNameCommand() {
nameCommand = commandName.entrySet()
.stream()
Expand Down Expand Up @@ -237,14 +251,22 @@ private Map<String,String> commandOptions(String command) {
try {
execute(command, args);
} catch (HelpException e) {
List<String> info = new ArrayList<>();
String[] msg = e.getMessage().replaceAll("\r\n", "\n").replaceAll("\r", "\n").split("\n");
boolean start = false;
boolean first = true;
for (String s: msg) {
if (!start) {
if (s.trim().startsWith("Usage: ")) {
s = s.split("Usage:")[1];
start = true;
} else {
if (first && s.contains(" - ")) {
info.add(s.substring(s.indexOf(" - ") + 3).trim());
} else {
info.add(s.trim());
}
first = false;
continue;
}
}
Expand All @@ -261,6 +283,7 @@ private Map<String,String> commandOptions(String command) {
}
}
}
commandInfo.put(command(command), info);
} catch (Exception e) {

}
Expand Down
60 changes: 60 additions & 0 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.builtins;

import org.jline.builtins.Completers;
import org.jline.builtins.Widgets;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface CommandRegistry {

/**
* Returns the command names known by this registry.
* @return the set of known command names, excluding aliases
*/
Set<String> commandNames();

/**
* Returns a map of alias-to-command names known by this registry.
* @return a map with alias keys and command name values
*/
Map<String, String> commandAliases();

/**
* Returns a short info about command known by this registry.
* @return a short info about command
*/
List<String> commandInfo(String command);

/**
* Returns whether a command with the specified name is known to this registry.
* @param command the command name to test
* @return true if the specified command is registered
*/
boolean hasCommand(String command);

/**
* Returns a {@code SystemCompleter} that can provide detailed completion
* information for all registered commands.
*
* @return a SystemCompleter that can provide command completion for all registered commands
*/
Completers.SystemCompleter compileCompleters();

/**
* Returns a command description for use in the JLine Widgets framework.
* @param command name of the command whose description to return
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
Widgets.CmdDesc commandDescription(String command);
}

0 comments on commit 94b6fcf

Please sign in to comment.