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.
This commit is contained in:
@@ -22,6 +22,7 @@ public final class Log {
|
||||
private final TerminalSupport support;
|
||||
private final List<Entry> entries = new ArrayList<>();
|
||||
private boolean withTimestamp;
|
||||
private Level minLevel;
|
||||
|
||||
public Log(TerminalSupport 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) {
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss").withZone(ZoneId.systemDefault());
|
||||
for (Entry e : entries) {
|
||||
if (minLevel != null && e.level.ordinal() < minLevel.ordinal()) continue;
|
||||
String prefix = withTimestamp ? "[" + fmt.format(Instant.now()) + "] " : "";
|
||||
String label = "[" + e.level.name() + "]";
|
||||
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),
|
||||
WARN(Ansi.FG_YELLOW),
|
||||
ERROR(Ansi.FG_RED),
|
||||
DEBUG(Ansi.FG_BRIGHT_BLACK);
|
||||
ERROR(Ansi.FG_RED);
|
||||
|
||||
final String ansi;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user