Skip to content

Commit

Permalink
Adds text-only prompt element for console-ui
Browse files Browse the repository at this point in the history
Fixes #1136
  • Loading branch information
quintesse committed Dec 19, 2024
1 parent b2f9c20 commit da89289
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 19 deletions.
28 changes: 28 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,28 @@
/*
* 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) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -252,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 @@ -274,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 @@ -354,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

0 comments on commit da89289

Please sign in to comment.