Skip to content

Commit

Permalink
Fix ManCommand Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Feb 21, 2013
1 parent 8f8b06f commit 53dd039
Showing 1 changed file with 113 additions and 95 deletions.
208 changes: 113 additions & 95 deletions aesh/src/main/java/org/jboss/forge/aesh/commands/ManCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
*/
package org.jboss.forge.aesh.commands;

import java.net.URL;

import javax.inject.Inject;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.aesh.console.Config;
import org.jboss.aesh.console.Console;
import org.jboss.aesh.console.ConsoleOutput;
import org.jboss.aesh.extensions.manual.Man;
import org.jboss.aesh.util.Parser;
import org.jboss.forge.aesh.ShellContext;
Expand All @@ -19,111 +22,126 @@
import org.jboss.forge.ui.UIBuilder;
import org.jboss.forge.ui.UICommand;
import org.jboss.forge.ui.UICommandMetadata;
import org.jboss.forge.ui.base.UICommandMetadataBase;
import org.jboss.forge.ui.context.UIContext;
import org.jboss.forge.ui.context.UIValidationContext;
import org.jboss.forge.ui.input.UIInputMany;
import org.jboss.forge.ui.result.Result;
import org.jboss.forge.ui.result.Results;

import javax.inject.Inject;
import java.io.IOException;
import java.net.URL;
import org.jboss.forge.ui.util.Metadata;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
*/
public class ManCommand implements UICommand, Completion {

private AddonRegistry registry;

@Inject
private UIInputMany<String> arguments;

@Inject
public ManCommand(AddonRegistry registry) {
this.registry = registry;
}

@Override
public UICommandMetadata getMetadata() {
return new UICommandMetadataBase("man",
"man - an interface to the online reference manuals",
UICommandMetadataBase.getDocLocationFor(ManCommand.class));
}

@Override
public boolean isEnabled(UIContext context) {
return true;
}

@Override
public void initializeUI(UIBuilder builder) throws Exception {
arguments.setLabel("");
arguments.setRequired(false);
builder.add(arguments);
}

@Override
public void validate(UIValidationContext validator) {

}

@Override
public Result execute(UIContext context) throws Exception {
if(arguments.getValue() != null &&
context instanceof ShellContext) {
Console console = ((ShellContext) context).getShell().getConsole();
try {
Man man = new Man(console);
//for now we only try to display the first
String commandName = arguments.getValue().iterator().next();
URL docUrl = getCommand(commandName);
if(docUrl != null) {
man.setFile(docUrl.openStream(), docUrl.getPath());
man.attach(((ShellContext) context).getConsoleOutput());
}
else
console.pushToStdOut("No manual page found for: "+commandName+ Config.getLineSeparator());
public class ManCommand implements UICommand, Completion
{

private AddonRegistry registry;

@Inject
private UIInputMany<String> arguments;

@Inject
public ManCommand(AddonRegistry registry)
{
this.registry = registry;
}

@Override
public UICommandMetadata getMetadata()
{
return Metadata.forCommand(getClass()).name("man")
.description("man - an interface to the online reference manuals");
}

@Override
public boolean isEnabled(UIContext context)
{
return true;
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
arguments.setLabel("");
arguments.setRequired(false);
builder.add(arguments);
}

@Override
public void validate(UIValidationContext validator)
{

}

@Override
public Result execute(UIContext context) throws Exception
{
if (arguments.getValue() != null &&
context instanceof ShellContext)
{
Console console = ((ShellContext) context).getShell().getConsole();
try
{
Man man = new Man(console);
// for now we only try to display the first
String commandName = arguments.getValue().iterator().next();
URL docUrl = getCommand(commandName);
if (docUrl != null)
{
man.setFile(docUrl.openStream(), docUrl.getPath());
man.attach(((ShellContext) context).getConsoleOutput());
}
catch (Exception ioe) {
return Results.fail(ioe.getMessage());
}
}
return null;
}

@Override
public void complete(CompleteOperation completeOperation) {
try {
//list all commands
if(completeOperation.getBuffer().trim().equals("man")) {
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class)) {
completeOperation.addCompletionCandidate(instance.get().getMetadata().getName());
}
else
console.pushToStdOut("No manual page found for: " + commandName + Config.getLineSeparator());

}
catch (Exception ioe)
{
return Results.fail(ioe.getMessage());
}
}
return null;
}

@Override
public void complete(CompleteOperation completeOperation)
{
try
{
// list all commands
if (completeOperation.getBuffer().trim().equals("man"))
{
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class))
{
completeOperation.addCompletionCandidate(instance.get().getMetadata().getName());
}
//find the last
else {
String item = Parser.findEscapedSpaceWordCloseToEnd(completeOperation.getBuffer().trim());
completeOperation.setOffset( completeOperation.getCursor()-
item.length());
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class)) {
if(instance.get().getMetadata().getName().startsWith(item))
completeOperation.addCompletionCandidate(instance.get().getMetadata().getName());
}
}
// find the last
else
{
String item = Parser.findEscapedSpaceWordCloseToEnd(completeOperation.getBuffer().trim());
completeOperation.setOffset(completeOperation.getCursor() -
item.length());
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class))
{
if (instance.get().getMetadata().getName().startsWith(item))
completeOperation.addCompletionCandidate(instance.get().getMetadata().getName());
}
}
catch(Exception e) {
e.printStackTrace();
}
}

private URL getCommand(String name) {
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class)) {
if(instance.get().getMetadata().getName().equals(name))
return instance.get().getMetadata().getDocLocation();
}
return null;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

private URL getCommand(String name)
{
for (ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class))
{
if (instance.get().getMetadata().getName().equals(name))
return instance.get().getMetadata().getDocLocation();
}
return null;
}
}

0 comments on commit 53dd039

Please sign in to comment.