geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r417084 - in /geronimo/sandbox/gshell/trunk/gshell-core/src/main: java/org/apache/geronimo/gshell/ resources/org/apache/geronimo/gshell/
Date Sun, 25 Jun 2006 22:35:04 GMT
Author: jdillon
Date: Sun Jun 25 15:35:03 2006
New Revision: 417084

URL: http://svn.apache.org/viewvc?rev=417084&view=rev
Log:
Adding custom impl of jLine CompletionHandler due to bug in jLine 0.9.5 impl
This change fixes the problem w/no completion list being shown

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
  (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveShell.java

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java?rev=417084&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
Sun Jun 25 15:35:03 2006
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell;
+
+import jline.CompletionHandler;
+import jline.ConsoleReader;
+import jline.CursorBuffer;
+
+import java.util.List;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.ArrayList;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.geronimo.gshell.command.MessageSource;
+import org.apache.geronimo.gshell.command.MessageSourceImpl;
+
+//
+// NOTE: Based on jline.CandidateListCompletionHandler.  CLCH that comes with
+//       jLine 0.9.5 has a bug that never displays the completion list.  When that
+//       is fixed, then this class can be removed.
+//
+
+/**
+ * A candindate list completion handler (that actually works).
+ *
+ * <p>
+ * Follows the style of Bash.
+ *
+ * @version $Id$
+ */
+public class CompletionHandlerImpl
+    implements CompletionHandler
+{
+    private static final Log log = LogFactory.getLog(CompletionHandlerImpl.class);
+
+    private static MessageSource messages = new MessageSourceImpl(CompletionHandlerImpl.class.getName());
+
+    public boolean complete(final ConsoleReader reader, final List candidates, final int
pos)
+        throws IOException
+    {
+        if (log.isDebugEnabled()) {
+            log.debug("Complete; candicates=" + candidates + "; pos=" + pos);
+        }
+
+        CursorBuffer buf = reader.getCursorBuffer();
+
+        // if there is only one completion, then fill in the buffer
+        if (candidates.size() == 1) {
+            String value = candidates.get(0).toString();
+
+            // fail if the only candidate is the same as the current buffer
+            if (value.equals(buf.toString())) {
+                return false;
+            }
+
+            setBuffer(reader, value, pos);
+            return true;
+        }
+        else if (candidates.size() > 1) {
+            String value = getUnambiguousCompletions(candidates);
+            setBuffer(reader, value, pos);
+        }
+
+        reader.printNewline();
+        printCandidates(reader, candidates);
+
+        // redraw the current console buffer
+        reader.drawLine();
+
+        return true;
+    }
+
+
+    private static void setBuffer(ConsoleReader reader, String value, int offset) throws
IOException {
+        if (log.isDebugEnabled()) {
+            log.debug("Setting buffer: value=" + value + "; offset=" + offset);
+        }
+
+        while (reader.getCursorBuffer().cursor >= offset && reader.backspace())
{
+            // empty
+        }
+
+        reader.putString(value);
+        reader.setCursorPosition(offset + value.length());
+    }
+
+    private void printCandidates(ConsoleReader reader, Collection<String> candidates)
throws IOException {
+        if (log.isDebugEnabled()) {
+            log.debug("Printing candidates: " + candidates);
+        }
+
+        Set<String> distinct = new HashSet<String>(candidates);
+
+        if (distinct.size() > reader.getAutoprintThreshhold()) {
+            reader.printString(messages.getMessage("display-candidates", candidates.size())
+ " ");
+            reader.flushConsole();
+
+            int c;
+
+            String no = messages.getMessage("display-candidates-no");
+            String yes = messages.getMessage("display-candidates-yes");
+
+            while ((c = reader.readCharacter(new char[]{ yes.charAt(0), no.charAt(0) }))
!= -1) {
+                if (no.startsWith(String.valueOf(c))) {
+                    reader.printNewline();
+                    return;
+                }
+                else if (yes.startsWith(String.valueOf(c))) {
+                    break;
+                }
+                else {
+                    reader.beep();
+                }
+            }
+        }
+
+        // copy the values and make them distinct, without otherwise
+        // affecting the ordering. Only do it if the sizes differ.
+        if (distinct.size() != candidates.size()) {
+            Collection<String> copy = new ArrayList<String>(candidates.size());
+
+            for (String candidate : candidates) {
+                copy.add(candidate);
+            }
+
+            candidates = copy;
+        }
+
+        reader.printColumns(candidates);
+    }
+
+    private String getUnambiguousCompletions(final List<String> candidates) {
+        if (log.isDebugEnabled()) {
+            log.debug("Get unambiguous completions: " + candidates);
+        }
+
+        if (candidates == null || candidates.size() == 0) {
+            return null;
+        }
+
+        // convert to an array for speed
+        String [] strings = candidates.toArray(new String[candidates.size()]);
+
+        String first = strings[0];
+        StringBuffer candidate = new StringBuffer();
+
+        for (int i = 0; i < first.length(); i++) {
+            if (startsWith(first.substring(0, i + 1), strings)) {
+                candidate.append(first.charAt(i));
+            }
+            else {
+                break;
+            }
+        }
+
+        return candidate.toString();
+    }
+
+    private boolean startsWith(final String starts, final String[] candidates) {
+        for (String candidate : candidates) {
+            if (!candidate.startsWith(starts)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CompletionHandlerImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveShell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveShell.java?rev=417084&r1=417083&r2=417084&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveShell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveShell.java
Sun Jun 25 15:35:03 2006
@@ -27,7 +27,6 @@
 
 import jline.ConsoleReader;
 import jline.SimpleCompletor;
-import jline.CandidateListCompletionHandler;
 
 /**
  * Provides the user-interaction bits for Shell.
@@ -77,12 +76,11 @@
         // Add a command completer of we are using JLine
         if (console instanceof JLineConsole) {
             ConsoleReader jline = ((JLineConsole)console).getReader();
+            jline.setCompletionHandler(new CompletionHandlerImpl());
 
             //
-            // TODO: Figure out why this does not work!!!!
+            // TODO: Need to include aliases too
             //
-            jline.setCompletionHandler(new CandidateListCompletionHandler());
-
             String[] commands = (String[])shell.getCommandManager().commandNames().toArray(new
String[0]);
 
             SimpleCompletor c = new SimpleCompletor(commands);

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties?rev=417084&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
Sun Jun 25 15:35:03 2006
@@ -0,0 +1,7 @@
+##
+## $Id$
+##
+
+display-candidates=Display all %s possibilities? (y or n)
+display-candidates-yes=y
+display-candidates-no=n

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/resources/org/apache/geronimo/gshell/CompletionHandlerImpl.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message