use of com.googlecode.lanterna.input.KeyStroke in project security-lib by ncsa.
the class GUIDemo method userInput2.
/**
* Use terminal like standard text mode. First try and stuff just gets tested here.
*
* @param terminal
* @param screen
* @param textGraphics
* @throws IOException
*/
protected static void userInput2(Terminal terminal, Screen screen, TextGraphics textGraphics) throws IOException {
textGraphics.putString(5, 4, "Last Keystroke: ", SGR.BOLD);
textGraphics.putString(5 + "Last Keystroke: ".length(), 4, "<Pending>");
terminal.flush();
int row = 0;
TerminalPosition cursor = terminal.getCursorPosition();
KeyStroke keyStroke = terminal.readInput();
while (keyStroke.getKeyType() != Escape) {
System.out.println("Char in = " + keyStroke.getCharacter());
textGraphics.drawLine(5, 4, terminal.getTerminalSize().getColumns() - 1, 4, ' ');
textGraphics.putString(5, 4, "Last Keystroke: ", SGR.BOLD);
textGraphics.putString(5, 4, "cursor at: " + cursor.getColumn() + ", " + cursor.getRow(), SGR.BOLD);
textGraphics.putString(5 + "Last Keystroke: ".length(), 4, keyStroke.toString());
terminal.flush();
keyStroke = terminal.readInput();
}
}
use of com.googlecode.lanterna.input.KeyStroke in project security-lib by ncsa.
the class GUIDemo method terminalInput.
/**
* Another example. Tries to read user input and put on screen. If you hit up/down arros the color changes
* Escape exits.
* This writes using the terminal, not text graphics. it uses text graphics to change colors.
* <br>Note that this cannot do things like set th terminal color (just text). It is very basic,
* but it would allow for things like command history and completion.
*
* @param terminal
* @param screen
* @param textGraphics
* @throws IOException
*/
protected static void terminalInput(Terminal terminal, Screen screen, TextGraphics textGraphics) throws IOException {
boolean keepRunning = true;
int line = 0;
StringBuffer stringBuffer = new StringBuffer();
terminal.setCursorVisible(true);
terminal.setCursorPosition(0, 0);
terminal.setForegroundColor(TextColor.ANSI.YELLOW);
// String is of form # RGB where each color is a hex number 0 - 256 aka x0 - xFF
// terminal.setForegroundColor(TextColor.RGB.Factory.fromString("#0000FF"));
terminal.enableSGR(SGR.BOLD);
List<String> commandBuffer = new ArrayList<>();
int currentBufferPosition = 0;
while (keepRunning) {
// Block input or this does not draw right at all.
KeyStroke keyStroke = terminal.readInput();
if (keyStroke != null) {
switch(keyStroke.getKeyType()) {
case MouseEvent:
System.out.println("Yo!" + keyStroke);
break;
case Escape:
componentTest(screen, textGraphics);
println(terminal, 0, line, input == null ? "(empty)" : input.getAbsolutePath());
keepRunning = false;
break;
case // If there is some issue shutting down the JVM, it starts spitting these out. Just exit.
EOF:
keepRunning = false;
break;
case Enter:
System.out.println("printing buffer = " + stringBuffer);
commandBuffer.add(0, stringBuffer.toString());
// ++ so it advances on first return.
terminal.setCursorPosition(0, ++line);
terminal.flush();
currentBufferPosition = 0;
stringBuffer = new StringBuffer();
break;
case ArrowUp:
terminal.setForegroundColor(TextColor.ANSI.MAGENTA);
println(terminal, 0, line, commandBuffer.get(currentBufferPosition));
currentBufferPosition = Math.min(currentBufferPosition + 1, commandBuffer.size() - 1);
System.out.println("Buff pos = " + currentBufferPosition);
terminal.flush();
break;
case ArrowDown:
terminal.setForegroundColor(TextColor.ANSI.YELLOW);
currentBufferPosition = Math.max(currentBufferPosition - 1, 0);
println(terminal, 0, line, commandBuffer.get(currentBufferPosition));
System.out.println("Buff pos = " + currentBufferPosition);
terminal.flush();
break;
case Character:
currentBufferPosition = 0;
if (keyStroke.isAltDown()) {
System.out.println("got alt " + keyStroke.getCharacter());
break;
}
if (keyStroke.isCtrlDown()) {
System.out.println("got crtl " + keyStroke.getCharacter());
break;
}
stringBuffer.append(keyStroke.getCharacter());
terminal.putCharacter(keyStroke.getCharacter());
terminal.flush();
break;
case ArrowLeft:
currentBufferPosition = 0;
terminal.setCursorPosition(terminal.getCursorPosition().getColumn() - 1, terminal.getCursorPosition().getRow());
terminal.flush();
break;
case ArrowRight:
// Move cursor right, don't overrun end of line.
currentBufferPosition = 0;
terminal.setCursorPosition(Math.min(stringBuffer.length() - 1, terminal.getCursorPosition().getColumn() + 1), terminal.getCursorPosition().getRow());
terminal.flush();
break;
case Backspace:
if (stringBuffer != null && 0 < stringBuffer.length()) {
stringBuffer = stringBuffer.deleteCharAt(stringBuffer.length() - 1);
terminal.setCursorPosition(terminal.getCursorPosition().getColumn() - 1, terminal.getCursorPosition().getRow());
// blank what was there
terminal.putCharacter(' ');
terminal.setCursorPosition(terminal.getCursorPosition().getColumn() - 1, terminal.getCursorPosition().getRow());
terminal.flush();
}
break;
default:
}
}
}
}
use of com.googlecode.lanterna.input.KeyStroke in project security-lib by ncsa.
the class LanternaScreenIO method readInput.
protected String readInput() throws IOException {
// terminal.setForegroundColor(defaultTextColor); // just in case
// column where we start
int startCol = getCurrentCursorPosition().getColumn();
// row where we start
int startRow = getCurrentCursorPosition().getRow();
boolean keepReading = true;
int line = 0;
StringBuilder currentLine = new StringBuilder();
// push it on the stack
commandBuffer.add(0, currentLine);
// start at current line
int currentBufferPosition = 0;
while (keepReading) {
// Block input or this does not draw right at all(!).
KeyStroke keyStroke = screen.readInput();
if (keyStroke != null) {
switch(keyStroke.getKeyType()) {
case MouseEvent:
System.out.println("Yo!" + keyStroke);
break;
case Escape:
// return stringBuffer.toString() + "\n";
case // If there is some issue shutting down the JVM, it starts spitting these out. Just exit.
EOF:
keepReading = false;
break;
case Enter:
commandBufferMaxWidth = Math.max(commandBufferMaxWidth, currentLine.length());
if (currentBufferPosition == 0) {
if (1 < commandBuffer.size() && !StringUtils.equals(currentLine.toString(), commandBuffer.get(1).toString())) {
commandBuffer.set(0, currentLine);
}
} else {
if (1 < commandBuffer.size()) {
if (StringUtils.equals(currentLine.toString(), commandBuffer.get(1).toString())) {
// don't add just take away unused buffer.
commandBuffer.remove(0);
} else {
commandBuffer.set(0, currentLine);
}
}
}
return currentLine.toString();
case ArrowUp:
if (!commandBuffer.isEmpty()) {
screen.setCursorPosition(new TerminalPosition(startCol, screen.getCursorPosition().getRow()));
// terminal.setForegroundColor(TextColor.ANSI.MAGENTA);
currentBufferPosition = Math.min(++currentBufferPosition, commandBuffer.size() - 1);
currentLine = new StringBuilder(commandBuffer.get(currentBufferPosition));
print(StringUtils.pad2(currentLine.toString(), commandBufferMaxWidth));
screen.setCursorPosition(new TerminalPosition(startCol, startRow));
flush();
}
break;
case ArrowDown:
if (!commandBuffer.isEmpty()) {
// terminal.setForegroundColor(TextColor.ANSI.MAGENTA);
screen.setCursorPosition(new TerminalPosition(startCol, terminal.getCursorPosition().getRow()));
currentBufferPosition = Math.max(--currentBufferPosition, 0);
currentLine = new StringBuilder(commandBuffer.get(currentBufferPosition));
print(StringUtils.pad2(currentLine.toString(), commandBufferMaxWidth));
screen.setCursorPosition(new TerminalPosition(startCol, startRow));
flush();
}
break;
case Character:
int currentCol0 = screen.getCursorPosition().getColumn();
int position = currentCol0 - startCol;
char character = keyStroke.getCharacter();
if (position < 0) {
position = 0;
}
if (currentLine.length() < position) {
position = currentLine.length() - 1;
}
currentLine.insert(position, character);
screen.setCursorPosition(new TerminalPosition(startCol + position, terminal.getCursorPosition().getRow()));
print(currentLine.substring(position));
screen.setCursorPosition(new TerminalPosition(startCol + position + 1, terminal.getCursorPosition().getRow()));
flush();
break;
case ArrowLeft:
screen.setCursorPosition(new TerminalPosition(Math.max(0, terminal.getCursorPosition().getColumn() - 1), terminal.getCursorPosition().getRow()));
flush();
break;
case ArrowRight:
// Move cursor right, don't overrun end of line.
screen.setCursorPosition(new TerminalPosition(Math.min(startCol + currentLine.length(), terminal.getCursorPosition().getColumn() + 1), terminal.getCursorPosition().getRow()));
flush();
break;
case Backspace:
// delete character to LEFT of cursor and redraw
int currentCol = getCurrentCursorPosition().getColumn() - startCol;
int currentRow = getCurrentCursorPosition().getRow();
if (0 < currentCol && 0 < currentLine.length()) {
currentCol = Math.max(0, currentCol - 1);
currentLine = currentLine.deleteCharAt(currentCol);
screen.setCursorPosition(new TerminalPosition(startCol + currentCol, currentRow));
// terminal.setCursorPosition(terminal.getCursorPosition().getColumn() - 1, terminal.getCursorPosition().getRow());
// terminal.flush();
// blanks out last char
print(currentLine.substring(currentCol) + " ");
screen.setCursorPosition(new TerminalPosition(startCol + currentCol, currentRow));
flush();
}
break;
case Delete:
// delete character to RIGHT of cursor and redraw
int currentCol1 = getCurrentCursorPosition().getColumn() - startCol;
int currentRow1 = getCurrentCursorPosition().getRow();
if (0 < currentCol1 && currentCol1 < currentLine.length()) {
currentCol1 = Math.min(startCol + currentLine.length() - 1, currentCol1);
currentLine = currentLine.deleteCharAt(currentCol1);
screen.setCursorPosition(new TerminalPosition(startCol + currentCol1, currentRow1));
// blanks out last char
print(currentLine.substring(currentCol1) + " ");
screen.setCursorPosition(new TerminalPosition(startCol + currentCol1, currentRow1));
flush();
}
break;
case End:
screen.setCursorPosition(new TerminalPosition(startCol + currentLine.length(), startRow));
flush();
break;
case Home:
screen.setCursorPosition(new TerminalPosition(startCol, startRow));
flush();
break;
case PageUp:
int x = terminal.getTerminalSize().getRows();
// screen.clear();
screen.scrollLines(0, x, 10);
// screen.setCursorPosition(new TerminalPosition(0, 0));
flush();
break;
case PageDown:
x = terminal.getTerminalSize().getRows();
// screen.clear();
// scroll in units of 25 lines.
screen.scrollLines(0, x, -10);
// screen.setCursorPosition(new TerminalPosition(0, 0));
flush();
break;
default:
}
}
}
return "";
}
Aggregations