Skip to content

Commit

Permalink
Almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 19, 2013
1 parent 0729d6f commit 7f65f07
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -30,8 +30,7 @@
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.aesh.ForgeConsoleCallback;
import org.jboss.forge.addon.shell.aesh.ShellCommand;
import org.jboss.forge.addon.shell.aesh.completion.ForgeCommandCompletion;
import org.jboss.forge.addon.shell.aesh.completion.ForgeOptionCompletion;
import org.jboss.forge.addon.shell.aesh.completion.ForgeCompletion;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.shell.ui.ShellContextImpl;
import org.jboss.forge.addon.ui.CommandExecutionListener;
Expand Down Expand Up @@ -97,8 +96,7 @@ void init(Settings settings)
console = null;
}
console = new Console(settings);
console.addCompletion(new ForgeCommandCompletion(this));
console.addCompletion(new ForgeOptionCompletion(this));
console.addCompletion(new ForgeCompletion(this));
console.setConsoleCallback(new ForgeConsoleCallback(this));
try
{
Expand Down Expand Up @@ -158,31 +156,21 @@ public Map<String, ShellCommand> getEnabledShellCommands(ShellContext context)
}

/**
* Used in {@link ForgeOptionCompletion} and {@link ForgeConsoleCallback}
* Used in {@link ForgeCompletion} and {@link ForgeConsoleCallback}
*/
public ShellCommand findCommand(ShellContext shelLContext, String line)
public ShellCommand findCommand(ShellContext shellContext, String line)
{
String[] tokens = line.split(" ");
if (tokens.length >= 1)
{
return getEnabledShellCommands(shelLContext).get(tokens[0]);
return getEnabledShellCommands(shellContext).get(tokens[0]);
}
return null;
}

/**
* Use {@link ForgeCommandCompletion}
*/
public Iterable<ShellCommand> findMatchingCommands(ShellContext shellContext, String line)
public Collection<ShellCommand> findMatchingCommands(ShellContext shellContext, String line)
{
Set<ShellCommand> result = new TreeSet<ShellCommand>(new Comparator<ShellCommand>()
{
@Override
public int compare(ShellCommand o1, ShellCommand o2)
{
return o1.getName().compareTo(o2.getName());
}
});
Set<ShellCommand> result = new TreeSet<ShellCommand>();

String[] tokens = line == null ? new String[0] : line.split(" ");
if (tokens.length <= 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jboss.forge.addon.shell;

import java.io.File;

import javax.annotation.PreDestroy;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
Expand All @@ -11,6 +9,7 @@
import org.jboss.aesh.console.settings.SettingsBuilder;
import org.jboss.forge.furnace.event.PostStartup;
import org.jboss.forge.furnace.event.PreShutdown;
import org.jboss.forge.furnace.util.OperatingSystemUtils;

/**
* Starts up the shell if not in the IDE
Expand All @@ -30,7 +29,7 @@ public void startupDefaultShell(@Observes PostStartup startup) throws Exception
if (!Boolean.getBoolean("forge.compatibility.IDE"))
{
Settings settings = new SettingsBuilder().create();
this.shell = shellFactory.createShell(new File(""), settings);
this.shell = shellFactory.createShell(OperatingSystemUtils.getUserHomeDir(), settings);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import org.jboss.aesh.cl.internal.ParameterInt;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.shell.util.ShellUtil;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.ManyValued;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.input.UISelectMany;
Expand Down Expand Up @@ -52,15 +55,14 @@ public CommandLineParser generateParser(UICommand command,
ParserBuilder builder = new ParserBuilder();

UICommandMetadata metadata = command.getMetadata();
ParameterInt parameter = new ParameterInt(metadata.getName(), metadata.getDescription());
ParameterInt parameter = new ParameterInt(ShellUtil.shellifyName(metadata.getName()), metadata.getDescription());
for (InputComponent<?, Object> input : inputs.values())
{
if (!input.getName().equals("arguments"))
{
Object defaultValue = InputComponents.getValueFor(input);
// TODO
boolean isMultiple = false;
boolean flagOnly = false;
boolean isMultiple = input instanceof ManyValued;
boolean hasValue = (InputComponents.getInputType(input) != InputType.CHECKBOX);
try
{
OptionBuilder optionBuilder = new OptionBuilder();
Expand All @@ -69,7 +71,7 @@ public CommandLineParser generateParser(UICommand command,
.defaultValue(defaultValue == null ? null : defaultValue.toString())
.description(input.getLabel())
.hasMultipleValues(isMultiple)
.hasValue(flagOnly)
.hasValue(hasValue)
.required(input.isRequired());

parameter.addOption(optionBuilder.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class ShellCommand
public class ShellCommand implements Comparable<ShellCommand>
{
private final String name;
private final UICommand command;
Expand Down Expand Up @@ -135,4 +135,10 @@ public String toString()
{
return getName();
}

@Override
public int compareTo(ShellCommand o)
{
return getName().compareTo(o.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.input.InputComponent;

/**
* Typed completion
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface CompletionStrategy
{
/**
* Invoked when an autocomplete for a specific component is required
*
* @param completeOperation
* @param input
* @param context
* @param typedValue
*/
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.forge.addon.ui.facets.HintsFacet;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;

/**
* Returns the completion based on the input component
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class CompletionStrategyFactory
{
public static CompletionStrategy getCompletionFor(InputComponent<?, Object> component)
{
InputType inputType = component.getFacet(HintsFacet.class).getInputType();
switch (inputType)
{
case FILE_PICKER:
return new FileInputCompletionStrategy(false);
case DIRECTORY_PICKER:
return new FileInputCompletionStrategy(true);
default:
return new DefaultInputCompletionStrategy();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UICompleter;
import org.jboss.forge.addon.ui.util.InputComponents;

public class DefaultInputCompletionStrategy implements CompletionStrategy
{
@Override
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue)
{
UICompleter<Object> completer = InputComponents.getCompleterFor(input);
if (completer != null)
{
for (String proposal : completer.getCompletionProposals(context, input, typedValue))
{
completeOperation.addCompletionCandidate(proposal);
}
}
else
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jboss.forge.addon.shell.aesh.completion;

import java.io.File;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.util.FileLister;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.context.UISelection;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.furnace.util.OperatingSystemUtils;

class FileInputCompletionStrategy implements CompletionStrategy
{

private final boolean directory;

public FileInputCompletionStrategy(boolean directory)
{
this.directory = directory;
}

@Override
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue)
{
completeOperation.setOffset(completeOperation.getCursor());

final File cwd;
Object value = InputComponents.getValueFor(input);
if (value == null)
{
UISelection<FileResource<?>> selection = context.getInitialSelection();
cwd = selection.isEmpty() ? OperatingSystemUtils.getUserHomeDir() : selection.get()
.getUnderlyingResourceObject();
}
else
{
// TODO: Use ConverterFactory ?
cwd = new File(value.toString());
}
FileLister fileLister = new FileLister(typedValue == null ? "" : typedValue, cwd,
directory ? FileLister.Filter.DIRECTORY
: FileLister.Filter.ALL);
fileLister.findMatchingDirectories(completeOperation);
}
}

This file was deleted.

Loading

0 comments on commit 7f65f07

Please sign in to comment.