-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright (c) 2002-2020, 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 java.util.*; | ||
|
||
import org.jline.builtins.CommandRegistry; | ||
import org.jline.builtins.Widgets; | ||
import org.jline.reader.ParsedLine; | ||
import org.jline.reader.Parser; | ||
import org.jline.utils.AttributedStringBuilder; | ||
|
||
/** | ||
* Aggregate command registeries. | ||
* | ||
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a> | ||
*/ | ||
public class Master { | ||
public static class Registry implements CommandRegistry { | ||
private final CommandRegistry[] commandRegistries; | ||
private Integer consoleId = null; | ||
|
||
public Registry(CommandRegistry... commandRegistries) { | ||
this.commandRegistries = commandRegistries; | ||
for (int i = 0; i < commandRegistries.length; i++) { | ||
if (commandRegistries[i].getType() == CommandRegistry.Type.CONSOLE) { | ||
if (consoleId != null) { | ||
throw new IllegalArgumentException(); | ||
} else { | ||
this.consoleId = i; | ||
commandRegistries[i].setMasterRegistry(this); | ||
} | ||
} else if (commandRegistries[i].getType() == CommandRegistry.Type.SYSTEM) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
} | ||
|
||
public CommandRegistry.Type getType() { | ||
return CommandRegistry.Type.SYSTEM; | ||
} | ||
|
||
public Set<String> commandNames() { | ||
Set<String> out = new HashSet<>(); | ||
for (CommandRegistry r : commandRegistries) { | ||
out.addAll(r.commandNames()); | ||
} | ||
return out; | ||
} | ||
|
||
public Map<String, String> commandAliases() { | ||
Map<String, String> out = new HashMap<>(); | ||
for (CommandRegistry r : commandRegistries) { | ||
out.putAll(r.commandAliases()); | ||
} | ||
return out; | ||
} | ||
|
||
public List<String> commandInfo(String command) { | ||
int id = registryId(command); | ||
return id > -1 ? commandRegistries[id].commandInfo(command) : new ArrayList<>(); | ||
} | ||
|
||
public boolean hasCommand(String command) { | ||
return registryId(command) > -1; | ||
} | ||
|
||
public Completers.SystemCompleter compileCompleters() { | ||
return CommandRegistry.compileCompleters(commandRegistries); | ||
} | ||
|
||
public Widgets.CmdDesc commandDescription(String command) { | ||
int id = registryId(command); | ||
return id > -1 ? commandRegistries[id].commandDescription(command) : new Widgets.CmdDesc(false); | ||
} | ||
|
||
public Object execute(ParsedLine pl) throws Exception { | ||
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]); | ||
String cmd = Parser.getCommand(pl.word()); | ||
Object out = null; | ||
boolean done = false; | ||
if ("help".equals(cmd) || "?".equals(cmd)) { | ||
help(); | ||
done = true; | ||
} | ||
else { | ||
int id = registryId(cmd); | ||
if (id > -1) { | ||
out = commandRegistries[id].execute(cmd, argv); | ||
} else if (consoleId != null) { | ||
out = commandRegistries[0].execute(pl); | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
private void help() { | ||
System.out.println("List of available commands:"); | ||
for (CommandRegistry r : commandRegistries) { | ||
TreeSet<String> commands = new TreeSet<>(r.commandNames()); | ||
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(2); | ||
asb.append("\t"); | ||
asb.append(r.getClass().getSimpleName()); | ||
asb.append(":"); | ||
System.out.println(asb.toString()); | ||
for (String c: commands) { | ||
asb = new AttributedStringBuilder().tabs(Arrays.asList(4,20)); | ||
asb.append("\t"); | ||
asb.append(c); | ||
asb.append("\t"); | ||
asb.append(r.commandInfo(c).size() > 0 ? r.commandInfo(c).get(0) : " "); | ||
System.out.println(asb.toString()); | ||
} | ||
} | ||
System.out.println(" Additional help:"); | ||
System.out.println(" <command> --help"); | ||
} | ||
|
||
private int registryId(String command) { | ||
for (int i = 0; i < commandRegistries.length; i++) { | ||
if (commandRegistries[i].hasCommand(command)) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.