📄 console.java
字号:
public int getTypingLocation() { return typingLocation; } /** Completes current filename if possible. */ public void doCompletion() { int index = 0; int caret = textArea.getCaretPosition() - userLimit; String wholeText = getText(); String text; try { text = outputDocument.getText(userLimit, caret); } catch (BadLocationException ble) { return; } for (int i = text.length() - 1; i >= 0; i--) { if (COMPLETION_SEPARATORS.indexOf(text.charAt(i)) != -1) { if (i == index) return; index = i + 1; break; } } String current = text.substring(index); java.util.ArrayList matching = new java.util.ArrayList(); String[] files = ConsoleUtilities.getWildCardMatches("*", true); if (files == null) return; for (int i = 0; i < files.length; i++) { if (files[i].startsWith(current)) matching.add(files[i]); } if (matching.size() == 0) return; int length = 0; int _length = 0; int mIndex = 0; for (int i = 0; i < matching.size(); i++) { _length = ((String) matching.get(i)).length(); length = length < _length ? _length : length; if (length == _length) mIndex = i; } char c; boolean isSame = true; int diffIndex = length; String compare; String source = (String) matching.get(mIndex); for (int i = 0; i < length; i++) { c = source.charAt(i); for (int j = 0; j < matching.size(); j++) { if (j == mIndex) continue; compare = (String) matching.get(j); if (i >= compare.length()) continue; isSame = (compare.charAt(i) == c); } if (!isSame) { diffIndex = i; break; } } compare = text.substring(0, index) + source.substring(0, diffIndex); setText(compare + wholeText.substring(caret)); textArea.setCaretPosition(userLimit + compare.length()); } /** Search backward in the history for a matching command, * according to the command typed in the user typing space. */ public void doBackwardSearch() { String text = getText(); if (text == null) { historyPrevious(); return; } for(int i = index + 1; i < historyModel.getSize(); i++) { String item = historyModel.getItem(i); if (item.startsWith(text)) { setText(item); index = i; return; } } } /** Get previous item in the history list. */ public void historyPrevious() { if (index == historyModel.getSize() - 1) getToolkit().beep(); else if (index == -1) { current = getText(); setText(historyModel.getItem(0)); index = 0; } else { int newIndex = index + 1; setText(historyModel.getItem(newIndex)); index = newIndex; } } /** Get next item in the history list. */ public void historyNext() { if (index == -1) getToolkit().beep(); else if (index == 0) setText(current); else { int newIndex = index - 1; setText(historyModel.getItem(newIndex)); index = newIndex; } } /** Set user's command line content. * * @param text Text to be put on command line. */ public void setText(String text) { try { outputDocument.remove(userLimit, typingLocation - userLimit); outputDocument.insertString(userLimit, text, null); typingLocation = outputDocument.getLength(); index = -1; } catch (BadLocationException ble) {} } /** Returns current command line. */ public String getText() { try { return outputDocument.getText(userLimit, typingLocation - userLimit); } catch (BadLocationException ble) {} return null; } /** Display a message using output color. * * @param display <code>String</code> to be displayed */ public void output(String display) { append('\n' + display, outputColor, false, false); } /** Displays console help. */ public void help() { Command _current = firstCmd; StringBuffer buf = new StringBuffer(); while (_current != null) { buf.append(" - ").append(_current.getCommandName()); buf.append(ConsoleUtilities.createWhiteSpace(30 - _current.getCommandName().length())).append('('); buf.append(_current.getCommandSummary()).append(')').append('\n'); _current = _current.next; } help(SystemProperties.getProperty("console","console.help", new String[] { buf.toString() })); } /** Display a message using help color. * * @param display <code>String</code> to be displayed */ public void help(String display) { append('\n' + display, infoColor, true, true); } /** Display a message using error color. * * @param display <code>String</code> to be displayed */ public void error(String display) { append('\n' + display, errorColor, false, false); } /** Stops current task. */ public void stop() { if (stdout != null) { stdout.interrupt(); stdout = null; //stdout.stop(); } if (stderr != null) { stderr.interrupt(); stderr = null; //stderr.stop(); } if (process != null) { process.destroy(); Object[] args = { processName }; error(SystemProperties.getProperty("console", "console.killed", args)); } } /** Parse a command. Replace internal variables by their * values. * * @param command Command to be parsed */ public String parseCommand(String command) { String file; StringBuffer buf = new StringBuffer(); String userDir = System.getProperty("user.dir"); String userHome = System.getProperty("user.home"); for (int i = 0; i < command.length(); i++) { char c = command.charAt(i); switch(c) { case '$': if (i == command.length() - 1) buf.append(c); else { switch (command.charAt(++i)) { case 'd': // current dir buf.append(currentPath); break; case 'h': // home dir buf.append(userHome); break; case 'j': // program dir buf.append(userDir); break; case '$': buf.append('$'); break; } } break; default: buf.append(c); } } return buf.toString(); } public String getCurrentPath() { return currentPath; } public void setCurrentPath(String _currentPath) { currentPath = _currentPath; } /** Execute command. First parse it then check if command * is built-in. At last, a process is created and threads * which handle output streams are started. * * @param command Command to be execute */ public void execute(String command) { stop(); if (command.length() == 0 || command == null) return; int index = command.indexOf(' '); if (index != -1) processName = command.substring(0, index); else processName = command; command = parseCommand(command); if (command == null || command.length() == 0) return; if (builtInCommand(command)) { displayPrompt(); return; } append("\n> " + command, infoColor); try { if (osType == WINDOWS_OS) { WINDOWS_EXEC[2] = command; process = Runtime.getRuntime().exec(WINDOWS_EXEC, null, new File(currentPath)); } else { process = Runtime.getRuntime().exec(command, null, new File(currentPath)); } process.getOutputStream().close(); } catch (IOException ioe) { error(SystemProperties.getProperty("console", "console.error")); displayPrompt(); return; } stdout = new StdoutThread(); stderr = new StderrThread(); if (process == null) displayPrompt(); } class StdoutThread extends Thread { StdoutThread() { super("----thread: stout: executequery----"); start(); } public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while((line = in.readLine()) != null) { output(line); } in.close(); int exitCode = process.waitFor(); Object[] args = { processName, new Integer(exitCode) }; append('\n' + SystemProperties.getProperty("console", "console.exited", args), infoColor); Thread.sleep(500); process.destroy(); displayPrompt(); } catch(IOException io) {} catch(InterruptedException ie) {} catch (NullPointerException npe) {} } } class StderrThread extends Thread { StderrThread() { super("----thread: stderr: executequery----"); start(); } public void run() { try { if (process == null) { return; } BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line; while((line = in.readLine()) != null) { append('\n' + line, errorColor); } in.close(); } catch(IOException io) {} } } ////////////////////////////////////////////////////////////////////////////////////////////// // NEEDED BY JavaScriptParser PLUGIN ////////////////////////////////////////////////////////////////////////////////////////////// /* private Writer writerSTDOUT = new Writer() { public void close() {} public void flush() { repaint(); } public void write(char cbuf[], int off, int len) { append(new String(cbuf, off, len), outputColor); } }; private Writer writeSTDERR = new Writer() { public void close() {} public void flush() { repaint(); } public void write(char cbuf[], int off, int len) { append(new String(cbuf, off, len), errorColor); } }; */ /** Returns a writer in which external classes can send * <code>String</code> to make them being displayed in the * console as standard output. */ /* public Writer getStdOut() { return writerSTDOUT; } */ /** Returns a writer in which external classes can send * <code>String</code> to make them being displayed in the * console as error output. */ /* public Writer getStdErr() { return writeSTDERR; } */ public void cleanup() { currentCmd = null; firstCmd = null; process = null; processName = null; stdout = null; stderr = null; current = null; outputDocument = null; textArea = null; if (historyModel != null) { historyModel.cleanup(); } historyModel = null; infoColor = null; prompt = null; hostName = null; promptPattern = null; } }// End of Console.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -