Class AnsiWriter

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class AnsiWriter extends FilterWriter
A writer that processes ANSI escape sequences.

The AnsiWriter class extends FilterWriter to intercept and process ANSI escape sequences written to the underlying writer. It extracts ANSI escape codes and calls corresponding process* methods for each recognized sequence.

This class just filters out the escape codes so that they are not sent out to the underlying Writer: process* methods are empty. Subclasses should actually perform the ANSI escape behaviors by implementing active code in process* methods.

This class is useful for implementing terminal emulation, where ANSI escape sequences need to be interpreted to control cursor movement, text attributes, colors, and other terminal features.

The class handles various ANSI escape sequences, including:

  • Cursor movement (up, down, left, right)
  • Cursor positioning (absolute and relative)
  • Text attributes (bold, underline, blink, etc.)
  • Colors (foreground and background)
  • Screen clearing and line manipulation

For more information about ANSI escape codes, see: Wikipedia: ANSI escape code

Since:
1.0
  • Field Details

  • Constructor Details

    • AnsiWriter

      public AnsiWriter(Writer out)
  • Method Details

    • write

      public void write(int data) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • processRestoreCursorPosition

      protected void processRestoreCursorPosition() throws IOException
      Process CSI u ANSI code, corresponding to RCP ? Restore Cursor Position
      Throws:
      IOException - if an error occurs
    • processSaveCursorPosition

      protected void processSaveCursorPosition() throws IOException
      Process CSI s ANSI code, corresponding to SCP ? Save Cursor Position
      Throws:
      IOException - if an error occurs
    • processInsertLine

      protected void processInsertLine(int optionInt) throws IOException
      Process CSI s ANSI code, corresponding to IL ? Insert Line
      Parameters:
      optionInt - the option
      Throws:
      IOException - if an error occurs
    • processDeleteLine

      protected void processDeleteLine(int optionInt) throws IOException
      Process CSI s ANSI code, corresponding to DL ? Delete Line
      Parameters:
      optionInt - the option
      Throws:
      IOException - if an error occurs
    • processScrollDown

      protected void processScrollDown(int optionInt) throws IOException
      Process CSI n T ANSI code, corresponding to SD ? Scroll Down
      Parameters:
      optionInt - the option
      Throws:
      IOException - if an error occurs
    • processScrollUp

      protected void processScrollUp(int optionInt) throws IOException
      Process CSI n U ANSI code, corresponding to SU ? Scroll Up
      Parameters:
      optionInt - the option
      Throws:
      IOException - if an error occurs
    • processEraseScreen

      protected void processEraseScreen(int eraseOption) throws IOException
      Process CSI n J ANSI code, corresponding to ED ? Erase in Display
      Parameters:
      eraseOption - the erase option
      Throws:
      IOException - if an error occurs
    • processEraseLine

      protected void processEraseLine(int eraseOption) throws IOException
      Process CSI n K ANSI code, corresponding to ED ? Erase in Line
      Parameters:
      eraseOption - the erase option
      Throws:
      IOException - if an error occurs
    • processSetAttribute

      protected void processSetAttribute(int attribute) throws IOException
      process SGR other than 0 (reset), 30-39 (foreground), 40-49 (background), 90-97 (foreground high intensity) or 100-107 (background high intensity)
      Parameters:
      attribute - the attribute to set
      Throws:
      IOException - if an error occurs
      See Also:
    • processSetForegroundColor

      protected void processSetForegroundColor(int color) throws IOException
      process SGR 30-37 corresponding to Set text color (foreground).
      Parameters:
      color - the text color
      Throws:
      IOException - if an error occurs
    • processSetForegroundColor

      protected void processSetForegroundColor(int color, boolean bright) throws IOException
      process SGR 30-37 or SGR 90-97 corresponding to Set text color (foreground) either in normal mode or high intensity.
      Parameters:
      color - the text color
      bright - is high intensity?
      Throws:
      IOException - if an error occurs
    • processSetForegroundColorExt

      protected void processSetForegroundColorExt(int paletteIndex) throws IOException
      process SGR 38 corresponding to extended set text color (foreground) with a palette of 255 colors.
      Parameters:
      paletteIndex - the text color in the palette
      Throws:
      IOException - if an error occurs
    • processSetForegroundColorExt

      protected void processSetForegroundColorExt(int r, int g, int b) throws IOException
      process SGR 38 corresponding to extended set text color (foreground) with a 24 bits RGB definition of the color.
      Parameters:
      r - red
      g - green
      b - blue
      Throws:
      IOException - if an error occurs
    • processSetBackgroundColor

      protected void processSetBackgroundColor(int color) throws IOException
      process SGR 40-47 corresponding to Set background color.
      Parameters:
      color - the background color
      Throws:
      IOException - if an error occurs
    • processSetBackgroundColor

      protected void processSetBackgroundColor(int color, boolean bright) throws IOException
      process SGR 40-47 or SGR 100-107 corresponding to Set background color either in normal mode or high intensity.
      Parameters:
      color - the background color
      bright - is high intensity?
      Throws:
      IOException - if an error occurs
    • processSetBackgroundColorExt

      protected void processSetBackgroundColorExt(int paletteIndex) throws IOException
      process SGR 48 corresponding to extended set background color with a palette of 255 colors.
      Parameters:
      paletteIndex - the background color in the palette
      Throws:
      IOException - if an error occurs
    • processSetBackgroundColorExt

      protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException
      process SGR 48 corresponding to extended set background color with a 24 bits RGB definition of the color.
      Parameters:
      r - red
      g - green
      b - blue
      Throws:
      IOException - if an error occurs
    • processDefaultTextColor

      protected void processDefaultTextColor() throws IOException
      process SGR 39 corresponding to Default text color (foreground)
      Throws:
      IOException - if an error occurs
    • processDefaultBackgroundColor

      protected void processDefaultBackgroundColor() throws IOException
      process SGR 49 corresponding to Default background color
      Throws:
      IOException - if an error occurs
    • processAttributeRest

      protected void processAttributeRest() throws IOException
      process SGR 0 corresponding to Reset / Normal
      Throws:
      IOException - if an error occurs
    • processCursorTo

      protected void processCursorTo(int row, int col) throws IOException
      process CSI n ; m H corresponding to CUP ? Cursor Position or CSI n ; m f corresponding to HVP ? Horizontal and Vertical Position
      Parameters:
      row - the row
      col - the column
      Throws:
      IOException - if an error occurs
    • processCursorToColumn

      protected void processCursorToColumn(int x) throws IOException
      process CSI n G corresponding to CHA ? Cursor Horizontal Absolute
      Parameters:
      x - the column
      Throws:
      IOException - if an error occurs
    • processCursorUpLine

      protected void processCursorUpLine(int count) throws IOException
      process CSI n F corresponding to CPL ? Cursor Previous Line
      Parameters:
      count - line count
      Throws:
      IOException - if an error occurs
    • processCursorDownLine

      protected void processCursorDownLine(int count) throws IOException
      process CSI n E corresponding to CNL ? Cursor Next Line
      Parameters:
      count - line count
      Throws:
      IOException - if an error occurs
    • processCursorLeft

      protected void processCursorLeft(int count) throws IOException
      process CSI n D corresponding to CUB ? Cursor Back
      Parameters:
      count - the count
      Throws:
      IOException - if an error occurs
    • processCursorRight

      protected void processCursorRight(int count) throws IOException
      process CSI n C corresponding to CUF ? Cursor Forward
      Parameters:
      count - the count
      Throws:
      IOException - if an error occurs
    • processCursorDown

      protected void processCursorDown(int count) throws IOException
      process CSI n B corresponding to CUD ? Cursor Down
      Parameters:
      count - the count
      Throws:
      IOException - if an error occurs
    • processCursorUp

      protected void processCursorUp(int count) throws IOException
      process CSI n A corresponding to CUU ? Cursor Up
      Parameters:
      count - the count
      Throws:
      IOException - if an error occurs
    • processUnknownExtension

      protected void processUnknownExtension(ArrayList<Object> options, int command)
    • processChangeIconNameAndWindowTitle

      protected void processChangeIconNameAndWindowTitle(String label)
      process OSC 0;text BEL corresponding to Change Window and Icon label
      Parameters:
      label - the label
    • processChangeIconName

      protected void processChangeIconName(String name)
      process OSC 1;text BEL corresponding to Change Icon label
      Parameters:
      name - the icon name
    • processChangeWindowTitle

      protected void processChangeWindowTitle(String title)
      process OSC 2;text BEL corresponding to Change Window title
      Parameters:
      title - the title
    • processUnknownOperatingSystemCommand

      protected void processUnknownOperatingSystemCommand(int command, String param)
      Process unknown OSC command.
      Parameters:
      command - the command
      param - the param
    • processCharsetSelect

      protected void processCharsetSelect(int set, char seq)
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • write

      public void write(String str, int off, int len) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterWriter
      Throws:
      IOException