Replace SENFSCons.InstallIncludeFiles with InstallSubdir builder calls
[senf.git] / Utils / Console / Executor.cc
index 1a93b6e..49e7eb9 100644 (file)
 #include <boost/utility.hpp>
 #include <boost/range/iterator_range.hpp>
 #include <boost/bind.hpp>
+#include <boost/format.hpp>
+#include <boost/preprocessor/stringize.hpp>
 #include "../../Utils/senfassert.hh"
 #include "../../Utils/Range.hh"
 #include "../../Utils/String.hh"
 #include "../../Utils/range.hh"
+#include "Server.hh"
 
 //#include "Executor.mpp"
 #define prefix_
@@ -111,6 +114,20 @@ prefix_ void senf::console::Executor::execute(std::ostream & output,
             ls( output, command.commandPath() );
             break;
 
+        case ParseCommandInfo::BuiltinLL :
+            if (skipping())
+                break;
+            // The parser ensures, we have either one or no argument
+            ll( output, command.commandPath() );
+            break;
+
+        case ParseCommandInfo::BuiltinLR :
+            if (skipping())
+                break;
+            // The parser ensures, we have either one or no argument
+            lr( output, command.commandPath() );
+            break;
+
         case ParseCommandInfo::BuiltinPUSHD :
             // The parser ensures, we have exactly one argument
             if (skipping())
@@ -152,6 +169,12 @@ prefix_ void senf::console::Executor::execute(std::ostream & output,
     catch (IgnoreCommandException &) {}
 }
 
+prefix_ senf::console::GenericNode &
+senf::console::Executor::getNode(ParseCommandInfo const & command)
+{
+    return traverseNode(command.commandPath());
+}
+
 prefix_ void senf::console::Executor::exec(std::ostream & output,
                                            ParseCommandInfo const & command)
 {
@@ -222,16 +245,92 @@ 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";
+    for (; i != i_end; ++i)
+        output << i->first << "\n";
+}
+
+prefix_ void senf::console::Executor::ll(std::ostream & output,
+                                         ParseCommandInfo::TokensRange path)
+{
+#   define HELP_COLUMN 28
+
+    unsigned width (senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1));
+    Path dir (cwd_);
+    traverseDirectory(path, dir);
+    DirectoryNode & node (*dir.back().lock());
+    DirectoryNode::child_iterator i (node.children().begin());
+    DirectoryNode::child_iterator const i_end (node.children().end());
+    boost::format fmt ("%s%s  %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
+    for (; i != i_end; ++i)
+        output << fmt
+            % i->first
+            % ( i->second->isDirectory()
+                ? "/"
+                : i->second->isLink()
+                ? "@"
+                : "" )
+            % i->second->shorthelp().substr(0,width);
+
+#   undef HELP_COLUMN
+}
+
+#   define HELP_COLUMN 40
+
+namespace {
+
+    typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
+
+    void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base, 
+              unsigned level, senf::console::DirectoryNode & node)
+    {
+        boost::format fmt ("%s%s%s  %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
+        std::string pad (2*level, ' ');
+        senf::console::DirectoryNode::child_iterator i (node.children().begin());
+        senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
+        for (; i != i_end; ++i) {
+            if (i->second->followLink().isDirectory()) {
+                senf::console::DirectoryNode & subnode (
+                    static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
+                NodesMap::iterator j (nodes.find(&subnode));
+                if (j == nodes.end()) {
+                    output << fmt
+                        % pad % i->first 
+                        % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
+                        % i->second->shorthelp().substr(0,width);
+                    std::string subbase (base);
+                    if (! subbase.empty())
+                        subbase += "/";
+                    subbase += i->first;
+                    nodes.insert(std::make_pair(&subnode, subbase));
+                    dolr(output, width, nodes, subbase, level+1, subnode);
+                } else
+                    output << pad << i->first 
+                           << ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
+                           << " -> " << j->second << "\n";
+            } else {
+                output << fmt
+                    % pad % i->first 
+                    % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
+                    % i->second->shorthelp().substr(0,width);
+            }
+        }
     }
+
+}
+
+prefix_ void senf::console::Executor::lr(std::ostream & output,
+                                         ParseCommandInfo::TokensRange path)
+{
+    Path dir (cwd_);
+    traverseDirectory(path, dir);
+    DirectoryNode & node (*dir.back().lock());
+    NodesMap nodes;
+    dolr(output, senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1), 
+         nodes, "", 0, node);
 }
 
+#undef HELP_COLUMN
+
 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
 {
     Path newDir (cwd_);
@@ -299,9 +398,7 @@ senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path
     catch (UnknownNodeNameException &) {
         throw InvalidPathException(
             senf::stringJoin(
-                senf::make_transform_range(
-                    boost::make_iterator_range(path.begin(), path.end()),
-                    boost::bind(&Token::value, _1)),
+                senf::make_transform_range(path, boost::bind(&Token::value, _1)),
                 "/"));
     }
 }