@@ -329,13 +329,56 @@ link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo[source code]
329
329
and the link:{blob-root}/dist/xbin[configuration files]
330
330
of the https://github.com/beryx/text-io/releases/download/v{project-version}/textio-demo-{project-version}.zip[demo application].
331
331
332
+ [[terminal_temporary_props]]
333
+ === TextTerminal temporary properties
334
+
335
+ Sometimes you want to temporarily change some TextTerminal properties and revert them to their previous values after a couple of operations.
336
+ You can achieve this by passing the sequence of operations to be executed with modified properties as argument to the
337
+ link:javadoc/org/beryx/textio/TextTerminal.html#executeWithPropertiesConfigurator-java.util.function.Consumer-java.util.function.Consumer-[executeWithPropertiesConfigurator()]
338
+ method, as shown in the example below.
339
+ [source,java]
340
+ ----
341
+ textTerminal.getProperties().setPromptColor("cyan");
342
+ textTerminal.println("1. Choose the desired hard drive.");
343
+ textTerminal.executeWithPropertiesConfigurator(
344
+ props -> props.setPromptColor("red"),
345
+ t -> t.println("2. Backup all your data."));
346
+ textTerminal.println("3. Start the formatting process.");
347
+ ----
348
+ The second message will appear in red, while the other two will be printed in cyan.
349
+
350
+ The code above uses hard-coded property values.
351
+ A more elegant solution is to specify these values in the `textio.properties` file.
352
+ TextTerminal offers the
353
+ link:javadoc/org/beryx/textio/TextTerminal.html#executeWithPropertiesPrefix-java.util.function.Consumer-java.util.function.Consumer-[executeWithPropertiesConfigurator()]
354
+ convenience method to help you accomplish this task.
355
+
356
+ Consider the code below:
357
+ [source,java]
358
+ ----
359
+ textTerminal.println("1. Choose the desired hard drive.");
360
+ textTerminal.executeWithPropertiesPrefix("warn",
361
+ t -> t.println("2. Backup all your data."));
362
+ textTerminal.println("3. Start the formatting process.");
363
+ ----
364
+ and let's assume that your `textio.properties` contains:
365
+ [source]
366
+ ----
367
+ textio.prompt.color = cyan
368
+ textio.warn.prompt.color = red
369
+ ----
370
+ Then, the second message will appear in red, while the other two will be printed in cyan.
371
+
372
+ TIP: Look at the source code of
373
+ link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo/app/Cuboid.java[Cuboid.java]
374
+ for an example of using temporary TextTerminal properties.
375
+
332
376
[[input_reader_props]]
333
377
=== InputReader-specific properties
334
378
335
- Sometimes you want to change some TextTerminal properties for the next read operation, but want to revert to the previous property values at the end of this read operation.
336
- You can achieve this by configuring InputReader-specific properties via the
337
- link:javadoc/org/beryx/textio/InputReader.html#withPropertiesConfigurator-java.util.function.Consumer-withPropertiesConfigurator[withPropertiesConfigurator()]
338
- method, as shown in the example below.
379
+ If you want to apply some temporary TextTerminal properties only during the next read operation, you can call the
380
+ link:javadoc/org/beryx/textio/InputReader.html#withPropertiesConfigurator-java.util.function.Consumer-[withPropertiesConfigurator()]
381
+ method of your InputReader, as shown in the example below.
339
382
[source,java]
340
383
----
341
384
textIO.getTextTerminal().getProperties().setPromptColor("cyan");
@@ -347,6 +390,36 @@ String directory = textIO.newStringInputReader().read("Home directory");
347
390
----
348
391
The question _"Erase all data?"_ will appear in red, while _"User name"_ and _"Home directory"_ will be printed in cyan.
349
392
393
+ The code above uses hard-coded property values.
394
+ A more elegant solution is to specify these values in the `textio.properties` file.
395
+ InputReader offers the
396
+ link:javadoc/org/beryx/textio/InputReader.html#withPropertiesPrefix-java.lang.String-[withPropertiesPrefix()]
397
+ convenience method to help you accomplish this task.
398
+
399
+ Consider the code below:
400
+ [source,java]
401
+ ----
402
+ String user = textIO.newStringInputReader().read("User name");
403
+ textIO.newBooleanInputReader()
404
+ .withPropertiesPrefix("warn")
405
+ .read("Erase all data?");
406
+ String directory = textIO.newStringInputReader().read("Home directory");
407
+ ----
408
+ and let's assume that your `textio.properties` contains:
409
+ [source]
410
+ ----
411
+ textio.prompt.color = green
412
+ textio.input.color = yellow
413
+ textio.warn.prompt.color = red
414
+ textio.warn.input.color = orange
415
+ ----
416
+ Then, the question _"Erase all data?"_ will appear in red and its corresponding user input in orange.
417
+ For the other two read operations the questions will be displayed in green and the user input in yellow.
418
+
419
+ TIP: Look at the source code of
420
+ link:{blob-root}/text-io-demo/src/main/java/org/beryx/textio/demo/app/Cuboid.java[Cuboid.java]
421
+ for an example of using InputReader-specific properties.
422
+
350
423
351
424
[[web_text_term]]
352
425
== Using the WebTextTerminal
0 commit comments