/** * Clears the text after the given bookmark. * The bookmark name can be subsequently used in a call to {@link #resetToBookmark(String)} in order to clear the text after this bookmark. * Since not all terminals support this feature, the default implementation calls {@link #println()} and returns false. * @return true, if the terminal supports bookmarking, the given bookmark exists and the text after it has been successfully cleared. */ default boolean resetToBookmark(String bookmark) { println(); return false; }
/** * Moves the cursor to the start of the current line of text in order to allow overwriting the current line. * Since not all terminals support this feature, the default implementation calls {@link #println()} and returns false. * @return true, if the terminal supports this feature and the cursor has been moved to the start of the current line. */ default boolean moveToLineStart() { println(); return false; }
/** * Clears the current line of text. * Since not all terminals support this feature, the default implementation calls {@link #println()} and returns false. * @return true, if the terminal supports this feature and the current line has been successfully cleared. */ default boolean resetLine() { println(); return false; }
public static void printGsonMessage(TextTerminal<?> terminal, String initData) { if(initData != null && !initData.isEmpty()) { String message = new Gson().fromJson(initData, String.class); if(message != null && !message.isEmpty()) { terminal.println(message); } } } }
public static void printGsonMessage(TextTerminal<?> terminal, String initData) { if(initData != null && !initData.isEmpty()) { String message = new Gson().fromJson(initData, String.class); if(message != null && !message.isEmpty()) { terminal.println(message); } } } }
/** * Prints a message that possibly contains line separators and subsequently prints a line separator. */ default void println(String message) { print(message); println(); }
/** * Prints each message in the list, inserting the line separator string between messages. * A separator string is also printed after the last message. * The messages in the list may contain line separators. */ default void println(List<String> messages) { print(messages); println(); }
/** * Prints each message in the list in its raw form, inserting the line separator string between messages. * No separator string is printed after the last message. * The behavior is undefined if one or more strings in the list contain line separators. */ default void rawPrint(List<String> messages) { if(messages != null && !messages.isEmpty()) { rawPrint(messages.get(0)); messages.subList(1, messages.size()).forEach(msg -> { println(); print(msg); }); } }
private T getValueFromIndex(String sVal, TextTerminal<?> textTerminal) { try { int optIndex = Integer.parseInt(sVal); if(optIndex > 0 && optIndex <= possibleValues.size()) { return possibleValues.get(optIndex - 1); } } catch (NumberFormatException e) { // Continue the execution. The next statement will print the error message. } textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> { textTerminal.print(getDefaultErrorMessage(sVal)); textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + "."); }); textTerminal.println(); return null; }
private T getValueFromString(String sVal, TextTerminal<?> textTerminal) { ParseResult<T> result = parseAndCheck(sVal); List<String> errMessages = result.getErrorMessages(); if(errMessages == null) { Optional<T> value = getPossibleValue(result.getValue()); if(value.isPresent()) return value.get(); textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> { t.print(getDefaultErrorMessage(sVal)); if(inlinePossibleValues) { String options = possibleValues.stream() .map(val -> "'" + valueFormatter.apply(val) + "'") .collect(Collectors.joining(", ")); t.println(" Please enter one of: " + options + "."); } else { t.println(" Please enter one of the displayed values."); } }); textTerminal.println(); } else { textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> t.println(errMessages)); textTerminal.println(); } return null; }
terminal.println(); return new ReadHandlerData(ReadInterruptionStrategy.Action.RETURN) .withReturnValueProvider(partialInput -> partialInput.isEmpty() ? "nothing" : "high-quality-" + partialInput); terminal.println("No handlers can be registered."); } else { terminal.println("--------------------------------------------------------------------------------"); if(registeredReboot) { terminal.println("Press " + keyStrokeReboot + " to display a 'reboot' message box"); terminal.println("Press " + keyStrokeAutoValue + " to provide a product name based on the current input text"); terminal.println("Press " + keyStrokeHelp + " to print a help message"); terminal.println("Press " + keyStrokeAbort + " to abort the program"); terminal.println("You can use these key combinations at any moment during your data entry session."); terminal.println("--------------------------------------------------------------------------------"); } catch (ReadAbortedException e) { terminal.executeWithPropertiesPrefix("abort", t -> t.println("\nRead aborted by user " + e.getPayload())); break; terminal.executeWithPropertiesPrefix("content", t ->t.println("Your shopping list contains: " + content)); terminal.println();
terminal.println("Seconds to start:"); for(int i=5; i>=0; i--) { terminal.resetLine(); terminal.println("################"); terminal.println("# WEATHER INFO #"); terminal.println("################"); terminal.println(); t.println("-------------------------------------------------------"); t.println(" Temperature Wind speed Atmospheric pressure"); t.println("-------------------------------------------------------"); for(int i=0; i<20; i++) { t.moveToLineStart(); terminal.println();terminal.println();terminal.println();
protected String readWithPrompt(TextTerminal<?> textTerminal, List<String> prompt) { printPrompt(prompt, textTerminal); while(true) { String sInput = null; try { sInput = textTerminal.read(inputMasking); } catch (ReadInterruptionException e) { ReadInterruptionData data = e.getReadInterruptionData(); logger.debug("ReadInterruptionException with data: " + data); switch (data.getAction()) { case CONTINUE: logger.error("ReadInterruptionException with action CONTINUE."); // no break here: handle it as RESTART case RESTART: if(data.isRedrawRequired()) { logger.trace("Re-printing prompt before read restart"); textTerminal.println(); printPrompt(prompt, textTerminal); logger.trace("Prompt re-printed."); } continue; case RETURN: return data.getReturnValue(); case ABORT: throw new ReadAbortedException(data.getPayload(), e.getPartialInput()); } } return sInput; } }
@Override public void accept(TextIO textIO, RunnerData runnerData) { TextTerminal<?> terminal = textIO.getTextTerminal(); String initData = (runnerData == null) ? null : runnerData.getInitData(); AppUtil.printGsonMessage(terminal, initData); terminal.executeWithPropertiesPrefix("custom.title", t -> t.print("Cuboid dimensions: ")); terminal.println(); double length = textIO.newDoubleInputReader() .withMinVal(0.0) .withPropertiesPrefix("custom.length") .read("Length"); double width = textIO.newDoubleInputReader() .withMinVal(0.0) .withPropertiesPrefix("custom.width") .read("Width"); double height = textIO.newDoubleInputReader() .withMinVal(0.0) .withPropertiesPrefix("custom.height") .read("Height"); terminal.executeWithPropertiesPrefix("custom.title", t -> t.print("The volume of your cuboid is: " + length * width * height)); terminal.println(); textIO.newStringInputReader() .withMinLength(0) .withPropertiesPrefix("custom.neutral") .read("\nPress enter to terminate..."); textIO.dispose(); }
props.setPromptUnderline(true); props.setPromptColor("cyan"); terminal.println("Order details"); props.setPromptUnderline(true); props.setPromptColor("green"); terminal.println("\nShipping Information"); props.setPromptUnderline(true); props.setPromptColor("white"); terminal.println("\nPayment Details"); props.setPromptUnderline(true); props.setPromptColor("red"); terminal.println("\nOrder Overview");