Skip to content

Commit

Permalink
Undeprecate ConsolePrompt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed Jan 16, 2025
1 parent 4131478 commit cdce944
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<>();

/**
*
Expand All @@ -52,6 +53,7 @@ public ConsolePrompt(Terminal terminal) {
public ConsolePrompt(Terminal terminal, UiConfig config) {
this(null, terminal, config);
}

/**
*
* @param reader the lineReader.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<>();
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
205 changes: 101 additions & 104 deletions console-ui/src/test/java/org/jline/consoleui/Issue1025.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
Loading

0 comments on commit cdce944

Please sign in to comment.