Skip to content

Commit

Permalink
FORGE-2040: Enhanced man to show allowed values for SelectComponents …
Browse files Browse the repository at this point in the history
…and default values when supplied
  • Loading branch information
lincolnthree committed Sep 24, 2014
1 parent 00aa9c3 commit a76385e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.aesh.console.helper.ManProvider;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.shell.ShellImpl;
import org.jboss.forge.addon.shell.ui.ShellContextImpl;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.ManyValued;
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.furnace.util.Streams;
Expand Down Expand Up @@ -122,6 +126,7 @@ public UIBuilder add(InputComponent<?, ?> input)
result = result.replaceAll("%synopsis%", buildSynopsis(cmd, context, inputs));
result = result.replaceAll("%options%", buildOptions(cmd, context, inputs));
result = result.replaceAll("%addon%", getSourceAddonName(cmd, context));
result = result.replaceAll("%year%", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));

return new ByteArrayInputStream(result.getBytes());
}
Expand Down Expand Up @@ -181,6 +186,7 @@ else if (input.isRequired())
return result.toString();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private String buildOptions(UICommand cmd, UIContext context, List<InputComponent<?, ?>> inputs)
{
StringBuilder result = new StringBuilder();
Expand All @@ -202,12 +208,14 @@ private String buildOptions(UICommand cmd, UIContext context, List<InputComponen
}
else
{
result.append(" ");
if (input.getShortName() != InputComponents.DEFAULT_SHORT_NAME)
result.append("-").append(input.getShortName()).append(" ");
result.append("*-").append(input.getShortName()).append("* ");
else
result.append(" ");

result.append(input.getName());
result.append("\n ");
result.append("*--").append(input.getName()).append("*");
result.append("\n");
result.append(" ");

if (!input.getName().equals(input.getLabel()))
{
Expand All @@ -222,6 +230,62 @@ private String buildOptions(UICommand cmd, UIContext context, List<InputComponen
if (input.isRequired())
result.append(" (*required*)");

if (input instanceof SelectComponent)
{
result.append(" Valid choices: [");
Iterable<?> valueChoices = ((SelectComponent) input).getValueChoices();
Converter itemLabelConverter = ((SelectComponent) input).getItemLabelConverter();

This comment has been minimized.

Copy link
@gastaldi

gastaldi Sep 25, 2014

Member

Use InputComponents.getItemLabelConverter instead

for (Object choice : valueChoices)
{
if (choice != null)
{
Object itemLabel = choice.toString();
if (itemLabelConverter != null)
itemLabel = itemLabelConverter.convert(choice);
result.append("\"" + itemLabel + "\" ");
}
}
result.append("] ");

if (input.hasDefaultValue() && input.hasValue())
{
result.append(" defaults to: [");
if (input instanceof ManyValued)
{
Iterable values = ((ManyValued) input).getValue();
if (values.iterator().hasNext())
{
for (Object value : values)
{
if (value != null)
{
Object itemLabel = value.toString();
if (itemLabelConverter != null)
itemLabel = itemLabelConverter.convert(value);
result.append("\"" + itemLabel + "\" ");
}
}
}
else
{
Object value = input.getValue();
if (value != null)
{
Object itemLabel = value.toString();
if (itemLabelConverter != null)
itemLabel = itemLabelConverter.convert(value);
result.append("\"" + itemLabel + "\" ");
}
}
}
result.append("]");
}
}
else if (input.hasDefaultValue())
{
result.append(" defaults to: [" + input.getValue() + "]");
}

result.append("\n\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ forge: <http://forge.jboss.org/>

COPYING
-------
Copyright 2013 Red Hat, Inc. and/or its affiliates.
Copyright %year% Red Hat, Inc. and/or its affiliates.
Licensed under the Eclipse Public License version 1.0, available at
http://www.eclipse.org/legal/epl-v10.html

0 comments on commit a76385e

Please sign in to comment.