improve ls output
authorcrawshaw <crawshaw@ibex.org>
Sun, 28 Nov 2004 22:27:52 +0000 (22:27 +0000)
committercrawshaw <crawshaw@ibex.org>
Sun, 28 Nov 2004 22:27:52 +0000 (22:27 +0000)
darcs-hash:20041128222752-2eb37-91ec9c3bf9eeaa6d1b08f97e4512382d40432a1c.gz

src/java/org/ibex/xt/shell/Command.java
src/java/org/ibex/xt/shell/Request.java

index f5ad6fb..7edc5ac 100644 (file)
@@ -126,14 +126,13 @@ public abstract class Command {
             } else {
                 String n = env.path(c[1]);
 
-                Request.Response ret = env.send(new Request.Key(n));
-                if (!(ret instanceof Request.Key.Res)) {
+                Request.Response ret = env.send(new Request.IsKey(n));
+                if (!(ret instanceof Request.IsKey.Res)) {
                     w.write("error: ");
                     w.write(ret.error().getMessage());
                     w.write("\n");
                 } else {
-                    List l = ((Request.Key.Res)ret).keys();
-                    if (l.size() == 0) {
+                    if (((Request.IsKey.Res)ret).exists()) {
                         w.write("error: ");
                         w.write(c[1]);
                         w.write(": no such path\n");
index 2f85247..8cb2be6 100644 (file)
@@ -45,7 +45,9 @@ public abstract class Request implements Serializable {
 
         /** Returns the keys in <tt>js</tt> that match <tt>matcher</tt>. */
         protected List matches(JS js) throws JSExn {
-            Pattern pat = Pattern.compile(matcher);
+            String m = matcher;
+
+            Pattern pat = Pattern.compile(m);
             List keys = new ArrayList();
 
             Iterator i = js.keys().iterator(); while(i.hasNext()) {
@@ -69,6 +71,11 @@ public abstract class Request implements Serializable {
 
         public Response process(JSScope root) throws JSExn {
             JS js = keyed(path(root));
+
+            // if matcher is exact and a keyed object, return its keys
+            JS o = keyed(js.get(matcher));
+            if (o != null) { js = o; matcher = ".*"; }
+
             return js == null ? new Res() : new Res(matches(js));
         }
 
@@ -81,6 +88,22 @@ public abstract class Request implements Serializable {
         }
     }
 
+    public static class IsKey extends Key {
+        public IsKey() {}
+        public IsKey(String c) { super(c); }
+        public Response process(JSScope root) throws JSExn {
+            JS js = keyed(path(root));
+            return js == null ? new Res(false) : new Res(js.get(matcher) != null);
+        }
+
+        public static class Res extends Response {
+            private boolean exists;
+            public Res() {}
+            public Res(boolean e) { exists = e; }
+            public boolean exists() { return exists; }
+        }
+    }
+
     public static class RemoveKey extends Key {
         public RemoveKey(String c) { super(c); }
         public RemoveKey(String p, String m) { super(p, m); }