diff --git a/console-ui/jline-console-ui.bat b/console-ui/jline-console-ui.bat index 3edca9fb7..a167d4e81 100644 --- a/console-ui/jline-console-ui.bat +++ b/console-ui/jline-console-ui.bat @@ -59,6 +59,6 @@ popd echo Launching ConsoleUI... echo Classpath: %cp% -java -cp %cp% %opts% org.jline.consoleui.examples.Basic +java -cp %cp% %opts% org.jline.consoleui.examples.BasicDynamic :END \ No newline at end of file diff --git a/console-ui/jline-console-ui.sh b/console-ui/jline-console-ui.sh index f878b4fda..00730f61f 100755 --- a/console-ui/jline-console-ui.sh +++ b/console-ui/jline-console-ui.sh @@ -80,5 +80,5 @@ echo "Classpath: $cp" set mouse=a java -cp $cp \ $opts \ - org.jline.consoleui.examples.Basic + org.jline.consoleui.examples.BasicDynamic diff --git a/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java b/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java index 83b3ebfb1..558ae8a01 100644 --- a/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java +++ b/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java @@ -29,10 +29,12 @@ /** * ConsolePrompt encapsulates the prompting of a list of input questions for the user. */ -public class ConsolePrompt { +public class ConsolePrompt implements AutoCloseable { protected final LineReader reader; protected final Terminal terminal; protected final UiConfig config; + private Attributes attributes; + private List header = new ArrayList<>(); /** * @@ -66,6 +68,29 @@ public ConsolePrompt(LineReader reader, Terminal terminal, UiConfig config) { } config.setReaderOptions(options); } + attributes = terminal.enterRawMode(); + terminal.puts(InfoCmp.Capability.enter_ca_mode); + terminal.puts(InfoCmp.Capability.keypad_xmit); + terminal.writer().flush(); + } + + @Override + public void close() { + if (terminalInRawMode()) { + terminal.setAttributes(attributes); + terminal.puts(InfoCmp.Capability.exit_ca_mode); + terminal.puts(InfoCmp.Capability.keypad_local); + terminal.writer().flush(); + for (AttributedString as : header) { + as.println(terminal); + } + terminal.writer().flush(); + attributes = null; + } + } + + private boolean terminalInRawMode() { + return attributes != null; } /** @@ -79,6 +104,7 @@ public ConsolePrompt(LineReader reader, Terminal terminal, UiConfig config) { * @throws IOException may be thrown by terminal * @throws UserInterruptException if user interrupt handling is enabled and the user types the interrupt character (ctrl-C) */ + @Deprecated public Map prompt(List promptableElementList) throws IOException, UserInterruptException { return prompt(new ArrayList<>(), promptableElementList); @@ -96,53 +122,13 @@ public Map prompt(List promptab * @throws IOException may be thrown by terminal * @throws UserInterruptException if user interrupt handling is enabled and the user types the interrupt character (ctrl-C) */ + @Deprecated public Map prompt( List header, List promptableElementList) throws IOException, UserInterruptException { - Attributes attributes = terminal.enterRawMode(); - boolean cancelled = false; try { - terminal.puts(InfoCmp.Capability.enter_ca_mode); - terminal.puts(InfoCmp.Capability.keypad_xmit); - terminal.writer().flush(); - Map resultMap = new HashMap<>(); - - for (int i = 0; i < promptableElementList.size(); i++) { - PromptableElementIF pe = promptableElementList.get(i); - PromptResultItemIF result = promptElement(header, pe); - if (result == null) { - // Prompt was cancelled by the user - if (i > 0) { - // Remove last result - header.remove(header.size() - 1); - // Go back to previous prompt - i -= 2; - continue; - } else { - if (config.cancellableFirstPrompt()) { - cancelled = true; - return null; - } else { - // Repeat current prompt - i -= 1; - continue; - } - } - } - String resp = result.getDisplayResult(); - if (result instanceof ConfirmResult) { - ConfirmResult cr = (ConfirmResult) result; - if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) { - resp = config.resourceBundle().getString("confirmation_yes_answer"); - } else { - resp = config.resourceBundle().getString("confirmation_no_answer"); - } - } - AttributedStringBuilder message = createMessage(pe.getMessage(), resp); - header.add(message.toAttributedString()); - resultMap.put(pe.getName(), result); - } + prompt(header, promptableElementList, resultMap); return resultMap; } catch (IOError e) { if (e.getCause() instanceof InterruptedIOException) { @@ -151,16 +137,81 @@ public Map prompt( throw e; } } finally { - terminal.setAttributes(attributes); - terminal.puts(InfoCmp.Capability.exit_ca_mode); - terminal.puts(InfoCmp.Capability.keypad_local); - terminal.writer().flush(); - if (!cancelled) { - for (AttributedString as : header) { - as.println(terminal); + close(); + } + } + + /** + * Prompt a list of choices (questions). This method takes a list of promptable elements, typically + * created with {@link PromptBuilder}. Each of the elements is processed and the user entries and + * answers are filled in to the result map. The result map contains the key of each promptable element + * and the user entry as an object implementing {@link PromptResultItemIF}. + * + * @param promptableElementList the list of questions / prompts to ask the user for. + * @param resultMap a map containing a result for each element of promptableElementList + * @throws IOException may be thrown by terminal + */ + public void prompt(List promptableElementList, Map resultMap) + throws IOException { + prompt(new ArrayList<>(), promptableElementList, resultMap); + } + + /** + * Prompt a list of choices (questions). This method takes a list of promptable elements, typically + * created with {@link PromptBuilder}. Each of the elements is processed and the user entries and + * answers are filled in to the result map. The result map contains the key of each promptable element + * and the user entry as an object implementing {@link PromptResultItemIF}. + * + * @param headerIn info to be displayed before first prompt. + * @param promptableElementList the list of questions / prompts to ask the user for. + * @param resultMap a map containing a result for each element of promptableElementList + * @throws IOException may be thrown by terminal + */ + public void prompt( + List headerIn, + List promptableElementList, + Map resultMap) + throws IOException { + if (!terminalInRawMode()) { + throw new IllegalStateException("Terminal is not in raw mode! Maybe ConsolePrompt is closed?"); + } + this.header = headerIn; + + for (int i = resultMap.isEmpty() ? 0 : resultMap.size() - 1; i < promptableElementList.size(); i++) { + PromptableElementIF pe = promptableElementList.get(i); + PromptResultItemIF result = promptElement(header, pe); + if (result == null) { + // Prompt was cancelled by the user + if (i > 0) { + // Remove last result + header.remove(header.size() - 1); + // Go back to previous prompt + i -= 2; + continue; + } else { + if (config.cancellableFirstPrompt()) { + header.remove(header.size() - 1); + resultMap.clear(); + return; + } else { + // Repeat current prompt + i -= 1; + continue; + } + } + } + String resp = result.getDisplayResult(); + if (result instanceof ConfirmResult) { + ConfirmResult cr = (ConfirmResult) result; + if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) { + resp = config.resourceBundle().getString("confirmation_yes_answer"); + } else { + resp = config.resourceBundle().getString("confirmation_no_answer"); } - terminal.writer().flush(); } + AttributedStringBuilder message = createMessage(pe.getMessage(), resp); + header.add(message.toAttributedString()); + resultMap.put(pe.getName(), result); } } diff --git a/console-ui/src/test/java/org/jline/consoleui/Issue1025.java b/console-ui/src/test/java/org/jline/consoleui/Issue1025.java index 9c376375d..368adf7f2 100644 --- a/console-ui/src/test/java/org/jline/consoleui/Issue1025.java +++ b/console-ui/src/test/java/org/jline/consoleui/Issue1025.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -87,107 +88,109 @@ private static void test(Terminal terminal) throws IOException { // If you are not using Completers you do not need to create LineReader. // LineReader reader = LineReaderBuilder.builder().terminal(terminal).build(); - ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config); - PromptBuilder promptBuilder = prompt.getPromptBuilder(); - - promptBuilder - .createInputPrompt() - .name("name") - .message("Please enter your name") - .defaultValue("John Doe") - // .mask('*') - .addCompleter( - // new Completers.FilesCompleter(() -> Paths.get(System.getProperty("user.dir")))) - new StringsCompleter("Jim", "Jack", "John", "Donald", "Dock")) - .addPrompt(); - - promptBuilder - .createListPrompt() - .name("pizzatype") - .message("Which pizza do you want?") - .newItem() - .text("Margherita") - .add() // without name (name defaults to text) - .newItem("veneziana") - .text("Veneziana") - .add() - .newItem("hawai") - .text("Hawai") - .add() - .newItem("quattro") - .text("Quattro Stagioni") - .add() - .addPrompt(); - - promptBuilder - .createCheckboxPrompt() - .name("topping") - .message("Please select additional toppings:") - .newSeparator("standard toppings") - .add() - .newItem() - .name("cheese") - .text("Cheese") - .add() - .newItem("bacon") - .text("Bacon") - .add() - .newItem("onions") - .text("Onions") - .disabledText("Sorry. Out of stock.") - .add() - .newSeparator() - .text("special toppings") - .add() - .newItem("salami") - .text("Very hot salami") - .check() - .add() - .newItem("salmon") - .text("Smoked Salmon") - .add() - .newSeparator("and our speciality...") - .add() - .newItem("special") - .text("Anchovies, and olives") - .checked(true) - .add() - .addPrompt(); - - promptBuilder - .createChoicePrompt() - .name("payment") - .message("How do you want to pay?") - .newItem() - .name("cash") - .message("Cash") - .key('c') - .asDefault() - .add() - .newItem("visa") - .message("Visa Card") - .key('v') - .add() - .newItem("master") - .message("Master Card") - .key('m') - .add() - .newSeparator("online payment") - .add() - .newItem("paypal") - .message("Paypal") - .key('p') - .add() - .addPrompt(); - - promptBuilder - .createConfirmPromp() - .name("delivery") - .message("Is this pizza for delivery?") - .defaultValue(ConfirmChoice.ConfirmationValue.YES) - .addPrompt(); - - Map result = prompt.prompt(header, promptBuilder.build()); + Map result = new HashMap<>(); + try (ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config)) { + PromptBuilder promptBuilder = prompt.getPromptBuilder(); + + promptBuilder + .createInputPrompt() + .name("name") + .message("Please enter your name") + .defaultValue("John Doe") + // .mask('*') + .addCompleter( + // new Completers.FilesCompleter(() -> Paths.get(System.getProperty("user.dir")))) + new StringsCompleter("Jim", "Jack", "John", "Donald", "Dock")) + .addPrompt(); + + promptBuilder + .createListPrompt() + .name("pizzatype") + .message("Which pizza do you want?") + .newItem() + .text("Margherita") + .add() // without name (name defaults to text) + .newItem("veneziana") + .text("Veneziana") + .add() + .newItem("hawai") + .text("Hawai") + .add() + .newItem("quattro") + .text("Quattro Stagioni") + .add() + .addPrompt(); + + promptBuilder + .createCheckboxPrompt() + .name("topping") + .message("Please select additional toppings:") + .newSeparator("standard toppings") + .add() + .newItem() + .name("cheese") + .text("Cheese") + .add() + .newItem("bacon") + .text("Bacon") + .add() + .newItem("onions") + .text("Onions") + .disabledText("Sorry. Out of stock.") + .add() + .newSeparator() + .text("special toppings") + .add() + .newItem("salami") + .text("Very hot salami") + .check() + .add() + .newItem("salmon") + .text("Smoked Salmon") + .add() + .newSeparator("and our speciality...") + .add() + .newItem("special") + .text("Anchovies, and olives") + .checked(true) + .add() + .addPrompt(); + + promptBuilder + .createChoicePrompt() + .name("payment") + .message("How do you want to pay?") + .newItem() + .name("cash") + .message("Cash") + .key('c') + .asDefault() + .add() + .newItem("visa") + .message("Visa Card") + .key('v') + .add() + .newItem("master") + .message("Master Card") + .key('m') + .add() + .newSeparator("online payment") + .add() + .newItem("paypal") + .message("Paypal") + .key('p') + .add() + .addPrompt(); + + promptBuilder + .createConfirmPromp() + .name("delivery") + .message("Is this pizza for delivery?") + .defaultValue(ConfirmChoice.ConfirmationValue.YES) + .addPrompt(); + + prompt.prompt(header, promptBuilder.build(), result); + } System.out.println("result = " + result); ConfirmResult delivery = (ConfirmResult) result.get("delivery"); diff --git a/console-ui/src/test/java/org/jline/consoleui/examples/Basic.java b/console-ui/src/test/java/org/jline/consoleui/examples/Basic.java index c532b5aa4..806cd7825 100644 --- a/console-ui/src/test/java/org/jline/consoleui/examples/Basic.java +++ b/console-ui/src/test/java/org/jline/consoleui/examples/Basic.java @@ -9,6 +9,7 @@ package org.jline.consoleui.examples; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -66,107 +67,110 @@ public static void main(String[] args) { // If you are not using Completers you do not need to create LineReader. // LineReader reader = LineReaderBuilder.builder().terminal(terminal).build(); - ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config); - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + Map result = new HashMap<>(); - promptBuilder - .createInputPrompt() - .name("name") - .message("Please enter your name") - .defaultValue("John Doe") - // .mask('*') - .addCompleter( - // new Completers.FilesCompleter(() -> Paths.get(System.getProperty("user.dir")))) - new StringsCompleter("Jim", "Jack", "John", "Donald", "Dock")) - .addPrompt(); + try (ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config)) { + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - promptBuilder - .createListPrompt() - .name("pizzatype") - .message("Which pizza do you want?") - .newItem() - .text("Margherita") - .add() // without name (name defaults to text) - .newItem("veneziana") - .text("Veneziana") - .add() - .newItem("hawai") - .text("Hawai") - .add() - .newItem("quattro") - .text("Quattro Stagioni") - .add() - .addPrompt(); + promptBuilder + .createInputPrompt() + .name("name") + .message("Please enter your name") + .defaultValue("John Doe") + // .mask('*') + .addCompleter( + // new Completers.FilesCompleter(() -> Paths.get(System.getProperty("user.dir")))) + new StringsCompleter("Jim", "Jack", "John", "Donald", "Dock")) + .addPrompt(); - promptBuilder - .createCheckboxPrompt() - .name("topping") - .message("Please select additional toppings:") - .newSeparator("standard toppings") - .add() - .newItem() - .name("cheese") - .text("Cheese") - .add() - .newItem("bacon") - .text("Bacon") - .add() - .newItem("onions") - .text("Onions") - .disabledText("Sorry. Out of stock.") - .add() - .newSeparator() - .text("special toppings") - .add() - .newItem("salami") - .text("Very hot salami") - .check() - .add() - .newItem("salmon") - .text("Smoked Salmon") - .add() - .newSeparator("and our speciality...") - .add() - .newItem("special") - .text("Anchovies, and olives") - .checked(true) - .add() - .addPrompt(); + promptBuilder + .createListPrompt() + .name("pizzatype") + .message("Which pizza do you want?") + .newItem() + .text("Margherita") + .add() // without name (name defaults to text) + .newItem("veneziana") + .text("Veneziana") + .add() + .newItem("hawai") + .text("Hawai") + .add() + .newItem("quattro") + .text("Quattro Stagioni") + .add() + .addPrompt(); - promptBuilder - .createChoicePrompt() - .name("payment") - .message("How do you want to pay?") - .newItem() - .name("cash") - .message("Cash") - .key('c') - .asDefault() - .add() - .newItem("visa") - .message("Visa Card") - .key('v') - .add() - .newItem("master") - .message("Master Card") - .key('m') - .add() - .newSeparator("online payment") - .add() - .newItem("paypal") - .message("Paypal") - .key('p') - .add() - .addPrompt(); + promptBuilder + .createCheckboxPrompt() + .name("topping") + .message("Please select additional toppings:") + .newSeparator("standard toppings") + .add() + .newItem() + .name("cheese") + .text("Cheese") + .add() + .newItem("bacon") + .text("Bacon") + .add() + .newItem("onions") + .text("Onions") + .disabledText("Sorry. Out of stock.") + .add() + .newSeparator() + .text("special toppings") + .add() + .newItem("salami") + .text("Very hot salami") + .check() + .add() + .newItem("salmon") + .text("Smoked Salmon") + .add() + .newSeparator("and our speciality...") + .add() + .newItem("special") + .text("Anchovies, and olives") + .checked(true) + .add() + .addPrompt(); - promptBuilder - .createConfirmPromp() - .name("delivery") - .message("Is this pizza for delivery?") - .defaultValue(ConfirmChoice.ConfirmationValue.YES) - .addPrompt(); + promptBuilder + .createChoicePrompt() + .name("payment") + .message("How do you want to pay?") + .newItem() + .name("cash") + .message("Cash") + .key('c') + .asDefault() + .add() + .newItem("visa") + .message("Visa Card") + .key('v') + .add() + .newItem("master") + .message("Master Card") + .key('m') + .add() + .newSeparator("online payment") + .add() + .newItem("paypal") + .message("Paypal") + .key('p') + .add() + .addPrompt(); - Map result = prompt.prompt(header, promptBuilder.build()); + promptBuilder + .createConfirmPromp() + .name("delivery") + .message("Is this pizza for delivery?") + .defaultValue(ConfirmChoice.ConfirmationValue.YES) + .addPrompt(); + + prompt.prompt(header, promptBuilder.build(), result); + } System.out.println("result = " + result); ConfirmResult delivery = (ConfirmResult) result.get("delivery"); diff --git a/console-ui/src/test/java/org/jline/consoleui/examples/BasicDynamic.java b/console-ui/src/test/java/org/jline/consoleui/examples/BasicDynamic.java index 100715278..0371209cd 100644 --- a/console-ui/src/test/java/org/jline/consoleui/examples/BasicDynamic.java +++ b/console-ui/src/test/java/org/jline/consoleui/examples/BasicDynamic.java @@ -8,7 +8,7 @@ */ package org.jline.consoleui.examples; -import java.io.InterruptedIOException; +import java.io.IOError; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -71,31 +71,31 @@ public static void main(String[] args) { // If you are not using Completers you do not need to create LineReader. // LineReader reader = LineReaderBuilder.builder().terminal(terminal).build(); - ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config); + Map result1 = new HashMap<>(), + result2 = new HashMap<>(), + result3 = new HashMap<>(); - Map result1 = null, result2 = null, result3 = null; - while (result2 == null) { - List header1 = new ArrayList<>(header); - result1 = prompt.prompt(header1, pizzaOrHamburgerPrompt(prompt).build()); - if (result1 == null) { - System.out.println("User cancelled order."); - return; - } - while (result3 == null) { - if ("Pizza".equals(result1.get("product").getResult())) { - result2 = prompt.prompt(pizzaPrompt(prompt).build()); - } else { - result2 = prompt.prompt(hamburgerPrompt(prompt).build()); + try (ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config)) { + while (result2.isEmpty()) { + prompt.prompt(header, pizzaOrHamburgerPrompt(prompt).build(), result1); + if (result1.isEmpty()) { + throw new Exception("User cancelled order."); } - if (result2 == null) { - break; + while (result3.isEmpty()) { + if ("Pizza".equals(result1.get("product").getResult())) { + prompt.prompt(header, pizzaPrompt(prompt).build(), result2); + } else { + prompt.prompt(header, hamburgerPrompt(prompt).build(), result2); + } + if (result2.isEmpty()) { + break; + } + prompt.prompt(header, finalPrompt(prompt).build(), result3); } - result3 = prompt.prompt(finalPrompt(prompt).build()); } } - Map result = new HashMap<>(); - result.putAll(result1); + Map result = new HashMap<>(result1); result.putAll(result2); result.putAll(result3); System.out.println("result = " + result); @@ -105,10 +105,10 @@ public static void main(String[] args) { System.out.println("We will deliver the order in 5 minutes"); } - } catch (InterruptedIOException e) { - System.out.println("Exiting application."); + } catch (IOError e) { + System.out.println("-c pressed"); } catch (Exception e) { - e.printStackTrace(); + System.out.println(e.getMessage()); } } @@ -124,7 +124,7 @@ static PromptBuilder pizzaOrHamburgerPrompt(ConsolePrompt prompt) { promptBuilder .createListPrompt() .name("product") - .message("Which do you want to order?") + .message("What do you want to order?") .newItem() .text("Pizza") .add() // without name (name defaults to text) diff --git a/console-ui/src/test/java/org/jline/consoleui/examples/LongList.java b/console-ui/src/test/java/org/jline/consoleui/examples/LongList.java index 92ed5a386..83f0df223 100644 --- a/console-ui/src/test/java/org/jline/consoleui/examples/LongList.java +++ b/console-ui/src/test/java/org/jline/consoleui/examples/LongList.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,29 +38,35 @@ public static void main(String[] args) { try (Terminal terminal = TerminalBuilder.builder().build()) { ConsolePrompt.UiConfig config = new ConsolePrompt.UiConfig(">", "( )", "(x)", "( )"); - ConsolePrompt prompt = new ConsolePrompt(terminal, config); - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + Map result = new HashMap<>(); - ListPromptBuilder listPrompt = promptBuilder.createListPrompt(); - listPrompt.name("longlist").message("What's your favourite Letter?").relativePageSize(66); + try (ConsolePrompt prompt = new ConsolePrompt(terminal, config)) { + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - for (char letter = 'A'; letter <= 'C'; letter++) - for (char letter2 = 'A'; letter2 <= 'Z'; letter2++) - listPrompt.newItem().text("" + letter + letter2).add(); - listPrompt.addPrompt(); + ListPromptBuilder listPrompt = promptBuilder.createListPrompt(); + listPrompt + .name("longlist") + .message("What's your favourite Letter?") + .relativePageSize(66); - CheckboxPromptBuilder checkboxPrompt = promptBuilder.createCheckboxPrompt(); - checkboxPrompt - .name("longcheckbox") - .message("What's your favourite Letter? Select all you want...") - .relativePageSize(66); + for (char letter = 'A'; letter <= 'C'; letter++) + for (char letter2 = 'A'; letter2 <= 'Z'; letter2++) + listPrompt.newItem().text("" + letter + letter2).add(); + listPrompt.addPrompt(); - for (char letter = 'A'; letter <= 'C'; letter++) - for (char letter2 = 'A'; letter2 <= 'Z'; letter2++) - checkboxPrompt.newItem().text("" + letter + letter2).add(); - checkboxPrompt.addPrompt(); + CheckboxPromptBuilder checkboxPrompt = promptBuilder.createCheckboxPrompt(); + checkboxPrompt + .name("longcheckbox") + .message("What's your favourite Letter? Select all you want...") + .relativePageSize(66); - Map result = prompt.prompt(header, promptBuilder.build()); + for (char letter = 'A'; letter <= 'C'; letter++) + for (char letter2 = 'A'; letter2 <= 'Z'; letter2++) + checkboxPrompt.newItem().text("" + letter + letter2).add(); + checkboxPrompt.addPrompt(); + + prompt.prompt(header, promptBuilder.build(), result); + } System.out.println("result = " + result); } catch (IOException e) { e.printStackTrace(); diff --git a/console-ui/src/test/java/org/jline/consoleui/examples/SimpleExample.java b/console-ui/src/test/java/org/jline/consoleui/examples/SimpleExample.java index 6d9ef0dc7..827afd50b 100644 --- a/console-ui/src/test/java/org/jline/consoleui/examples/SimpleExample.java +++ b/console-ui/src/test/java/org/jline/consoleui/examples/SimpleExample.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,28 +29,30 @@ public static void main(String[] args) { header.add(new AttributedStringBuilder().append("Simple list example:").toAttributedString()); try (Terminal terminal = TerminalBuilder.builder().build()) { - ConsolePrompt prompt = new ConsolePrompt(terminal); - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + Map result = new HashMap<>(); + try (ConsolePrompt prompt = new ConsolePrompt(terminal)) { + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - promptBuilder - .createListPrompt() - .name("pizzatype") - .message("Which pizza do you want?") - .newItem() - .text("Margherita") - .add() // without name (name defaults to text) - .newItem("veneziana") - .text("Veneziana") - .add() - .newItem("hawai") - .text("Hawai") - .add() - .newItem("quattro") - .text("Quattro Stagioni") - .add() - .addPrompt(); + promptBuilder + .createListPrompt() + .name("pizzatype") + .message("Which pizza do you want?") + .newItem() + .text("Margherita") + .add() // without name (name defaults to text) + .newItem("veneziana") + .text("Veneziana") + .add() + .newItem("hawai") + .text("Hawai") + .add() + .newItem("quattro") + .text("Quattro Stagioni") + .add() + .addPrompt(); - Map result = prompt.prompt(header, promptBuilder.build()); + prompt.prompt(header, promptBuilder.build(), result); + } System.out.println("result = " + result); } catch (IOException e) { e.printStackTrace(); diff --git a/console-ui/src/test/java/org/jline/consoleui/prompt/PromptBuilderTest.java b/console-ui/src/test/java/org/jline/consoleui/prompt/PromptBuilderTest.java index 756ef72e4..7b8919433 100644 --- a/console-ui/src/test/java/org/jline/consoleui/prompt/PromptBuilderTest.java +++ b/console-ui/src/test/java/org/jline/consoleui/prompt/PromptBuilderTest.java @@ -23,109 +23,109 @@ public class PromptBuilderTest { @Test public void testBuilder() throws IOException { - ConsolePrompt prompt = new ConsolePrompt(TerminalBuilder.builder().build()); - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + try (ConsolePrompt prompt = new ConsolePrompt(TerminalBuilder.builder().build())) { + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - promptBuilder - .createConfirmPromp() - .name("wantapizza") - .message("Do you want to order a pizza?") - .defaultValue(ConfirmChoice.ConfirmationValue.YES) - .addPrompt(); + promptBuilder + .createConfirmPromp() + .name("wantapizza") + .message("Do you want to order a pizza?") + .defaultValue(ConfirmChoice.ConfirmationValue.YES) + .addPrompt(); - promptBuilder - .createInputPrompt() - .name("name") - .message("Please enter your name") - .defaultValue("John Doe") - .addPrompt(); + promptBuilder + .createInputPrompt() + .name("name") + .message("Please enter your name") + .defaultValue("John Doe") + .addPrompt(); - promptBuilder - .createListPrompt() - .name("pizzatype") - .message("Which pizza do you want?") - .newItem() - .text("Margherita") - .add() // without name (name defaults to text) - .newItem("veneziana") - .text("Veneziana") - .add() - .newItem("hawai") - .text("Hawai") - .add() - .newItem("quattro") - .text("Quattro Stagioni") - .add() - .addPrompt(); + promptBuilder + .createListPrompt() + .name("pizzatype") + .message("Which pizza do you want?") + .newItem() + .text("Margherita") + .add() // without name (name defaults to text) + .newItem("veneziana") + .text("Veneziana") + .add() + .newItem("hawai") + .text("Hawai") + .add() + .newItem("quattro") + .text("Quattro Stagioni") + .add() + .addPrompt(); - promptBuilder - .createCheckboxPrompt() - .name("topping") - .message("Please select additional toppings:") - .newSeparator("standard toppings") - .add() - .newItem() - .name("cheese") - .text("Cheese") - .add() - .newItem("bacon") - .text("Bacon") - .add() - .newItem("onions") - .text("Onions") - .disabledText("Sorry. Out of stock.") - .add() - .newSeparator() - .text("special toppings") - .add() - .newItem("salami") - .text("Very hot salami") - .check() - .add() - .newItem("salmon") - .text("Smoked Salmon") - .add() - .newSeparator("and our speciality...") - .add() - .newItem("special") - .text("Anchovies, and olives") - .checked(true) - .add() - .addPrompt(); + promptBuilder + .createCheckboxPrompt() + .name("topping") + .message("Please select additional toppings:") + .newSeparator("standard toppings") + .add() + .newItem() + .name("cheese") + .text("Cheese") + .add() + .newItem("bacon") + .text("Bacon") + .add() + .newItem("onions") + .text("Onions") + .disabledText("Sorry. Out of stock.") + .add() + .newSeparator() + .text("special toppings") + .add() + .newItem("salami") + .text("Very hot salami") + .check() + .add() + .newItem("salmon") + .text("Smoked Salmon") + .add() + .newSeparator("and our speciality...") + .add() + .newItem("special") + .text("Anchovies, and olives") + .checked(true) + .add() + .addPrompt(); - assertNotNull(promptBuilder); - promptBuilder - .createChoicePrompt() - .name("payment") - .message("How do you want to pay?") - .newItem() - .name("cash") - .message("Cash") - .key('c') - .asDefault() - .add() - .newItem("visa") - .message("Visa Card") - .key('v') - .add() - .newItem("master") - .message("Master Card") - .key('m') - .add() - .newSeparator("online payment") - .add() - .newItem("paypal") - .message("Paypal") - .key('p') - .add() - .addPrompt(); + assertNotNull(promptBuilder); + promptBuilder + .createChoicePrompt() + .name("payment") + .message("How do you want to pay?") + .newItem() + .name("cash") + .message("Cash") + .key('c') + .asDefault() + .add() + .newItem("visa") + .message("Visa Card") + .key('v') + .add() + .newItem("master") + .message("Master Card") + .key('m') + .add() + .newSeparator("online payment") + .add() + .newItem("paypal") + .message("Paypal") + .key('p') + .add() + .addPrompt(); - List promptableElementList = promptBuilder.build(); + List promptableElementList = promptBuilder.build(); - // only for test. reset the default reader to a test reader to automate the input - // promptableElementList.get(0) - - // HashMap result = prompt.prompt(promptableElementList); + // only for test. reset the default reader to a test reader to automate the input + // promptableElementList.get(0) + // HashMap result = prompt.prompt(promptableElementList); + } } }