Skip to content

Commit 81c93b2

Browse files
committed
add standard prefix for error messaged
1 parent 4e43a35 commit 81c93b2

File tree

11 files changed

+94
-37
lines changed

11 files changed

+94
-37
lines changed

dist/xbin/ContactInfo.properties

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ textio.user.interrupt.key = ctrl Q
55
textio.prompt.color = #2fe040
66
textio.input.color = yellow
77

8+
textio.error.prompt.color = red
9+
810
textio.pane.width = 760

dist/xbin/Cuboid.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
textio.user.interrupt.key = ctrl Q
44

5+
textio.error.prompt.color = red
6+
57
textio.custom.neutral.prompt.color = white
68
textio.custom.neutral.prompt.bgcolor = black
79
textio.custom.neutral.input.color = white

dist/xbin/ECommerce.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
textio.user.interrupt.key = ctrl Q
44

5+
textio.error.prompt.color = red
6+
57
# textio.pane.bgcolor = #004400

dist/xbin/ShoppingList.properties

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
textio.user.interrupt.key = ctrl Q
44

5-
textio.prompt.color = #2fe040
6-
textio.input.color = yellow
5+
textio.prompt.color = cyan
6+
textio.input.color = white
7+
8+
textio.error.prompt.color = red
9+
10+
textio.help.prompt.color = pink
11+
jline.help.prompt.color = magenta
12+
13+
textio.abort.prompt.color = orange
14+
jline.abort.prompt.color = red
15+
16+
textio.content.prompt.color = lightblue
17+
jline.content.prompt.color = white
18+
19+
textio.product.prompt.color = #2fe040
20+
textio.product.input.color = yellow
21+
722

823
textio.pane.width = 760

dist/xbin/UserDataCollector.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ textio.input.color = yellow
2323
# textio.input.bold = true
2424
# textio.input.superscript = true
2525

26+
textio.error.prompt.color = red
27+
2628
# swing.prompt.color = #22ff88
27-
textio.pane.bgcolor = #002f00
29+
# textio.pane.bgcolor = #00002f
30+
textio.pane.width = 840
2831
textio.pane.title = User Data Collector
2932
textio.pane.icon.file = data.png
3033
# textio.pane.icon.resource = /your/resource/path/data.png

dist/xbin/Weather.properties

+7
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ textio.user.interrupt.key = ctrl Q
55
textio.prompt.color = #2fe040
66
textio.input.color = yellow
77

8+
textio.prompt.style.class = textterm-white-space-normal
9+
textio.pre.prompt.style.class = textterm-white-space-pre
10+
11+
textio.error.prompt.color = red
12+
13+
textio.exit.prompt.color = cyan
14+
815
textio.pane.width = 720

doc/user_guide.adoc

+14-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ displaying a help text, saving the data collected so far, aborting the current r
8888

8989
TIP: Look at the
9090
link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo/app/ShoppingList.java[ShoppingList]
91-
example in the
91+
and
92+
link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo/app/ContactInfo.java[ContactInfo]
93+
examples in the
9294
https://github.com/beryx/text-io/releases/download/v{project-version}/textio-demo-{project-version}.zip[demo application]
9395
for usage details.
9496

@@ -350,7 +352,7 @@ The second message will appear in red, while the other two will be printed in cy
350352
The code above uses hard-coded property values.
351353
A more elegant solution is to specify these values in the `textio.properties` file.
352354
TextTerminal offers the
353-
link:javadoc/org/beryx/textio/TextTerminal.html#executeWithPropertiesPrefix-java.util.function.Consumer-java.util.function.Consumer-[executeWithPropertiesConfigurator()]
355+
link:javadoc/org/beryx/textio/TextTerminal.html#executeWithPropertiesPrefix-java.lang.String-java.util.function.Consumer-[executeWithPropertiesPrefix()]
354356
convenience method to help you accomplish this task.
355357

356358
Consider the code below:
@@ -373,6 +375,16 @@ TIP: Look at the source code of
373375
link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo/app/Cuboid.java[Cuboid.java]
374376
for an example of using temporary TextTerminal properties.
375377

378+
[[error_message_props]]
379+
==== Error message prefix
380+
When printing error messages, an InputReader temporarily changes the TextTerminal properties using the prefix `error`.
381+
For example, in order to have error messages displayed in red, you can insert the following line into `textio.properties`:
382+
[source]
383+
----
384+
textio.error.prompt.color = red
385+
----
386+
387+
376388
[[input_reader_props]]
377389
=== InputReader-specific properties
378390

text-io-demo/src/main/java/org/beryx/textio/demo/app/ShoppingList.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void accept(TextIO textIO, RunnerData runnerData) {
6262
});
6363

6464
boolean registeredHelp = terminal.registerHandler(keyStrokeHelp, t -> {
65-
terminal.executeWithPropertiesConfigurator(props -> props.setPromptColor("cyan"),
65+
terminal.executeWithPropertiesPrefix("help",
6666
tt -> tt.print("\n\nType the name of a product to be included in your shopping list."));
6767
return new ReadHandlerData(ReadInterruptionStrategy.Action.RESTART).withRedrawRequired(true);
6868
});
@@ -95,13 +95,15 @@ public void accept(TextIO textIO, RunnerData runnerData) {
9595
while(true) {
9696
String product;
9797
try {
98-
product = textIO.newStringInputReader().read("product");
98+
product = textIO.newStringInputReader().withPropertiesPrefix("product").read("product");
9999
} catch (ReadAbortedException e) {
100-
terminal.println("\nRead aborted by user " + e.getPayload());
100+
terminal.executeWithPropertiesPrefix("abort",
101+
t -> t.println("\nRead aborted by user " + e.getPayload()));
101102
break;
102103
}
103104
products.add(product);
104-
terminal.println("Your shopping list contains: " + products.stream().collect(Collectors.joining(", ")));
105+
String content = products.stream().collect(Collectors.joining(", "));
106+
terminal.executeWithPropertiesPrefix("content", t ->t.println("Your shopping list contains: " + content));
105107
terminal.println();
106108
}
107109
}

text-io-demo/src/main/java/org/beryx/textio/demo/app/Weather.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,21 @@ public void accept(TextIO textIO, RunnerData runnerData) {
6767
terminal.resetToBookmark("MAIN");
6868

6969
terminal.getProperties().put(PropertiesConstants.PROP_PROMPT_STYLE_CLASS, "textterm-white-space-pre");
70-
terminal.println("-------------------------------------------------------");
71-
terminal.println(" Temperature Wind speed Atmospheric pressure");
72-
terminal.println("-------------------------------------------------------");
73-
for(int i=0; i<20; i++) {
74-
terminal.moveToLineStart();
75-
delay(80);
76-
terminal.print(getData());
77-
delay(400);
78-
}
79-
terminal.getProperties().put(PropertiesConstants.PROP_PROMPT_STYLE_CLASS, "textterm-white-space-normal");
70+
terminal.executeWithPropertiesPrefix("pre", t -> {
71+
t.println("-------------------------------------------------------");
72+
t.println(" Temperature Wind speed Atmospheric pressure");
73+
t.println("-------------------------------------------------------");
74+
for(int i=0; i<20; i++) {
75+
t.moveToLineStart();
76+
delay(80);
77+
t.print(getData());
78+
delay(400);
79+
}
80+
});
8081
terminal.println();terminal.println();terminal.println();
8182

8283
if(!textIO.newBooleanInputReader()
83-
.withPropertiesConfigurator(props -> props.setPromptColor("cyan"))
84+
.withPropertiesPrefix("exit")
8485
.withDefaultValue(true).read("Run again?")) break;
8586
terminal.resetToBookmark("MAIN");
8687
}

text-io/src/main/java/org/beryx/textio/InputReader.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
public abstract class InputReader<T, B extends InputReader<T, B>> {
3535
private static final Logger logger = LoggerFactory.getLogger(InputReader.class);
3636

37+
public static final String PROPS_PREFIX_ERROR_MESSAGE = "error";
38+
3739
/** Functional interface for providing error messages */
3840
@FunctionalInterface
3941
public interface ErrorMessagesProvider {
@@ -418,7 +420,7 @@ public List<T> readList(List<String> prompt) {
418420
}
419421
if(!allErrors.isEmpty()) {
420422
allErrors.add(0, getDefaultErrorMessage(null));
421-
textTerminal.println(allErrors);
423+
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t ->t.println(allErrors));
422424
textTerminal.println();
423425
continue;
424426
}
@@ -470,18 +472,20 @@ private T getValueFromString(String sVal, TextTerminal<?> textTerminal) {
470472
if(errMessages == null) {
471473
Optional<T> value = getPossibleValue(result.getValue());
472474
if(value.isPresent()) return value.get();
473-
textTerminal.print(getDefaultErrorMessage(sVal));
474-
if(inlinePossibleValues) {
475-
String options = possibleValues.stream()
476-
.map(val -> "'" + valueFormatter.apply(val) + "'")
477-
.collect(Collectors.joining(", "));
478-
textTerminal.println(" Please enter one of: " + options + ".");
479-
} else {
480-
textTerminal.println(" Please enter one of the displayed values.");
481-
}
482-
textTerminal.println( );
475+
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> {
476+
t.print(getDefaultErrorMessage(sVal));
477+
if(inlinePossibleValues) {
478+
String options = possibleValues.stream()
479+
.map(val -> "'" + valueFormatter.apply(val) + "'")
480+
.collect(Collectors.joining(", "));
481+
t.println(" Please enter one of: " + options + ".");
482+
} else {
483+
t.println(" Please enter one of the displayed values.");
484+
}
485+
});
486+
textTerminal.println();
483487
} else {
484-
textTerminal.println(errMessages);
488+
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> t.println(errMessages));
485489
textTerminal.println();
486490
}
487491
return null;
@@ -496,8 +500,10 @@ private T getValueFromIndex(String sVal, TextTerminal<?> textTerminal) {
496500
} catch (NumberFormatException e) {
497501
// Continue the execution. The next statement will print the error message.
498502
}
499-
textTerminal.print(getDefaultErrorMessage(sVal));
500-
textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + ".");
503+
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> {
504+
textTerminal.print(getDefaultErrorMessage(sVal));
505+
textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + ".");
506+
});
501507
textTerminal.println();
502508
return null;
503509
}

text-io/src/main/java/org/beryx/textio/TextTerminal.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package org.beryx.textio;
1717

1818
import java.util.*;
19-
import java.util.function.*;
19+
import java.util.function.Consumer;
20+
import java.util.function.Function;
2021
import java.util.stream.Collectors;
2122

2223
import static org.beryx.textio.TerminalProperties.ExtendedChangeListener;
@@ -227,7 +228,12 @@ default void printf(Locale l, String format, Object... args) {
227228
default <R> R applyWithPropertiesConfigurator(Consumer<TerminalProperties<?>> propertiesConfigurator,
228229
Function<TextTerminal<T>, R> action) {
229230
LinkedList<String[]> toRestore = new LinkedList<>();
230-
ExtendedChangeListener listener = (term, key, oldVal, newVal) -> toRestore.add(new String[] {key, oldVal});
231+
ExtendedChangeListener listener = (term, key, oldVal, newVal) -> {
232+
boolean exists = toRestore.stream().filter(e -> e[0].equals(key)).findAny().isPresent();
233+
if (!exists) {
234+
toRestore.add(new String[]{key, oldVal});
235+
}
236+
};
231237
TerminalProperties<?> props = getProperties();
232238
if(propertiesConfigurator != null) {
233239
props.addListener(listener);
@@ -282,5 +288,4 @@ default void executeWithPropertiesPrefix(String prefix, Consumer<TextTerminal<T>
282288
});
283289
}, action);
284290
}
285-
286291
}

0 commit comments

Comments
 (0)