@Override public String promptSecret(final String message, final String defaultIfEmpty) { if (this.isAcceptDefaults()) { return defaultIfEmpty; } String secret = promptSecret(message + " [ENTER for default]"); if ((secret == null) || secret.trim().isEmpty()) secret = defaultIfEmpty; return secret; }
@Override public boolean promptBoolean(final String message, final boolean defaultIfEmpty) { return prompt(message, Boolean.class, defaultIfEmpty); }
@Override public boolean promptBoolean(final String message) { return promptBoolean(message, true); }
@Override public String promptAndSwallowCR() { int c; StringBuilder buf = new StringBuilder(); while (((c = scan()) != '\n') && (c != '\r')) { if ((c == 127) || (c == 8)) { if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); cursorLeft(1); print(" "); cursorLeft(1); flush(); } continue; } write((byte) c); buf.append((char) c); flush(); } return buf.toString(); }
public <T> T promptChoiceTyped(final String message, List<T> options, final T defaultIfEmpty) if (this.isAcceptDefaults()) println(message); println(); for (T entry : options) print(ShellColor.BOLD, " " + count + " - [" + entry + "]"); print(" " + count + " - [" + entry + "]"); print(ShellColor.BOLD, " " + count + " - (none)"); print("*"); println(); count++; println(); int input = prompt( "Choose an option by typing the number of the selection " + renderColor(ShellColor.BOLD, "[*-default] "), Integer.class, 0) - 1; if ((input >= 0) && (input < options.size())) println("Invalid selection, please try again.");
@Override public FileResource<?> promptFile(final String message, final FileResource<?> defaultIfEmpty) { if (this.isAcceptDefaults()) { return defaultIfEmpty; } FileResource<?> result = defaultIfEmpty; String query = " [ .../" + defaultIfEmpty.getName() + "] "; String path = promptWithCompleter(message + query, new FileNameCompleter()); if ((path != null) && !path.trim().isEmpty()) { path = Files.canonicalize(path); Resource<File> resource = getResourceFactory().getResourceFrom(new File(path)); if (resource instanceof FileResource) { result = (FileResource<?>) resource; } else { result = null; } } return result; }
public <T extends Enum<T>> T promptEnum(final String message, final Class<T> type, final T defaultIfEmpty) if (this.isAcceptDefaults()) String value = promptWithCompleter(message + query, new CompleterAdaptor( new EnumCompleter(type))); if (result == null) result = promptChoiceTyped(message + query, Arrays.asList(type.getEnumConstants()), defaultIfEmpty);
@Override public String promptCommon(final String message, final PromptType type, final String defaultIfEmpty) { if (this.isAcceptDefaults()) { return defaultIfEmpty; } if (!type.matches(defaultIfEmpty)) { throw new IllegalArgumentException("Default value [" + defaultIfEmpty + "] is not a valid match for the given prompt type [" + type.name() + "]"); } String input; do { String query = " [" + defaultIfEmpty + "] "; input = prompt(message + query); if ((input == null) || input.isEmpty()) { input = defaultIfEmpty; } } while (!type.matches(input)); input = getPromptTypeConverter().convert(type, input); return input; }
@Override public String promptRegex(final String message, final String pattern, final String defaultIfEmpty) { if (this.isAcceptDefaults()) { return defaultIfEmpty; } if (!defaultIfEmpty.matches(pattern)) { throw new IllegalArgumentException("Default value [" + defaultIfEmpty + "] does not match required pattern [" + pattern + "]"); } String input; do { String query = " [" + defaultIfEmpty + "] "; input = prompt(message + query); if ("".equals(input.trim())) { input = defaultIfEmpty; } } while (!input.matches(pattern)); return input; }
println(message); println(); for (Object entry : options) println(" " + count + " - [" + entry + "]"); println(" " + count + " - (none)"); println(); int input = prompt("Choose an option by typing the number of the selection: ", Integer.class) - 1; if (input < options.size()) println("Invalid selection, please try again.");
@Override public String promptCompleter(final String string, final Class<? extends CommandCompleter> type) { return promptWithCompleter(string, new CompleterAdaptor(BeanManagerUtils.getContextualInstance(getBeanManager(), type))); }
@Override public String promptCommon(final String message, final PromptType type) { String input; do { input = prompt(message); } while (!type.matches(input)); input = getPromptTypeConverter().convert(type, input); return input; }
public <T> T promptChoice(final String message, final Map<String, T> options) println(message); final List<Entry<String, T>> entries = new ArrayList<Map.Entry<String, T>>(); entries.addAll(options.entrySet()); println(); for (Entry<String, T> entry : entries) println(" " + count + " - [" + entry.getKey() + "]"); count++; println(); String input = promptWithCompleter("Choose an option by typing the name or number of the selection: ", new ChoiceCompleter()); if (options.containsKey(input))
@Override public FileResource<?> promptFile(final String message) { String path = ""; while ((path == null) || path.trim().isEmpty()) { path = promptWithCompleter(message, new FileNameCompleter()); } path = Files.canonicalize(path); Resource<File> resource = getResourceFactory().getResourceFrom(new File(path)); if (resource instanceof FileResource) { return (FileResource<?>) resource; } return null; }
public <T> T prompt(final String message, final Class<T> clazz, final T defaultIfEmpty) if (this.isAcceptDefaults()) input = prompt(message + query); if ((input == null) || "".equals(input.trim()))
println(message); println(); for (T entry : options) println(" " + count + " - [" + entry + "]"); println(" " + count + " - (none)"); println(); int input = prompt("Choose an option by typing the number of the selection: ", Integer.class) - 1; if ((input >= 0) && (input < options.size())) println("Invalid selection, please try again.");
@Override public String prompt() { return prompt(""); }
@Override public String prompt(final String message, final String defaultIfEmpty) { return prompt(message, String.class, defaultIfEmpty); }
@Override public String promptRegex(final String message, final String regex) { String input; do { input = prompt(message); } while (!input.matches(regex)); return input; }
@Override @SuppressWarnings("unchecked") public <T> T prompt(final String message, final Class<T> clazz) { Object result; Object input; do { input = prompt(message); try { result = DataConversion.convert(input, clazz); } catch (Exception e) { result = InvalidInput.INSTANCE; } } while ((result instanceof InvalidInput)); return (T) result; }