/** Clear the screen by issuing the ANSI "clear screen" code. */ public boolean clearScreen() throws IOException { if (!terminal.isANSISupported()) { return false; } // send the ANSI code to clear the screen printString(((char) 27) + "[2J"); flushConsole(); // then send the ANSI code to go to position 1,1 printString(((char) 27) + "[1;1H"); flushConsole(); redrawLine(); return true; }
/** Issue an audible keyboard bell, if {@link #getBellEnabled} return true. */ public final void beep() throws IOException { if (!(getBellEnabled())) { return; } printCharacter(KEYBOARD_BELL); // need to flush so the console actually beeps flushConsole(); }
/** Clear ahead the specified number of characters without moving the cursor. */ private final void clearAhead(final int num) throws IOException { if (num == 0) { return; } // debug ("clearAhead: " + num); // print blank extra characters printCharacters(' ', num); // we need to flush here so a "clever" console // doesn't just ignore the redundancy of a space followed by // a backspace. flushConsole(); // reset the visual cursor back(num); flushConsole(); }
/** * Redraw the rest of the buffer from the cursor onwards. This is necessary for inserting text * into the buffer. * * @param clear the number of characters to clear after the end of the buffer */ private final void drawBuffer(final int clear) throws IOException { // debug ("drawBuffer: " + clear); char[] chars = buf.buffer.substring(buf.cursor).toCharArray(); if (mask != null) Arrays.fill(chars, mask.charValue()); printCharacters(chars); clearAhead(clear); back(chars.length); flushConsole(); }
/** * Output the specified {@link Collection} in proper columns. * * @param stuff the stuff to print */ public void printColumns(final Collection stuff) throws IOException { if ((stuff == null) || (stuff.size() == 0)) { return; } int width = getTermwidth(); int maxwidth = 0; for (Iterator i = stuff.iterator(); i.hasNext(); maxwidth = Math.max(maxwidth, i.next().toString().length())) {; } StringBuffer line = new StringBuffer(); int showLines; if (usePagination) showLines = getTermheight() - 1; // page limit else showLines = Integer.MAX_VALUE; for (Iterator i = stuff.iterator(); i.hasNext(); ) { String cur = (String) i.next(); if ((line.length() + maxwidth) > width) { printString(line.toString().trim()); printNewline(); line.setLength(0); if (--showLines == 0) { // Overflow printString(loc.getString("display-more")); flushConsole(); int c = readVirtualKey(); if (c == '\r' || c == '\n') showLines = 1; // one step forward else if (c != 'q') showLines = getTermheight() - 1; // page forward back(loc.getString("display-more").length()); if (c == 'q') break; // cancel } } pad(cur, maxwidth + 3, line); } if (line.length() > 0) { printString(line.toString().trim()); printNewline(); line.setLength(0); } }
/** Output a platform-dependant newline. */ public final void printNewline() throws IOException { printString(CR); flushConsole(); }
/** Clear the line and redraw it. */ public final void redrawLine() throws IOException { printCharacter(RESET_LINE); flushConsole(); drawLine(); }
/** * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing * newlines). * * @param prompt the prompt to issue to the console, may be null. * @return a line that is read from the terminal, or null if there was null input (e.g., * <i>CTRL-D</i> was pressed). */ public String readLine(final String prompt, final Character mask) throws IOException { this.mask = mask; if (prompt != null) this.prompt = prompt; try { terminal.beforeReadLine(this, this.prompt, mask); if ((this.prompt != null) && (this.prompt.length() > 0)) { out.write(this.prompt); out.flush(); } // if the terminal is unsupported, just use plain-java reading if (!terminal.isSupported()) { return readLine(in); } while (true) { int[] next = readBinding(); if (next == null) { return null; } int c = next[0]; int code = next[1]; if (c == -1) { return null; } boolean success = true; switch (code) { case EXIT: // ctrl-d if (buf.buffer.length() == 0) { return null; } case COMPLETE: // tab success = complete(); break; case MOVE_TO_BEG: success = setCursorPosition(0); break; case KILL_LINE: // CTRL-K success = killLine(); break; case CLEAR_SCREEN: // CTRL-L success = clearScreen(); break; case KILL_LINE_PREV: // CTRL-U success = resetLine(); break; case NEWLINE: // enter moveToEnd(); printNewline(); // output newline return finishBuffer(); case DELETE_PREV_CHAR: // backspace success = backspace(); break; case DELETE_NEXT_CHAR: // delete success = deleteCurrentCharacter(); break; case MOVE_TO_END: success = moveToEnd(); break; case PREV_CHAR: success = moveCursor(-1) != 0; break; case NEXT_CHAR: success = moveCursor(1) != 0; break; case NEXT_HISTORY: success = moveHistory(true); break; case PREV_HISTORY: success = moveHistory(false); break; case REDISPLAY: break; case PASTE: success = paste(); break; case DELETE_PREV_WORD: success = deletePreviousWord(); break; case PREV_WORD: success = previousWord(); break; case NEXT_WORD: success = nextWord(); break; case START_OF_HISTORY: success = history.moveToFirstEntry(); if (success) setBuffer(history.current()); break; case END_OF_HISTORY: success = history.moveToLastEntry(); if (success) setBuffer(history.current()); break; case CLEAR_LINE: moveInternal(-(buf.buffer.length())); killLine(); break; case INSERT: buf.setOvertyping(!buf.isOvertyping()); break; case UNKNOWN: default: if (c != 0) { // ignore null chars ActionListener action = (ActionListener) triggeredActions.get(new Character((char) c)); if (action != null) action.actionPerformed(null); else putChar(c, true); } else success = false; } if (!(success)) { beep(); } flushConsole(); } } finally { terminal.afterReadLine(this, this.prompt, mask); } }
/** Move the visual cursor backwards without modifying the buffer cursor. */ private final void back(final int num) throws IOException { printCharacters(BACKSPACE, num); flushConsole(); }