Files
terminal-ui/docs/Examples.md
!verity b17b278840 Introduce Examples class with comprehensive demos and enhance table/log functionalities
- Add `Examples` class showcasing extensive usage of terminal-ui components (e.g., tables, logs, heatmaps, charts, and trees).
- Extend `Table` API with `rows` and `fromRows` methods for batch row addition and simplified table creation.
- Enhance `Log` component with a `minLevel` filtering feature for selective log visibility.
- Update `build.gradle.kts` to set `Examples` class as the main application entry point.
- Expand `Examples.md` documentation with new examples, including table rows and filtered logs.
2026-03-14 11:52:42 +01:00

5.5 KiB

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

Terminal.print("Hello").color(Color.CYAN).bold().println();

Terminal.rule().character('=').print();
Terminal.rule().doubles().prefix("-- ").suffix(" --").print();

Key-value & box

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

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

Terminal.tree()
    .node("root")
        .child("src")
            .child("main").end()
            .child("test").end()
        .end()
        .child("docs")
    .end()
    .print(System.out);

Columns

Terminal.columns()
    .column("Left\ncolumn\ntext")
    .column("Middle")
    .column("Right")
    .print(System.out);

Steps (wizard-style)

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

Terminal.breadcrumb()
    .crumb("Home")
    .crumb("Projects")
    .crumb("terminal-ui")
    .separator(" / ")
    .print(System.out);

Interactive: Prompt

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);

Interactive: Confirm

boolean yes = Terminal.confirm("Continue?").defaultYes().ask();
boolean no = Terminal.confirm("Delete?").defaultNo().ask();
boolean answer = Terminal.confirm("Proceed?").ask(sharedScanner);

Interactive: Menu

String env = Terminal.menu()
    .title("Choose environment")
    .option("Development")
    .option("Staging")
    .option("Production")
    .select();

Interactive: SelectList (arrows + Enter)

String choice = Terminal.selectList()
    .title("Pick one")
    .option("Option A")
    .option("Option B")
    .option("Option C")
    .select();

Interactive: Pager

Terminal.pager()
    .lines("Line 1", "Line 2", "Line 3", "Line 4", "Line 5")
    .pageSize(3)
    .interactive();

Progress bar

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

Spinner spin = Terminal.spinner().message("Loading...").start();
// ... do work ...
spin.stop();

(Use import dev.jakub.terminal.live.Spinner;.)

Diff

Terminal.diff()
    .before("old line 1\nold line 2")
    .after("new line 1\nnew line 2\nnew line 3")
    .print(System.out);

Log

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

Terminal.timeline()
    .event("2024-01-01", "First release")
    .event("2024-06-01", "Added SelectList")
    .event("2025-01-01", "GitHub Packages")
    .print(System.out);

Heatmap

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)

Terminal.chart()
    .data(1.0, 2.0, 1.5, 3.0, 2.5)
    .height(5)
    .print(System.out);

Badge & notification

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

Terminal.code("java")
    .line("public static void main(String[] args) {")
    .line("    System.out.println(\"Hello\");")
    .line("}")
    .lineNumbers()
    .print(System.out);

SysInfo

Terminal.sysinfo().print(System.out);

Markdown

Terminal.markdown("# Title\n\n**Bold** and *italic*.\n\n- Item 1\n- Item 2\n\n---")
    .print(System.out);

Table of contents

Terminal.toc()
    .section("Introduction").sub("Overview").sub("Install")
    .section("API").sub("Reference")
    .section("Examples")
    .print(System.out);

For more, see the README and the source.