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

@@ -26,11 +26,12 @@ Overview of what terminal-ui provides. All entry points are on `Terminal.*`.
| API | Description |
|-----|-------------|
| `Terminal.prompt(String)` | Text input (`.ask()`, `.masked().ask()`) |
| `Terminal.prompt(String)` | Text input (`.ask()`, `.masked().ask()`, `.validate(Predicate).retryMessage(String)`) |
| `Terminal.confirm(String)` | Y/n confirmation |
| `Terminal.menu()` | Numbered menu (`.option()`, `.select()`) |
| `Terminal.selectList()` | List with arrow keys + Enter |
| `Terminal.pager()` | Paged output (Enter/arrows/q) |
| `Terminal.help()` | CLI usage block (`.option(opt, desc)`, `.title()`) |
## Live / progress
@@ -50,6 +51,13 @@ Overview of what terminal-ui provides. All entry points are on `Terminal.*`.
| `Terminal.heatmap()` | Heatmap |
| `Terminal.chart()` | Charts |
## Terminal control (ANSI)
| API | Description |
|-----|-------------|
| `Terminal.clearScreen()` | Clear entire screen |
| `Terminal.cursorTo(row, col)` | Move cursor (1-based) |
## Other
| API | Description |

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