org.fusesource.jansi.io.AnsiOutputStream Maven / Gradle / Ivy
Show all versions of log4j-core Show documentation
/*
* Copyright (C) 2009-2020 the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fusesource.jansi.io;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import org.fusesource.jansi.AnsiColors;
import org.fusesource.jansi.AnsiMode;
import org.fusesource.jansi.AnsiType;
/**
* A ANSI print stream extracts ANSI escape codes written to
* an output stream and calls corresponding AnsiProcessor.process*
methods.
* This particular class is not synchronized for improved performances.
*
* For more information about ANSI escape codes, see
* Wikipedia article
*
* @author Guillaume Nodet
* @since 1.0
* @see AnsiProcessor
*/
public class AnsiOutputStream extends FilterOutputStream {
public static final byte[] RESET_CODE = "\033[0m".getBytes();
public interface IoRunnable {
void run() throws IOException;
}
public interface WidthSupplier {
int getTerminalWidth();
}
public static class ZeroWidthSupplier implements WidthSupplier {
@Override
public int getTerminalWidth() {
return 0;
}
}
private static final int LOOKING_FOR_FIRST_ESC_CHAR = 0;
private static final int LOOKING_FOR_SECOND_ESC_CHAR = 1;
private static final int LOOKING_FOR_NEXT_ARG = 2;
private static final int LOOKING_FOR_STR_ARG_END = 3;
private static final int LOOKING_FOR_INT_ARG_END = 4;
private static final int LOOKING_FOR_OSC_COMMAND = 5;
private static final int LOOKING_FOR_OSC_COMMAND_END = 6;
private static final int LOOKING_FOR_OSC_PARAM = 7;
private static final int LOOKING_FOR_ST = 8;
private static final int LOOKING_FOR_CHARSET = 9;
private static final int FIRST_ESC_CHAR = 27;
private static final int SECOND_ESC_CHAR = '[';
private static final int SECOND_OSC_CHAR = ']';
private static final int BEL = 7;
private static final int SECOND_ST_CHAR = '\\';
private static final int SECOND_CHARSET0_CHAR = '(';
private static final int SECOND_CHARSET1_CHAR = ')';
private AnsiProcessor ap;
private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100;
private final byte[] buffer = new byte[MAX_ESCAPE_SEQUENCE_LENGTH];
private int pos = 0;
private int startOfValue;
private final ArrayList