Class StyledWriter

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class StyledWriter extends PrintWriter
A PrintWriter extension that understands and evaluates StyleExpression syntax.

This class extends PrintWriter to provide automatic evaluation of style expressions in the format @{style value} when writing strings. It uses a StyleExpression to evaluate the expressions and a Terminal to convert the resulting AttributedStrings to ANSI escape sequences appropriate for the terminal.

Example usage:

 Terminal terminal = ...; // Get a Terminal instance
 StyleResolver resolver = Styler.resolver("mygroup");

 // Create a StyledWriter that writes to System.out
 StyledWriter writer = new StyledWriter(System.out, terminal, resolver, true);

 // Write styled text
 writer.println("Normal text with @{bold,fg:red important} parts");
 writer.printf("@{bold %s}: @{fg:blue %s}", "Name", "John Doe");
 
Since:
3.4
See Also:
  • Constructor Details

    • StyledWriter

      public StyledWriter(Writer out, Terminal terminal, StyleResolver resolver, boolean autoFlush)
      Constructs a new StyledWriter that writes to a Writer.

      This constructor creates a StyledWriter that will write to the specified Writer, using the specified Terminal to convert AttributedStrings to ANSI escape sequences and the specified StyleResolver to resolve style specifications.

      Parameters:
      out - the Writer to write to
      terminal - the Terminal to use for ANSI conversion (must not be null)
      resolver - the StyleResolver to use for style resolution (must not be null)
      autoFlush - whether to automatically flush the output after each print operation
      Throws:
      NullPointerException - if terminal is null
    • StyledWriter

      public StyledWriter(OutputStream out, Terminal terminal, StyleResolver resolver, boolean autoFlush)
      Constructs a new StyledWriter that writes to an OutputStream.

      This constructor creates a StyledWriter that will write to the specified OutputStream, using the specified Terminal to convert AttributedStrings to ANSI escape sequences and the specified StyleResolver to resolve style specifications.

      Parameters:
      out - the OutputStream to write to
      terminal - the Terminal to use for ANSI conversion (must not be null)
      resolver - the StyleResolver to use for style resolution (must not be null)
      autoFlush - whether to automatically flush the output after each print operation
      Throws:
      NullPointerException - if terminal is null
  • Method Details

    • write

      public void write(@Nonnull String value)
      Writes a string after evaluating any style expressions it contains.

      This method overrides the standard write method to evaluate any style expressions in the format @{style value} in the input string before writing it. The resulting AttributedString is converted to ANSI escape sequences appropriate for the terminal.

      Overrides:
      write in class PrintWriter
      Parameters:
      value - the string to write (must not be null)
      Throws:
      NullPointerException - if value is null
    • format

      public PrintWriter format(@Nonnull String format, Object... args)
      Formats a string and writes it after evaluating any style expressions it contains.

      This method overrides the standard format method to ensure that the entire formatted string is evaluated for style expressions before any output is written. This prevents partial output from being written, which could lead to rendering exceptions with ANSI escape sequences.

      Overrides:
      format in class PrintWriter
      Parameters:
      format - the format string (must not be null)
      args - the arguments referenced by the format specifiers in the format string
      Returns:
      this StyledWriter
      Throws:
      NullPointerException - if format is null
      See Also:
    • format

      public PrintWriter format(Locale locale, @Nonnull String format, Object... args)
      Formats a string using the specified locale and writes it after evaluating any style expressions it contains.

      This method overrides the standard format method to ensure that the entire formatted string is evaluated for style expressions before any output is written. This prevents partial output from being written, which could lead to rendering exceptions with ANSI escape sequences.

      Overrides:
      format in class PrintWriter
      Parameters:
      locale - the locale to use for formatting
      format - the format string (must not be null)
      args - the arguments referenced by the format specifiers in the format string
      Returns:
      this StyledWriter
      Throws:
      NullPointerException - if format is null
      See Also: