Utils/Console: Add short help to 'ls' output
g0dil [Wed, 3 Jun 2009 14:47:16 +0000 (14:47 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1220 270642c3-0616-0410-b53a-bc976706d245

Utils/Console/Executor.cc
Utils/Console/Executor.test.cc
Utils/Console/OverloadedCommand.cc
Utils/Console/ParsedCommand.test.cc

index 3bfa19b..e3f0921 100644 (file)
@@ -30,6 +30,7 @@
 #include <boost/utility.hpp>
 #include <boost/range/iterator_range.hpp>
 #include <boost/bind.hpp>
+#include <boost/format.hpp>
 #include "../../Utils/senfassert.hh"
 #include "../../Utils/Range.hh"
 #include "../../Utils/String.hh"
@@ -222,14 +223,16 @@ prefix_ void senf::console::Executor::ls(std::ostream & output,
     DirectoryNode & node (*dir.back().lock());
     DirectoryNode::child_iterator i (node.children().begin());
     DirectoryNode::child_iterator const i_end (node.children().end());
-    for (; i != i_end; ++i) {
-        output << i->first;
-        if (i->second->isDirectory())
-            output << "/";
-        else if (i->second->isLink())
-            output << "@";
-        output << "\n";
-    }
+    boost::format fmt ("%s%s  %|20t|%.59s\n");
+    for (; i != i_end; ++i)
+        output << fmt
+            % i->first
+            % ( i->second->isDirectory()
+                ? "/"
+                : i->second->isLink()
+                ? "@"
+                : "" )
+            % i->second->shorthelp();
 }
 
 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
index 833aa45..f02943b 100644 (file)
@@ -99,7 +99,10 @@ BOOST_AUTO_UNIT_TEST(executor)
         parser.parse("ls", &setCommand);
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
-        BOOST_CHECK_EQUAL( os.str(), "dir1/\ndir2/\nsys/\n" );
+        BOOST_CHECK_EQUAL( os.str(), 
+                           "dir1/               \n"
+                           "dir2/               Helptext\n"
+                           "sys/                \n" );
     }
 
     {
@@ -107,7 +110,7 @@ BOOST_AUTO_UNIT_TEST(executor)
         parser.parse("ls dir1", &setCommand);
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
-        BOOST_CHECK_EQUAL( os.str(), "dir3/\n" );
+        BOOST_CHECK_EQUAL( os.str(), "dir3/               \n" );
     }
 
     {
@@ -213,7 +216,7 @@ BOOST_AUTO_UNIT_TEST(executorPolicy)
         parser.parse("ls dir1", &setCommand);
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
-        BOOST_CHECK_EQUAL( os.str(), "dir3/\n" );
+        BOOST_CHECK_EQUAL( os.str(), "dir3/               \n" );
     }
 
     {
index 8a0c631..cb405a7 100644 (file)
@@ -160,7 +160,16 @@ prefix_ std::string senf::console::OverloadedCommandNode::v_shorthelp()
 {
     if (!shortdoc_.empty())
         return shortdoc_;
-    return doc_.substr(0,doc_.find('\n'));
+    if (!doc_.empty())
+        return doc_.substr(0,doc_.find('\n'));
+    Overloads::const_iterator i (overloads_.begin());
+    Overloads::const_iterator const i_end (overloads_.end());
+    for (; i != i_end; ++i) {
+        std::string overloadDoc ((*i)->doc());
+        if (! overloadDoc.empty()) 
+            return overloadDoc.substr(0,doc_.find('\n'));
+    }
+    return "";
 }
 
 prefix_ void senf::console::OverloadedCommandNode::v_execute(boost::any & rv,
index 74f57d7..5953658 100644 (file)
@@ -303,7 +303,7 @@ BOOST_AUTO_UNIT_TEST(directoryReturn)
         SENF_CHECK_NO_THROW(
             parser.parse("test/test { ls; }",
                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
-        BOOST_CHECK_EQUAL( ss.str(), "<Directory at '/test/dircb'>\ncb1\n" );
+        BOOST_CHECK_EQUAL( ss.str(), "<Directory at '/test/dircb'>\ncb1                 \n" );
     }
     
 }