Class DefaultPrinter

  • All Implemented Interfaces:
    CommandRegistry, Printer

    public class DefaultPrinter
    extends JlineCommandRegistry
    implements Printer
    Default implementation of the Printer interface that provides syntax highlighting and formatting.

    DefaultPrinter provides functionality for printing various types of objects to the console with syntax highlighting and formatting. It supports printing:

    • Simple objects (strings, numbers, etc.)
    • Collections and maps
    • Tables with row highlighting
    • JSON and other structured data
    • Source code with syntax highlighting

    The printer can be configured with various options to control the formatting and highlighting of the output, such as maximum number of rows, indentation, and highlighting styles.

    • Method Detail

      • println

        public void println​(java.lang.Object object)
        Description copied from interface: Printer
        Prints an object to the console using default formatting options. This is a convenience method that calls Printer.println(Map, Object) with an empty options map.
        Specified by:
        println in interface Printer
        Parameters:
        object - the object to print
      • println

        public void println​(java.util.Map<java.lang.String,​java.lang.Object> optionsIn,
                            java.lang.Object object)
        Description copied from interface: Printer
        Prints an object to the console with the specified formatting options. The method handles different object types and formats them according to the provided options.
        Specified by:
        println in interface Printer
        Parameters:
        optionsIn - a map of formatting options that control how the object is displayed
        object - the object to print
      • refresh

        public boolean refresh()
        Description copied from interface: Printer
        Clears the printer's syntax highlighter cache and refreshes internal state. This method should be called when the highlighting rules or styles have changed and need to be reapplied.
        Specified by:
        refresh in interface Printer
        Returns:
        true if the refresh operation was successful, false otherwise
      • appendUsage

        public java.lang.String[] appendUsage​(java.lang.String[] customUsage)
      • compileOptions

        public java.util.Map<java.lang.String,​java.lang.Object> compileOptions​(Options opt)
      • prntCommand

        public java.lang.Exception prntCommand​(CommandInput input)
        Description copied from interface: Printer
        Executes a print command with the given input. This method can be implemented by printer implementations to handle specific print commands. The default implementation returns null, indicating no error occurred.
        Specified by:
        prntCommand in interface Printer
        Parameters:
        input - the command input containing the command and its arguments
        Returns:
        an Exception if an error occurred during command execution, or null if successful
      • setObjectToMap

        public void setObjectToMap​(java.util.Map<java.lang.Class<?>,​java.util.function.Function<java.lang.Object,​java.util.Map<java.lang.String,​java.lang.Object>>> objectToMap)
        Override ScriptEngine toMap() method
        Parameters:
        objectToMap - key: object class, value: toMap function
      • setObjectToString

        public void setObjectToString​(java.util.Map<java.lang.Class<?>,​java.util.function.Function<java.lang.Object,​java.lang.String>> objectToString)
        Override ScriptEngine toString() method
        Parameters:
        objectToString - key: object class, value: toString function
      • setHighlightValue

        public void setHighlightValue​(java.util.Map<java.lang.String,​java.util.function.Function<java.lang.Object,​AttributedString>> highlightValue)
        Highlight column value
        Parameters:
        highlightValue - key: regex for column name, value: highlight function
      • terminal

        protected Terminal terminal()
        Returns:
        terminal to which will be printed
      • manageBooleanOptions

        protected void manageBooleanOptions​(java.util.Map<java.lang.String,​java.lang.Object> options)
        Boolean printing options Printer checks only if key is present. Boolean options that have false value are removed from the options Map.
        Parameters:
        options - printing options
      • defaultPrntOptions

        protected java.util.Map<java.lang.String,​java.lang.Object> defaultPrntOptions​(boolean skipDefault)
        Set default and mandatory printing options. Also unsupported options will be removed when Printer is used without scriptEngine
        Parameters:
        skipDefault - when true does not set default options
        Returns:
        default, mandatory and supported options
      • highlightAndPrint

        protected void highlightAndPrint​(java.util.Map<java.lang.String,​java.lang.Object> options,
                                         java.lang.Throwable exception)
        Highlight and print an exception
        Parameters:
        options - Printing options
        exception - Exception to be printed