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.

  • Field Details

  • Constructor Details

  • Method Details

    • println

      public void println(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(Map<String,Object> optionsIn, 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 String[] appendUsage(String[] customUsage)
    • compileOptions

      public Map<String,Object> compileOptions(Options opt)
    • optionRowHighlight

      private Printer.TableRows optionRowHighlight(Object value)
    • prntCommand

      public 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(Map<Class<?>,Function<Object,Map<String,Object>>> objectToMap)
      Override ScriptEngine toMap() method
      Parameters:
      objectToMap - key: object class, value: toMap function
    • setObjectToString

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

      public void setHighlightValue(Map<String,Function<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(Map<String,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 Map<String,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
    • internalPrintln

      private void internalPrintln(Map<String,Object> options, Object object)
    • highlightAndPrint

      protected void highlightAndPrint(Map<String,Object> options, Throwable exception)
      Highlight and print an exception
      Parameters:
      options - Printing options
      exception - Exception to be printed
    • highlight

      private AttributedString highlight(CmdDesc cmdDesc)
    • valueHighlighter

      private SyntaxHighlighter valueHighlighter(String style)
    • truncate4nanorc

      private String truncate4nanorc(String obj)
    • highlight

      private AttributedString highlight(Integer width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight)
    • doValueHighlight

      private boolean doValueHighlight(Map<String,Object> options, String value)
    • highlightAndPrint

      private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight, int maxRows)
    • highlightAndPrint

      private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight)
    • keysToString

      private Map<String,Object> keysToString(Map<Object,Object> map)
    • mapValue

      private Object mapValue(Map<String,Object> options, String key, Map<String,Object> map)
    • optionList

      private List<String> optionList(String key, Map<String,Object> options)
    • hasMatch

      private boolean hasMatch(List<String> regexes, String value)
    • addPadding

      private AttributedString addPadding(AttributedString str, int width)
    • addPadding

      private String addPadding(String str, int width)
    • columnValue

      private String columnValue(String value)
    • objectToMap

      private Map<String,Object> objectToMap(Map<String,Object> options, Object obj)
    • objectToString

      private String objectToString(Map<String,Object> options, Object obj)
    • highlightMapValue

      private AttributedString highlightMapValue(Map<String,Object> options, String key, Map<String,Object> map)
    • isHighlighted

      private boolean isHighlighted(AttributedString value)
    • highlightValue

      private AttributedString highlightValue(Map<String,Object> options, String column, Object obj)
    • truncateValue

      private AttributedString truncateValue(Map<String,Object> options, AttributedString value)
    • truncateValue

      private String truncateValue(int maxWidth, String value)
    • objectToList

      private List<Object> objectToList(Object obj)
    • similarSets

      private boolean similarSets(List<String> ref, Set<String> c2, int matchLimit)
    • println

      private void println(AttributedString line, int maxrows)
    • columnName

      private String columnName(String name, boolean shortName)
    • isNumber

      private boolean isNumber(String str)
    • highlightAndPrint

      private void highlightAndPrint(Map<String,Object> options, Object obj)
    • doRowHighlight

      private boolean doRowHighlight(int row, Printer.TableRows tableRows)
    • highlightList

      private void highlightList(Map<String,Object> options, List<Object> collection, int width)
    • highlightList

      private void highlightList(Map<String,Object> options, List<Object> collection, int width, int depth)
    • collectionObject

      private boolean collectionObject(Object obj)
    • simpleObject

      private boolean simpleObject(Object obj)
    • canConvert

      private boolean canConvert(Object obj)
    • digits

      private int digits(int number)
    • cellWidth

      private int cellWidth(int pos, List<Integer> columns, boolean rownum, String columnSep)
    • toTabStops

      private void toTabStops(List<Integer> columns, int rows, boolean rownum, String columnSep)
    • highlightMap

      private void highlightMap(Map<String,Object> options, Map<String,Object> map, int width)
    • highlightMap

      private void highlightMap(Map<String,Object> options, Map<String,Object> map, int width, int depth)