|
18 | 18 | import org.beryx.textio.TextIO;
|
19 | 19 | import org.beryx.textio.TextIoFactory;
|
20 | 20 | import org.beryx.textio.TextTerminal;
|
| 21 | +import org.beryx.textio.TextTerminalProvider; |
| 22 | +import org.beryx.textio.console.ConsoleTextTerminalProvider; |
| 23 | +import org.beryx.textio.jline.AnsiTextTerminal; |
| 24 | +import org.beryx.textio.jline.JLineTextTerminalProvider; |
| 25 | +import org.beryx.textio.swing.SwingTextTerminalProvider; |
| 26 | +import org.beryx.textio.system.SystemTextTerminal; |
| 27 | +import org.beryx.textio.system.SystemTextTerminalProvider; |
| 28 | +import org.beryx.textio.web.WebTextTerminal; |
| 29 | +import spark.Service; |
21 | 30 |
|
22 |
| -import java.time.Month; |
| 31 | +import java.util.function.Supplier; |
23 | 32 |
|
24 | 33 | /**
|
25 |
| - * Demo application illustrating the use of TextIO. |
26 |
| - * <br>If an argument is provided, it will be used to set the value of the <tt>{@value TextIoFactory#TEXT_TERMINAL_CLASS_PROPERTY}</tt> system property. |
27 |
| - * This means that the program will interpret the argument as the fully-qualified name of a concrete {@link TextTerminal} class and will try to create and use an instance of this class. |
28 |
| - * <br>Example: run the program with the argument <tt>org.beryx.textio.demo.ColorTextTerminal</tt>. |
| 34 | + * Demo application showing various TextTerminals. |
29 | 35 | */
|
30 | 36 | public class TextIoDemo {
|
| 37 | + private static int webServerPort = -1; |
| 38 | + |
| 39 | + private static class NamedProvider implements TextTerminalProvider { |
| 40 | + final String name; |
| 41 | + final Supplier<TextTerminal> supplier; |
| 42 | + |
| 43 | + NamedProvider(String name, Supplier<TextTerminal> supplier) { |
| 44 | + this.name = name; |
| 45 | + this.supplier = supplier; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TextTerminal getTextTerminal() { |
| 50 | + return supplier.get(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String toString() { |
| 55 | + return name; |
| 56 | + } |
| 57 | + } |
| 58 | + |
31 | 59 | public static void main(String[] args) {
|
32 |
| - if(args.length > 0) { |
33 |
| - System.setProperty(TextIoFactory.TEXT_TERMINAL_CLASS_PROPERTY, args[0]); |
| 60 | + TextIO textIO = chooseTextIO(); |
| 61 | + if(textIO.getTextTerminal() instanceof WebTextTerminal) { |
| 62 | + WebTextIoExecutor webTextIoExecutor = new WebTextIoExecutor().withPort(webServerPort); |
| 63 | + webTextIoExecutor.execute(SimpleApp::execute); |
| 64 | + } else { |
| 65 | + SimpleApp.execute(textIO); |
34 | 66 | }
|
35 |
| - TextIO textIO = TextIoFactory.getTextIO(); |
36 |
| - TextTerminal terminal = textIO.getTextTerminal(); |
37 |
| - if(args.length == 0) { |
38 |
| - terminal.println("-------------------------------------------------------------------------"); |
39 |
| - terminal.println("Usage tip:"); |
40 |
| - terminal.println(" Provide as argument the fully-qualified name of a TextTerminal class."); |
41 |
| - terminal.println(" Example: run with the argument org.beryx.textio.demo.ColorTextTerminal."); |
42 |
| - terminal.println("-------------------------------------------------------------------------"); |
43 |
| - terminal.println(); |
44 |
| - terminal.println(); |
| 67 | + } |
| 68 | + |
| 69 | + private static TextIO chooseTextIO() { |
| 70 | + TextTerminal terminal = new SystemTextTerminal(); |
| 71 | + TextIO textIO = new TextIO(terminal); |
| 72 | + while(true) { |
| 73 | + TextTerminalProvider terminalProvider = textIO.<TextTerminalProvider>newGenericInputReader(null) |
| 74 | + .withNumberedPossibleValues( |
| 75 | + new NamedProvider("Default terminal (provided by TextIoFactory)", TextIoFactory::getTextTerminal), |
| 76 | + new SystemTextTerminalProvider(), |
| 77 | + new ConsoleTextTerminalProvider(), |
| 78 | + new JLineTextTerminalProvider(), |
| 79 | + new NamedProvider("ANSI terminal", () -> createAnsiTextTerminal(textIO)), |
| 80 | + new SwingTextTerminalProvider(), |
| 81 | + new NamedProvider("Web terminal", () -> createWebTextTerminal(textIO)) |
| 82 | + ) |
| 83 | + .read("Choose the terminal to be used for running the demo"); |
| 84 | + |
| 85 | + TextTerminal chosenTerminal = null; |
| 86 | + String errMsg = null; |
| 87 | + try { |
| 88 | + chosenTerminal = terminalProvider.getTextTerminal(); |
| 89 | + } catch (Exception e) { |
| 90 | + errMsg = e.getMessage(); |
| 91 | + } |
| 92 | + if(chosenTerminal == null) { |
| 93 | + terminal.printf("\nCannot create a %s%s\n\n", terminalProvider, ((errMsg != null) ? (": " + errMsg) : ".")); |
| 94 | + continue; |
| 95 | + } |
| 96 | + return new TextIO(chosenTerminal); |
45 | 97 | }
|
| 98 | + } |
46 | 99 |
|
47 |
| - String user = textIO.newStringInputReader() |
48 |
| - .withDefaultValue("admin") |
49 |
| - .read("Username"); |
| 100 | + private static AnsiTextTerminal createAnsiTextTerminal(TextIO textIO) { |
| 101 | + boolean bold = textIO.newBooleanInputReader() |
| 102 | + .withDefaultValue(false) |
| 103 | + .read("Bold text?"); |
50 | 104 |
|
51 |
| - String password = textIO.newStringInputReader() |
52 |
| - .withMinLength(6) |
53 |
| - .withInputMasking(true) |
54 |
| - .read("Password"); |
| 105 | + String[] colors = AnsiTextTerminal.ANSI_COLOR_MAP.keySet().toArray(new String[0]); |
| 106 | + String color = textIO.newStringInputReader() |
| 107 | + .withNumberedPossibleValues(colors) |
| 108 | + .withDefaultValue("yellow") |
| 109 | + .read("Text color"); |
55 | 110 |
|
56 |
| - int age = textIO.newIntInputReader() |
57 |
| - .withMinVal(13) |
58 |
| - .read("Age"); |
| 111 | + String bgColor = textIO.newStringInputReader() |
| 112 | + .withNumberedPossibleValues(colors) |
| 113 | + .withDefaultValue("blue") |
| 114 | + .read("Background color"); |
59 | 115 |
|
60 |
| - Month month = textIO.newEnumInputReader(Month.class) |
61 |
| - .read("What month were you born in?"); |
| 116 | + AnsiTextTerminal terminal = new AnsiTextTerminal(); |
| 117 | + terminal.withBold(bold); |
| 118 | + terminal.withColor(color); |
| 119 | + terminal.withBackgroundColor(bgColor); |
| 120 | + return terminal; |
| 121 | + } |
62 | 122 |
|
63 |
| - terminal.printf("\nUser %s is %d years old, was born in %s and has the password %s.\n", user, age, month, password); |
| 123 | + private static WebTextTerminal createWebTextTerminal(TextIO textIO) { |
| 124 | + webServerPort = textIO.newIntInputReader() |
| 125 | + .withDefaultValue(Service.SPARK_DEFAULT_PORT) |
| 126 | + .read("Server port number"); |
64 | 127 |
|
65 |
| - textIO.newStringInputReader().withMinLength(0).read("\nPress enter to terminate..."); |
66 |
| - textIO.dispose(); |
| 128 | + // The returned WebTextTerminal is not actually used, but treated as a marker that triggers the creation of a WebTextIoExecutor. |
| 129 | + // This WebTextIoExecutor will instantiate a new WebTextTerminal each time a client starts a new session. |
| 130 | + return new WebTextTerminal(); |
67 | 131 | }
|
68 | 132 | }
|
0 commit comments