Skip to content

Commit

Permalink
Still not working
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 16, 2013
1 parent a55a8c4 commit 7dae016
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -162,16 +163,37 @@ public Map<String, ShellCommand> getEnabledShellCommands()
/**
* Used in {@link ForgeOptionCompletion} and {@link ForgeConsoleCallback}
*/
public ShellCommand findCommand(String buffer)
public ShellCommand findCommand(String line)
{
String[] tokens = buffer.split(" ");
String[] tokens = line.split(" ");
if (tokens.length >= 1)
{
return getEnabledShellCommands().get(tokens[0]);
}
return null;
}

/**
* Use {@link ForgeCommandCompletion}
*/
public Iterable<ShellCommand> findMatchingCommands(String line)
{
List<ShellCommand> result = new ArrayList<ShellCommand>();
Map<String, ShellCommand> commandMap = getEnabledShellCommands();

String[] tokens = line == null ? new String[0] : line.split(" ");
if (tokens.length <= 1)
{
String token = (tokens.length == 1) ? tokens[0] : null;
for (Entry<String, ShellCommand> entry : commandMap.entrySet())
{
if (token == null || entry.getKey().startsWith(token))
result.add(entry.getValue());
}
}
return result;
}

public Result execute(ShellCommand shellCommand)
{
// TODO: Fire pre/post listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,33 @@ public ForgeConsoleCallback(ShellImpl shell)
@Override
public int readConsoleOutput(ConsoleOutput output) throws IOException
{
String line = output.getBuffer();
ShellCommand command = shell.findCommand(line);
if (command == null)
{
throw new IOException("Command not found for line: " + line);
}
try
{
command.populateInputs(line);
String line = output.getBuffer();
ShellCommand command = shell.findCommand(line);
if (command == null)
{
throw new IOException("Command not found for line: " + line);
}
try
{
command.populateInputs(line);
}
catch (CommandLineParserException e)
{
throw new IOException(e);
}
Result result = shell.execute(command);
if (result != null)
{
shell.getConsole().pushToStdOut(result.getMessage());
}
}
catch (CommandLineParserException e)
catch (Exception e)
{
throw new IOException(e);
shell.getConsole().pushToStdErr("**ERROR**: " + e.getMessage());
return -1;
}
Result result = shell.execute(command);
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import java.util.Map;

import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.CommandLineCompletionParser;
import org.jboss.aesh.cl.CommandLineParser;
import org.jboss.aesh.cl.ParsedCompleteObject;
import org.jboss.aesh.cl.exception.CommandLineParserException;
import org.jboss.aesh.cl.internal.ParameterInt;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.shell.ui.ShellUIBuilderImpl;
import org.jboss.forge.addon.shell.util.ShellUtil;
Expand Down Expand Up @@ -75,14 +78,28 @@ public Map<String, InputComponent<?, Object>> getInputs()
return inputs;
}

public CommandLineParser getCommandLineParser()
public CommandLine parse(String line) throws CommandLineParserException
{
return commandLineParser;
return commandLineParser.parse(line);
}

public ParameterInt getParameter()
{
return commandLineParser.getParameters().get(0);
}

public ParsedCompleteObject parseCompleteObject(String line) throws CommandLineParserException
{
return new CommandLineCompletionParser(commandLineParser).findCompleteObject(line);
}

public void populateInputs(String line) throws CommandLineParserException
{
CommandLine commandLine = this.commandLineParser.parse(line);
this.commandLineUtil.populateUIInputs(parse(line), inputs);
}

public void populateInputs(CommandLine commandLine) throws CommandLineParserException
{
this.commandLineUtil.populateUIInputs(commandLine, inputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

package org.jboss.forge.addon.shell.aesh.completion;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.forge.addon.shell.ShellImpl;
Expand All @@ -32,29 +27,12 @@ public ForgeCommandCompletion(ShellImpl shell)
@Override
public void complete(CompleteOperation completeOperation)
{
Iterable<ShellCommand> commands = findMatchingCommands(completeOperation);
Iterable<ShellCommand> commands = shell.findMatchingCommands(completeOperation.getBuffer());
for (ShellCommand cmd : commands)
{
String name = cmd.getName();
completeOperation.addCompletionCandidate(name);
}
}

private Iterable<ShellCommand> findMatchingCommands(CompleteOperation completeOperation)
{
List<ShellCommand> result = new ArrayList<ShellCommand>();
Map<String, ShellCommand> commandMap = shell.getEnabledShellCommands();

String[] tokens = completeOperation.getBuffer().split(String.valueOf(completeOperation.getSeparator()));
if (tokens.length <= 1)
{
String token = (tokens.length == 1) ? tokens[0] : null;
for (Entry<String, ShellCommand> entry : commandMap.entrySet())
{
if (token == null || entry.getKey().startsWith(token))
result.add(entry.getValue());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
package org.jboss.forge.addon.shell.aesh.completion;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.jboss.aesh.cl.CommandLineCompletionParser;
import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.ParsedCompleteObject;
import org.jboss.aesh.cl.exception.CommandLineParserException;
import org.jboss.aesh.cl.internal.ParameterInt;
Expand All @@ -21,6 +23,8 @@
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.ShellImpl;
import org.jboss.forge.addon.shell.aesh.ShellCommand;
import org.jboss.forge.addon.ui.facets.HintsFacet;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.HasCompleter;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.SelectComponent;
Expand All @@ -44,62 +48,71 @@ public ForgeOptionCompletion(ShellImpl shellImpl)
@Override
public void complete(CompleteOperation completeOperation)
{
ShellCommand cmd = shell.findCommand(completeOperation.getBuffer());
String line = completeOperation.getBuffer();
ShellCommand cmd = shell.findCommand(line);
if (cmd != null)
{
try
{
// We are dealing with one-level commands atm.
// Eg. new-project-type --named ... instead of new-project-type setup --named ...
ParameterInt param = cmd.getCommandLineParser().getParameters().get(0);
ParsedCompleteObject completeObject = new CommandLineCompletionParser(cmd.getCommandLineParser())
.findCompleteObject(completeOperation.getBuffer());
ParameterInt param = cmd.getParameter();
ParsedCompleteObject completeObject = cmd.parseCompleteObject(line);
List<String> optionNames = param.getOptionLongNamesWithDash();

try
{
CommandLine commandLine = cmd.parse(line);
removeExistingOptions(commandLine, optionNames);
cmd.populateInputs(commandLine);
}
catch (CommandLineParserException parseException)
{
// ignore for now
}

if (completeObject.doDisplayOptions())
{
// we have a partial/full name
if (completeObject.getName() != null && completeObject.getName().length() > 0)
if (completeObject.getName() != null && !completeObject.getName().isEmpty())
{
if (param.findPossibleLongNamesWitdDash(completeObject.getName()).size() > 0)
List<String> possibleOptions = param.findPossibleLongNamesWitdDash(completeObject.getName());
if (!possibleOptions.isEmpty())
{
// only one param
if (param.findPossibleLongNamesWitdDash(completeObject.getName()).size() == 1)
if (possibleOptions.size() == 1)
{
completeOperation.addCompletionCandidate(param.findPossibleLongNamesWitdDash(
completeObject.getName()).get(0));
completeOperation.addCompletionCandidate(possibleOptions.get(0));
completeOperation.setOffset(completeOperation.getCursor() -
completeObject.getOffset());
}
// multiple params
else
completeOperation.addCompletionCandidates(param.findPossibleLongNamesWitdDash(completeObject
.getName()));
{
completeOperation.addCompletionCandidates(possibleOptions);
}
}
}
// display all our params
else
{
completeOperation.addCompletionCandidates(param.getOptionLongNamesWithDash());
if (param.getOptionLongNamesWithDash().size() == 1)
completeOperation.addCompletionCandidates(optionNames);
if (optionNames.size() == 1)
{
completeOperation.setOffset(completeOperation.getCursor() -
completeObject.getOffset());
completeOperation.setOffset(completeOperation.getCursor() - completeObject.getOffset());
}
}
}
// try to complete an options value
// try to complete an options value "--xxx"
else if (completeObject.isOption())
{
optionCompletion(completeOperation, completeObject, cmd);
}
// try to complete a argument value
// try to complete a argument value Eg: ls . (. is the argument)
else if (completeObject.isArgument())
{
argumentCompletion(completeOperation, completeObject, cmd);
}
else
{
completeOperation.addCompletionCandidates(param.getOptionLongNamesWithDash());
}
}
catch (CommandLineParserException e)
{
Expand All @@ -109,6 +122,18 @@ else if (completeObject.isArgument())
}
}

private void removeExistingOptions(CommandLine commandLine, List<String> availableOptions)
{
Iterator<String> it = availableOptions.iterator();
while (it.hasNext())
{
if (commandLine.hasOption(it.next()))
{
it.remove();
}
}
}

@SuppressWarnings({ "rawtypes" })
private void defaultCompletion(CompleteOperation completeOperation, ShellCommand shellCommand)
throws CommandLineParserException
Expand All @@ -119,13 +144,12 @@ private void defaultCompletion(CompleteOperation completeOperation, ShellCommand
// use the arguments completor as default if it has any
if (inputOption != null)
{
argumentCompletion(completeOperation, new CommandLineCompletionParser(shellCommand.getCommandLineParser())
.findCompleteObject(completeOperation.getBuffer()), shellCommand);
argumentCompletion(completeOperation, shellCommand.parseCompleteObject(completeOperation.getBuffer()),
shellCommand);
}
else
{
completeOperation.addCompletionCandidates(shellCommand.getCommandLineParser().getParameters().get(0)
.getOptionLongNamesWithDash());
completeOperation.addCompletionCandidates(shellCommand.getParameter().getOptionLongNamesWithDash());
}
}

Expand Down Expand Up @@ -237,7 +261,8 @@ private void argumentCompletion(CompleteOperation completeOperation, ParsedCompl
ShellCommand shellCommand)
{
InputComponent inputOption = shellCommand.getInputs().get("arguments"); // default for arguments

// InputType inputType = ((HintsFacet) inputOption.getFacet(HintsFacet.class)).getInputType();
InputType inputType = InputType.FILE_PICKER;
// use the arguments completor as default if it has any
if (inputOption != null
&& (inputOption instanceof HasCompleter && ((HasCompleter) inputOption).getCompleter() != null))
Expand All @@ -251,7 +276,7 @@ private void argumentCompletion(CompleteOperation completeOperation, ParsedCompl
}
}

else if (inputOption != null && inputOption.getValueType() == File.class)
else if (inputType == InputType.FILE_PICKER)
{
completeOperation.setOffset(completeOperation.getCursor());
if (completeObject.getValue() == null)
Expand All @@ -261,7 +286,7 @@ else if (inputOption != null && inputOption.getValueType() == File.class)
new FileLister(completeObject.getValue(), new File(System.getProperty("user.dir")))
.findMatchingDirectories(completeOperation);
}
else if (inputOption != null && inputOption.getValueType() == Boolean.class)
else if (inputType == InputType.CHECKBOX)
{
// TODO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.forge.addon.shell.ShellImpl;

/**
* Typed completion
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface InputTypeCompletion
{
public void complete(ShellImpl shellImpl, CompleteOperation completeOperation);
}
Loading

0 comments on commit 7dae016

Please sign in to comment.