From: <cr...@us...> - 2008-11-12 13:47:21
|
Revision: 4702 http://jnode.svn.sourceforge.net/jnode/?rev=4702&view=rev Author: crawley Date: 2008-11-12 13:47:18 +0000 (Wed, 12 Nov 2008) Log Message: ----------- This implements the input and shell-side parts of a "command help" keyboard action: typing ESC at a partial command line will print help for the command. Modified Paths: -------------- trunk/core/src/driver/org/jnode/driver/console/InputCompleter.java trunk/core/src/driver/org/jnode/driver/console/textscreen/ConsoleKeyEventBindings.java trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReader.java trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReaderAction.java trunk/core/src/driver/org/jnode/driver/console/textscreen/Line.java trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java trunk/shell/src/shell/org/jnode/shell/CommandLine.java trunk/shell/src/shell/org/jnode/shell/CommandShell.java trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java trunk/shell/src/shell/org/jnode/shell/RedirectingInterpreter.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java Modified: trunk/core/src/driver/org/jnode/driver/console/InputCompleter.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/InputCompleter.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/core/src/driver/org/jnode/driver/console/InputCompleter.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -20,6 +20,8 @@ */ package org.jnode.driver.console; +import java.io.PrintWriter; + /** * This interface is implemented by objects registered with a Console as * input completers. @@ -34,7 +36,16 @@ * @param partial the partial input line. * @return a CompletionInfo that contains the possible completions. */ - CompletionInfo complete(String partial); + public CompletionInfo complete(String partial); + + /** + * Show incremental syntax help for the supplied partial input line. + * + * @param partial the partial input line. + * @param out the PrintWriter that help information should be written to. + * @return <code>true</code> if any help information was written. + */ + public boolean help(String partial, PrintWriter out); /** * Gets the completer's current InputHistory object. If the completer is modal, Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/ConsoleKeyEventBindings.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/ConsoleKeyEventBindings.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/ConsoleKeyEventBindings.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -63,6 +63,8 @@ res.setVKAction(KeyEvent.VK_TAB, 0, KeyboardReaderAction.KR_COMPLETE); res.setVKAction(KeyEvent.VK_I, KeyEvent.CTRL_DOWN_MASK, KeyboardReaderAction.KR_COMPLETE); res.setCharAction('\t', KeyboardReaderAction.KR_COMPLETE); + res.setVKAction(KeyEvent.VK_ESCAPE, 0, KeyboardReaderAction.KR_HELP); + res.setVKAction(KeyEvent.VK_SLASH, KeyEvent.CTRL_DOWN_MASK, KeyboardReaderAction.KR_HELP); res.setVKAction(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK, KeyboardReaderAction.KR_SOFT_EOF); res.setCharAction('\004', KeyboardReaderAction.KR_SOFT_EOF); res.setVKAction(KeyEvent.VK_L, KeyEvent.CTRL_DOWN_MASK, KeyboardReaderAction.KR_KILL_LINE); Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReader.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReader.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReader.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -21,6 +21,7 @@ package org.jnode.driver.console.textscreen; import java.awt.event.KeyEvent; +import java.io.CharArrayWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; @@ -187,13 +188,23 @@ case KR_COMPLETE: // Perform completion if (completer != null) { - if (currentLine.complete()) { + if (currentLine.complete(completer)) { currentLine.start(true); } out.write(currentPrompt); refreshCurrentLine(); } break; + case KR_HELP: + // Request incremental help + if (completer != null) { + if (currentLine.help(completer)) { + currentLine.start(true); + } + out.write(currentPrompt); + refreshCurrentLine(); + } + break; case KR_SOFT_EOF: // Set soft EOF status and commit currentLine.moveEnd(); Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReaderAction.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReaderAction.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardReaderAction.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -33,11 +33,18 @@ KR_KILL_LINE, /** - * This action causes the input completion to be performed. + * This action causes the input completion to be performed on the current + * partial input line. */ KR_COMPLETE, /** + * This action causes incremental help to be output for the current + * partial input line. + */ + KR_HELP, + + /** * This action causes the input line to be refreshed to the * console. */ Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/Line.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/Line.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/Line.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -21,7 +21,9 @@ package org.jnode.driver.console.textscreen; +import java.io.CharArrayWriter; import java.io.IOException; +import java.io.PrintWriter; import java.util.Arrays; import java.util.SortedSet; @@ -165,9 +167,16 @@ } } - public boolean complete() throws IOException { + /** + * Perform completion on the current input, output any completion alternatives + * then rebuild line the input with the common completion appended. + * + * @param completer the object (e.g. shell) responsible for completion. + * @return <code>true</code> if we output a list of completion alternatives. + * @throws IOException + */ + public boolean complete(InputCompleter completer) throws IOException { CompletionInfo info = null; - InputCompleter completer = console.getCompleter(); String ending = posOnCurrentLine != currentLine.length() ? currentLine.substring(posOnCurrentLine) : ""; info = completer.complete(currentLine.substring(0, posOnCurrentLine)); @@ -186,6 +195,38 @@ return res; } + /** + * Get and output incremental help for the current input line. + * + * @param completer the object (e.g. shell) responsible for providing help. + * @return <code>true</code> if we output any help. + * @throws IOException + * @throws IOException + */ + public boolean help(InputCompleter completer) throws IOException { + CharArrayWriter caw = new CharArrayWriter(); + PrintWriter pw = new PrintWriter(caw); + boolean res = completer.help(currentLine.substring(0, posOnCurrentLine), pw); + if (!res) { + return false; + } + char[] chars = caw.toCharArray(); + if (chars.length == 0 || chars.length == 1 && chars[0] == '\n') { + return false; + } + + int oldPosOnCurrentLine = posOnCurrentLine; + moveEnd(); + refreshCurrentLine(); + out.write('\n'); + out.write(chars); + if (chars[chars.length - 1] != '\n') { + out.write('\n'); + } + posOnCurrentLine = oldPosOnCurrentLine; + return true; + } + protected boolean printList(CompletionInfo info) throws IOException { SortedSet<String> completions = info.getCompletions(); if (completions == null || completions.size() <= 1) { Modified: trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -21,6 +21,8 @@ package org.jnode.shell; +import java.io.PrintWriter; + /** * This is the API that a shell-based interpreter must implement. * @@ -50,7 +52,7 @@ * completed. * * @param shell the current CommandShell. - * @param partial a input to the interpreter. + * @param partial a partial command line * @return the CommandLine represent the fragment of the supplied command * input to be completed. * @throws ShellException @@ -73,4 +75,15 @@ * @return the word with any necessary escaping or quoting added. */ String escapeWord(String word); + + /** + * Get incremental help for the partial command line. + * + * @param shell the current CommandShell. + * @param partial a partial command line + * @param pw the destination for any help information + * @return <code>true</code> if useful help information was written to 'pw' + * @throws ShellException + */ + boolean help(CommandShell shell, String partial, PrintWriter pw) throws ShellException; } Modified: trunk/shell/src/shell/org/jnode/shell/CommandLine.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -661,4 +661,16 @@ completion.setCompletionStart(commandToken == null ? 0 : commandToken.start); } } + + public CommandInfo getCommandInfo(CommandShell shell) { + String cmd = (commandToken == null) ? "" : commandToken.token.trim(); + if (cmd.equals("")) { + return null; + } + try { + return shell.getCommandInfo(cmd); + } catch (ClassNotFoundException ex) { + return null; + } + } } Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -51,6 +51,7 @@ import org.jnode.driver.console.InputHistory; import org.jnode.driver.console.TextConsole; import org.jnode.driver.console.textscreen.KeyboardReader; +import org.jnode.driver.console.textscreen.KeyboardReaderAction; import org.jnode.naming.InitialNaming; import org.jnode.shell.alias.AliasManager; import org.jnode.shell.alias.NoSuchAliasException; @@ -647,15 +648,11 @@ } return result.toString(); } - - public Completable parseCommandLine(String cmdLineStr) - throws ShellException { - return interpreter.parsePartial(this, cmdLineStr); - } /** * This method is called by the console input driver to perform command line - * completion in response to a TAB character. + * completion in response to a {@link KeyboardReaderAction#KR_COMPLETE} action; + * typically a TAB character. */ public CompletionInfo complete(String partial) { if (!readingCommand) { @@ -674,7 +671,7 @@ // do command completion completion = new CommandCompletions(interpreter); try { - Completable cl = parseCommandLine(partial); + Completable cl = interpreter.parsePartial(this, partial); if (cl != null) { cl.complete(completion, this); } @@ -682,12 +679,11 @@ outPW.println(); // next line errPW.println("Cannot parse: " + ex.getMessage()); stackTrace(ex); - - } catch (CompletionException ex) { + } catch (Throwable ex) { outPW.println(); // next line errPW.println("Problem in completer: " + ex.getMessage()); stackTrace(ex); - } + } // Make sure that the shell's completion context gets nulled. CompletionInfo myCompletion = completion; @@ -695,6 +691,29 @@ return myCompletion; } + /** + * This method is responsible for generating incremental help in response + * to a @link KeyboardReaderAction#KR_HELP} action. + */ + public boolean help(String partial, PrintWriter pw) { + if (!readingCommand) { + return false; + } + try { + return interpreter.help(this, partial, pw); + } catch (ShellException ex) { + outPW.println(); // next line + errPW.println("Cannot parse: " + ex.getMessage()); + stackTrace(ex); + return false; + } catch (Throwable ex) { + outPW.println(); // next line + errPW.println("Problem in incremental help: " + ex.getMessage()); + stackTrace(ex); + return false; + } + } + public void addCommandToHistory(String cmdLineStr) { // Add this command to the command history. if (isHistoryEnabled() && !cmdLineStr.equals(lastCommandLine)) { Modified: trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -21,6 +21,7 @@ package org.jnode.shell; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.NoSuchElementException; @@ -76,31 +77,17 @@ private static final char ESCAPE_T = '\t'; private static final char T = 't'; + @Override public String getName() { return "default"; } + @Override public int interpret(CommandShell shell, String line) throws ShellException { - LinkedList<CommandLine.Token> tokens = - new LinkedList<CommandLine.Token>(); - Tokenizer tokenizer = new Tokenizer(line); - while (tokenizer.hasNext()) { - tokens.add(tokenizer.next()); - } - int nosTokens = tokens.size(); - if (nosTokens == 0) { + CommandLine cmd = doParseCommandLine(line); + if (cmd == null) { return 0; } - CommandLine cmd; - if (nosTokens == 1) { - cmd = new CommandLine(tokens.get(0), null, null); - } else { - CommandLine.Token commandToken = tokens.removeFirst(); - CommandLine.Token[] argTokens = - new CommandLine.Token[nosTokens - 1]; - cmd = new CommandLine( - commandToken, tokens.toArray(argTokens), null); - } shell.addCommandToHistory(line); try { CommandInfo cmdInfo = cmd.parseCommandLine(shell); @@ -110,10 +97,29 @@ } } + @Override public Completable parsePartial(CommandShell shell, String line) throws ShellException { + CommandLine res = doParseCommandLine(line); + return res == null ? new CommandLine("", null) : res; + } + + @Override + public boolean help(CommandShell shell, String line, PrintWriter pw) throws ShellException { + CommandLine cmd = doParseCommandLine(line); + CommandInfo cmdInfo = cmd.getCommandInfo(shell); + if (cmdInfo == null) { + return false; + } else { + // This will do for a start. + pw.println("Command class is " + cmdInfo.getCommandClass().getName()); + return true; + } + } + + private CommandLine doParseCommandLine(String line) throws ShellException { Tokenizer tokenizer = new Tokenizer(line); if (!tokenizer.hasNext()) { - return new CommandLine("", null); + return null; } CommandLine.Token commandToken = tokenizer.next(); LinkedList<CommandLine.Token> tokenList = @@ -127,7 +133,7 @@ res.setArgumentAnticipated(tokenizer.whitespaceAfterLast()); return res; } - + @Override public String escapeWord(String word) { return escapeWord(word, false); Modified: trunk/shell/src/shell/org/jnode/shell/RedirectingInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/RedirectingInterpreter.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/RedirectingInterpreter.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -27,6 +27,7 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -56,11 +57,13 @@ return "redirecting"; } }; - + + @Override public String getName() { return "redirecting"; } + @Override public int interpret(CommandShell shell, String line) throws ShellException { Tokenizer tokenizer = new Tokenizer(line, REDIRECTS_FLAG); List<CommandDescriptor> commands = new LinkedList<CommandDescriptor>(); @@ -77,6 +80,7 @@ } } + @Override public Completable parsePartial(CommandShell shell, String line) throws ShellException { Tokenizer tokenizer = new Tokenizer(line, REDIRECTS_FLAG); @@ -85,6 +89,12 @@ } @Override + public boolean help(CommandShell shell, String line, PrintWriter pw) throws ShellException { + pw.println("Don't panic!"); + return true; + } + + @Override public String escapeWord(String word) { return escapeWord(word, true); } Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2008-11-11 12:45:05 UTC (rev 4701) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2008-11-12 13:47:18 UTC (rev 4702) @@ -13,6 +13,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; +import java.io.PrintWriter; import java.util.HashMap; import org.jnode.shell.CommandInfo; @@ -120,20 +121,29 @@ public BjorneInterpreter() { } + @Override public String getName() { return "bjorne"; } + @Override public int interpret(CommandShell shell, String command) throws ShellException { return interpret(shell, command, null, false); } + @Override public Completable parsePartial(CommandShell shell, String partial) throws ShellSyntaxException { // TODO Auto-generated method stub return null; } @Override + public boolean help(CommandShell shell, String partial, PrintWriter pw) throws ShellException { + // TODO Auto-generated method stub + return false; + } + + @Override public String escapeWord(String word) { // TODO Auto-generated method stub return null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |