Class AnsiWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class AnsiWriter
    extends java.io.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
    • Constructor Detail

      • AnsiWriter

        public AnsiWriter​(java.io.Writer out)
    • Method Detail

      • write

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

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

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

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

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

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

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

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

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

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

        protected void processSetForegroundColor​(int color,
                                                 boolean bright)
                                          throws java.io.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:
        java.io.IOException - if an error occurs
      • processSetForegroundColorExt

        protected void processSetForegroundColorExt​(int paletteIndex)
                                             throws java.io.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:
        java.io.IOException - if an error occurs
      • processSetForegroundColorExt

        protected void processSetForegroundColorExt​(int r,
                                                    int g,
                                                    int b)
                                             throws java.io.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:
        java.io.IOException - if an error occurs
      • processSetBackgroundColor

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

        protected void processSetBackgroundColor​(int color,
                                                 boolean bright)
                                          throws java.io.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:
        java.io.IOException - if an error occurs
      • processSetBackgroundColorExt

        protected void processSetBackgroundColorExt​(int paletteIndex)
                                             throws java.io.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:
        java.io.IOException - if an error occurs
      • processSetBackgroundColorExt

        protected void processSetBackgroundColorExt​(int r,
                                                    int g,
                                                    int b)
                                             throws java.io.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:
        java.io.IOException - if an error occurs
      • processDefaultTextColor

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

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

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

        protected void processCursorTo​(int row,
                                       int col)
                                throws java.io.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:
        java.io.IOException - if an error occurs
      • processCursorToColumn

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

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

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

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

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

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

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

        protected void processUnknownExtension​(java.util.ArrayList<java.lang.Object> options,
                                               int command)
      • processChangeIconNameAndWindowTitle

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

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

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

        protected void processUnknownOperatingSystemCommand​(int command,
                                                            java.lang.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 java.io.IOException
        Overrides:
        write in class java.io.FilterWriter
        Throws:
        java.io.IOException
      • write

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

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