Class WanakuPrinter

java.lang.Object
org.jline.console.impl.AbstractCommandRegistry
org.jline.console.impl.JlineCommandRegistry
org.jline.console.impl.DefaultPrinter
ai.wanaku.cli.main.support.WanakuPrinter
All Implemented Interfaces:
org.jline.console.CommandRegistry, org.jline.console.Printer

public class WanakuPrinter extends org.jline.console.impl.DefaultPrinter
Enhanced printer utility for the Wanaku CLI application that provides formatted output capabilities.

This class extends JLine's DefaultPrinter to provide a rich set of printing methods with consistent styling and formatting for terminal output. It supports various output formats including styled messages, tables, and object representations.

Key features include:

  • Styled message printing (error, warning, info, success)
  • Markdown rendering with terminal styling
  • Table printing with customizable options and column selection
  • Object-to-map conversion and printing
  • Exception highlighting with configurable display modes
  • Terminal color and ANSI support

Example usage:


 WanakuPrinter printer = new WanakuPrinter(configPath, terminal);
 printer.printSuccessMessage("Operation completed successfully");
 printer.printMarkdown("# Heading\n\nThis is **bold** text.");
 printer.printTable(dataList, "name", "status", "timestamp");
 printer.printAsMap(configuration, "host", "port", "enabled");
 
Since:
1.0
Version:
1.0
Author:
Wanaku CLI Team
See Also:
  • DefaultPrinter
  • Terminal
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.jline.console.CommandRegistry

    org.jline.console.CommandRegistry.CommandSession

    Nested classes/interfaces inherited from interface org.jline.console.Printer

    org.jline.console.Printer.TableRows
  • Field Summary

    Fields inherited from class org.jline.console.impl.DefaultPrinter

    PRNT_INDENTION, PRNT_MAX_DEPTH, PRNT_MAX_ROWS, VAR_PRNT_OPTIONS

    Fields inherited from interface org.jline.console.Printer

    ALL, BOOLEAN_KEYS, BORDER, COLUMNS, COLUMNS_IN, COLUMNS_OUT, EXCLUDE, HIGHLIGHT_VALUE, INCLUDE, INDENTION, MAP_SIMILARITY, MAX_COLUMN_WIDTH, MAX_DEPTH, MAXROWS, MULTI_COLUMNS, OBJECT_TO_MAP, OBJECT_TO_STRING, ONE_ROW_TABLE, ROW_HIGHLIGHT, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS, STRUCT_ON_TABLE, STYLE, TO_STRING, VALUE_STYLE, VALUE_STYLE_ALL, WIDTH
  • Constructor Summary

    Constructors
    Constructor
    Description
    WanakuPrinter(org.jline.builtins.ConfigurationPath configPath, org.jline.terminal.Terminal terminal)
    Constructs a new WanakuPrinter with the specified configuration and terminal.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Highlights and prints exception information based on configured display mode.
    void
    pageMarkdown(String markdown)
    Displays Markdown text in an interactive pager (similar to Unix 'less').
    <T> void
    printAsMap(T object)
    Prints an object as a map with all available properties.
    <T> void
    printAsMap(T object, String... keys)
    Prints an object as a map with only specified keys.
    void
    Prints an error message with red styling.
    void
    Prints an informational message with blue styling.
    void
    Prints Markdown text with terminal styling and formatting.
    void
    Prints a success message with green styling.
    <T> void
    printTable(List<T> printables)
    Prints a list of objects as a table with all available columns.
    <T> void
    printTable(List<T> printables, String... columns)
    Prints a list of objects as a table with specified columns.
    <T> void
    printTable(Map<String,Object> options, List<T> printables, String... columns)
    Prints a list of objects as a table with custom formatting options and specified columns.
    <T> void
    printTable(Map<String,Object> options, List<T> objectsToPrint, Function<T,Map<String,Object>> toMap, String... columns)
    Prints a list of objects as a table with full customization options.
    void
    Prints a warning message with yellow styling.
    protected org.jline.terminal.Terminal
    Returns the terminal instance used by this printer.
    static org.jline.terminal.Terminal
    Creates a new terminal instance with resilient fallback handling.

    Methods inherited from class org.jline.console.impl.DefaultPrinter

    appendUsage, compileOptions, defaultPrntOptions, manageBooleanOptions, println, println, prntCommand, refresh, setHighlightValue, setObjectToMap, setObjectToString

    Methods inherited from class org.jline.console.impl.JlineCommandRegistry

    commandDescription, commandInfo, commandOptions, compileCommandDescription, compileCommandInfo, compileCommandOptions, defaultCompleter, parseOptions

    Methods inherited from class org.jline.console.impl.AbstractCommandRegistry

    alias, commandAliases, commandNames, compileCompleters, doHelpDesc, getCommandMethods, hasCommand, invoke, registerCommands, registerCommands, registeredCommand, rename, saveException

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.jline.console.CommandRegistry

    name
  • Constructor Details

    • WanakuPrinter

      public WanakuPrinter(org.jline.builtins.ConfigurationPath configPath, org.jline.terminal.Terminal terminal)
      Constructs a new WanakuPrinter with the specified configuration and terminal.
      Parameters:
      configPath - the configuration path for printer settings
      terminal - the terminal instance to use for output operations
      Throws:
      IllegalArgumentException - if terminal is null
  • Method Details

    • terminalInstance

      public static org.jline.terminal.Terminal terminalInstance() throws IOException
      Creates a new terminal instance with resilient fallback handling.

      This factory method attempts to create a terminal in the following order:

      1. System terminal with Jansi and color support (best experience)
      2. System terminal without Jansi if native libraries fail
      3. Dumb terminal if system terminal is unavailable (fallback)

      This approach ensures the CLI works in various environments including: standard terminals, CI/CD pipelines, Docker containers, and IDEs.

      Returns:
      a new terminal instance, guaranteed to be non-null
      Throws:
      IOException - if terminal creation fails catastrophically
    • terminal

      protected org.jline.terminal.Terminal terminal()
      Returns the terminal instance used by this printer.
      Overrides:
      terminal in class org.jline.console.impl.DefaultPrinter
      Returns:
      the terminal instance for output operations
    • printTable

      public <T> void printTable(List<T> printables)
      Prints a list of objects as a table with all available columns.

      This is a convenience method that displays all properties of the objects in the list as table columns using default table formatting options.

      Type Parameters:
      T - the type of objects in the list
      Parameters:
      printables - the list of objects to display as a table
    • printTable

      public <T> void printTable(List<T> printables, String... columns)
      Prints a list of objects as a table with specified columns.

      Displays only the specified columns from the objects in the list. Uses default table formatting options.

      Type Parameters:
      T - the type of objects in the list
      Parameters:
      printables - the list of objects to display as a table
      columns - the column names to include in the table output
    • printTable

      public <T> void printTable(Map<String,Object> options, List<T> printables, String... columns)
      Prints a list of objects as a table with custom formatting options and specified columns.

      Provides full control over table appearance through custom options while using the default object-to-map conversion strategy.

      Type Parameters:
      T - the type of objects in the list
      Parameters:
      options - custom formatting options for table display
      printables - the list of objects to display as a table
      columns - the column names to include in the table output
    • printTable

      public <T> void printTable(Map<String,Object> options, List<T> objectsToPrint, Function<T,Map<String,Object>> toMap, String... columns)
      Prints a list of objects as a table with full customization options.

      This is the most flexible table printing method, allowing custom formatting options, custom object-to-map conversion logic, and column selection. Handles empty or null input gracefully and provides error handling for conversion failures.

      Type Parameters:
      T - the type of objects in the list
      Parameters:
      options - custom formatting options for table display
      objectsToPrint - the list of objects to display as a table
      toMap - function to convert objects to map representation
      columns - the column names to include in the table output
    • printAsMap

      public <T> void printAsMap(T object)
      Prints an object as a map with all available properties.

      Converts the object to a map representation and displays all key-value pairs using default map formatting options.

      Type Parameters:
      T - the type of the object to print
      Parameters:
      object - the object to display as a map
    • printAsMap

      public <T> void printAsMap(T object, String... keys)
      Prints an object as a map with only specified keys.

      Converts the object to a map representation and displays only the key-value pairs for the specified keys. Keys not present in the object are silently ignored.

      Type Parameters:
      T - the type of the object to print
      Parameters:
      object - the object to display as a map
      keys - the specific keys to include in the output
    • printErrorMessage

      public void printErrorMessage(String message)
      Prints an error message with red styling.

      Uses bold red text to display error messages. Null messages are silently ignored.

      Parameters:
      message - the error message to display, null values are ignored
    • printWarningMessage

      public void printWarningMessage(String message)
      Prints a warning message with yellow styling.

      Uses yellow text to display warning messages. Null messages are silently ignored.

      Parameters:
      message - the warning message to display, null values are ignored
    • printInfoMessage

      public void printInfoMessage(String message)
      Prints an informational message with blue styling.

      Uses blue text to display informational messages. Null messages are silently ignored.

      Parameters:
      message - the informational message to display, null values are ignored
    • printSuccessMessage

      public void printSuccessMessage(String message)
      Prints a success message with green styling.

      Uses bold green text to display success messages. Null messages are silently ignored.

      Parameters:
      message - the success message to display, null values are ignored
    • printMarkdown

      public void printMarkdown(String markdown)
      Prints Markdown text with terminal styling and formatting.

      Parses and renders Markdown text with ANSI color codes and text formatting for rich terminal output. Supports headings, bold, italic, code blocks, lists, and links.

      Example Markdown features:

      • Headings: # Heading - rendered in bold cyan
      • Bold: **text** - rendered in bold
      • Italic: *text* - rendered in italic
      • Code: `code` - rendered in yellow
      • Lists: - item - rendered with bullets
      • Links: [text](url) - rendered in blue underlined
      • Tables: GitHub Flavored Markdown tables with borders
      Parameters:
      markdown - the Markdown text to render and display
      Throws:
      IllegalArgumentException - if markdown is null
    • pageMarkdown

      public void pageMarkdown(String markdown) throws IOException, InterruptedException
      Displays Markdown text in an interactive pager (similar to Unix 'less').

      This method renders Markdown text with terminal styling and displays it in an interactive pager that allows users to navigate through the content. The pager supports keyboard navigation:

      • Space/f: Forward one page
      • b: Backward one page
      • j/↓: Forward one line
      • k/↑: Backward one line
      • g/Home: Go to first line
      • G/End: Go to last line
      • /pattern: Search forward for pattern
      • ?pattern: Search backward for pattern
      • n: Repeat previous search
      • N: Repeat previous search in reverse direction
      • q: Quit the pager
      • h: Display help

      This is useful for displaying long documentation or help text that doesn't fit on one screen.

      Parameters:
      markdown - the Markdown text to render and display in the pager
      Throws:
      IllegalArgumentException - if markdown is null
      IOException - if there's an error displaying the pager
      InterruptedException - if the pager is interrupted
    • highlightAndPrint

      protected void highlightAndPrint(Map<String,Object> options, Throwable exception)
      Highlights and prints exception information based on configured display mode.

      Supports two display modes:

      • stack (default): Prints full stack trace to stderr
      • message: Prints only the exception message with emphasis styling

      The display mode is controlled by the "exception" option in the provided options map.

      Overrides:
      highlightAndPrint in class org.jline.console.impl.DefaultPrinter
      Parameters:
      options - configuration options including exception display mode
      exception - the exception to display, null exceptions are silently ignored