Class StyledWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
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
AttributedString
s 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:
-
Field Summary
Fields inherited from class java.io.PrintWriter
out
-
Constructor Summary
ConstructorsConstructorDescriptionStyledWriter
(OutputStream out, Terminal terminal, StyleResolver resolver, boolean autoFlush) Constructs a new StyledWriter that writes to an OutputStream.StyledWriter
(Writer out, Terminal terminal, StyleResolver resolver, boolean autoFlush) Constructs a new StyledWriter that writes to a Writer. -
Method Summary
Modifier and TypeMethodDescriptionFormats a string and writes it after evaluating any style expressions it contains.Formats a string using the specified locale and writes it after evaluating any style expressions it contains.void
Writes a string after evaluating any style expressions it contains.Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, close, flush, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write
Methods inherited from class java.io.Writer
nullWriter
-
Constructor Details
-
StyledWriter
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 toterminal
- 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
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 toterminal
- 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
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 classPrintWriter
- Parameters:
value
- the string to write (must not be null)- Throws:
NullPointerException
- if value is null
-
format
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 classPrintWriter
- 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
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 classPrintWriter
- Parameters:
locale
- the locale to use for formattingformat
- 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:
-