Skip to content

Commit

Permalink
updated to æsh 0.34
Browse files Browse the repository at this point in the history
added man command, lets call it early alpha...
  • Loading branch information
stalep committed Feb 21, 2013
1 parent 1276d44 commit 8f8b06f
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 64 deletions.
7 changes: 6 additions & 1 deletion aesh/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
<dependency>
<groupId>org.jboss.aesh</groupId>
<artifactId>aesh</artifactId>
<version>0.32</version>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.jboss.aesh</groupId>
<artifactId>aesh-extensions</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.jboss.forge</groupId>
Expand Down
115 changes: 59 additions & 56 deletions aesh/src/main/java/org/jboss/forge/aesh/ForgeShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.console.Config;
import org.jboss.aesh.console.Console;
import org.jboss.aesh.console.ConsoleCallback;
import org.jboss.aesh.console.ConsoleOutput;
import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.settings.Settings;
Expand All @@ -44,7 +45,6 @@ public class ForgeShell
private static final Logger logger = Logger.getLogger(ForgeShell.class.getName());

private Console console;
private ConsoleOutput output;
private Prompt prompt;

private List<ShellCommand> commands;
Expand Down Expand Up @@ -85,7 +85,9 @@ public void initShell() throws Exception
Settings.getInstance().setLogging(true);

commands = new ArrayList<ShellCommand>();
console = new Console();
console = Console.getInstance();
console.setPrompt(prompt);
console.setConsoleCallback(new ForgeConsoleCallback());

// internal commands
// addCommand(new ShellCommand(listServicesCommand));
Expand Down Expand Up @@ -151,60 +153,7 @@ private boolean isCommandFound(String name, Set<ExportedInstance<UICommand>> uiC

public void startShell() throws Exception
{
output = null;
while ((output = console.read(prompt, null)) != null)
{
CommandLine cl = null;
if (output.getBuffer() != null && !output.getBuffer().trim().isEmpty())
{
verifyLoadedCommands();
for (ShellCommand command : commands)
{
try
{
cl = command.parse(output.getBuffer());
if (cl != null)
{
// need some way of deciding if the command is standalone
if (command.getContext().isStandalone())
{
// console.

}
else
{
try {
command.run(output, cl);
break;
}
catch (Exception e) {
logger.log(Level.SEVERE, "Command "+command+" failed to run with: "+output);
}
}
}
}
catch (IllegalArgumentException iae)
{
// System.out.println("Command: " + command + ", did not match: " + output.getBuffer());
// ignored for now
}
}
// if we didnt find any commands matching
if (cl == null)
{
console.pushToStdOut(output.getBuffer() + ": command not found."
+ Config.getLineSeparator());
}
// hack to just read one and one line when we're testing
if (Settings.getInstance().getName().equals("test"))
break;

if (!console.isRunning())
{
break;
}
}
}
console.start();
}

public AddonRegistry getRegistry()
Expand Down Expand Up @@ -251,4 +200,58 @@ private Prompt createPrompt()

return new Prompt(chars);
}

class ForgeConsoleCallback implements ConsoleCallback {
@Override
public int readConsoleOutput(ConsoleOutput output) throws IOException {
CommandLine cl = null;
if (output.getBuffer() != null && !output.getBuffer().trim().isEmpty())
{
//TODO: should clean this up
try {
verifyLoadedCommands();
} catch (Exception e) {
e.printStackTrace();
}
for (ShellCommand command : commands)
{
try
{
cl = command.parse(output.getBuffer());
if (cl != null)
{
// need some way of deciding if the command is standalone
if (command.getContext().isStandalone())
{
// console.

}
else
{
try {
command.run(output, cl);
break;
}
catch (Exception e) {
logger.log(Level.SEVERE, "Command "+command+" failed to run with: "+output);
}
}
}
}
catch (IllegalArgumentException iae)
{
// System.out.println("Command: " + command + ", did not match: " + output.getBuffer());
// ignored for now
}
}
// if we didnt find any commands matching
if (cl == null)
{
console.pushToStdOut(output.getBuffer() + ": command not found."
+ Config.getLineSeparator());
}
}
return 0;
}
}
}
23 changes: 18 additions & 5 deletions aesh/src/main/java/org/jboss/forge/aesh/ShellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
public class ShellCommand implements Completion {

private CommandLineParser parser;
private UICommand command;
private ShellContext context;

Expand Down Expand Up @@ -62,15 +61,16 @@ public UICommand getCommand() {
}

public void generateParser(UICommand command) {
parser = CommandLineUtil.generateParser(command, context);
context.setParser(CommandLineUtil.generateParser(command, context));
}

public CommandLine parse(String line) throws IllegalArgumentException {
return parser.parse(line);
return context.getParser().parse(line);
}

public void run(ConsoleOutput consoleOutput, CommandLine commandLine) throws Exception {
CommandLineUtil.populateUIInputs(commandLine, context, getAeshell().getRegistry());
context.setConsoleOutput(consoleOutput);
Result result = command.execute(context);
if(result != null &&
result.getMessage() != null && result.getMessage().length() > 0)
Expand All @@ -83,14 +83,14 @@ public boolean isStandalone() {

@Override
public void complete(CompleteOperation completeOperation) {
ParameterInt param = parser.getParameters().get(0);
ParameterInt param = context.getParser().getParameters().get(0);
//complete command names
if(param.getName().startsWith(completeOperation.getBuffer()))
completeOperation.addCompletionCandidate(param.getName());
//complete options/arguments
else if(completeOperation.getBuffer().startsWith(param.getName())) {
ParsedCompleteObject completeObject =
new CommandLineCompletionParser(parser).findCompleteObject(completeOperation.getBuffer());
new CommandLineCompletionParser(context.getParser()).findCompleteObject(completeOperation.getBuffer());
if(completeObject.doDisplayOptions()) {
if(param.getOptionLongNamesWithDash().size() > 1) {
completeOperation.addCompletionCandidates( param.getOptionLongNamesWithDash());
Expand Down Expand Up @@ -142,6 +142,10 @@ else if(completeObject.isArgument()) {
else if(inputOption != null && inputOption.getValueType() == Boolean.class) {
//TODO
}
//check if the command actually implements Completion
else if(command instanceof Completion) {
((Completion) command).complete(completeOperation);
}
else {
//this shouldnt be needed
if(inputOption != null && inputOption instanceof UIInputMany) {
Expand All @@ -161,4 +165,13 @@ else if(inputOption != null && inputOption.getValueType() == Boolean.class) {

}
}

@Override
public String toString() {
return "ShellCommand{" +
"command=" + command +
", context=" + context +
", aeshell=" + aeshell +
'}';
}
}
24 changes: 24 additions & 0 deletions aesh/src/main/java/org/jboss/forge/aesh/ShellContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Iterator;
import java.util.List;

import org.jboss.aesh.cl.CommandLineParser;
import org.jboss.aesh.console.ConsoleOutput;
import org.jboss.forge.ui.UIBuilder;
import org.jboss.forge.ui.context.UIContext;
import org.jboss.forge.ui.context.UIValidationContext;
Expand All @@ -25,6 +27,8 @@ public class ShellContext implements UIValidationContext, UIContext, UIBuilder
private boolean standalone = false;
private List<UIInputComponent<?, ?>> inputs = new ArrayList<UIInputComponent<?, ?>>();
private ForgeShell aeshell;
private CommandLineParser parser;
private ConsoleOutput consoleOutput;

public ShellContext(ForgeShell aeshell)
{
Expand All @@ -46,6 +50,26 @@ public ForgeShell getShell()
return aeshell;
}

public void setParser(CommandLineParser parser)
{
this.parser = parser;
}

public CommandLineParser getParser()
{
return parser;
}

public void setConsoleOutput(ConsoleOutput output)
{
this.consoleOutput = output;
}

public ConsoleOutput getConsoleOutput()
{
return consoleOutput;
}

@Override
public UIBuilder add(UIInputComponent<?, ?> input)
{
Expand Down
Loading

0 comments on commit 8f8b06f

Please sign in to comment.