📄 console.java
字号:
public void removeChar() { try { //if (typingLocation != userLimit) // outputDocument.remove(--typingLocation, 1); int pos = textArea.getCaretPosition(); if (pos <= typingLocation && pos > userLimit) { outputDocument.remove(pos - 1, 1); typingLocation--; } } catch (BadLocationException ble) { } } /** * Delete a char from command line. * Stands for DELETE action. */ public void deleteChar() { try { int pos = textArea.getCaretPosition(); if (pos == outputDocument.getLength()) return; if (pos < typingLocation && pos >= userLimit) { outputDocument.remove(pos, 1); typingLocation--; } } catch (BadLocationException ble) { } } /** * Adds a <code>String</code> to the current command line. * @param add <code>String</code> to be added */ public void add(String add) { try { int pos = textArea.getCaretPosition(); if (pos <= typingLocation && pos >= userLimit) outputDocument.insertString(pos, add, null); typingLocation += add.length(); } catch (BadLocationException ble) { } } /** * Returns the position in characters at which * user is allowed to type his commands. * @return Beginning of user typing space */ public int getUserLimit() { return userLimit; } /** * Returns the position of the end of the console prompt. */ public int getTypingLocation() { return typingLocation; } //TODO: this method, in case of ambiguity, should print completions as bash. //And should also expand the ~(not by calling constructPath, which is normally used //to do this: this expansion should go, probably, inside parseCommand(). /** * Completes current filename if possible. */ public void doCompletion() { int index = 0; int caret = textArea.getCaretPosition() - userLimit; String wholeText = getText(); String text; String finalCompletion; if (Jext.getBooleanProperty("console.jythonMode") && !wholeText.startsWith("!")) return; 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 == 0) return; index = i + 1; break; } } String current = text.substring(index); String path = ""; int separatorIdx = current.lastIndexOf(File.separatorChar); if (separatorIdx != -1) { path = current.substring(0, separatorIdx + 1); //the slash is inside path. current = current.substring(separatorIdx + 1); } String[] files = Utilities.getWildCardMatches(path, current + "*", true); if (files == null || files.length == 0) { if (current.equals("..")) finalCompletion = current + File.separator; else return; } else if (files.length != 1) { int length = 0; //maximum length of a completion int mIndex = 0; //index of the longest completion(if more longest ones, //we take the index of the last one). for (int i = 0; i < files.length; i++) { int _length = files[i].length(); length = length < _length ? _length : length; if (length == _length) mIndex = i; } char c; int diffIndex = length; //source[0:diffIndex] is the completion common to //everything. Note that if there are, i.e., "file" and "fileLonger", the //completion used to be fileLonger. Now we want to get, instead, file; but //modify below where MARKed to get old behaviour. String compare; String source = files[mIndex];out: for (int i = 0; i < length; i++) { c = source.charAt(i); for (int j = 0; j < files.length; j++) { if (j == mIndex) continue; compare = files[j]; //MARK /*if (i >= compare.length()) continue;*/ //if (compare.charAt(i) != c) if (i >= compare.length() || compare.charAt(i) != c) { diffIndex = i; break out; } } } finalCompletion = source.substring(0, diffIndex); } else { finalCompletion = files[0]; File f = new File(path + finalCompletion); if (!f.isAbsolute()) f = new File(Utilities.getUserDirectory(), path + finalCompletion); if (f.isDirectory()) finalCompletion += File.separator; } String textToInsert = text.substring(0, index) + path + finalCompletion; setText(textToInsert + wholeText.substring(caret)); textArea.setCaretPosition(userLimit + textToInsert.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; } /** * Displays console help. */ public void help() { Command _current = firstCmd; StringBuffer buf = new StringBuffer(); while (_current != null) { buf.append(" - ").append(_current.getCommandName()); buf.append(Utilities.createWhiteSpace(30 - _current.getCommandName().length())).append('('); buf.append(_current.getCommandSummary()).append(')').append('\n'); _current = _current.next; } buf.append('\n'); help(Jext.getProperty("console.help", new String[] { buf.toString() })); } /** * Display a message using information color. * @since Jext3.2pre4 * @param display <code>String</code> to be displayed */ public void info(String display) { //append('\n' + display, infoColor, false, false); append(display + '\n', infoColor, false, false); } /** * 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); append(display + '\n', 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); append(display + '\n', errorColor, false, false); } /** * 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); append(display + '\n', outputColor, false, false); } /** * Stops current task. */ public void stop() { if (cProcess != null) { cProcess.stop(); cProcess = null; } } /** * 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(); 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 'f': // current opened file (absolute path) file = parent.getTextArea().getCurrentFile(); if (file != null) buf.append(file); break; case 'd': // user directory buf.append(Utilities.getUserDirectory()); break; case 'p': // current opened file name buf.append(parent.getTextArea().getName()); break; case 'e': // current opened file name without extension file = parent.getTextArea().getName(); int index = file.lastIndexOf('.'); if (index != -1 && index + 1 < file.length()) buf.append(file.substring(0, index)); else buf.append(file); break; case 'n': // current opened file directory file = parent.getTextArea().getCurrentFile(); if (file != null) buf.append(file.substring(0, file.lastIndexOf(File.separator))); break; case 'h': // home dir buf.append(Utilities.getHomeDirectory()); break; case 'j': // jext dir buf.append(Jext.getHomeDirectory()); break; case 's': // selected text buf.append(parent.getTextArea().getSelectedText()); break; case '$': buf.append('$'); break; } } break; default: buf.append(c); } } return buf.toString(); } /** * 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) { if (command == null) return; stop(); info(""); //userLimit = typingLocation;//FIXME: this is part of saved positions refactoring? //However, it's needed to avoid that pressing Enter while a command executes //makes Jext read the same command with getText() and execute it again. //But I've put it out, since with it, when I press Up, the command typed //remains there, since that line make Jext think it's part of the prompt. // check to see if in "jython" mode... boolean isJython = Jext.getBooleanProperty("console.jythonMode"); // if in jython mode, look for '!' as first charater...means // treat it like a "normal console" command if (isJython) { if (!command.startsWith("!")) { if (command.startsWith("?")) { // kludge shorthand for 'print' String ts = command.substring(1); command = "print " + ts; } else if (command.startsWith("exit")) { Jext.setProperty("console.jythonMode", "off"); displayPrompt(); return; } //evalCom.handleCommand(this, "eval:" + command); if (parser == null) { parser = new InteractiveInterpreter(); Run.startupPythonInterpreter(parser); } Run.setupPythonInterpreter(parser, parent, this); if (pythonBuf.length() > 0) pythonBuf.append("\n"); pythonBuf.append(command); if (!parser.runsource(pythonBuf.toString())) //FIXME: use return value to display //secondary prompt. pythonBuf.setLength(0); displayPrompt(); return; } else { //the command starts with !, so normal processing command = command.substring(1); } } command = command.trim(); command = parseCommand(command); if (command == null || command.length() == 0 || builtInCommand(command)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -