Compare commits
10 Commits
v1.0.1
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b464e060b | ||
|
|
fc7a2f4770 | ||
|
|
e004e8acc5 | ||
|
|
4b44c1d155 | ||
|
|
21256dc2dd | ||
|
|
8f56551e3d | ||
|
|
b17b278840 | ||
|
|
047dcf3f4d | ||
|
|
97a43d9850 | ||
|
|
34b683aacf |
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
@@ -6,7 +6,6 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches: [ main, master ]
|
branches: [ main, master ]
|
||||||
|
|
||||||
# Alte Läufe abbrechen, wenn ein neuer Push kommt (spart Zeit)
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
|
|||||||
42
.github/workflows/codeql.yml
vendored
42
.github/workflows/codeql.yml
vendored
@@ -1,42 +0,0 @@
|
|||||||
# CodeQL: static security analysis (free for public repos).
|
|
||||||
name: CodeQL
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main, master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ main, master ]
|
|
||||||
schedule:
|
|
||||||
- cron: '0 6 * * 1'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
security-events: write
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up JDK 21
|
|
||||||
uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
distribution: temurin
|
|
||||||
java-version: 21
|
|
||||||
cache: gradle
|
|
||||||
|
|
||||||
- name: Initialize CodeQL
|
|
||||||
uses: github/codeql-action/init@v3
|
|
||||||
with:
|
|
||||||
languages: java
|
|
||||||
|
|
||||||
- name: Build (for CodeQL)
|
|
||||||
run: |
|
|
||||||
chmod +x gradlew
|
|
||||||
./gradlew compileJava --no-daemon
|
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
|
||||||
uses: github/codeql-action/analyze@v3
|
|
||||||
53
.github/workflows/pages.yml
vendored
Normal file
53
.github/workflows/pages.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# Deploy docs to GitHub Pages (optional)
|
||||||
|
# Enable in repo: Settings → Pages → Source: GitHub Actions
|
||||||
|
# Site will be at https://jakubbbdev.github.io/terminal-ui/
|
||||||
|
|
||||||
|
name: Pages
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master, main]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pages: write
|
||||||
|
id-token: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
environment: github-pages
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Pages
|
||||||
|
uses: actions/configure-pages@v5
|
||||||
|
|
||||||
|
- name: Build site
|
||||||
|
run: |
|
||||||
|
mkdir -p _site
|
||||||
|
cp -r docs _site/
|
||||||
|
cat > _site/index.html << 'EOF'
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html><head><meta charset="utf-8"><title>terminal-ui</title></head>
|
||||||
|
<body style="font-family:sans-serif;max-width:600px;margin:2rem auto;padding:0 1rem">
|
||||||
|
<h1>terminal-ui</h1>
|
||||||
|
<p>Java library for terminal UIs: tables, prompts, menus, and more.</p>
|
||||||
|
<ul>
|
||||||
|
<li><a href="https://github.com/jakubbbdev/terminal-ui">GitHub repo</a></li>
|
||||||
|
<li><a href="https://github.com/jakubbbdev/terminal-ui#install">Install (README)</a></li>
|
||||||
|
<li><a href="https://github.com/jakubbbdev/terminal-ui/releases">Releases</a></li>
|
||||||
|
<li><a href="https://github.com/jakubbbdev/terminal-ui/tree/master/docs">Docs (markdown)</a></li>
|
||||||
|
</ul>
|
||||||
|
</body></html>
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-pages-artifact@v3
|
||||||
|
with:
|
||||||
|
path: _site
|
||||||
|
|
||||||
|
- name: Deploy to GitHub Pages
|
||||||
|
id: deployment
|
||||||
|
uses: actions/deploy-pages@v4
|
||||||
9
.github/workflows/release.yml
vendored
9
.github/workflows/release.yml
vendored
@@ -29,10 +29,15 @@ jobs:
|
|||||||
- name: Strip v from version
|
- name: Strip v from version
|
||||||
run: echo "RELEASE_VERSION=${RELEASE_VERSION#v}" >> $GITHUB_ENV
|
run: echo "RELEASE_VERSION=${RELEASE_VERSION#v}" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Build and publish to GitHub Packages
|
- name: Build
|
||||||
run: chmod +x gradlew && ./gradlew build publish --no-daemon
|
run: chmod +x gradlew && ./gradlew build --no-daemon
|
||||||
|
|
||||||
|
- name: Publish to GitHub Packages
|
||||||
|
run: ./gradlew publish --no-daemon
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
continue-on-error: true
|
||||||
|
# 409 = version already exists; use a new tag (e.g. v1.0.2) next time
|
||||||
|
|
||||||
- name: Create Release
|
- name: Create Release
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
|
|||||||
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -1,4 +1,3 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="FrameworkDetectionExcludesConfiguration">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
|
|||||||
17
CODE_OF_CONDUCT.md
Normal file
17
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Code of Conduct
|
||||||
|
|
||||||
|
## Our Pledge
|
||||||
|
|
||||||
|
We are committed to providing a welcoming and harassment-free experience for everyone. Be respectful, inclusive, and constructive when contributing or discussing this project.
|
||||||
|
|
||||||
|
## Unacceptable Behavior
|
||||||
|
|
||||||
|
Harassment, trolling, insulting comments, personal or political attacks, and other conduct that could make others feel unsafe are not acceptable.
|
||||||
|
|
||||||
|
## Enforcement
|
||||||
|
|
||||||
|
Maintainers may remove or edit content that violates this code. Repeated or serious violations may result in being blocked from the repository.
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
If you have concerns, open an issue or contact the maintainers privately if preferred.
|
||||||
5
FUNDING.yml
Normal file
5
FUNDING.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Optional: Uncomment to show "Sponsor" button on the repo
|
||||||
|
# github: [jakubbbdev]
|
||||||
|
# patreon: your-username
|
||||||
|
# ko_fi: your-username
|
||||||
|
# custom: ['https://your-link.com']
|
||||||
43
Jenkinsfile
vendored
Normal file
43
Jenkinsfile
vendored
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
|
||||||
|
environment {
|
||||||
|
GITHUB_ACTOR = 'jakubbbdev'
|
||||||
|
GITHUB_TOKEN = credentials('github-credentials')
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Checkout') {
|
||||||
|
steps {
|
||||||
|
checkout scm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Build') {
|
||||||
|
steps {
|
||||||
|
sh 'chmod +x gradlew && ./gradlew build'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Test') {
|
||||||
|
steps {
|
||||||
|
sh './gradlew test'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Publish') {
|
||||||
|
steps {
|
||||||
|
sh './gradlew publish'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
success {
|
||||||
|
echo '✅ Build erfolgreich!'
|
||||||
|
}
|
||||||
|
failure {
|
||||||
|
echo '❌ Build fehlgeschlagen!'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,13 +5,12 @@ Java library for terminal UI: tables, rules, colors, prompts, menus, SelectList,
|
|||||||
[](https://github.com/jakubbbdev/terminal-ui/actions/workflows/ci.yml)
|
[](https://github.com/jakubbbdev/terminal-ui/actions/workflows/ci.yml)
|
||||||
[](https://openjdk.org/)
|
[](https://openjdk.org/)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
|
[](https://github.com/jakubbbdev/terminal-ui/releases)
|
||||||
|
|
||||||
[](https://codespaces.new/jakubbbdev/terminal-ui)
|
[](https://codespaces.new/jakubbbdev/terminal-ui)
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
Published to [GitHub Packages](https://github.com/jakubbbdev/terminal-ui/packages). Replace `VERSION` with a [release](https://github.com/jakubbbdev/terminal-ui/releases) tag (e.g. `1.0.0`).
|
|
||||||
|
|
||||||
**Gradle (Kotlin DSL):**
|
**Gradle (Kotlin DSL):**
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
@@ -50,8 +49,6 @@ dependencies {
|
|||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
For public packages, read access usually works without credentials. If your tool asks for auth, use a [GitHub PAT](https://github.com/settings/tokens) with `read:packages` as username and the token as password.
|
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- **Java 21**
|
- **Java 21**
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ plugins {
|
|||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClass.set("dev.jakub.terminal.example.Examples")
|
||||||
|
}
|
||||||
|
|
||||||
tasks.named<JavaExec>("run") {
|
tasks.named<JavaExec>("run") {
|
||||||
jvmArgs("-Ddev.jakub.terminal.ansi=true", "-Dfile.encoding=UTF-8")
|
jvmArgs("-Ddev.jakub.terminal.ansi=true", "-Dfile.encoding=UTF-8")
|
||||||
}
|
}
|
||||||
|
|||||||
71
docs/Components.md
Normal file
71
docs/Components.md
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# Components
|
||||||
|
|
||||||
|
Overview of what terminal-ui provides. All entry points are on `Terminal.*`.
|
||||||
|
|
||||||
|
## Output & text
|
||||||
|
|
||||||
|
| API | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `Terminal.print(String)` | Styled text (chain `.color()`, `.bold()`, `.println()`) |
|
||||||
|
| `Terminal.rule()` | Horizontal rule (e.g. `====`) |
|
||||||
|
| `Terminal.keyValue()` | Key-value pairs |
|
||||||
|
| `Terminal.box()` | Box around content |
|
||||||
|
|
||||||
|
## Tables & layout
|
||||||
|
|
||||||
|
| API | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `Terminal.table()` | Tables with header/rows |
|
||||||
|
| `Terminal.tree()` | Tree structure |
|
||||||
|
| `Terminal.columns()` | Multi-column layout |
|
||||||
|
| `Terminal.steps()` | Step indicator |
|
||||||
|
| `Terminal.breadcrumb()` | Breadcrumb trail |
|
||||||
|
| `Terminal.toc()` | Table of contents |
|
||||||
|
|
||||||
|
## Interactive
|
||||||
|
|
||||||
|
| API | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `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
|
||||||
|
|
||||||
|
| API | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `Terminal.progressBar()` | Progress bar (`.total()`, `.build()`) |
|
||||||
|
| `Terminal.spinner()` | Spinning indicator (`.message()`, `.start()`) |
|
||||||
|
| `Terminal.dashboard()` | Refreshing widget panel |
|
||||||
|
|
||||||
|
## Data & visuals
|
||||||
|
|
||||||
|
| API | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `Terminal.diff()` | Diff view |
|
||||||
|
| `Terminal.log()` | Log-style output |
|
||||||
|
| `Terminal.timeline()` | Timeline |
|
||||||
|
| `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 |
|
||||||
|
|-----|-------------|
|
||||||
|
| `Terminal.badge(String, Color)` | Colored badge |
|
||||||
|
| `Terminal.notify(String, Type)` | Notification (info/success/warning/error) |
|
||||||
|
| `Terminal.code(String)` | Code block with language |
|
||||||
|
| `Terminal.sysinfo()` | System info (OS, Java, etc.) |
|
||||||
|
| `Terminal.markdown(String)` | Render markdown to terminal |
|
||||||
|
|
||||||
|
Use the fluent API from `Terminal.*`. For tests or to disable ANSI, inject a custom `TerminalSupport`.
|
||||||
314
docs/Examples.md
Normal file
314
docs/Examples.md
Normal file
@@ -0,0 +1,314 @@
|
|||||||
|
# Examples
|
||||||
|
|
||||||
|
All examples use `import dev.jakub.terminal.Terminal;` and `import dev.jakub.terminal.core.Color;` where needed. Use `System.out` or omit the argument where `.print()` has a no-arg overload.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Text & rules
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.print("Hello").color(Color.CYAN).bold().println();
|
||||||
|
|
||||||
|
Terminal.rule().character('=').print();
|
||||||
|
Terminal.rule().doubles().prefix("-- ").suffix(" --").print();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Key-value & box
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.keyValue()
|
||||||
|
.row("Name", "terminal-ui")
|
||||||
|
.row("Version", "1.0")
|
||||||
|
.separator(" = ")
|
||||||
|
.print(System.out);
|
||||||
|
|
||||||
|
Terminal.box()
|
||||||
|
.title("Info")
|
||||||
|
.line("First line")
|
||||||
|
.lines("Second", "Third")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Table
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.table()
|
||||||
|
.header("Col A", "Col B", "Col C")
|
||||||
|
.row("1", "2", "3")
|
||||||
|
.row("4", "5", "6")
|
||||||
|
.print(System.out);
|
||||||
|
|
||||||
|
// From a list of rows (e.g. from CSV or DB)
|
||||||
|
List<String[]> rows = List.of(
|
||||||
|
new String[]{"Alice", "10"},
|
||||||
|
new String[]{"Bob", "20"}
|
||||||
|
);
|
||||||
|
Terminal.table()
|
||||||
|
.header("Name", "Score")
|
||||||
|
.rows(rows)
|
||||||
|
.print(System.out);
|
||||||
|
|
||||||
|
// One-liner with header + data
|
||||||
|
Terminal.table()
|
||||||
|
.fromRows(new String[]{"A", "B"}, List.of(new String[]{"1", "2"}, new String[]{"3", "4"}))
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tree
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.tree()
|
||||||
|
.node("root")
|
||||||
|
.child("src")
|
||||||
|
.child("main").end()
|
||||||
|
.child("test").end()
|
||||||
|
.end()
|
||||||
|
.child("docs")
|
||||||
|
.end()
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Columns
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.columns()
|
||||||
|
.column("Left\ncolumn\ntext")
|
||||||
|
.column("Middle")
|
||||||
|
.column("Right")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Steps (wizard-style)
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.steps()
|
||||||
|
.step("Setup", Steps.Status.DONE)
|
||||||
|
.step("Build", Steps.Status.RUNNING)
|
||||||
|
.step("Deploy", Steps.Status.PENDING)
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
(Use `import dev.jakub.terminal.components.Steps;` for `Steps.Status`.)
|
||||||
|
|
||||||
|
## Breadcrumb
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.breadcrumb()
|
||||||
|
.crumb("Home")
|
||||||
|
.crumb("Projects")
|
||||||
|
.crumb("terminal-ui")
|
||||||
|
.separator(" / ")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Interactive: Prompt
|
||||||
|
|
||||||
|
```java
|
||||||
|
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
|
||||||
|
|
||||||
|
```java
|
||||||
|
boolean yes = Terminal.confirm("Continue?").defaultYes().ask();
|
||||||
|
boolean no = Terminal.confirm("Delete?").defaultNo().ask();
|
||||||
|
boolean answer = Terminal.confirm("Proceed?").ask(sharedScanner);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Interactive: Menu
|
||||||
|
|
||||||
|
```java
|
||||||
|
String env = Terminal.menu()
|
||||||
|
.title("Choose environment")
|
||||||
|
.option("Development")
|
||||||
|
.option("Staging")
|
||||||
|
.option("Production")
|
||||||
|
.select();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Interactive: SelectList (arrows + Enter)
|
||||||
|
|
||||||
|
```java
|
||||||
|
String choice = Terminal.selectList()
|
||||||
|
.title("Pick one")
|
||||||
|
.option("Option A")
|
||||||
|
.option("Option B")
|
||||||
|
.option("Option C")
|
||||||
|
.select();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Interactive: Pager
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.pager()
|
||||||
|
.lines("Line 1", "Line 2", "Line 3", "Line 4", "Line 5")
|
||||||
|
.pageSize(3)
|
||||||
|
.interactive();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Progress bar
|
||||||
|
|
||||||
|
```java
|
||||||
|
ProgressBar bar = Terminal.progressBar()
|
||||||
|
.total(100)
|
||||||
|
.width(40)
|
||||||
|
.style(ProgressBar.Style.BLOCK)
|
||||||
|
.build();
|
||||||
|
for (int i = 0; i <= 100; i++) {
|
||||||
|
bar.update(i);
|
||||||
|
Thread.sleep(30);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
(Use `import dev.jakub.terminal.live.ProgressBar;`.)
|
||||||
|
|
||||||
|
## Spinner
|
||||||
|
|
||||||
|
```java
|
||||||
|
Spinner spin = Terminal.spinner().message("Loading...").start();
|
||||||
|
// ... do work ...
|
||||||
|
spin.stop();
|
||||||
|
```
|
||||||
|
|
||||||
|
(Use `import dev.jakub.terminal.live.Spinner;`.)
|
||||||
|
|
||||||
|
## Diff
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.diff()
|
||||||
|
.before("old line 1\nold line 2")
|
||||||
|
.after("new line 1\nnew line 2\nnew line 3")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Log
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.log()
|
||||||
|
.info("Started")
|
||||||
|
.warn("Deprecation notice")
|
||||||
|
.error("Something failed")
|
||||||
|
.withTimestamp()
|
||||||
|
.print(System.out);
|
||||||
|
|
||||||
|
// Only WARN and ERROR (e.g. for production)
|
||||||
|
Terminal.log()
|
||||||
|
.info("Hidden")
|
||||||
|
.warn("Visible")
|
||||||
|
.error("Visible")
|
||||||
|
.minLevel(Log.Level.WARN)
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
(Use `import dev.jakub.terminal.components.Log;` for `Log.Level`.)
|
||||||
|
|
||||||
|
## Timeline
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.timeline()
|
||||||
|
.event("2024-01-01", "First release")
|
||||||
|
.event("2024-06-01", "Added SelectList")
|
||||||
|
.event("2025-01-01", "GitHub Packages")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Heatmap
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.heatmap()
|
||||||
|
.row("Mon", 10, 20, 5, 30)
|
||||||
|
.row("Tue", 15, 25, 10, 20)
|
||||||
|
.row("Wed", 5, 15, 25, 35)
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Chart (sparkline / bar)
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.chart()
|
||||||
|
.data(1.0, 2.0, 1.5, 3.0, 2.5)
|
||||||
|
.height(5)
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Badge & notification
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.badge("OK", Color.GREEN).println();
|
||||||
|
Terminal.badge("WARN", Color.YELLOW).print(System.out);
|
||||||
|
|
||||||
|
Terminal.notify("Done!", Notification.Type.SUCCESS);
|
||||||
|
Terminal.notify("Warning", Notification.Type.WARNING);
|
||||||
|
Terminal.notify("Error", Notification.Type.ERROR);
|
||||||
|
Terminal.notify("Info", Notification.Type.INFO);
|
||||||
|
```
|
||||||
|
|
||||||
|
(Use `import dev.jakub.terminal.components.Notification;`.)
|
||||||
|
|
||||||
|
## Code block
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.code("java")
|
||||||
|
.line("public static void main(String[] args) {")
|
||||||
|
.line(" System.out.println(\"Hello\");")
|
||||||
|
.line("}")
|
||||||
|
.lineNumbers()
|
||||||
|
.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
|
||||||
|
Terminal.sysinfo().print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Markdown
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.markdown("# Title\n\n**Bold** and *italic*.\n\n- Item 1\n- Item 2\n\n---")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
|
||||||
|
```java
|
||||||
|
Terminal.toc()
|
||||||
|
.section("Introduction").sub("Overview").sub("Install")
|
||||||
|
.section("API").sub("Reference")
|
||||||
|
.section("Examples")
|
||||||
|
.print(System.out);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
For more, see the [README](https://github.com/jakubbbdev/terminal-ui#usage-excerpt) and the source.
|
||||||
25
docs/Home.md
Normal file
25
docs/Home.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# terminal-ui
|
||||||
|
|
||||||
|
Java library for building terminal UIs: tables, rules, colors, prompts, menus, SelectList, pager, and more. Fluent API, ANSI support, testable.
|
||||||
|
|
||||||
|
## Quick links
|
||||||
|
|
||||||
|
- **[Install](Install)** – Gradle / Maven dependency
|
||||||
|
- **[Components](Components)** – What's in the library
|
||||||
|
- **[Examples](Examples)** – Code snippets
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- Java 21
|
||||||
|
- Gradle 9.x or Maven (wrapper included in repo)
|
||||||
|
|
||||||
|
## Build from source
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew build
|
||||||
|
./gradlew test
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT – see [LICENSE](https://github.com/jakubbbdev/terminal-ui/blob/master/LICENSE) in the repo.
|
||||||
45
docs/Install.md
Normal file
45
docs/Install.md
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# Install
|
||||||
|
|
||||||
|
Published to [GitHub Packages](https://github.com/jakubbbdev/terminal-ui/packages). Use a [release](https://github.com/jakubbbdev/terminal-ui/releases) version (e.g. `1.0.1`) for `VERSION`.
|
||||||
|
|
||||||
|
## Gradle (Kotlin DSL)
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven { url = uri("https://maven.pkg.github.com/jakubbbdev/terminal-ui") }
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
implementation("dev.jakub.terminal:terminal-ui:VERSION")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Gradle (Groovy)
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven { url "https://maven.pkg.github.com/jakubbbdev/terminal-ui" }
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
implementation "dev.jakub.terminal:terminal-ui:VERSION"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Maven
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<repository>
|
||||||
|
<id>github</id>
|
||||||
|
<url>https://maven.pkg.github.com/jakubbbdev/terminal-ui</url>
|
||||||
|
</repository>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.jakub.terminal</groupId>
|
||||||
|
<artifactId>terminal-ui</artifactId>
|
||||||
|
<version>VERSION</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Auth (if required)
|
||||||
|
|
||||||
|
For public packages, resolution often works without credentials. If your build asks for auth, use a [GitHub PAT](https://github.com/settings/tokens) with `read:packages`: username = your GitHub username, password = token.
|
||||||
10
docs/index.md
Normal file
10
docs/index.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# terminal-ui
|
||||||
|
|
||||||
|
Java library for terminal UIs: tables, prompts, menus, and more.
|
||||||
|
|
||||||
|
- **[Home](Home)** – overview
|
||||||
|
- **[Install](Install)** – Gradle / Maven
|
||||||
|
- **[Components](Components)** – what's in the library
|
||||||
|
- **[Examples](Examples)** – runnable code snippets
|
||||||
|
|
||||||
|
[GitHub repo](https://github.com/jakubbbdev/terminal-ui) · [Releases](https://github.com/jakubbbdev/terminal-ui/releases) · [Packages](https://github.com/jakubbbdev/terminal-ui/packages)
|
||||||
@@ -205,6 +205,27 @@ public final class Terminal {
|
|||||||
return new Pager(DEFAULT.getSupport());
|
return new Pager(DEFAULT.getSupport());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a help/usage builder for CLI --help output. */
|
||||||
|
public static Help help() {
|
||||||
|
return new Help(DEFAULT.getSupport());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clears the terminal screen (ANSI). No-op if ANSI is disabled. */
|
||||||
|
public static void clearScreen() {
|
||||||
|
if (DEFAULT.getSupport().isAnsiEnabled()) {
|
||||||
|
System.out.print(dev.jakub.terminal.core.Ansi.CLEAR_SCREEN);
|
||||||
|
System.out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Moves the cursor to the given 1-based row and column (ANSI). No-op if ANSI is disabled. */
|
||||||
|
public static void cursorTo(int row, int col) {
|
||||||
|
if (DEFAULT.getSupport().isAnsiEnabled()) {
|
||||||
|
System.out.print(dev.jakub.terminal.core.Ansi.cursorTo(row, col));
|
||||||
|
System.out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TerminalSupport getSupport() {
|
TerminalSupport getSupport() {
|
||||||
return support;
|
return support;
|
||||||
}
|
}
|
||||||
|
|||||||
87
src/main/java/dev/jakub/terminal/components/Help.java
Normal file
87
src/main/java/dev/jakub/terminal/components/Help.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package dev.jakub.terminal.components;
|
||||||
|
|
||||||
|
import dev.jakub.terminal.Terminal;
|
||||||
|
import dev.jakub.terminal.core.TerminalSupport;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CLI help / usage block. Use via {@link Terminal#help()}.
|
||||||
|
* Formats options and descriptions for a typical --help output.
|
||||||
|
*/
|
||||||
|
public final class Help {
|
||||||
|
|
||||||
|
private static final int DEFAULT_OPTION_WIDTH = 24;
|
||||||
|
|
||||||
|
private final TerminalSupport support;
|
||||||
|
private final List<String> options = new ArrayList<>();
|
||||||
|
private final List<String> descriptions = new ArrayList<>();
|
||||||
|
private String title = "Usage";
|
||||||
|
private int optionWidth = DEFAULT_OPTION_WIDTH;
|
||||||
|
private PrintStream out = System.out;
|
||||||
|
|
||||||
|
public Help(TerminalSupport support) {
|
||||||
|
this.support = support;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the title (e.g. "Usage" or "Options").
|
||||||
|
*/
|
||||||
|
public Help title(String title) {
|
||||||
|
this.title = title != null ? title : "";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an option with description (e.g. "-v, --verbose" and "Enable verbose output").
|
||||||
|
*/
|
||||||
|
public Help option(String option, String description) {
|
||||||
|
options.add(option != null ? option : "");
|
||||||
|
descriptions.add(description != null ? description : "");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the width of the option column (default 24).
|
||||||
|
*/
|
||||||
|
public Help optionWidth(int width) {
|
||||||
|
this.optionWidth = Math.max(8, width);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets output stream (default: stdout).
|
||||||
|
*/
|
||||||
|
public Help output(PrintStream out) {
|
||||||
|
this.out = out != null ? out : System.out;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the help block to the given stream.
|
||||||
|
*/
|
||||||
|
public void print(PrintStream stream) {
|
||||||
|
if (stream == null) stream = System.out;
|
||||||
|
if (!title.isEmpty()) {
|
||||||
|
stream.println(title);
|
||||||
|
stream.println();
|
||||||
|
}
|
||||||
|
int ow = Math.max(optionWidth, 8);
|
||||||
|
for (int i = 0; i < options.size(); i++) {
|
||||||
|
String opt = options.get(i);
|
||||||
|
String desc = i < descriptions.size() ? descriptions.get(i) : "";
|
||||||
|
String padded = opt.length() <= ow ? opt + " ".repeat(ow - opt.length()) : opt;
|
||||||
|
stream.print(" " + padded + " ");
|
||||||
|
stream.println(desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints to stdout.
|
||||||
|
*/
|
||||||
|
public void print() {
|
||||||
|
print(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ public final class Log {
|
|||||||
private final TerminalSupport support;
|
private final TerminalSupport support;
|
||||||
private final List<Entry> entries = new ArrayList<>();
|
private final List<Entry> entries = new ArrayList<>();
|
||||||
private boolean withTimestamp;
|
private boolean withTimestamp;
|
||||||
|
private Level minLevel;
|
||||||
|
|
||||||
public Log(TerminalSupport support) {
|
public Log(TerminalSupport support) {
|
||||||
this.support = support;
|
this.support = support;
|
||||||
@@ -68,11 +69,21 @@ public final class Log {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints all log entries to the given stream.
|
* Only print entries at this level or higher (e.g. WARN shows WARN and ERROR).
|
||||||
|
* Useful for "quiet" mode or production.
|
||||||
|
*/
|
||||||
|
public Log minLevel(Level level) {
|
||||||
|
this.minLevel = level;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints log entries to the given stream (respects minLevel if set).
|
||||||
*/
|
*/
|
||||||
public void print(PrintStream out) {
|
public void print(PrintStream out) {
|
||||||
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss").withZone(ZoneId.systemDefault());
|
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss").withZone(ZoneId.systemDefault());
|
||||||
for (Entry e : entries) {
|
for (Entry e : entries) {
|
||||||
|
if (minLevel != null && e.level.ordinal() < minLevel.ordinal()) continue;
|
||||||
String prefix = withTimestamp ? "[" + fmt.format(Instant.now()) + "] " : "";
|
String prefix = withTimestamp ? "[" + fmt.format(Instant.now()) + "] " : "";
|
||||||
String label = "[" + e.level.name() + "]";
|
String label = "[" + e.level.name() + "]";
|
||||||
while (label.length() < LABEL_WIDTH) label += " ";
|
while (label.length() < LABEL_WIDTH) label += " ";
|
||||||
@@ -104,11 +115,14 @@ public final class Log {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum Level {
|
/**
|
||||||
|
* Log level for filtering. Order: DEBUG < INFO < WARN < ERROR.
|
||||||
|
*/
|
||||||
|
public enum Level {
|
||||||
|
DEBUG(Ansi.FG_BRIGHT_BLACK),
|
||||||
INFO(Ansi.FG_CYAN),
|
INFO(Ansi.FG_CYAN),
|
||||||
WARN(Ansi.FG_YELLOW),
|
WARN(Ansi.FG_YELLOW),
|
||||||
ERROR(Ansi.FG_RED),
|
ERROR(Ansi.FG_RED);
|
||||||
DEBUG(Ansi.FG_BRIGHT_BLACK);
|
|
||||||
|
|
||||||
final String ansi;
|
final String ansi;
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,26 @@ public final class Table {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds many rows from a list of arrays (e.g. from CSV or a list of records).
|
||||||
|
* Each array is one row; null cells are treated as empty string.
|
||||||
|
*/
|
||||||
|
public Table rows(Iterable<String[]> dataRows) {
|
||||||
|
if (dataRows == null) return this;
|
||||||
|
for (String[] cells : dataRows) {
|
||||||
|
if (cells != null) row(cells);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a table from a header and a list of row arrays in one call.
|
||||||
|
*/
|
||||||
|
public Table fromRows(String[] headerRow, Iterable<String[]> dataRows) {
|
||||||
|
if (headerRow != null && headerRow.length > 0) header(headerRow);
|
||||||
|
return rows(dataRows);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints the table to the given stream.
|
* Prints the table to the given stream.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -61,5 +61,15 @@ public final class Ansi {
|
|||||||
/** Clear line */
|
/** Clear line */
|
||||||
public static final String ERASE_LINE = "\u001B[2K";
|
public static final String ERASE_LINE = "\u001B[2K";
|
||||||
|
|
||||||
|
/** Clear entire screen and move cursor to home (1,1) */
|
||||||
|
public static final String CLEAR_SCREEN = "\u001B[H\u001B[2J";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cursor position (1-based row and column). ESC [ row ; col H
|
||||||
|
*/
|
||||||
|
public static String cursorTo(int row, int col) {
|
||||||
|
return "\u001B[" + Math.max(1, row) + ";" + Math.max(1, col) + "H";
|
||||||
|
}
|
||||||
|
|
||||||
private Ansi() {}
|
private Ansi() {}
|
||||||
}
|
}
|
||||||
|
|||||||
244
src/main/java/dev/jakub/terminal/example/Examples.java
Normal file
244
src/main/java/dev/jakub/terminal/example/Examples.java
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
package dev.jakub.terminal.example;
|
||||||
|
|
||||||
|
import dev.jakub.terminal.Terminal;
|
||||||
|
import dev.jakub.terminal.components.Log;
|
||||||
|
import dev.jakub.terminal.components.Notification;
|
||||||
|
import dev.jakub.terminal.components.Steps;
|
||||||
|
import dev.jakub.terminal.core.Color;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
public final class Examples {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
runAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void runAll() {
|
||||||
|
textAndRule();
|
||||||
|
keyValueAndBox();
|
||||||
|
table();
|
||||||
|
tableFromRows();
|
||||||
|
tree();
|
||||||
|
columns();
|
||||||
|
steps();
|
||||||
|
breadcrumb();
|
||||||
|
helpBlock();
|
||||||
|
log();
|
||||||
|
logWithMinLevel();
|
||||||
|
diff();
|
||||||
|
timeline();
|
||||||
|
heatmap();
|
||||||
|
chart();
|
||||||
|
badgeAndNotify();
|
||||||
|
codeBlock();
|
||||||
|
sysInfo();
|
||||||
|
markdown();
|
||||||
|
toc();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void textAndRule() {
|
||||||
|
Terminal.print("=== Text & Rule ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.rule().character('=').print();
|
||||||
|
Terminal.print("Hello").color(Color.GREEN).println();
|
||||||
|
Terminal.rule().doubles().prefix("-- ").suffix(" --").print();
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyValueAndBox() {
|
||||||
|
Terminal.print("=== KeyValue & Box ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.keyValue()
|
||||||
|
.row("Name", "terminal-ui")
|
||||||
|
.row("Version", "1.0")
|
||||||
|
.separator(" = ")
|
||||||
|
.print(System.out);
|
||||||
|
Terminal.box()
|
||||||
|
.title("Info")
|
||||||
|
.line("First line")
|
||||||
|
.lines("Second", "Third")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void table() {
|
||||||
|
Terminal.print("=== Table ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.table()
|
||||||
|
.header("Col A", "Col B", "Col C")
|
||||||
|
.row("1", "2", "3")
|
||||||
|
.row("4", "5", "6")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tableFromRows() {
|
||||||
|
Terminal.print("=== Table from rows ===").color(Color.CYAN).bold().println();
|
||||||
|
List<String[]> rows = List.of(
|
||||||
|
new String[]{"Alice", "10"},
|
||||||
|
new String[]{"Bob", "20"}
|
||||||
|
);
|
||||||
|
Terminal.table()
|
||||||
|
.header("Name", "Score")
|
||||||
|
.rows(rows)
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tree() {
|
||||||
|
Terminal.print("=== Tree ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.tree()
|
||||||
|
.node("root")
|
||||||
|
.child("src")
|
||||||
|
.child("main").end()
|
||||||
|
.child("test").end()
|
||||||
|
.end()
|
||||||
|
.child("docs")
|
||||||
|
.end()
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void columns() {
|
||||||
|
Terminal.print("=== Columns ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.columns()
|
||||||
|
.column("Left\ncolumn\ntext")
|
||||||
|
.column("Middle")
|
||||||
|
.column("Right")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void steps() {
|
||||||
|
Terminal.print("=== Steps ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.steps()
|
||||||
|
.step("Setup", Steps.Status.DONE)
|
||||||
|
.step("Build", Steps.Status.RUNNING)
|
||||||
|
.step("Deploy", Steps.Status.PENDING)
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void breadcrumb() {
|
||||||
|
Terminal.print("=== Breadcrumb ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.breadcrumb()
|
||||||
|
.crumb("Home")
|
||||||
|
.crumb("Projects")
|
||||||
|
.crumb("terminal-ui")
|
||||||
|
.separator(" / ")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void helpBlock() {
|
||||||
|
Terminal.print("=== Help (CLI usage) ===").color(Color.CYAN).bold().println();
|
||||||
|
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);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log() {
|
||||||
|
Terminal.print("=== Log ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.log()
|
||||||
|
.info("Started")
|
||||||
|
.warn("Deprecation notice")
|
||||||
|
.error("Something failed")
|
||||||
|
.withTimestamp()
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void logWithMinLevel() {
|
||||||
|
Terminal.print("=== Log (minLevel WARN) ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.log()
|
||||||
|
.info("Hidden")
|
||||||
|
.warn("Visible")
|
||||||
|
.error("Visible")
|
||||||
|
.minLevel(Log.Level.WARN)
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void diff() {
|
||||||
|
Terminal.print("=== Diff ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.diff()
|
||||||
|
.before("old line 1\nold line 2")
|
||||||
|
.after("new line 1\nnew line 2\nnew line 3")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void timeline() {
|
||||||
|
Terminal.print("=== Timeline ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.timeline()
|
||||||
|
.event("2025-01-01", "First release")
|
||||||
|
.event("2025-06-01", "Added SelectList")
|
||||||
|
.event("2026-01-01", "GitHub Packages")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void heatmap() {
|
||||||
|
Terminal.print("=== Heatmap ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.heatmap()
|
||||||
|
.row("Mon", 10, 20, 5, 30)
|
||||||
|
.row("Tue", 15, 25, 10, 20)
|
||||||
|
.row("Wed", 5, 15, 25, 35)
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chart() {
|
||||||
|
Terminal.print("=== Chart ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.chart()
|
||||||
|
.data(1.0, 2.0, 1.5, 3.0, 2.5)
|
||||||
|
.height(5)
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void badgeAndNotify() {
|
||||||
|
Terminal.print("=== Badge & Notify ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.badge("OK", Color.GREEN).println();
|
||||||
|
Terminal.badge("WARN", Color.YELLOW).print(System.out);
|
||||||
|
Terminal.notify("Done!", Notification.Type.SUCCESS);
|
||||||
|
Terminal.notify("Warning", Notification.Type.WARNING);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void codeBlock() {
|
||||||
|
Terminal.print("=== Code block ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.code("java")
|
||||||
|
.line("public static void main(String[] args) {")
|
||||||
|
.line(" System.out.println(\"Hello\");")
|
||||||
|
.line("}")
|
||||||
|
.lineNumbers()
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sysInfo() {
|
||||||
|
Terminal.print("=== SysInfo ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.sysinfo().print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void markdown() {
|
||||||
|
Terminal.print("=== Markdown ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.markdown("# Title\n\n**Bold** and *italic*.\n\n- Item 1\n- Item 2\n\n---")
|
||||||
|
.print(System.out);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void toc() {
|
||||||
|
Terminal.print("=== Table of contents ===").color(Color.CYAN).bold().println();
|
||||||
|
Terminal.toc()
|
||||||
|
.section("Introduction").sub("Overview").sub("Install")
|
||||||
|
.section("API").sub("Reference")
|
||||||
|
.section("Examples")
|
||||||
|
.print(System.out);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import dev.jakub.terminal.core.TerminalSupport;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Input prompt for reading user input. Use via {@link Terminal#prompt(String)}.
|
* Input prompt for reading user input. Use via {@link Terminal#prompt(String)}.
|
||||||
@@ -18,6 +19,8 @@ public final class Prompt {
|
|||||||
private boolean masked;
|
private boolean masked;
|
||||||
private PrintStream out = System.out;
|
private PrintStream out = System.out;
|
||||||
private InputStream in = System.in;
|
private InputStream in = System.in;
|
||||||
|
private Predicate<String> validator;
|
||||||
|
private String retryMessage = "Invalid, try again: ";
|
||||||
|
|
||||||
public Prompt(String message, TerminalSupport support) {
|
public Prompt(String message, TerminalSupport support) {
|
||||||
this.message = message != null ? message : "";
|
this.message = message != null ? message : "";
|
||||||
@@ -48,33 +51,62 @@ public final class Prompt {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates input: asks again until the predicate returns true. Null = no validation.
|
||||||
|
*/
|
||||||
|
public Prompt validate(Predicate<String> validator) {
|
||||||
|
this.validator = validator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message shown when validation fails (default: "Invalid, try again: ").
|
||||||
|
*/
|
||||||
|
public Prompt retryMessage(String retryMessage) {
|
||||||
|
this.retryMessage = retryMessage != null ? retryMessage : "Invalid, try again: ";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prompts and returns the entered string. Blocks until a line is read.
|
* Prompts and returns the entered string. Blocks until a line is read.
|
||||||
|
* If {@link #validate(Predicate)} is set, repeats until the predicate accepts the input.
|
||||||
*/
|
*/
|
||||||
public String ask() {
|
public String ask() {
|
||||||
out.print(message);
|
|
||||||
out.flush();
|
|
||||||
if (masked) {
|
if (masked) {
|
||||||
return readMasked();
|
return readMasked();
|
||||||
}
|
}
|
||||||
try (Scanner scan = new Scanner(in)) {
|
try (Scanner scan = new Scanner(in)) {
|
||||||
return scan.hasNextLine() ? scan.nextLine() : "";
|
return askLoop(scan);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prompts and returns the next line from the given scanner. Use this when
|
* Prompts and returns the next line from the given scanner. Use this when
|
||||||
* sharing one scanner (e.g. in an interactive app) so input blocks correctly.
|
* sharing one scanner (e.g. in an interactive app) so input blocks correctly.
|
||||||
|
* If {@link #validate(Predicate)} is set, repeats until the predicate accepts the input.
|
||||||
*/
|
*/
|
||||||
public String ask(Scanner sharedScanner) {
|
public String ask(Scanner sharedScanner) {
|
||||||
if (sharedScanner == null) return ask();
|
if (sharedScanner == null) return ask();
|
||||||
|
if (masked && System.console() != null) {
|
||||||
out.print(message);
|
out.print(message);
|
||||||
out.flush();
|
out.flush();
|
||||||
if (masked && System.console() != null) {
|
|
||||||
char[] chars = System.console().readPassword();
|
char[] chars = System.console().readPassword();
|
||||||
return chars != null ? new String(chars) : "";
|
return chars != null ? new String(chars) : "";
|
||||||
}
|
}
|
||||||
return sharedScanner.hasNextLine() ? sharedScanner.nextLine() : "";
|
return askLoop(sharedScanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String askLoop(Scanner scan) {
|
||||||
|
while (true) {
|
||||||
|
out.print(message);
|
||||||
|
out.flush();
|
||||||
|
if (!scan.hasNextLine()) return "";
|
||||||
|
String line = scan.nextLine();
|
||||||
|
if (line == null) line = "";
|
||||||
|
if (validator == null || validator.test(line)) return line;
|
||||||
|
out.print(retryMessage);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readMasked() {
|
private String readMasked() {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import dev.jakub.terminal.interactive.Prompt;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fluent builder for {@link Prompt}. Use via {@link Terminal#prompt(String)}.
|
* Fluent builder for {@link Prompt}. Use via {@link Terminal#prompt(String)}.
|
||||||
@@ -19,6 +20,8 @@ public final class PromptBuilder {
|
|||||||
private boolean masked;
|
private boolean masked;
|
||||||
private PrintStream output = System.out;
|
private PrintStream output = System.out;
|
||||||
private InputStream input = System.in;
|
private InputStream input = System.in;
|
||||||
|
private Predicate<String> validator;
|
||||||
|
private String retryMessage = "Invalid, try again: ";
|
||||||
|
|
||||||
public PromptBuilder(String message, TerminalSupport support) {
|
public PromptBuilder(String message, TerminalSupport support) {
|
||||||
this.message = message != null ? message : "";
|
this.message = message != null ? message : "";
|
||||||
@@ -49,14 +52,27 @@ public final class PromptBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates input: prompts again until the predicate returns true. Null = no validation.
|
||||||
|
*/
|
||||||
|
public PromptBuilder validate(Predicate<String> validator) {
|
||||||
|
this.validator = validator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message shown when validation fails (default: "Invalid, try again: ").
|
||||||
|
*/
|
||||||
|
public PromptBuilder retryMessage(String retryMessage) {
|
||||||
|
this.retryMessage = retryMessage != null ? retryMessage : "Invalid, try again: ";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prompts and returns the entered string.
|
* Prompts and returns the entered string.
|
||||||
*/
|
*/
|
||||||
public String ask() {
|
public String ask() {
|
||||||
Prompt p = new Prompt(message, support);
|
Prompt p = buildPrompt();
|
||||||
if (masked) p.masked();
|
|
||||||
p.output(output);
|
|
||||||
p.input(input);
|
|
||||||
return p.ask();
|
return p.ask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,10 +82,17 @@ public final class PromptBuilder {
|
|||||||
*/
|
*/
|
||||||
public String ask(Scanner sharedScanner) {
|
public String ask(Scanner sharedScanner) {
|
||||||
if (sharedScanner == null) return ask();
|
if (sharedScanner == null) return ask();
|
||||||
|
Prompt p = buildPrompt();
|
||||||
|
return p.ask(sharedScanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Prompt buildPrompt() {
|
||||||
Prompt p = new Prompt(message, support);
|
Prompt p = new Prompt(message, support);
|
||||||
if (masked) p.masked();
|
if (masked) p.masked();
|
||||||
p.output(output);
|
p.output(output);
|
||||||
p.input(input);
|
p.input(input);
|
||||||
return p.ask(sharedScanner);
|
if (validator != null) p.validate(validator);
|
||||||
|
p.retryMessage(retryMessage);
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user