Package software.amazon.smithy.cli
Class ColorBuffer
- java.lang.Object
-
- software.amazon.smithy.cli.ColorBuffer
-
- All Implemented Interfaces:
java.lang.Appendable
,java.lang.AutoCloseable
public final class ColorBuffer extends java.lang.Object implements java.lang.Appendable, java.lang.AutoCloseable
AnAppendable
that supports color provided by aColorFormatter
.A
ColorBuffer
is not thread-safe and is meant to write short contiguous output text that will eventually be written to other things like aCliPrinter
.When wrapping a
CliPrinter
, ensure you useclose()
to write to the printer. Alternatively, wrap the buffer in a try-with-resources block.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ColorBuffer
append(char c)
ColorBuffer
append(java.lang.CharSequence csq)
ColorBuffer
append(java.lang.CharSequence csq, int start, int end)
void
close()
static ColorBuffer
of(ColorFormatter colors, java.lang.Appendable sink)
Create a new ColorBuffer that directly writes to the givensink
.static ColorBuffer
of(ColorFormatter colors, CliPrinter sink)
Create a new ColorBuffer that stores all output to an internal buffer and only writes to the givenCliPrinter
whenclose()
is called.ColorBuffer
print(java.lang.String text, Style... styles)
Writes styled text to the builder using the CliPrinter's color settings.ColorBuffer
println()
Writes a system-dependent new line.ColorBuffer
println(java.lang.String text, Style... styles)
Prints a line of styled text to the buffer.ColorBuffer
style(java.util.function.Consumer<ColorBuffer> bufferConsumer, Style... styles)
java.lang.String
toString()
-
-
-
Method Detail
-
of
public static ColorBuffer of(ColorFormatter colors, java.lang.Appendable sink)
Create a new ColorBuffer that directly writes to the givensink
.No additional buffering is used when buffering over an
Appendable
. Each call to write to the buffer will write to the appendable.- Parameters:
colors
- ColorFormatter used to provide colors and style.sink
- Where to write.- Returns:
- Returns the created buffer.
-
of
public static ColorBuffer of(ColorFormatter colors, CliPrinter sink)
Create a new ColorBuffer that stores all output to an internal buffer and only writes to the givenCliPrinter
whenclose()
is called.- Parameters:
colors
- ColorFormatter used to provide colors and style.sink
- Where to write.- Returns:
- Returns the created buffer.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
append
public ColorBuffer append(java.lang.CharSequence csq)
- Specified by:
append
in interfacejava.lang.Appendable
-
append
public ColorBuffer append(java.lang.CharSequence csq, int start, int end)
- Specified by:
append
in interfacejava.lang.Appendable
-
append
public ColorBuffer append(char c)
- Specified by:
append
in interfacejava.lang.Appendable
-
print
public ColorBuffer print(java.lang.String text, Style... styles)
Writes styled text to the builder using the CliPrinter's color settings.- Parameters:
text
- Text to write.styles
- Styles to apply to the text.- Returns:
- Returns self.
-
println
public ColorBuffer println(java.lang.String text, Style... styles)
Prints a line of styled text to the buffer.- Parameters:
text
- Text to print.styles
- Styles to apply.- Returns:
- Returns self.
-
println
public ColorBuffer println()
Writes a system-dependent new line.- Returns:
- Returns the buffer.
-
style
public ColorBuffer style(java.util.function.Consumer<ColorBuffer> bufferConsumer, Style... styles)
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
-