Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text-only prompt element for console-ui #1138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions console-ui/src/main/java/org/jline/consoleui/elements/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.consoleui.elements;

import java.util.List;

import org.jline.utils.AttributedString;

public class Text extends AbstractPromptableElement {
private final List<AttributedString> lines;

private static int num = 0;

public Text(List<AttributedString> lines) {
// We don't actually care about names, so we just generate a unique one
super("", "_text_" + ++num);
this.lines = lines;
}

public List<AttributedString> getLines() {
return lines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import org.jline.consoleui.elements.ConfirmChoice;
import org.jline.consoleui.elements.ExpandableChoice;
import org.jline.consoleui.elements.InputValue;
import org.jline.consoleui.elements.items.CheckboxItemIF;
import org.jline.consoleui.elements.items.ChoiceItemIF;
import org.jline.consoleui.elements.items.ConsoleUIItemIF;
import org.jline.consoleui.elements.items.ListItemIF;
import org.jline.consoleui.elements.items.*;
import org.jline.consoleui.elements.items.impl.CheckboxItem;
import org.jline.consoleui.elements.items.impl.ChoiceItem;
import org.jline.consoleui.elements.items.impl.Separator;
Expand Down
114 changes: 71 additions & 43 deletions console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,39 +179,41 @@ public Map<String, PromptResultItemIF> prompt(
Deque<List<PromptableElementIF>> prevLists = new ArrayDeque<>();
Deque<Map<String, PromptResultItemIF>> prevResults = new ArrayDeque<>();
boolean cancellable = config.cancellableFirstPrompt();
// Get our first list of prompts
List<PromptableElementIF> peList = promptableElementLists.apply(new HashMap<>());
Map<String, PromptResultItemIF> peResult = new HashMap<>();
while (peList != null) {
// Second and later prompts should always be cancellable
config.setCancellableFirstPrompt(!prevLists.isEmpty() || cancellable);
// Prompt the user
prompt(headerIn, peList, peResult);
if (peResult.isEmpty()) {
// The prompt was cancelled by the user, so let's go back to the
// previous list of prompts and its results (if any)
peList = prevLists.pollFirst();
peResult = prevResults.pollFirst();
if (peResult != null) {
// Remove the results of the previous prompt from the main result map
peResult.forEach((k, v) -> resultMap.remove(k));
headerIn.remove(headerIn.size() - 1);
try {
// Get our first list of prompts
List<PromptableElementIF> peList = promptableElementLists.apply(new HashMap<>());
Map<String, PromptResultItemIF> peResult = new HashMap<>();
while (peList != null) {
// Second and later prompts should always be cancellable
config.setCancellableFirstPrompt(!prevLists.isEmpty() || cancellable);
// Prompt the user
prompt(headerIn, peList, peResult);
if (peResult.isEmpty()) {
// The prompt was cancelled by the user, so let's go back to the
// previous list of prompts and its results (if any)
peList = prevLists.pollFirst();
peResult = prevResults.pollFirst();
if (peResult != null) {
// Remove the results of the previous prompt from the main result map
peResult.forEach((k, v) -> resultMap.remove(k));
headerIn.remove(headerIn.size() - 1);
}
} else {
// We remember the list of prompts and their results
prevLists.push(peList);
prevResults.push(peResult);
// Add the results to the main result map
resultMap.putAll(peResult);
// And we get our next list of prompts (if any)
peList = promptableElementLists.apply(resultMap);
peResult = new HashMap<>();
}
} else {
// We remember the list of prompts and their results
prevLists.push(peList);
prevResults.push(peResult);
// Add the results to the main result map
resultMap.putAll(peResult);
// And we get our next list of prompts (if any)
peList = promptableElementLists.apply(resultMap);
peResult = new HashMap<>();
}
return resultMap;
} finally {
// Restore the original state of cancellable
config.setCancellableFirstPrompt(cancellable);
}
// Restore the original state of cancellable
config.setCancellableFirstPrompt(cancellable);

return resultMap;
}

/**
Expand Down Expand Up @@ -250,16 +252,21 @@ public void prompt(
}
this.header = headerIn;

boolean backward = false;
for (int i = resultMap.isEmpty() ? 0 : resultMap.size() - 1; i < promptableElementList.size(); i++) {
PromptableElementIF pe = promptableElementList.get(i);
PromptResultItemIF result = promptElement(header, pe);
if (backward) {
removePreviousResult(pe);
backward = false;
}
PromptResultItemIF oldResult = resultMap.get(pe.getName());
PromptResultItemIF result = promptElement(header, pe, oldResult);
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;
backward = true;
continue;
} else {
if (config.cancellableFirstPrompt()) {
Expand All @@ -272,22 +279,40 @@ public void prompt(
}
}
}
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;
if (pe instanceof Text) {
Text te = (Text) pe;
header.addAll(te.getLines());
} else {
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");
}
}
message = createMessage(pe.getMessage(), resp);
header.add(message.toAttributedString());
}
AttributedStringBuilder message = createMessage(pe.getMessage(), resp);
header.add(message.toAttributedString());
resultMap.put(pe.getName(), result);
}
}

protected PromptResultItemIF promptElement(List<AttributedString> header, PromptableElementIF pe) {
private void removePreviousResult(PromptableElementIF pe) {
if (pe instanceof Text) {
Text te = (Text) pe;
for (int i = 0; i < te.getLines().size(); i++) {
header.remove(header.size() - 1);
}
} else {
header.remove(header.size() - 1);
}
}

protected PromptResultItemIF promptElement(
List<AttributedString> header, PromptableElementIF pe, PromptResultItemIF oldResult) {
AttributedStringBuilder message = createMessage(pe.getMessage(), null);
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(message);
Expand Down Expand Up @@ -352,6 +377,9 @@ protected PromptResultItemIF promptElement(List<AttributedString> header, Prompt
asb.append(" ");
result = ConfirmPrompt.getPrompt(terminal, header, asb.toAttributedString(), cc, config)
.execute();
} else if (pe instanceof Text) {
Text te = (Text) pe;
result = oldResult == null ? NoResult.INSTANCE : null;
} else {
throw new IllegalArgumentException("wrong type of promptable element");
}
Expand Down
29 changes: 29 additions & 0 deletions console-ui/src/main/java/org/jline/consoleui/prompt/NoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.consoleui.prompt;

public class NoResult implements PromptResultItemIF {

public static final NoResult INSTANCE = new NoResult();

private NoResult() {}

public String getDisplayResult() {
return "";
}

public String getResult() {
return "";
}

@Override
public String toString() {
return "NoResult{}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public CheckboxPromptBuilder createCheckboxPrompt() {
public ConfirmPromptBuilder createConfirmPromp() {
return new ConfirmPromptBuilder(this);
}

public TextBuilder createText() {
return new TextBuilder(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.consoleui.prompt.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jline.consoleui.elements.Text;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

public class TextBuilder {
private final PromptBuilder promptBuilder;
private final List<AttributedString> lines = new ArrayList<>();

public TextBuilder(PromptBuilder promptBuilder) {
this.promptBuilder = promptBuilder;
}

public TextBuilder addLine(AttributedString text) {
lines.add(text);
return this;
}

public TextBuilder addLine(String line) {
lines.add(new AttributedString(line));
return this;
}

public TextBuilder addLine(AttributedStyle style, String line) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(line, style);
lines.add(asb.toAttributedString());
return this;
}

public TextBuilder addLines(AttributedString... lines) {
this.lines.addAll(Arrays.asList(lines));
return this;
}

public TextBuilder addLines(String... lines) {
for (String s : lines) {
this.lines.add(new AttributedString(s));
}
return this;
}

public TextBuilder addLines(AttributedStyle style, String... lines) {
for (String s : lines) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(s, style);
this.lines.add(asb.toAttributedString());
}
return this;
}

public PromptBuilder addPrompt() {
Text text = new Text(lines);
promptBuilder.addPrompt(text);
return promptBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.jline.utils.OSUtils;

public class BasicDynamic {
private static final AttributedStyle ITALIC_GREEN =
AttributedStyle.DEFAULT.italic().foreground(2);
private static final AttributedStyle BOLD_RED = AttributedStyle.BOLD.foreground(1);

private static void addInHeader(List<AttributedString> header, String text) {
addInHeader(header, AttributedStyle.DEFAULT, text);
Expand All @@ -43,8 +46,7 @@ private static void addInHeader(List<AttributedString> header, AttributedStyle s

public static void main(String[] args) {
List<AttributedString> header = new ArrayList<>();
AttributedStyle style = new AttributedStyle();
addInHeader(header, style.italic().foreground(2), "Hello Dynamic World!");
addInHeader(header, ITALIC_GREEN, "Hello Dynamic World!");
addInHeader(
header, "This is a demonstration of ConsoleUI java library. It provides a simple console interface");
addInHeader(
Expand Down Expand Up @@ -141,6 +143,7 @@ static List<PromptableElementIF> pizzaOrHamburgerPrompt(ConsolePrompt prompt) {

static List<PromptableElementIF> pizzaPrompt(ConsolePrompt prompt) {
PromptBuilder promptBuilder = prompt.getPromptBuilder();
promptBuilder.createText().addLine(ITALIC_GREEN, "Pizza time!").addPrompt();
promptBuilder
.createListPrompt()
.name("pizzatype")
Expand Down Expand Up @@ -197,6 +200,7 @@ static List<PromptableElementIF> pizzaPrompt(ConsolePrompt prompt) {

static List<PromptableElementIF> hamburgerPrompt(ConsolePrompt prompt) {
PromptBuilder promptBuilder = prompt.getPromptBuilder();
promptBuilder.createText().addLine(ITALIC_GREEN, "Hamburger time!").addPrompt();
promptBuilder
.createListPrompt()
.name("hamburgertype")
Expand Down Expand Up @@ -241,6 +245,12 @@ static List<PromptableElementIF> hamburgerPrompt(ConsolePrompt prompt) {

static List<PromptableElementIF> finalPrompt(ConsolePrompt prompt) {
PromptBuilder promptBuilder = prompt.getPromptBuilder();
promptBuilder
.createText()
.addLine(BOLD_RED, "###################")
.addLine(ITALIC_GREEN, "Finalize your order")
.addLine(BOLD_RED, "###################")
.addPrompt();
promptBuilder
.createChoicePrompt()
.name("payment")
Expand Down
Loading