⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandshell.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						case 'G' :
							result.append("> ");
							break;
						case 'D' :
							final Date now = new Date();
							DateFormat.getDateTimeInstance().format(now, result, null);
							break;
						default :
							result.append(c);
					}
					commandMode = false;
				} else {
					switch (c) {
						case '$' :
							commandMode = true;
							break;
						default :
							result.append(c);
					}
				}
			}
		} catch (Exception ioex) {
			// This should never occur
			log.error("Error in prompt()", ioex);
		}
		return result.toString();
	}
	
	//********** KeyboardListener implementation **********//
	/**
	 * Method keyPressed
	 *
	 * @param    ke                  a  KeyboardEvent
	 *
	 * @version  2/5/2004
	 */
	public void keyPressed(KeyboardEvent ke) {
		//  make sure we are ready to intercept the keyboard
		if (!isActive)
			return;
		
		switch(ke.getKeyCode()) {
			//  intercept the up and down arrow keys
			case KeyEvent.VK_UP:
				ke.consume();
				if (historyIndex == -1) {
					newestLine = currentLine;
					historyIndex = history.size();
				}
				historyIndex--;
				redisplay();
				break;
			case KeyEvent.VK_DOWN:
				ke.consume();
				if (historyIndex == history.size() - 1)
					historyIndex = -2;
				else if (historyIndex == -1)
					newestLine = currentLine;
				historyIndex++;
				redisplay();
				break;
			case KeyEvent.VK_LEFT: //Left the cursor goes left
				ke.consume();
				if(posOnCurrentLine > 0) {
					posOnCurrentLine--;
					refreshCurrentLine();
				}
				break;
			case KeyEvent.VK_RIGHT: // Right the cursor goes right
				ke.consume();
				if(posOnCurrentLine < currentLine.length()) {
					posOnCurrentLine++;
					refreshCurrentLine();
				}
				break;
			case KeyEvent.VK_HOME: // The cursor goes at the start
				ke.consume();
				posOnCurrentLine = 0;
				refreshCurrentLine();
				break;
			case KeyEvent.VK_END: // the cursor goes at the end of line
				ke.consume();
				posOnCurrentLine = currentLine.length();
				refreshCurrentLine();
				break;
				//  if its a backspace we want to remove one from the end of our current line
			case KeyEvent.VK_BACK_SPACE:
				ke.consume();
				if (currentLine.length() != 0 && posOnCurrentLine > 0) {
					posOnCurrentLine --;
					currentLine = currentLine.substring(0, posOnCurrentLine)+ currentLine.substring(posOnCurrentLine+1, currentLine.length());
					
					refreshCurrentLine();
				}
				break;
				
				//  if its a delete we want to remove one under the cursor
			case KeyEvent.VK_DELETE:
				ke.consume();
				if(posOnCurrentLine == 0 && currentLine.length() > 0) {
					currentLine = currentLine.substring(1);
				} else if(posOnCurrentLine < currentLine.length()) {
					currentLine = currentLine.substring(0, posOnCurrentLine)+ currentLine.substring(posOnCurrentLine+1, currentLine.length());
				}
				refreshCurrentLine();
				break;
				
				//  if its an enter key we want to process the command, and then resume the thread
			case KeyEvent.VK_ENTER:
				out.print(ke.getKeyChar());
				ke.consume();
				posOnCurrentLine = 0;
				synchronized (this) {
					isActive = false;
					threadSuspended = false;
					notifyAll();
				}
				break;
				
				// if it's the tab key, we want to trigger command line completion
			case KeyEvent.VK_TAB:
				ke.consume();
				if(posOnCurrentLine != currentLine.length()) {
					String ending = currentLine.substring(posOnCurrentLine);
					currentLine = complete(currentLine.substring(0, posOnCurrentLine))+ending;
					posOnCurrentLine = currentLine.length() - ending.length();
				} else {
					currentLine = complete(currentLine);
					posOnCurrentLine = currentLine.length();
				}
				break;
				
			default:
				//  if its a useful key we want to add it to our current line
				if (!Character.isISOControl(ke.getKeyChar())) {
					ke.consume();
					if(posOnCurrentLine == currentLine.length()) {
						currentLine += ke.getKeyChar();
					} else {
						currentLine = currentLine.substring(0, posOnCurrentLine) +
							ke.getKeyChar() + currentLine.substring(posOnCurrentLine);
					}
					posOnCurrentLine++;
					refreshCurrentLine();
				}
		}
	}
	
	private void refreshCurrentLine() {
		try {
			int y = console.getCursorY();
			console.clearLine(y);
			/* Uncomment this to display cursor position
			console.clearLine(y-25);
			console.setCursor(0, y-1);
			err.print("Cursor pos : x = "+currentPrompt.length() + posOnCurrentLine+", y = "+y);
			console.setCursor(0, y);
			 */
			out.print(currentPrompt + currentLine + " ");
			console.setCursor(currentPrompt.length() + posOnCurrentLine, console.getCursorY());
		}
		catch (Exception e) {
		}
	}
	
	public void keyReleased(KeyboardEvent ke) {
		// do nothing
	}
	
	private void redisplay() {
		//  clear the line
		if (console != null) {
			console.clearLine(console.getCursorY());
		}
		//  display the prompt
		out.print(currentPrompt);
		//  display the required history/current line
		if (historyIndex == -1)
			currentLine = newestLine;
		else
			currentLine = history.getCommand(historyIndex);
		out.print(currentLine);
		posOnCurrentLine = currentLine.length();
	}
	
	// Command line completion
	
	private final Argument defaultArg = new AliasArgument("command", "the command to be called");
	private boolean dirty = false;
	private String complete(String partial) {
		// workaround to set the currentShell to this shell
		try {
			ShellUtils.getShellManager().registerShell(this);
		} catch (NameNotFoundException ex) {
		}
		
		dirty = false;
		String result = null;
		try {
			CommandLine cl = new CommandLine(partial);
			String cmd = "";
			if (cl.hasNext()) {
				cmd = cl.next();
				if (!cmd.trim().equals("") && cl.hasNext())
					try {
						// get command's help info
						Class cmdClass = getCommandClass(cmd);
						Help.Info info = Help.getInfo(cmdClass);
						
						// perform completion
						result = cmd + " " + info.complete(cl); // prepend command name and space
						// again
					} catch (ClassNotFoundException ex) {
					throw new CompletionException("Command class not found");
				}
			}
			if (result == null) // assume this is the alias to be called
				result = defaultArg.complete(cmd);
			
			if (!partial.equals(result) && !dirty) { // performed direct completion without listing
				dirty = true; // indicate we want to have a new prompt
				for (int i = 0; i < partial.length() + currentPrompt.length(); i++)
					System.out.print("\b"); // clear line (cheap approach)
			}
		} catch (CompletionException ex) {
			System.out.println(); // next line
			System.err.println(ex.getMessage()); // print the error (optional)
			// this debug output is to trace where the Exception came from
			//ex.printStackTrace(System.err);
			result = partial; // restore old value
			dirty = true; // we need a new prompt
		}
		
		if (dirty) {
			dirty = false;
			System.out.print(currentPrompt + result); // print the prompt and go on with normal
			// operation
		}
		return result;
	}
	
	public void list(String[] items) {
		System.out.println();
		for (int i = 0; i < items.length; i++)
			System.out.println(items[i]);
		dirty = true;
	}
	
	public void addCommandToHistory(String cmdLineStr) {
		//  Add this command to the history.
		if (!cmdLineStr.equals(newestLine))
			history.addCommand(cmdLineStr);
	}
	
	public PrintStream getErrorStream() {
		return err;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -