Skip to content

Commit

Permalink
add standard prefix for error messaged
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Dec 15, 2017
1 parent 4e43a35 commit 81c93b2
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 37 deletions.
2 changes: 2 additions & 0 deletions dist/xbin/ContactInfo.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ textio.user.interrupt.key = ctrl Q
textio.prompt.color = #2fe040
textio.input.color = yellow

textio.error.prompt.color = red

textio.pane.width = 760
2 changes: 2 additions & 0 deletions dist/xbin/Cuboid.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

textio.user.interrupt.key = ctrl Q

textio.error.prompt.color = red

textio.custom.neutral.prompt.color = white
textio.custom.neutral.prompt.bgcolor = black
textio.custom.neutral.input.color = white
Expand Down
2 changes: 2 additions & 0 deletions dist/xbin/ECommerce.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

textio.user.interrupt.key = ctrl Q

textio.error.prompt.color = red

# textio.pane.bgcolor = #004400
19 changes: 17 additions & 2 deletions dist/xbin/ShoppingList.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

textio.user.interrupt.key = ctrl Q

textio.prompt.color = #2fe040
textio.input.color = yellow
textio.prompt.color = cyan
textio.input.color = white

textio.error.prompt.color = red

textio.help.prompt.color = pink
jline.help.prompt.color = magenta

textio.abort.prompt.color = orange
jline.abort.prompt.color = red

textio.content.prompt.color = lightblue
jline.content.prompt.color = white

textio.product.prompt.color = #2fe040
textio.product.input.color = yellow


textio.pane.width = 760
5 changes: 4 additions & 1 deletion dist/xbin/UserDataCollector.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ textio.input.color = yellow
# textio.input.bold = true
# textio.input.superscript = true

textio.error.prompt.color = red

# swing.prompt.color = #22ff88
textio.pane.bgcolor = #002f00
# textio.pane.bgcolor = #00002f
textio.pane.width = 840
textio.pane.title = User Data Collector
textio.pane.icon.file = data.png
# textio.pane.icon.resource = /your/resource/path/data.png
Expand Down
7 changes: 7 additions & 0 deletions dist/xbin/Weather.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ textio.user.interrupt.key = ctrl Q
textio.prompt.color = #2fe040
textio.input.color = yellow

textio.prompt.style.class = textterm-white-space-normal
textio.pre.prompt.style.class = textterm-white-space-pre

textio.error.prompt.color = red

textio.exit.prompt.color = cyan

textio.pane.width = 720
16 changes: 14 additions & 2 deletions doc/user_guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ displaying a help text, saving the data collected so far, aborting the current r

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

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

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

[[error_message_props]]
==== Error message prefix
When printing error messages, an InputReader temporarily changes the TextTerminal properties using the prefix `error`.
For example, in order to have error messages displayed in red, you can insert the following line into `textio.properties`:
[source]
----
textio.error.prompt.color = red
----


[[input_reader_props]]
=== InputReader-specific properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void accept(TextIO textIO, RunnerData runnerData) {
});

boolean registeredHelp = terminal.registerHandler(keyStrokeHelp, t -> {
terminal.executeWithPropertiesConfigurator(props -> props.setPromptColor("cyan"),
terminal.executeWithPropertiesPrefix("help",
tt -> tt.print("\n\nType the name of a product to be included in your shopping list."));
return new ReadHandlerData(ReadInterruptionStrategy.Action.RESTART).withRedrawRequired(true);
});
Expand Down Expand Up @@ -95,13 +95,15 @@ public void accept(TextIO textIO, RunnerData runnerData) {
while(true) {
String product;
try {
product = textIO.newStringInputReader().read("product");
product = textIO.newStringInputReader().withPropertiesPrefix("product").read("product");
} catch (ReadAbortedException e) {
terminal.println("\nRead aborted by user " + e.getPayload());
terminal.executeWithPropertiesPrefix("abort",
t -> t.println("\nRead aborted by user " + e.getPayload()));
break;
}
products.add(product);
terminal.println("Your shopping list contains: " + products.stream().collect(Collectors.joining(", ")));
String content = products.stream().collect(Collectors.joining(", "));
terminal.executeWithPropertiesPrefix("content", t ->t.println("Your shopping list contains: " + content));
terminal.println();
}
}
Expand Down
23 changes: 12 additions & 11 deletions text-io-demo/src/main/java/org/beryx/textio/demo/app/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ public void accept(TextIO textIO, RunnerData runnerData) {
terminal.resetToBookmark("MAIN");

terminal.getProperties().put(PropertiesConstants.PROP_PROMPT_STYLE_CLASS, "textterm-white-space-pre");
terminal.println("-------------------------------------------------------");
terminal.println(" Temperature Wind speed Atmospheric pressure");
terminal.println("-------------------------------------------------------");
for(int i=0; i<20; i++) {
terminal.moveToLineStart();
delay(80);
terminal.print(getData());
delay(400);
}
terminal.getProperties().put(PropertiesConstants.PROP_PROMPT_STYLE_CLASS, "textterm-white-space-normal");
terminal.executeWithPropertiesPrefix("pre", t -> {
t.println("-------------------------------------------------------");
t.println(" Temperature Wind speed Atmospheric pressure");
t.println("-------------------------------------------------------");
for(int i=0; i<20; i++) {
t.moveToLineStart();
delay(80);
t.print(getData());
delay(400);
}
});
terminal.println();terminal.println();terminal.println();

if(!textIO.newBooleanInputReader()
.withPropertiesConfigurator(props -> props.setPromptColor("cyan"))
.withPropertiesPrefix("exit")
.withDefaultValue(true).read("Run again?")) break;
terminal.resetToBookmark("MAIN");
}
Expand Down
34 changes: 20 additions & 14 deletions text-io/src/main/java/org/beryx/textio/InputReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
public abstract class InputReader<T, B extends InputReader<T, B>> {
private static final Logger logger = LoggerFactory.getLogger(InputReader.class);

public static final String PROPS_PREFIX_ERROR_MESSAGE = "error";

/** Functional interface for providing error messages */
@FunctionalInterface
public interface ErrorMessagesProvider {
Expand Down Expand Up @@ -418,7 +420,7 @@ public List<T> readList(List<String> prompt) {
}
if(!allErrors.isEmpty()) {
allErrors.add(0, getDefaultErrorMessage(null));
textTerminal.println(allErrors);
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t ->t.println(allErrors));
textTerminal.println();
continue;
}
Expand Down Expand Up @@ -470,18 +472,20 @@ private T getValueFromString(String sVal, TextTerminal<?> textTerminal) {
if(errMessages == null) {
Optional<T> value = getPossibleValue(result.getValue());
if(value.isPresent()) return value.get();
textTerminal.print(getDefaultErrorMessage(sVal));
if(inlinePossibleValues) {
String options = possibleValues.stream()
.map(val -> "'" + valueFormatter.apply(val) + "'")
.collect(Collectors.joining(", "));
textTerminal.println(" Please enter one of: " + options + ".");
} else {
textTerminal.println(" Please enter one of the displayed values.");
}
textTerminal.println( );
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> {
t.print(getDefaultErrorMessage(sVal));
if(inlinePossibleValues) {
String options = possibleValues.stream()
.map(val -> "'" + valueFormatter.apply(val) + "'")
.collect(Collectors.joining(", "));
t.println(" Please enter one of: " + options + ".");
} else {
t.println(" Please enter one of the displayed values.");
}
});
textTerminal.println();
} else {
textTerminal.println(errMessages);
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> t.println(errMessages));
textTerminal.println();
}
return null;
Expand All @@ -496,8 +500,10 @@ private T getValueFromIndex(String sVal, TextTerminal<?> textTerminal) {
} catch (NumberFormatException e) {
// Continue the execution. The next statement will print the error message.
}
textTerminal.print(getDefaultErrorMessage(sVal));
textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + ".");
textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> {
textTerminal.print(getDefaultErrorMessage(sVal));
textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + ".");
});
textTerminal.println();
return null;
}
Expand Down
11 changes: 8 additions & 3 deletions text-io/src/main/java/org/beryx/textio/TextTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package org.beryx.textio;

import java.util.*;
import java.util.function.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.beryx.textio.TerminalProperties.ExtendedChangeListener;
Expand Down Expand Up @@ -227,7 +228,12 @@ default void printf(Locale l, String format, Object... args) {
default <R> R applyWithPropertiesConfigurator(Consumer<TerminalProperties<?>> propertiesConfigurator,
Function<TextTerminal<T>, R> action) {
LinkedList<String[]> toRestore = new LinkedList<>();
ExtendedChangeListener listener = (term, key, oldVal, newVal) -> toRestore.add(new String[] {key, oldVal});
ExtendedChangeListener listener = (term, key, oldVal, newVal) -> {
boolean exists = toRestore.stream().filter(e -> e[0].equals(key)).findAny().isPresent();
if (!exists) {
toRestore.add(new String[]{key, oldVal});
}
};
TerminalProperties<?> props = getProperties();
if(propertiesConfigurator != null) {
props.addListener(listener);
Expand Down Expand Up @@ -282,5 +288,4 @@ default void executeWithPropertiesPrefix(String prefix, Consumer<TextTerminal<T>
});
}, action);
}

}

0 comments on commit 81c93b2

Please sign in to comment.