I've attached a patch to the commons-jelly-tags-interaction jar. This
patch makes it so the interaction task will try to use jline:
http://jline.sourceforge.net/

Jline makes it so a java console will have tab completion, and
history, and other goodies.

This is great, because the maven-console plugin uses the 
commons-jelly-tags-interaction jar. So if you update the
commons-jelly-tags-interaction jar, and then tell the maven console
plugin to use the new jar, then your maven console will have history,
and tab completion.

I've set it up to remember all of the commands typed in any console,
further it uses that history as the tab completion source - so you can
tab complete past commands.

I've tested this in windows and it works great, but in windows with
cygwin, it doesn't do the fancy completion, but still works.

By the way, in windows, jline's lib doesn't support arrows for
history, so use CONTROL+P and CONTROL+N.

Its possible that there might be a better way to integrate jline into
this lib, i've just done what looked like the quickest way to get it
working so my maven console would have history and tab completion.
Maybe this feature could be enabled with a tag attribute?

THANKS

Ryan
Index: project.xml
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/interaction/project.xml,v
retrieving revision 1.10
diff -u -r1.10 project.xml
--- project.xml 11 Sep 2004 22:21:15 -0000      1.10
+++ project.xml 8 Dec 2004 21:18:21 -0000
@@ -20,7 +20,7 @@
   <extend>${basedir}/../tag-project.xml</extend>
   <id>commons-jelly-tags-interaction</id>
   <name>commons-jelly-tags-interaction</name>
-  <currentVersion>1.0</currentVersion>
+  <currentVersion>1.1</currentVersion>
   <package>org.apache.commons.jelly.tags.interaction</package>
   <description>This is a Jelly interface to the user.</description>
   <shortDescription>Commons Jelly Interaction Tag Library</shortDescription>
@@ -33,6 +33,12 @@
   </versions>
 
   <dependencies>
+    <dependency>
+      <groupId>jline</groupId>
+      <artifactId>jline</artifactId>
+      <version>0.8.1</version>
+      <type>jar</type>
+    </dependency>  
     <dependency>
       <id>commons-jelly</id>
       <version>1.0-beta-4</version>
Index: src/java/org/apache/commons/jelly/tags/interaction/AskTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java,v
retrieving revision 1.4
diff -u -r1.4 AskTag.java
--- src/java/org/apache/commons/jelly/tags/interaction/AskTag.java      9 Sep 
2004 12:10:22 -0000       1.4
+++ src/java/org/apache/commons/jelly/tags/interaction/AskTag.java      8 Dec 
2004 21:18:21 -0000
@@ -15,27 +15,36 @@
  */
 package org.apache.commons.jelly.tags.interaction;
 
-import java.io.InputStreamReader;
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
+
+import jline.ConsoleReader;
+import jline.History;
+import jline.SimpleCompletor;
 
 import org.apache.commons.jelly.TagSupport;
 import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
- * Jelly Tag that asks the user a question, and puts his answer into
- * a variable, with the attribute "answer".
- * This variable may be reused further as any other Jelly variable.
- * @author <a href="mailto:[EMAIL PROTECTED]">St�phane Mor</a>
+ * Jelly Tag that asks the user a question, and puts his answer into a 
variable,
+ * with the attribute "answer". This variable may be reused further as any 
other
+ * Jelly variable.
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">St�phane Mor </a>
  */
-public class AskTag extends TagSupport
-{
+public class AskTag extends TagSupport {
+
+    private static Log logger = LogFactory.getLog(AskTag.class);
+
     /** The question to ask to the user */
     private String question;
 
     /**
-     * The variable in which we will stock the user's input.
-     * This defaults to "interact.answer".
+     * The variable in which we will stock the user's input. This defaults to
+     * "interact.answer".
      */
     private String answer = "interact.answer";
 
@@ -48,60 +57,62 @@
     /** The prompt to display before the user input */
     private String prompt = ">";
 
+    private static History consoleHistory = new History();
+
     /**
-     * Sets the question to ask to the user. If a "default" attribute
-     * is present, it will appear inside [].
-     * @param question The question to ask to the user
+     * Sets the question to ask to the user. If a "default" attribute is
+     * present, it will appear inside [].
+     * 
+     * @param question
+     *            The question to ask to the user
      */
-    public void setQuestion(String question)
-    {
+    public void setQuestion(String question) {
         this.question = question;
     }
 
     /**
-     * Sets the name of the variable that will hold the answer
-     * This defaults to "interact.answer".
-     * @param answer the name of the variable that will hold the answer
+     * Sets the name of the variable that will hold the answer This defaults to
+     * "interact.answer".
+     * 
+     * @param answer
+     *            the name of the variable that will hold the answer
      */
-    public void setAnswer(String answer)
-    {
+    public void setAnswer(String answer) {
         this.answer = answer;
     }
 
     /**
-     * Sets the default answer to the question.
-     * If it is present, it will appear inside [].
-     * @param default the default answer to the question
+     * Sets the default answer to the question. If it is present, it will 
appear
+     * inside [].
+     * 
+     * @param default
+     *            the default answer to the question
      */
-    public void setDefault(String defaultInput)
-    {
+    public void setDefault(String defaultInput) {
         this.defaultInput = defaultInput;
     }
 
     /**
      * Sets the prompt that will be displayed before the user's input.
-     * @param promt the prompt that will be displayed before the user's input.
+     * 
+     * @param promt
+     *            the prompt that will be displayed before the user's input.
      */
-    public void setPrompt(String prompt)
-    {
+    public void setPrompt(String prompt) {
         this.prompt = prompt;
     }
 
-
     /**
      * Perform functionality provided by the tag
-     * @param output the place to write output
-     */
-    public void doTag(XMLOutput output)
-    {
-        if (question != null)
-        {
-            if (defaultInput != null)
-            {
+     * 
+     * @param output
+     *            the place to write output
+     */
+    public void doTag(XMLOutput output) {
+        if (question != null) {
+            if (defaultInput != null) {
                 System.out.println(question + " [" + defaultInput + "]");
-            }
-            else
-            {
+            } else {
                 System.out.println(question);
             }
             // The prompt should be just before the user's input,
@@ -109,16 +120,54 @@
             //System.out.print(prompt + " ");
         }
 
-        BufferedReader br = new BufferedReader(new 
InputStreamReader(System.in));
+        ConsoleReader consoleReader;
 
         try {
-            input = br.readLine();
-            if (defaultInput != null && input.trim().equals(""))
-            {
-                input = defaultInput;
+            consoleReader = new ConsoleReader();
+        } catch (IOException e) {
+            logger.warn("couldnt create console reader", e);
+            consoleReader = null;
+        }
+
+        String disableJlineProp = System.getProperty("ask.jline.disable");
+        boolean disableJline = (disableJlineProp != null && disableJlineProp
+                .equals("true"));
+
+        try {
+            if (consoleReader != null
+                    && consoleReader.getTerminal().isSupported()) {
+
+                // resue the static history, so our commands are remeberered
+                consoleReader.setHistory(consoleHistory);
+
+                // hate the bell!
+                consoleReader.setBellEnabled(false);
+
+                // add old commands as tab completion history
+                String[] oldCommands = new String[consoleHistory
+                        .getHistoryList().size()];
+                consoleHistory.getHistoryList().toArray(oldCommands);
+                consoleReader.addCompletor(new SimpleCompletor(oldCommands));
+
+                // read the input!
+                input = consoleReader.readLine();
+                
+                // trim the input for tab completion
+                input = input.trim();
+
+                if (defaultInput != null && input.trim().equals("")) {
+                    input = defaultInput;
+                }
+            } else {
+                BufferedReader reader = new BufferedReader(
+                        new InputStreamReader(System.in));
+                input = reader.readLine();
             }
-        } catch (IOException ioe) {
+
+        } catch (IOException ex) {
+            logger.warn("error setting up the console reader", ex);
         }
+
         context.setVariable(answer, input);
     }
-}
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to