Skip to content

Commit

Permalink
Still Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 16, 2013
1 parent 06b1db1 commit ff5e0e0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,12 @@ void init(Settings settings)
console = null;
}
console = new Console(settings);

console.addCompletion(new ForgeCompositeCompletion(
new ForgeCommandCompletion(this, addonRegistry),
new ForgeOptionCompletion(this, addonRegistry)));


console.addCompletion(new ForgeCompositeCompletion(new ForgeCommandCompletion(this), new ForgeOptionCompletion(
this)));

console.setConsoleCallback(new ForgeConsoleCallback(this, addonRegistry));

try
{
console.setPrompt(createInitialPrompt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.util.Metadata;

/**
* Encapsulates a {@link UICommand} to be useful in a Shell context
Expand All @@ -30,8 +29,8 @@
*/
public class ShellCommand
{
private final String name;
private final UICommand command;
private final UICommandMetadata metadata;
private final ShellContextImpl context;
private final CommandLineParser commandLineParser;
private Map<String, InputComponent<?, Object>> inputs;
Expand All @@ -44,7 +43,7 @@ public ShellCommand(UICommand command, ShellContextImpl shellContext, CommandLin
{
this.command = command;
UICommandMetadata commandMetadata = command.getMetadata();
this.metadata = Metadata.from(commandMetadata).name(ShellUtil.shellifyName(commandMetadata.getName()));
this.name = ShellUtil.shellifyName(commandMetadata.getName());

// Create ShellContext
this.context = shellContext;
Expand All @@ -63,14 +62,14 @@ public ShellCommand(UICommand command, ShellContextImpl shellContext, CommandLin
this.commandLineParser = commandLineUtil.generateParser(this.command, inputs);
}

public UICommandMetadata getMetadata()
public UICommand getCommand()
{
return metadata;
return command;
}

public UICommand getCommand()
public String getName()
{
return command;
return name;
}

public Map<String, InputComponent<?, Object>> getInputs()
Expand Down Expand Up @@ -104,7 +103,7 @@ public boolean equals(Object o)

ShellCommand that = (ShellCommand) o;

if (!getMetadata().getName().equals(that.getMetadata().getName()))
if (!getName().equals(that.getName()))
return false;

return true;
Expand All @@ -113,12 +112,12 @@ public boolean equals(Object o)
@Override
public int hashCode()
{
return getMetadata().getName().hashCode();
return getName().hashCode();
}

@Override
public String toString()
{
return getMetadata().getName();
return getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.jboss.aesh.complete.Completion;
import org.jboss.forge.addon.shell.ShellImpl;
import org.jboss.forge.addon.shell.aesh.ShellCommand;
import org.jboss.forge.furnace.addons.AddonRegistry;

/**
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
Expand All @@ -24,12 +23,10 @@ public class ForgeCommandCompletion implements Completion
// private static final Logger logger = Logger.getLogger(ForgeCommandCompletion.class.getName());

private ShellImpl shell;
private AddonRegistry addonRegistry;

public ForgeCommandCompletion(ShellImpl shell, AddonRegistry addonRegistry)
public ForgeCommandCompletion(ShellImpl shell)
{
this.shell = shell;
this.addonRegistry = addonRegistry;
}

@Override
Expand All @@ -38,7 +35,7 @@ public void complete(CompleteOperation completeOperation)
Iterable<ShellCommand> commands = findMatchingCommands(completeOperation);
for (ShellCommand cmd : commands)
{
String name = cmd.getMetadata().getName();
String name = cmd.getName();
completeOperation.addCompletionCandidate(name);
}
}
Expand All @@ -54,7 +51,7 @@ private Iterable<ShellCommand> findMatchingCommands(CompleteOperation completeOp
String token = (tokens.length == 1) ? tokens[0] : null;
for (ShellCommand cmd : commands)
{
if (token == null || cmd.getMetadata().getName().startsWith(token))
if (token == null || cmd.getName().startsWith(token))
result.add(cmd);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.input.SingleValued;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.furnace.addons.AddonRegistry;

/**
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
Expand All @@ -36,12 +35,26 @@ public class ForgeOptionCompletion implements Completion
private static final Logger logger = Logger.getLogger(ForgeOptionCompletion.class.getName());

private ShellImpl shell;
private AddonRegistry addonRegistry;

public ForgeOptionCompletion(ShellImpl shellImpl, AddonRegistry addonRegistry)
public ForgeOptionCompletion(ShellImpl shellImpl)
{
this.shell = shellImpl;
this.addonRegistry = addonRegistry;
}

private ShellCommand findCurrentCommand(CompleteOperation completeOperation)
{
Iterable<ShellCommand> commands = shell.getEnabledShellCommands();

String[] tokens = completeOperation.getBuffer().split(String.valueOf(completeOperation.getSeparator()));
if (tokens.length >= 1)
{
for (ShellCommand cmd : commands)
{
if (cmd.getName().equals(tokens[0]))
return cmd;
}
}
return null;
}

@Override
Expand All @@ -52,8 +65,9 @@ public void complete(CompleteOperation completeOperation)
{
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);

// complete command names
String paramName = param.getName();

Expand Down Expand Up @@ -129,22 +143,6 @@ else if (completeObject.isArgument())
}
}

private ShellCommand findCurrentCommand(CompleteOperation completeOperation)
{
Iterable<ShellCommand> commands = shell.getEnabledShellCommands();

String[] tokens = completeOperation.getBuffer().split(String.valueOf(completeOperation.getSeparator()));
if (tokens.length >= 1)
{
for (ShellCommand cmd : commands)
{
if (cmd.getMetadata().getName().equals(tokens[0]))
return cmd;
}
}
return null;
}

@SuppressWarnings({ "rawtypes" })
private void defaultCompletion(CompleteOperation completeOperation, ShellCommand shellCommand)
throws CommandLineParserException
Expand Down

0 comments on commit ff5e0e0

Please sign in to comment.