Class StyledWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.PrintWriter
-
- org.jline.style.StyledWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.Appendable
,java.lang.AutoCloseable
public class StyledWriter extends java.io.PrintWriter
APrintWriter
extension that understands and evaluatesStyleExpression
syntax.This class extends PrintWriter to provide automatic evaluation of style expressions in the format
@{style value}
when writing strings. It uses aStyleExpression
to evaluate the expressions and aTerminal
to convert the resultingAttributedString
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:
StyleExpression
,PrintWriter
,Terminal
-
-
Constructor Summary
Constructors Constructor Description StyledWriter(java.io.OutputStream out, org.jline.terminal.Terminal terminal, StyleResolver resolver, boolean autoFlush)
Constructs a new StyledWriter that writes to an OutputStream.StyledWriter(java.io.Writer out, org.jline.terminal.Terminal terminal, StyleResolver resolver, boolean autoFlush)
Constructs a new StyledWriter that writes to a Writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.PrintWriter
format(java.lang.String format, java.lang.Object... args)
Formats a string and writes it after evaluating any style expressions it contains.java.io.PrintWriter
format(java.util.Locale locale, java.lang.String format, java.lang.Object... args)
Formats a string using the specified locale and writes it after evaluating any style expressions it contains.void
write(java.lang.String value)
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
-
-
-
-
Constructor Detail
-
StyledWriter
public StyledWriter(java.io.Writer out, org.jline.terminal.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 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:
java.lang.NullPointerException
- if terminal is null
-
StyledWriter
public StyledWriter(java.io.OutputStream out, org.jline.terminal.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 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:
java.lang.NullPointerException
- if terminal is null
-
-
Method Detail
-
write
public void write(@Nonnull java.lang.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 classjava.io.PrintWriter
- Parameters:
value
- the string to write (must not be null)- Throws:
java.lang.NullPointerException
- if value is null
-
format
public java.io.PrintWriter format(@Nonnull java.lang.String format, java.lang.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 classjava.io.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:
java.lang.NullPointerException
- if format is null- See Also:
String.format(String, Object...)
-
format
public java.io.PrintWriter format(java.util.Locale locale, @Nonnull java.lang.String format, java.lang.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 classjava.io.PrintWriter
- 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:
java.lang.NullPointerException
- if format is null- See Also:
String.format(Locale, String, Object...)
-
-