Search in sources :

Example 6 with KeyStroke

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();
    }
}
Also used : KeyStroke(com.googlecode.lanterna.input.KeyStroke)

Example 7 with KeyStroke

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:
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) KeyStroke(com.googlecode.lanterna.input.KeyStroke)

Example 8 with KeyStroke

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 "";
}
Also used : KeyStroke(com.googlecode.lanterna.input.KeyStroke) TerminalPosition(com.googlecode.lanterna.TerminalPosition)

Aggregations

KeyStroke (com.googlecode.lanterna.input.KeyStroke)8 IOException (java.io.IOException)3 TerminalPosition (com.googlecode.lanterna.TerminalPosition)2 TerminalSize (com.googlecode.lanterna.TerminalSize)2 TextGraphics (com.googlecode.lanterna.graphics.TextGraphics)2 DefaultTerminalFactory (com.googlecode.lanterna.terminal.DefaultTerminalFactory)2 ArrayList (java.util.ArrayList)2 TextCharacter (com.googlecode.lanterna.TextCharacter)1 MessageDialogButton (com.googlecode.lanterna.gui2.dialogs.MessageDialogButton)1 KeyType (com.googlecode.lanterna.input.KeyType)1 Screen (com.googlecode.lanterna.screen.Screen)1 UnixTerminal (com.googlecode.lanterna.terminal.ansi.UnixTerminal)1 Stack (java.util.Stack)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1