Author: jstrachan
Date: Wed Sep 15 16:06:31 2010
New Revision: 997377

URL: http://svn.apache.org/viewvc?rev=997377&view=rev
Log:
fix for most of KARAF-208 so that the help commands don't print unnecessary 
scope noise when the MULTI_SCOPE_MODE is set to false and a commands scope is 
the same as the application. Not yet fixed for tab completion showing each 
command twice (once with and without the scope) yet though...

Added:
    
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/NameScoping.java
Modified:
    
karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java
    
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/HelpAction.java
    
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java

Modified: 
karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java?rev=997377&r1=997376&r2=997377&view=diff
==============================================================================
--- 
karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java
 (original)
+++ 
karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java
 Wed Sep 15 16:06:31 2010
@@ -26,7 +26,6 @@ import java.io.StringWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Type;
-import java.net.URL;
 import java.util.*;
 import java.io.PrintStream;
 import java.util.regex.Matcher;
@@ -38,9 +37,9 @@ import org.apache.felix.gogo.commands.Op
 import org.apache.felix.gogo.commands.Action;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
-import org.apache.felix.gogo.commands.basic.ActionPreparator;
 import org.apache.felix.gogo.commands.converter.DefaultConverter;
 import org.apache.felix.gogo.commands.converter.GenericType;
+import org.apache.karaf.shell.console.NameScoping;
 import org.fusesource.jansi.Ansi;
 import org.osgi.service.command.CommandSession;
 
@@ -386,12 +385,17 @@ public class DefaultActionPreparator imp
         });
         Set<Option> options = new HashSet<Option>(optionsMap.keySet());
         options.add(HELP);
+        boolean globalScope = NameScoping.isGlobalScope(session, 
command.scope());
         if (command != null && (command.description() != null || 
command.name() != null))
         {
             
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
             out.print("        ");
             if (command.name() != null) {
-                
out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
+                if (globalScope) {
+                    
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
+                } else {
+                    
out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
+                }
                 out.println();
             }
             out.print("\t");
@@ -401,7 +405,11 @@ public class DefaultActionPreparator imp
         StringBuffer syntax = new StringBuffer();
         if (command != null)
         {
-            syntax.append(String.format("%s:%s", command.scope(), 
command.name()));
+            if (globalScope) {
+                syntax.append(command.name());
+            } else {
+                syntax.append(String.format("%s:%s", command.scope(), 
command.name()));
+            }
         }
         if (options.size() > 0)
         {

Modified: 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/HelpAction.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/HelpAction.java?rev=997377&r1=997376&r2=997377&view=diff
==============================================================================
--- 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/HelpAction.java
 (original)
+++ 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/HelpAction.java
 Wed Sep 15 16:06:31 2010
@@ -77,7 +77,8 @@ public class HelpAction extends Abstract
                 
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("COMMANDS").a(Ansi.Attribute.RESET));
                 for (Map.Entry<String,String> entry : commands.entrySet()) {
                     out.print("        ");
-                    
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(entry.getKey()).a(Ansi.Attribute.RESET));
+                    String key = 
NameScoping.getCommandNameWithoutGlobalPrefix(session, entry.getKey());
+                    
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(key).a(Ansi.Attribute.RESET));
                     if (entry.getValue() != null) {
                         DefaultActionPreparator.printFormatted("               
 ", entry.getValue(), term != null ? term.getTerminalWidth() : 80, out);
                     }

Modified: 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java?rev=997377&r1=997376&r2=997377&view=diff
==============================================================================
--- 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java
 (original)
+++ 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java
 Wed Sep 15 16:06:31 2010
@@ -142,6 +142,7 @@ public class Main {
         session.put("LINES", Integer.toString(terminal.getTerminalHeight()));
         session.put("COLUMNS", Integer.toString(terminal.getTerminalWidth()));
         session.put(".jline.terminal", terminal);
+        session.put(NameScoping.MULTI_SCOPE_MODE_KEY, 
Boolean.toString(isMultiScopeMode()));
 
         if (args.length > 0) {
             StringBuilder sb = new StringBuilder();
@@ -249,6 +250,16 @@ public class Main {
         this.user = user;
     }
 
+    /**
+     * Returns whether or not we are in multi-scope mode.
+     *
+     * The default mode is multi-scoped where we prefix commands by their 
scope. If we are in single
+     * scoped mode then we don't use scope prefixes when registering or tab 
completing commands.
+     */
+    public boolean isMultiScopeMode() {
+        return true;
+    }
+
     private static PrintStream wrap(PrintStream stream) {
         OutputStream o = AnsiConsole.wrapOutputStream(stream);
         if (o instanceof PrintStream) {

Added: 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/NameScoping.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/NameScoping.java?rev=997377&view=auto
==============================================================================
--- 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/NameScoping.java
 (added)
+++ 
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/NameScoping.java
 Wed Sep 15 16:06:31 2010
@@ -0,0 +1,72 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.karaf.shell.console;
+
+import org.osgi.service.command.CommandSession;
+
+/**
+ * A helper class for name scoping
+ */
+public class NameScoping {
+
+    public static final String MULTI_SCOPE_MODE_KEY = "MULTI_SCOPE_MODE";
+
+    /**
+     * Returns the name of the command which can omit the global scope prefix 
if the command starts with the
+     * same prefix as the current application
+     */
+    public static String getCommandNameWithoutGlobalPrefix(CommandSession 
session, String key) {
+        if (!isMultiScopeMode(session)) {
+            String globalScope = (String) session.get("APPLICATION");
+            if (globalScope != null) {
+                String prefix = globalScope + ":";
+                if (key.startsWith(prefix)) {
+                    // TODO we may only want to do this for single-scope mode 
when outside of OSGi?
+                    // so we may want to also check for a isMultiScope mode == 
false
+                    return key.substring(prefix.length());
+                }
+            }
+        }
+        return key;
+    }
+
+    /**
+     * Returns true if the given scope is the global scope so that it can be 
hidden from help messages
+     */
+    public static boolean isGlobalScope(CommandSession session, String scope) {
+        if (!isMultiScopeMode(session)) {
+            String globalScope = (String) session.get("APPLICATION");
+            if (globalScope != null) {
+                return scope.equals(globalScope);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns true if we are in multi-scope mode (the default) or if we are 
in single scope mode which means we
+     * avoid prefixing commands with their scope
+     */
+    public static boolean isMultiScopeMode(CommandSession session) {
+        Object value = session.get(MULTI_SCOPE_MODE_KEY);
+        if (value != null && value.equals("false")) {
+            return false;
+        }
+        return true;
+    }
+}


Reply via email to