From cdce944a9ba4aa98091755f08b536aaa8b35f212 Mon Sep 17 00:00:00 2001 From: Tako Schotanus <tako@codejive.org> Date: Wed, 8 Jan 2025 18:32:49 +0100 Subject: [PATCH] Undeprecate ConsolePrompt methods Fixes #1141 --- .../jline/consoleui/prompt/ConsolePrompt.java | 46 ++-- .../java/org/jline/consoleui/Issue1025.java | 205 +++++++++--------- .../org/jline/consoleui/examples/Basic.java | 194 ++++++++--------- .../consoleui/examples/BasicDynamic.java | 52 +++-- .../jline/consoleui/examples/LongList.java | 43 ++-- .../consoleui/examples/SimpleExample.java | 43 ++-- 6 files changed, 278 insertions(+), 305 deletions(-) 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 4d1e9b313..d66ef2335 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 @@ -8,6 +8,7 @@ */ package org.jline.consoleui.prompt; +import java.io.Closeable; import java.io.IOError; import java.io.IOException; import java.io.InterruptedIOException; @@ -30,12 +31,12 @@ /** * ConsolePrompt encapsulates the prompting of a list of input questions for the user. */ -public class ConsolePrompt implements AutoCloseable { +public class ConsolePrompt { protected final LineReader reader; protected final Terminal terminal; protected final UiConfig config; - private Attributes attributes; - private List<AttributedString> header = new ArrayList<>(); + protected Attributes attributes; + protected List<AttributedString> header = new ArrayList<>(); /** * @@ -52,6 +53,7 @@ public ConsolePrompt(Terminal terminal) { public ConsolePrompt(Terminal terminal, UiConfig config) { this(null, terminal, config); } + /** * * @param reader the lineReader. @@ -69,14 +71,18 @@ 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() { + protected void open() { + if (!terminalInRawMode()) { + attributes = terminal.enterRawMode(); + terminal.puts(InfoCmp.Capability.enter_ca_mode); + terminal.puts(InfoCmp.Capability.keypad_xmit); + terminal.writer().flush(); + } + } + + protected void close() { if (terminalInRawMode()) { terminal.setAttributes(attributes); terminal.puts(InfoCmp.Capability.exit_ca_mode); @@ -105,7 +111,6 @@ private boolean terminalInRawMode() { * @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<String, PromptResultItemIF> prompt(List<PromptableElementIF> promptableElementList) throws IOException, UserInterruptException { return prompt(new ArrayList<>(), promptableElementList); @@ -123,11 +128,11 @@ public Map<String, PromptResultItemIF> prompt(List<PromptableElementIF> 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<String, PromptResultItemIF> prompt( List<AttributedString> header, List<PromptableElementIF> promptableElementList) throws IOException, UserInterruptException { try { + open(); Map<String, PromptResultItemIF> resultMap = new HashMap<>(); prompt(header, promptableElementList, resultMap); return removeNoResults(resultMap); @@ -174,6 +179,7 @@ public Map<String, PromptResultItemIF> prompt( Deque<Map<String, PromptResultItemIF>> prevResults = new ArrayDeque<>(); boolean cancellable = config.cancellableFirstPrompt(); try { + open(); // Get our first list of prompts List<PromptableElementIF> peList = promptableElementLists.apply(new HashMap<>()); Map<String, PromptResultItemIF> peResult = new HashMap<>(); @@ -205,26 +211,12 @@ public Map<String, PromptResultItemIF> prompt( } return removeNoResults(resultMap); } finally { + close(); // Restore the original state of cancellable config.setCancellableFirstPrompt(cancellable); } } - /** - * 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<PromptableElementIF> promptableElementList, Map<String, PromptResultItemIF> 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 @@ -236,7 +228,7 @@ public void prompt(List<PromptableElementIF> promptableElementList, Map<String, * @param resultMap a map containing a result for each element of promptableElementList * @throws IOException may be thrown by terminal */ - public void prompt( + protected void prompt( List<AttributedString> headerIn, List<PromptableElementIF> promptableElementList, Map<String, PromptResultItemIF> resultMap) 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 368adf7f2..9c376375d 100644 --- a/console-ui/src/test/java/org/jline/consoleui/Issue1025.java +++ b/console-ui/src/test/java/org/jline/consoleui/Issue1025.java @@ -13,7 +13,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -88,109 +87,107 @@ 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(); - Map<String, PromptResultItemIF> 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); - } + 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<String, ? extends PromptResultItemIF> result = prompt.prompt(header, promptBuilder.build()); 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 806cd7825..c532b5aa4 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,7 +9,6 @@ package org.jline.consoleui.examples; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -67,110 +66,107 @@ 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(); - Map<String, PromptResultItemIF> result = new HashMap<>(); + ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config); + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - 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 - .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 - .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(); + 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 - .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(); - promptBuilder - .createConfirmPromp() - .name("delivery") - .message("Is this pizza for delivery?") - .defaultValue(ConfirmChoice.ConfirmationValue.YES) - .addPrompt(); - - prompt.prompt(header, promptBuilder.build(), result); - } + Map<String, ? extends PromptResultItemIF> result = prompt.prompt(header, promptBuilder.build()); 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 2c5ee8615..7e662dac4 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 @@ -72,36 +72,34 @@ public static void main(String[] args) { // LineReader is needed only if you are adding JLine Completers in your prompts. // If you are not using Completers you do not need to create LineReader. // - Map<String, PromptResultItemIF> result; LineReader reader = LineReaderBuilder.builder().terminal(terminal).build(); - try (ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config)) { - result = prompt.prompt(header, results -> { - if (results.isEmpty()) { - // No results yet, so we start with the first list of questions - return pizzaOrHamburgerPrompt(prompt); - } - // We have some results, so we know that the user chose a "product", - // so we can return the next list of questions based on that choice - if ("Pizza".equals(results.get("product").getResult())) { - // Check if the pizza questions were already answered - if (!results.containsKey("pizzatype")) { - // No, so let's return the pizza questions - return pizzaPrompt(prompt); - } - } else { - // Check if the hamburger questions were already answered - if (!results.containsKey("hamburgertype")) { - // No, so let's return the hamburger questions - return hamburgerPrompt(prompt); - } + ConsolePrompt prompt = new ConsolePrompt(reader, terminal, config); + Map<String, PromptResultItemIF> result = prompt.prompt(header, results -> { + if (results.isEmpty()) { + // No results yet, so we start with the first list of questions + return pizzaOrHamburgerPrompt(prompt); + } + // We have some results, so we know that the user chose a "product", + // so we can return the next list of questions based on that choice + if ("Pizza".equals(results.get("product").getResult())) { + // Check if the pizza questions were already answered + if (!results.containsKey("pizzatype")) { + // No, so let's return the pizza questions + return pizzaPrompt(prompt); } - // Check if the final questions were already answered - if (!results.containsKey("payment")) { - return finalPrompt(prompt); + } else { + // Check if the hamburger questions were already answered + if (!results.containsKey("hamburgertype")) { + // No, so let's return the hamburger questions + return hamburgerPrompt(prompt); } - return null; - }); - } + } + // Check if the final questions were already answered + if (!results.containsKey("payment")) { + return finalPrompt(prompt); + } + return null; + }); System.out.println("result = " + result); if (result.isEmpty()) { System.out.println("User cancelled order."); 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 83f0df223..92ed5a386 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,7 +10,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,35 +37,29 @@ public static void main(String[] args) { try (Terminal terminal = TerminalBuilder.builder().build()) { ConsolePrompt.UiConfig config = new ConsolePrompt.UiConfig(">", "( )", "(x)", "( )"); - Map<String, PromptResultItemIF> result = new HashMap<>(); + ConsolePrompt prompt = new ConsolePrompt(terminal, config); + PromptBuilder promptBuilder = prompt.getPromptBuilder(); - try (ConsolePrompt prompt = new ConsolePrompt(terminal, config)) { - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + ListPromptBuilder listPrompt = promptBuilder.createListPrompt(); + listPrompt.name("longlist").message("What's your favourite Letter?").relativePageSize(66); - ListPromptBuilder listPrompt = promptBuilder.createListPrompt(); - listPrompt - .name("longlist") - .message("What's your favourite Letter?") - .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++) - listPrompt.newItem().text("" + letter + letter2).add(); - listPrompt.addPrompt(); + CheckboxPromptBuilder checkboxPrompt = promptBuilder.createCheckboxPrompt(); + checkboxPrompt + .name("longcheckbox") + .message("What's your favourite Letter? Select all you want...") + .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++) + checkboxPrompt.newItem().text("" + letter + letter2).add(); + checkboxPrompt.addPrompt(); - 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); - } + Map<String, PromptResultItemIF> result = prompt.prompt(header, promptBuilder.build()); 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 827afd50b..6d9ef0dc7 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,7 +10,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,30 +28,28 @@ public static void main(String[] args) { header.add(new AttributedStringBuilder().append("Simple list example:").toAttributedString()); try (Terminal terminal = TerminalBuilder.builder().build()) { - Map<String, PromptResultItemIF> result = new HashMap<>(); - try (ConsolePrompt prompt = new ConsolePrompt(terminal)) { - PromptBuilder promptBuilder = prompt.getPromptBuilder(); + 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(); - prompt.prompt(header, promptBuilder.build(), result); - } + Map<String, PromptResultItemIF> result = prompt.prompt(header, promptBuilder.build()); System.out.println("result = " + result); } catch (IOException e) { e.printStackTrace();