-
Notifications
You must be signed in to change notification settings - Fork 244
/
Screen.java
559 lines (507 loc) · 20.1 KB
/
Screen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* This file is part of lanterna (http://code.google.com/p/lanterna/).
*
* lanterna is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2010-2012 Martin
*/
package com.googlecode.lanterna.screen;
import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.terminal.Terminal;
import com.googlecode.lanterna.terminal.TerminalPosition;
import com.googlecode.lanterna.terminal.TerminalSize;
import java.util.*;
/**
* A layer to put on top of a Terminal object, giving you a kind of screen buffer
* to use, which is a lot easier to work with. Drawing text or graphics to the
* terminal is kind of like writing to a bitmap.
* @author Martin
*/
public class Screen
{
private final Object mutex;
private final Terminal terminal;
private final LinkedList<TerminalSize> resizeQueue;
private TerminalPosition cursorPosition;
private TerminalSize terminalSize;
private ScreenCharacter [][] visibleScreen;
private ScreenCharacter [][] backbuffer;
private ScreenCharacter paddingCharacter;
private boolean wholeScreenInvalid;
private boolean hasBeenActivated;
//How to deal with \t characters
private TabBehaviour tabBehaviour;
/**
* Creates a new Screen on top of a supplied terminal, will query the terminal
* for its size. The screen is initially blank.
* @param terminal
* @throws LanternaException
*/
public Screen(Terminal terminal)
{
this(terminal, terminal.queryTerminalSize());
}
/**
* Creates a new Screen on top of a supplied terminal and will set the size
* of the screen to a supplied value. The screen is initially blank.
* @param terminal
* @param terminalSize
*/
public Screen(Terminal terminal, TerminalSize terminalSize)
{
this(terminal, terminalSize.getColumns(), terminalSize.getRows());
}
/**
* Creates a new Screen on top of a supplied terminal and will set the size
* of the screen to a supplied value. The screen is initially blank.
* @param terminal
* @param terminalWidth Width (number of columns) of the terminal
* @param terminalHeight Height (number of rows) of the terminal
*/
public Screen(Terminal terminal, int terminalWidth, int terminalHeight)
{
this.mutex = new Object();
this.terminal = terminal;
this.terminalSize = new TerminalSize(terminalWidth, terminalHeight);
this.visibleScreen = new ScreenCharacter[terminalHeight][terminalWidth];
this.backbuffer = new ScreenCharacter[terminalHeight][terminalWidth];
this.paddingCharacter = new ScreenCharacter('X', Terminal.Color.GREEN, Terminal.Color.BLACK);
this.resizeQueue = new LinkedList<TerminalSize>();
this.wholeScreenInvalid = false;
this.hasBeenActivated = false;
this.cursorPosition = new TerminalPosition(0, 0);
this.tabBehaviour = TabBehaviour.ALIGN_TO_COLUMN_8;
this.terminal.addResizeListener(new TerminalResizeListener());
//Initialize the screen
clear();
}
/**
* @return The terminal which is the backend for this screen
*/
public Terminal getTerminal() {
return terminal;
}
/**
* @return Position where the cursor will be located after the screen has
* been refreshed or {@code null} if the cursor is not visible
*/
public TerminalPosition getCursorPosition()
{
return cursorPosition;
}
/**
* Moves the current cursor position or hides it. If the cursor is hidden and given a new
* position, it will be visible after this method call.
* @param position 0-indexed column and row numbers of the new position, or if {@code null},
* hides the cursor
*/
public void setCursorPosition(TerminalPosition position)
{
if(position != null)
//TerminalPosition isn't immutable, so make a copy
this.cursorPosition = new TerminalPosition(position);
else
this.cursorPosition = null;
}
/**
* Moves the current cursor position, and if the cursor was hidden it will be visible after this
* call
* @param column 0-indexed column number of the new position
* @param row 0-indexed row number of the new position
*/
public void setCursorPosition(int column, int row)
{
synchronized(mutex) {
if(column >= 0 && column < terminalSize.getColumns() &&
row >= 0 && row < terminalSize.getRows()) {
setCursorPosition(new TerminalPosition(column, row));
}
}
}
/**
* Sets the behaviour for what to do about tab characters.
* @see TabBehaviour
*/
public void setTabBehaviour(TabBehaviour tabBehaviour) {
if(tabBehaviour != null)
this.tabBehaviour = tabBehaviour;
}
public void setPaddingCharacter(
char character,
Terminal.Color foregroundColor,
Terminal.Color backgroundColor,
ScreenCharacterStyle... style) {
this.paddingCharacter = new ScreenCharacter(character, foregroundColor, backgroundColor, new HashSet<ScreenCharacterStyle>(Arrays.asList(style)));
}
/**
* Gets the behaviour for what to do about tab characters.
* @see TabBehaviour
*/
public TabBehaviour getTabBehaviour() {
return tabBehaviour;
}
/**
* Reads the next {@code Key} from the input queue, or returns null if there
* is nothing on the queue.
*/
public Key readInput()
{
return terminal.readInput();
}
/**
* @return Size of the screen
*/
public TerminalSize getTerminalSize()
{
synchronized(mutex) {
return terminalSize;
}
}
/**
* Calling this method will put the underlying terminal in private mode,
* clear the screen, move the cursor and refresh.
* @throws LanternaException
*/
public void startScreen()
{
if(hasBeenActivated)
return;
hasBeenActivated = true;
terminal.enterPrivateMode();
terminal.getTerminalSize();
synchronized(mutex) {
resizeScreenIfNeeded();
}
terminal.clearScreen();
clear();
if(cursorPosition != null) {
terminal.setCursorVisible(true);
terminal.moveCursor(cursorPosition.getColumn(), cursorPosition.getRow());
}
else
terminal.setCursorVisible(false);
refresh();
}
/**
* Calling this method will make the underlying terminal leave private mode,
* effectively going back to whatever state the terminal was in before
* calling {@code startScreen()}
* @throws LanternaException
*/
public void stopScreen()
{
if(!hasBeenActivated)
return;
while(readInput() != null) {
//Drain the input queue before exiting private mode and closing the Screen.
}
terminal.exitPrivateMode();
hasBeenActivated = false;
}
/**
* Erases all the characters on the screen, effectively giving you a blank
* area. The default background color will be used, if you want to fill the
* screen with a different color you will need to do this manually.
*/
public void clear() {
//ScreenCharacter is immutable, so we can use it for every element
ScreenCharacter background = new ScreenCharacter(' ');
synchronized(mutex) {
for(int y = 0; y < terminalSize.getRows(); y++) {
for(int x = 0; x < terminalSize.getColumns(); x++) {
backbuffer[y][x] = background;
}
}
}
}
/**
* Draws a string on the screen at a particular position
* @param x 0-indexed column number of where to put the first character in the string
* @param y 0-indexed row number of where to put the first character in the string
* @param string Text to put on the screen
* @param foregroundColor What color to use for the text
* @param backgroundColor What color to use for the background
* @param styles Additional styles to apply to the text
*/
public void putString(int x, int y, String string, Terminal.Color foregroundColor,
Terminal.Color backgroundColor, ScreenCharacterStyle... styles)
{
Set<ScreenCharacterStyle> drawStyle = EnumSet.noneOf(ScreenCharacterStyle.class);
drawStyle.addAll(Arrays.asList(styles));
putString(x, y, string, foregroundColor, backgroundColor, drawStyle);
}
/**
* Draws a string on the screen at a particular position
* @param x 0-indexed column number of where to put the first character in the string
* @param y 0-indexed row number of where to put the first character in the string
* @param string Text to put on the screen
* @param foregroundColor What color to use for the text
* @param backgroundColor What color to use for the background
* @param styles Additional styles to apply to the text
*/
public void putString(int x, int y, String string, Terminal.Color foregroundColor,
Terminal.Color backgroundColor, Set<ScreenCharacterStyle> styles)
{
string = tabBehaviour.replaceTabs(string, x);
for(int i = 0; i < string.length(); i++)
putCharacter(x + i, y,
new ScreenCharacter(string.charAt(i),
foregroundColor,
backgroundColor,
styles));
}
void putCharacter(int x, int y, ScreenCharacter character)
{
synchronized(mutex) {
if(y < 0 || y >= backbuffer.length || x < 0 || x >= backbuffer[0].length)
return;
//Only create a new character if the
if(!backbuffer[y][x].equals(character))
backbuffer[y][x] = new ScreenCharacter(character);
}
}
/**
* This method will check if there are any resize commands pending. If true,
* you need to call refresh() to perform the screen resize
* @return true if the size is the same as before, false if the screen should be resized
*/
public boolean resizePending()
{
synchronized(resizeQueue) {
return !resizeQueue.isEmpty();
}
}
/**
* Calling this method will check if the terminal has changed since and in that case update the dimensions of this
* Screen to match.
* @return Will return true if dimensions were changed, otherwise false. You probably want to clear and
* redraw the entire screen if this method returns true.
*/
public boolean updateScreenSize() {
if(!resizePending()) {
return false;
}
synchronized(mutex) {
resizeScreenIfNeeded();
}
return true;
}
/**
* Clears the terminal and repaints with the whole content of the Screen. This is useful of
* something has written to the terminal outside of the Screen (System.out or through direct
* calls to the underlying Terminal) and you want to make sure that the content of Screen
* is completely pushed to the terminal.
*/
public void completeRefresh() {
wholeScreenInvalid = true;
refresh();
}
/**
* Call this method to make changes done through {@code putCharacter(...)},
* {@code putString(...)} visible on the terminal. The screen will calculate
* the changes that are required and send the necessary characters and
* control sequences to make it so.
*/
public void refresh()
{
if(!hasBeenActivated)
return;
synchronized(mutex) {
//If any resize operations are in the queue, execute them
resizeScreenIfNeeded();
Map<TerminalPosition, ScreenCharacter> updateMap = new TreeMap<TerminalPosition, ScreenCharacter>(new ScreenPointComparator());
for(int y = 0; y < terminalSize.getRows(); y++)
{
for(int x = 0; x < terminalSize.getColumns(); x++)
{
ScreenCharacter c = backbuffer[y][x];
if(!c.equals(visibleScreen[y][x]) || wholeScreenInvalid) {
visibleScreen[y][x] = c; //Remember, ScreenCharacter is immutable, we don't need to worry about it being modified
updateMap.put(new TerminalPosition(x, y), c);
}
}
}
Writer terminalWriter = new Writer();
terminalWriter.reset();
TerminalPosition previousPoint = null;
for(TerminalPosition nextUpdate: updateMap.keySet()) {
if(previousPoint == null || previousPoint.getRow() != nextUpdate.getRow() ||
previousPoint.getColumn() + 1 != nextUpdate.getColumn()) {
terminalWriter.setCursorPosition(nextUpdate.getColumn(), nextUpdate.getRow());
}
terminalWriter.writeCharacter(updateMap.get(nextUpdate));
previousPoint = nextUpdate;
}
if(cursorPosition != null) {
terminalWriter.setCursorVisible(true);
terminalWriter.setCursorPosition(cursorPosition.getColumn(), cursorPosition.getRow());
}
else {
terminalWriter.setCursorVisible(false);
}
wholeScreenInvalid = false;
}
terminal.flush();
}
//WARNING!!! Should only be called in a block synchronized on mutex! See refresh()
private void resizeScreenIfNeeded() {
TerminalSize newSize;
synchronized(resizeQueue) {
if(resizeQueue.isEmpty())
return;
newSize = resizeQueue.getLast();
resizeQueue.clear();
}
int height = newSize.getRows();
int width = newSize.getColumns();
ScreenCharacter [][]newBackBuffer = new ScreenCharacter[height][width];
ScreenCharacter [][]newVisibleScreen = new ScreenCharacter[height][width];
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
if(backbuffer.length > 0 && x < backbuffer[0].length && y < backbuffer.length)
newBackBuffer[y][x] = backbuffer[y][x];
else
newBackBuffer[y][x] = new ScreenCharacter(paddingCharacter);
if(visibleScreen.length > 0 && x < visibleScreen[0].length && y < visibleScreen.length)
newVisibleScreen[y][x] = visibleScreen[y][x];
else
newVisibleScreen[y][x] = new ScreenCharacter(paddingCharacter);
}
}
backbuffer = newBackBuffer;
visibleScreen = newVisibleScreen;
wholeScreenInvalid = true;
terminalSize = new TerminalSize(newSize);
}
private static class ScreenPointComparator implements Comparator<TerminalPosition>
{
public int compare(TerminalPosition o1, TerminalPosition o2)
{
if(o1.getRow() == o2.getRow())
if(o1.getColumn() == o2.getColumn())
return 0;
else
return new Integer(o1.getColumn()).compareTo(o2.getColumn());
else
return new Integer(o1.getRow()).compareTo(o2.getRow());
}
}
private class TerminalResizeListener implements Terminal.ResizeListener
{
@Override
public void onResized(TerminalSize newSize)
{
synchronized(resizeQueue) {
if(!terminalSize.equals(newSize)) {
resizeQueue.add(newSize);
}
}
}
}
private class Writer
{
private Terminal.Color currentForegroundColor;
private Terminal.Color currentBackgroundColor;
private boolean currentlyIsBold;
private boolean currentlyIsUnderline;
private boolean currentlyIsNegative;
private boolean currentlyIsBlinking;
public Writer()
{
currentForegroundColor = Terminal.Color.DEFAULT;
currentBackgroundColor = Terminal.Color.DEFAULT;
currentlyIsBold = false;
currentlyIsUnderline = false;
currentlyIsNegative = false;
currentlyIsBlinking = false;
}
void setCursorPosition(int x, int y)
{
terminal.moveCursor(x, y);
}
private void setCursorVisible(boolean visible) {
terminal.setCursorVisible(visible);
}
void writeCharacter(ScreenCharacter character)
{
if (currentlyIsBlinking != character.isBlinking()) {
if (character.isBlinking()) {
terminal.applySGR(Terminal.SGR.ENTER_BLINK);
currentlyIsBlinking = true;
}
else {
terminal.applySGR(Terminal.SGR.RESET_ALL);
terminal.applyBackgroundColor(character.getBackgroundColor());
terminal.applyForegroundColor(character.getForegroundColor());
// emulating "stop_blink_mode" so that previous formatting is preserved
currentlyIsBold = false;
currentlyIsUnderline = false;
currentlyIsNegative = false;
currentlyIsBlinking = false;
}
}
if(currentForegroundColor != character.getForegroundColor()) {
terminal.applyForegroundColor(character.getForegroundColor());
currentForegroundColor = character.getForegroundColor();
}
if(currentBackgroundColor != character.getBackgroundColor()) {
terminal.applyBackgroundColor(character.getBackgroundColor());
currentBackgroundColor = character.getBackgroundColor();
}
if(currentlyIsBold != character.isBold()) {
if(character.isBold()) {
terminal.applySGR(Terminal.SGR.ENTER_BOLD);
currentlyIsBold = true;
}
else {
terminal.applySGR(Terminal.SGR.EXIT_BOLD);
currentlyIsBold = false;
}
}
if(currentlyIsUnderline != character.isUnderline()) {
if(character.isUnderline()) {
terminal.applySGR(Terminal.SGR.ENTER_UNDERLINE);
currentlyIsUnderline = true;
}
else {
terminal.applySGR(Terminal.SGR.EXIT_UNDERLINE);
currentlyIsUnderline = false;
}
}
if(currentlyIsNegative != character.isNegative()) {
if(character.isNegative()) {
terminal.applySGR(Terminal.SGR.ENTER_REVERSE);
currentlyIsNegative = true;
}
else {
terminal.applySGR(Terminal.SGR.EXIT_REVERSE);
currentlyIsNegative = false;
}
}
terminal.putCharacter(character.getCharacter());
}
void reset()
{
terminal.applySGR(Terminal.SGR.RESET_ALL);
terminal.moveCursor(0, 0);
currentBackgroundColor = Terminal.Color.DEFAULT;
currentForegroundColor = Terminal.Color.DEFAULT;
currentlyIsBold = false;
currentlyIsNegative = false;
currentlyIsUnderline = false;
}
}
}