Add Help component for CLI usage and input validation support

- Introduced `Help` class for generating CLI help/usage blocks with options, descriptions, and customizable formatting.
- Enhanced `Prompt` and `PromptBuilder` with validation capabilities using predicates and retry messages.
- Added terminal control methods `clearScreen` and `cursorTo` with ANSI support.
- Updated examples and documentation (`Examples.md`, `Components.md`) to showcase the new features.
- Extended `Examples` class with a `helpBlock` method for demonstrating the `Help` usage.
This commit is contained in:
!verity
2026-03-14 12:00:31 +01:00
parent 21256dc2dd
commit 4b44c1d155
8 changed files with 232 additions and 12 deletions

View File

@@ -108,6 +108,13 @@ String name = Terminal.prompt("Name: ").ask();
String secret = Terminal.prompt("Password: ").masked().ask();
// With shared scanner (e.g. in a loop):
String line = Terminal.prompt("Input: ").ask(sharedScanner);
// With validation (asks again until input matches):
String age = Terminal.prompt("Age: ").validate(s -> s.matches("\\d+")).ask();
String email = Terminal.prompt("Email: ")
.validate(s -> s.contains("@"))
.retryMessage("Invalid email, try again: ")
.ask();
```
## Interactive: Confirm
@@ -259,6 +266,26 @@ Terminal.code("java")
.print(System.out);
```
## Help (CLI usage)
```java
Terminal.help()
.title("Options")
.option("-v, --verbose", "Enable verbose output")
.option("-h, --help", "Show this help")
.option("--file <path>", "Input file path")
.print(System.out);
```
## Clear screen & cursor
```java
Terminal.clearScreen(); // Clear terminal (ANSI)
Terminal.cursorTo(1, 1); // Move cursor to row 1, col 1 (1-based)
```
No-op when ANSI is disabled.
## SysInfo
```java