| 1 |
/* |
| 2 |
* Copyright (c) 2002-2016, the original author or authors. |
| 3 |
* |
| 4 |
* This software is distributable under the BSD license. See the terms of the |
| 5 |
* BSD license in the documentation provided with this software. |
| 6 |
* |
| 7 |
* http://www.opensource.org/licenses/bsd-license.php |
| 8 |
*/ |
| 9 |
package org.jline.terminal.impl.jna.win; |
| 10 |
|
| 11 |
import java.io.IOException; |
| 12 |
import java.io.OutputStream; |
| 13 |
|
| 14 |
import com.sun.jna.Pointer; |
| 15 |
import com.sun.jna.ptr.IntByReference; |
| 16 |
|
| 17 |
import static org.jline.terminal.impl.jna.win.Kernel32.BACKGROUND_BLUE; |
| 18 |
import static org.jline.terminal.impl.jna.win.Kernel32.BACKGROUND_GREEN; |
| 19 |
import static org.jline.terminal.impl.jna.win.Kernel32.BACKGROUND_INTENSITY; |
| 20 |
import static org.jline.terminal.impl.jna.win.Kernel32.BACKGROUND_RED; |
| 21 |
import static org.jline.terminal.impl.jna.win.Kernel32.FOREGROUND_BLUE; |
| 22 |
import static org.jline.terminal.impl.jna.win.Kernel32.FOREGROUND_GREEN; |
| 23 |
import static org.jline.terminal.impl.jna.win.Kernel32.FOREGROUND_INTENSITY; |
| 24 |
import static org.jline.terminal.impl.jna.win.Kernel32.FOREGROUND_RED; |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* A Windows ANSI escape processor, uses JNA to access native platform |
| 29 |
* API's to change the console attributes. |
| 30 |
* |
| 31 |
* @since 1.0 |
| 32 |
* @author <a href="http://hiramchirino.com">Hiram Chirino</a> |
| 33 |
* @author Joris Kuipers |
| 34 |
*/ |
| 35 |
public final class WindowsAnsiOutputStream extends AnsiOutputStream { |
| 36 |
|
| 37 |
private static final short FOREGROUND_BLACK = 0; |
| 38 |
private static final short FOREGROUND_YELLOW = (short) (FOREGROUND_RED|FOREGROUND_GREEN); |
| 39 |
private static final short FOREGROUND_MAGENTA = (short) (FOREGROUND_BLUE|FOREGROUND_RED); |
| 40 |
private static final short FOREGROUND_CYAN = (short) (FOREGROUND_BLUE|FOREGROUND_GREEN); |
| 41 |
private static final short FOREGROUND_WHITE = (short) (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); |
| 42 |
|
| 43 |
private static final short BACKGROUND_BLACK = 0; |
| 44 |
private static final short BACKGROUND_YELLOW = (short) (BACKGROUND_RED|BACKGROUND_GREEN); |
| 45 |
private static final short BACKGROUND_MAGENTA = (short) (BACKGROUND_BLUE|BACKGROUND_RED); |
| 46 |
private static final short BACKGROUND_CYAN = (short) (BACKGROUND_BLUE|BACKGROUND_GREEN); |
| 47 |
private static final short BACKGROUND_WHITE = (short) (BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE); |
| 48 |
|
| 49 |
private static final short ANSI_FOREGROUND_COLOR_MAP[] = { |
| 50 |
FOREGROUND_BLACK, |
| 51 |
FOREGROUND_RED, |
| 52 |
FOREGROUND_GREEN, |
| 53 |
FOREGROUND_YELLOW, |
| 54 |
FOREGROUND_BLUE, |
| 55 |
FOREGROUND_MAGENTA, |
| 56 |
FOREGROUND_CYAN, |
| 57 |
FOREGROUND_WHITE, |
| 58 |
}; |
| 59 |
|
| 60 |
private static final short ANSI_BACKGROUND_COLOR_MAP[] = { |
| 61 |
BACKGROUND_BLACK, |
| 62 |
BACKGROUND_RED, |
| 63 |
BACKGROUND_GREEN, |
| 64 |
BACKGROUND_YELLOW, |
| 65 |
BACKGROUND_BLUE, |
| 66 |
BACKGROUND_MAGENTA, |
| 67 |
BACKGROUND_CYAN, |
| 68 |
BACKGROUND_WHITE, |
| 69 |
}; |
| 70 |
|
| 71 |
private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100; |
| 72 |
|
| 73 |
private final Pointer console; |
| 74 |
|
| 75 |
private final Kernel32.CONSOLE_SCREEN_BUFFER_INFO info = new Kernel32.CONSOLE_SCREEN_BUFFER_INFO(); |
| 76 |
private final short originalColors; |
| 77 |
|
| 78 |
private boolean negative; |
| 79 |
private short savedX = -1; |
| 80 |
private short savedY = -1; |
| 81 |
|
| 82 |
public WindowsAnsiOutputStream(OutputStream os, Pointer console) throws IOException { |
| 83 |
super(os); |
| 84 |
this.console = console; |
| 85 |
getConsoleInfo(); |
| 86 |
originalColors = info.wAttributes; |
| 87 |
} |
| 88 |
|
| 89 |
private void getConsoleInfo() throws IOException { |
| 90 |
out.flush(); |
| 91 |
Kernel32.INSTANCE.GetConsoleScreenBufferInfo(console, info); |
| 92 |
if( negative ) { |
| 93 |
info.wAttributes = invertAttributeColors(info.wAttributes); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private void applyAttribute() throws IOException { |
| 98 |
out.flush(); |
| 99 |
short attributes = info.wAttributes; |
| 100 |
if( negative ) { |
| 101 |
attributes = invertAttributeColors(attributes); |
| 102 |
} |
| 103 |
Kernel32.INSTANCE.SetConsoleTextAttribute(console, attributes); |
| 104 |
} |
| 105 |
|
| 106 |
private short invertAttributeColors(short attributes) { |
| 107 |
// Swap the the Foreground and Background bits. |
| 108 |
int fg = 0x000F & attributes; |
| 109 |
fg <<= 4; |
| 110 |
int bg = 0X00F0 & attributes; |
| 111 |
bg >>= 4; |
| 112 |
attributes = (short) ((attributes & 0xFF00) | fg | bg); |
| 113 |
return attributes; |
| 114 |
} |
| 115 |
|
| 116 |
private void applyCursorPosition() throws IOException { |
| 117 |
Kernel32.INSTANCE.SetConsoleCursorPosition(console, info.dwCursorPosition); |
| 118 |
} |
| 119 |
|
| 120 |
protected void processDefaultTextColor() throws IOException { |
| 121 |
info.wAttributes = (short)((info.wAttributes & ~0x000F ) | (originalColors & 0x000F)); |
| 122 |
applyAttribute(); |
| 123 |
} |
| 124 |
|
| 125 |
protected void processDefaultBackgroundColor() throws IOException { |
| 126 |
info.wAttributes = (short)((info.wAttributes & ~0x00F0 ) | (originalColors & 0x00F0)); |
| 127 |
applyAttribute(); |
| 128 |
} |
| 129 |
|
| 130 |
protected void processEraseScreen(int eraseOption) throws IOException { |
| 131 |
getConsoleInfo(); |
| 132 |
IntByReference written = new IntByReference(); |
| 133 |
switch(eraseOption) { |
| 134 |
case ERASE_SCREEN: |
| 135 |
Kernel32.COORD topLeft = new Kernel32.COORD(); |
| 136 |
topLeft.X = 0; |
| 137 |
topLeft.Y = info.srWindow.Top; |
| 138 |
int screenLength = info.srWindow.height() * info.dwSize.X; |
| 139 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', screenLength, topLeft, written); |
| 140 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, screenLength, topLeft, written); |
| 141 |
break; |
| 142 |
case ERASE_SCREEN_TO_BEGINING: |
| 143 |
Kernel32.COORD topLeft2 = new Kernel32.COORD(); |
| 144 |
topLeft2.X = 0; |
| 145 |
topLeft2.Y = info.srWindow.Top; |
| 146 |
int lengthToCursor = (info.dwCursorPosition.Y - info.srWindow.Top) * info.dwSize.X + info.dwCursorPosition.X; |
| 147 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', lengthToCursor, topLeft2, written); |
| 148 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, lengthToCursor, topLeft2, written); |
| 149 |
break; |
| 150 |
case ERASE_SCREEN_TO_END: |
| 151 |
int lengthToEnd = (info.srWindow.Bottom - info.dwCursorPosition.Y) * info.dwSize.X + |
| 152 |
(info.dwSize.X - info.dwCursorPosition.X); |
| 153 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', lengthToEnd, info.dwCursorPosition, written); |
| 154 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, lengthToEnd, info.dwCursorPosition, written); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
protected void processEraseLine(int eraseOption) throws IOException { |
| 159 |
getConsoleInfo(); |
| 160 |
IntByReference written = new IntByReference(); |
| 161 |
switch(eraseOption) { |
| 162 |
case ERASE_LINE: |
| 163 |
Kernel32.COORD leftColCurrRow = new Kernel32.COORD((short) 0, info.dwCursorPosition.Y); |
| 164 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', info.dwSize.X, leftColCurrRow, written); |
| 165 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, info.dwSize.X, leftColCurrRow, written); |
| 166 |
break; |
| 167 |
case ERASE_LINE_TO_BEGINING: |
| 168 |
Kernel32.COORD leftColCurrRow2 = new Kernel32.COORD((short) 0, info.dwCursorPosition.Y); |
| 169 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', info.dwCursorPosition.X, leftColCurrRow2, written); |
| 170 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, info.dwCursorPosition.X, leftColCurrRow2, written); |
| 171 |
break; |
| 172 |
case ERASE_LINE_TO_END: |
| 173 |
int lengthToLastCol = info.dwSize.X - info.dwCursorPosition.X; |
| 174 |
Kernel32.INSTANCE.FillConsoleOutputCharacter(console, ' ', lengthToLastCol, info.dwCursorPosition, written); |
| 175 |
Kernel32.INSTANCE.FillConsoleOutputAttribute(console, info.wAttributes, lengthToLastCol, info.dwCursorPosition, written); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
protected void processCursorUpLine(int count) throws IOException { |
| 180 |
getConsoleInfo(); |
| 181 |
info.dwCursorPosition.X = 0; |
| 182 |
info.dwCursorPosition.Y = (short) Math.max(info.srWindow.Top, info.dwCursorPosition.Y-count); |
| 183 |
applyCursorPosition(); |
| 184 |
} |
| 185 |
|
| 186 |
protected void processCursorDownLine(int count) throws IOException { |
| 187 |
getConsoleInfo(); |
| 188 |
info.dwCursorPosition.X = 0; |
| 189 |
info.dwCursorPosition.Y = (short) Math.min(info.dwSize.Y, info.dwCursorPosition.Y+count); |
| 190 |
applyCursorPosition(); |
| 191 |
} |
| 192 |
|
| 193 |
protected void processCursorLeft(int count) throws IOException { |
| 194 |
getConsoleInfo(); |
| 195 |
info.dwCursorPosition.X = (short) Math.max(0, info.dwCursorPosition.X-count); |
| 196 |
applyCursorPosition(); |
| 197 |
} |
| 198 |
|
| 199 |
protected void processCursorRight(int count) throws IOException { |
| 200 |
getConsoleInfo(); |
| 201 |
info.dwCursorPosition.X = (short)Math.min(info.srWindow.width(), info.dwCursorPosition.X+count); |
| 202 |
applyCursorPosition(); |
| 203 |
} |
| 204 |
|
| 205 |
protected void processCursorDown(int count) throws IOException { |
| 206 |
getConsoleInfo(); |
| 207 |
info.dwCursorPosition.Y = (short) Math.min(info.dwSize.Y, info.dwCursorPosition.Y+count); |
| 208 |
applyCursorPosition(); |
| 209 |
} |
| 210 |
|
| 211 |
protected void processCursorUp(int count) throws IOException { |
| 212 |
getConsoleInfo(); |
| 213 |
info.dwCursorPosition.Y = (short) Math.max(info.srWindow.Top, info.dwCursorPosition.Y-count); |
| 214 |
applyCursorPosition(); |
| 215 |
} |
| 216 |
|
| 217 |
protected void processCursorTo(int row, int col) throws IOException { |
| 218 |
getConsoleInfo(); |
| 219 |
info.dwCursorPosition.Y = (short) Math.max(info.srWindow.Top, Math.min(info.dwSize.Y, info.srWindow.Top+row-1)); |
| 220 |
info.dwCursorPosition.X = (short) Math.max(0, Math.min(info.srWindow.width(), col-1)); |
| 221 |
applyCursorPosition(); |
| 222 |
} |
| 223 |
|
| 224 |
protected void processCursorToColumn(int x) throws IOException { |
| 225 |
getConsoleInfo(); |
| 226 |
info.dwCursorPosition.X = (short) Math.max(0, Math.min(info.srWindow.width(), x-1)); |
| 227 |
applyCursorPosition(); |
| 228 |
} |
| 229 |
|
| 230 |
protected void processSetForegroundColor(int color, boolean bright) throws IOException { |
| 231 |
info.wAttributes = (short)((info.wAttributes & ~0x0007 ) | ANSI_FOREGROUND_COLOR_MAP[color]); |
| 232 |
info.wAttributes = (short) ((info.wAttributes & ~FOREGROUND_INTENSITY) | (bright ? FOREGROUND_INTENSITY : 0)); |
| 233 |
applyAttribute(); |
| 234 |
} |
| 235 |
|
| 236 |
protected void processSetBackgroundColor(int color, boolean bright) throws IOException { |
| 237 |
info.wAttributes = (short)((info.wAttributes & ~0x0070 ) | ANSI_BACKGROUND_COLOR_MAP[color]); |
| 238 |
info.wAttributes = (short) ((info.wAttributes & ~BACKGROUND_INTENSITY) | (bright ? BACKGROUND_INTENSITY : 0)); |
| 239 |
applyAttribute(); |
| 240 |
} |
| 241 |
|
| 242 |
protected void processAttributeRest() throws IOException { |
| 243 |
info.wAttributes = (short)((info.wAttributes & ~0x00FF ) | originalColors); |
| 244 |
this.negative = false; |
| 245 |
applyAttribute(); |
| 246 |
} |
| 247 |
|
| 248 |
protected void processSetAttribute(int attribute) throws IOException { |
| 249 |
switch(attribute) { |
| 250 |
case ATTRIBUTE_INTENSITY_BOLD: |
| 251 |
info.wAttributes = (short)(info.wAttributes | FOREGROUND_INTENSITY ); |
| 252 |
applyAttribute(); |
| 253 |
break; |
| 254 |
case ATTRIBUTE_INTENSITY_NORMAL: |
| 255 |
info.wAttributes = (short)(info.wAttributes & ~FOREGROUND_INTENSITY ); |
| 256 |
applyAttribute(); |
| 257 |
break; |
| 258 |
|
| 259 |
// Yeah, setting the background intensity is not underlining.. but it's best we can do |
| 260 |
// using the Windows console API |
| 261 |
case ATTRIBUTE_UNDERLINE: |
| 262 |
info.wAttributes = (short)(info.wAttributes | BACKGROUND_INTENSITY ); |
| 263 |
applyAttribute(); |
| 264 |
break; |
| 265 |
case ATTRIBUTE_UNDERLINE_OFF: |
| 266 |
info.wAttributes = (short)(info.wAttributes & ~BACKGROUND_INTENSITY ); |
| 267 |
applyAttribute(); |
| 268 |
break; |
| 269 |
|
| 270 |
case ATTRIBUTE_NEGATIVE_ON: |
| 271 |
negative = true; |
| 272 |
applyAttribute(); |
| 273 |
break; |
| 274 |
case ATTRIBUTE_NEGATIVE_OFF: |
| 275 |
negative = false; |
| 276 |
applyAttribute(); |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
protected void processSaveCursorPosition() throws IOException { |
| 282 |
getConsoleInfo(); |
| 283 |
savedX = info.dwCursorPosition.X; |
| 284 |
savedY = info.dwCursorPosition.Y; |
| 285 |
} |
| 286 |
|
| 287 |
protected void processRestoreCursorPosition() throws IOException { |
| 288 |
// restore only if there was a save operation first |
| 289 |
if (savedX != -1 && savedY != -1) { |
| 290 |
out.flush(); |
| 291 |
info.dwCursorPosition.X = savedX; |
| 292 |
info.dwCursorPosition.Y = savedY; |
| 293 |
applyCursorPosition(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
protected void processInsertLine(int optionInt) throws IOException { |
| 299 |
getConsoleInfo(); |
| 300 |
Kernel32.SMALL_RECT scroll = new Kernel32.SMALL_RECT(info.srWindow); |
| 301 |
scroll.Top = info.dwCursorPosition.Y; |
| 302 |
Kernel32.COORD org = new Kernel32.COORD(); |
| 303 |
org.X = 0; |
| 304 |
org.Y = (short)(info.dwCursorPosition.Y + optionInt); |
| 305 |
Kernel32.CHAR_INFO info = new Kernel32.CHAR_INFO(' ', originalColors); |
| 306 |
Kernel32.INSTANCE.ScrollConsoleScreenBuffer(console, scroll, scroll, org, info); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
protected void processDeleteLine(int optionInt) throws IOException { |
| 311 |
getConsoleInfo(); |
| 312 |
Kernel32.SMALL_RECT scroll = new Kernel32.SMALL_RECT(info.srWindow); |
| 313 |
scroll.Top = info.dwCursorPosition.Y; |
| 314 |
Kernel32.COORD org = new Kernel32.COORD(); |
| 315 |
org.X = 0; |
| 316 |
org.Y = (short)(info.dwCursorPosition.Y - optionInt); |
| 317 |
Kernel32.CHAR_INFO info = new Kernel32.CHAR_INFO(' ', originalColors); |
| 318 |
Kernel32.INSTANCE.ScrollConsoleScreenBuffer(console, scroll, scroll, org, info); |
| 319 |
} |
| 320 |
|
| 321 |
protected void processChangeWindowTitle(String label) { |
| 322 |
Kernel32.INSTANCE.SetConsoleTitle(label); |
| 323 |
} |
| 324 |
} |