/** * Returns an ansi output stream handler. We return whatever was * passed if we determine we cannot handle ansi based on Kernel32 calls. * * @return an @{link AltWindowAnsiOutputStream} instance or the passed * stream. */ private static OutputStream wrapOutputStream(final OutputStream stream) { if (Configuration.isWindows()) { // On windows we know the console does not interpret ANSI codes.. try { return new WindowsAnsiOutputStream(stream); } catch (Throwable ignore) { // this happens when JNA is not in the path.. or // this happens when the stdout is being redirected to a file. } // Use the ANSIOutputStream to strip out the ANSI escape sequences. return new AnsiOutputStream(stream); } return stream; }
@Override protected void processSetBackgroundColor(int color, boolean bright) throws IOException { info.attributes = (short) ((info.attributes & ~0x0070) | ANSI_BACKGROUND_COLOR_MAP[color]); if (bright) { info.attributes |= BACKGROUND_INTENSITY; } applyAttribute(); }
@Override protected void processCursorUp(int count) throws IOException { getConsoleInfo(); info.cursorPosition.y = (short) Math.max(info.window.top, info.cursorPosition.y - count); applyCursorPosition(); }
public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java super(os); // expected diff with WindowsAnsiPrintStream.java getConsoleInfo(); originalColors = info.attributes; }
private void getConsoleInfo() throws IOException { out.flush(); // expected diff with WindowsAnsiPrintStream.java if (GetConsoleScreenBufferInfo(console, info) == 0) { throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage()); } if (negative) { info.attributes = invertAttributeColors(info.attributes); } }
@Override protected void processRestoreCursorPosition() throws IOException { // restore only if there was a save operation first if (savedX != -1 && savedY != -1) { out.flush(); // expected diff with WindowsAnsiPrintStream.java info.cursorPosition.x = savedX; info.cursorPosition.y = savedY; applyCursorPosition(); } }
@Override protected void processSaveCursorPosition() throws IOException { getConsoleInfo(); savedX = info.cursorPosition.x; savedY = info.cursorPosition.y; }
private void applyAttribute() throws IOException { out.flush(); // expected diff with WindowsAnsiPrintStream.java short attributes = info.attributes; if (negative) { attributes = invertAttributeColors(attributes); } if (SetConsoleTextAttribute(console, attributes) == 0) { throw new IOException(WindowsSupport.getLastErrorMessage()); } }
public boolean checkAnsiSupported(OutputStream out, String osName) throws Throwable { if (out != System.out) { return false; } if (osName.startsWith("windows") && osName.endsWith("10")) { new WindowsAnsiOutputStream(out); } else if (osName.startsWith("windows") && !osName.endsWith("10")) { return false; } if (System.console() == null) { return false; } return true; }
@Override protected void processCursorLeft(int count) throws IOException { getConsoleInfo(); info.cursorPosition.x = (short) Math.max(0, info.cursorPosition.x - count); applyCursorPosition(); }
@Override protected void processAttributeRest() throws IOException { info.attributes = (short) ((info.attributes & ~0x00FF) | originalColors); this.negative = false; applyAttribute(); }
@Override protected void processDeleteLine(int optionInt) throws IOException { getConsoleInfo(); SMALL_RECT scroll = info.window.copy(); scroll.top = info.cursorPosition.y; COORD org = new COORD(); org.x = 0; org.y = (short)(info.cursorPosition.y - optionInt); CHAR_INFO info = new CHAR_INFO(); info.attributes = originalColors; info.unicodeChar = ' '; if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { throw new IOException(WindowsSupport.getLastErrorMessage()); } }
public boolean checkAnsiSupported(OutputStream out, String osName) throws Throwable { if (out != System.out) { return false; } if (osName.startsWith("windows") && osName.endsWith("10")) { new WindowsAnsiOutputStream(out); } else if (osName.startsWith("windows") && !osName.endsWith("10")) { return false; } if (System.console() == null) { return false; } return true; }
@Override protected void processCursorDown(int count) throws IOException { getConsoleInfo(); info.cursorPosition.y = (short) Math.min(Math.max(0, info.size.y - 1), info.cursorPosition.y + count); applyCursorPosition(); }
@Override protected void processSetForegroundColor(int color, boolean bright) throws IOException { info.attributes = (short) ((info.attributes & ~0x0007) | ANSI_FOREGROUND_COLOR_MAP[color]); if (bright) { info.attributes |= FOREGROUND_INTENSITY; } applyAttribute(); }
@Override protected void processInsertLine(int optionInt) throws IOException { getConsoleInfo(); SMALL_RECT scroll = info.window.copy(); scroll.top = info.cursorPosition.y; COORD org = new COORD(); org.x = 0; org.y = (short)(info.cursorPosition.y + optionInt); CHAR_INFO info = new CHAR_INFO(); info.attributes = originalColors; info.unicodeChar = ' '; if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { throw new IOException(WindowsSupport.getLastErrorMessage()); } }
public boolean checkAnsiSupported(OutputStream out, String osName) throws Throwable { if (out != System.out) { return false; } if (osName.startsWith("windows") && osName.endsWith("10")) { new WindowsAnsiOutputStream(out); } else if (osName.startsWith("windows") && !osName.endsWith("10")) { return false; } if (System.console() == null) { return false; } return true; }
@Override protected void processCursorRight(int count) throws IOException { getConsoleInfo(); info.cursorPosition.x = (short) Math.min(info.window.width(), info.cursorPosition.x + count); applyCursorPosition(); }
@Override protected void processDefaultTextColor() throws IOException { info.attributes = (short) ((info.attributes & ~0x000F) | (originalColors & 0xF)); info.attributes = (short) (info.attributes & ~FOREGROUND_INTENSITY); applyAttribute(); }
@Override protected void processEraseLine(int eraseOption) throws IOException { getConsoleInfo(); int[] written = new int[1]; switch (eraseOption) { case ERASE_LINE: COORD leftColCurrRow = info.cursorPosition.copy(); leftColCurrRow.x = 0; FillConsoleOutputAttribute(console, originalColors, info.size.x, leftColCurrRow, written); FillConsoleOutputCharacterW(console, ' ', info.size.x, leftColCurrRow, written); break; case ERASE_LINE_TO_BEGINING: COORD leftColCurrRow2 = info.cursorPosition.copy(); leftColCurrRow2.x = 0; FillConsoleOutputAttribute(console, originalColors, info.cursorPosition.x, leftColCurrRow2, written); FillConsoleOutputCharacterW(console, ' ', info.cursorPosition.x, leftColCurrRow2, written); break; case ERASE_LINE_TO_END: int lengthToLastCol = info.size.x - info.cursorPosition.x; FillConsoleOutputAttribute(console, originalColors, lengthToLastCol, info.cursorPosition.copy(), written); FillConsoleOutputCharacterW(console, ' ', lengthToLastCol, info.cursorPosition.copy(), written); break; default: break; } }