-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
shell/impl/src/main/java/org/jboss/forge/addon/shell/ui/ShellUIPromptImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright 2013 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 | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell.ui; | ||
|
||
import java.io.PrintStream; | ||
import java.util.List; | ||
|
||
import org.jboss.aesh.console.AeshConsole; | ||
import org.jboss.aesh.console.command.CommandOperation; | ||
import org.jboss.aesh.console.command.invocation.CommandInvocation; | ||
import org.jboss.forge.addon.ui.input.UIPrompt; | ||
|
||
/** | ||
* Implementation of {@link UIPrompt} | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class ShellUIPromptImpl implements UIPrompt | ||
{ | ||
private final AeshConsole console; | ||
private final CommandInvocation commandInvocation; | ||
|
||
public ShellUIPromptImpl(AeshConsole console, CommandInvocation commandInvocation) | ||
{ | ||
this.console = console; | ||
this.commandInvocation = commandInvocation; | ||
} | ||
|
||
private String toString(List<CommandOperation> operationList) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
for (CommandOperation commandOperation : operationList) | ||
{ | ||
sb.append(commandOperation.getInputKey().getAsChar()); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public String prompt() | ||
{ | ||
List<CommandOperation> input = commandInvocation.getInput(); | ||
String output = toString(input); | ||
return output; | ||
} | ||
|
||
@Override | ||
public boolean promptBoolean(String message) | ||
{ | ||
PrintStream out = console.getShell().out(); | ||
out.print(message + " [y/N]"); | ||
List<CommandOperation> input = commandInvocation.getInput(); | ||
out.println(); | ||
String output = toString(input); | ||
return "Y".equalsIgnoreCase(output); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
ui/api/src/main/java/org/jboss/forge/addon/ui/input/UIPrompt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright 2013 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 | ||
*/ | ||
|
||
package org.jboss.forge.addon.ui.input; | ||
|
||
|
||
/** | ||
* Used when an addon needs to prompt for ad-hoc user input | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface UIPrompt | ||
{ | ||
/** | ||
* Prompt for user input, and return as a String. | ||
*/ | ||
String prompt(); | ||
|
||
/** | ||
* Prompt for boolean user input (Y/n), first printing the given message, then returning user input as a boolean. The | ||
* value returned will default to <code>true</code> if an empty or whitespace-only user input is read. | ||
*/ | ||
boolean promptBoolean(String message); | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
ui/example/src/main/java/org/jboss/forge/addon/ui/example/commands/PromptCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2013 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 | ||
*/ | ||
|
||
package org.jboss.forge.addon.ui.example.commands; | ||
|
||
import org.jboss.forge.addon.ui.AbstractUICommand; | ||
import org.jboss.forge.addon.ui.UIProvider; | ||
import org.jboss.forge.addon.ui.context.UIBuilder; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
import org.jboss.forge.addon.ui.context.UIExecutionContext; | ||
import org.jboss.forge.addon.ui.input.UIPrompt; | ||
import org.jboss.forge.addon.ui.metadata.UICommandMetadata; | ||
import org.jboss.forge.addon.ui.output.UIOutput; | ||
import org.jboss.forge.addon.ui.result.Result; | ||
import org.jboss.forge.addon.ui.util.Metadata; | ||
|
||
/** | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class PromptCommand extends AbstractUICommand | ||
{ | ||
|
||
@Override | ||
public void initializeUI(UIBuilder builder) throws Exception | ||
{ | ||
|
||
} | ||
|
||
@Override | ||
public UICommandMetadata getMetadata(UIContext context) | ||
{ | ||
return Metadata.forCommand(PromptCommand.class).name("prompt").description("Prompts for Information"); | ||
} | ||
|
||
@Override | ||
public Result execute(UIExecutionContext context) throws Exception | ||
{ | ||
UIProvider provider = context.getUIContext().getProvider(); | ||
UIOutput output = provider.getOutput(); | ||
UIPrompt prompt = provider.getPrompt(); | ||
boolean answer = prompt.promptBoolean("Do you love Forge 2?"); | ||
output.out().println("You answered: " + answer); | ||
return null; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
ui/ui-test-harness/src/main/java/org/jboss/forge/ui/test/impl/UIPromptImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright 2013 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 | ||
*/ | ||
|
||
package org.jboss.forge.ui.test.impl; | ||
|
||
import org.jboss.forge.addon.ui.input.UIPrompt; | ||
|
||
/** | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class UIPromptImpl implements UIPrompt | ||
{ | ||
|
||
@Override | ||
public String prompt() | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean promptBoolean(String message) | ||
{ | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters