From: g0dil Date: Tue, 27 May 2008 14:00:24 +0000 (+0000) Subject: Console: Fix autocomplete / autocd support X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=95ac66527fcf847f4194a3b6827add911f2f2e34;p=senf.git Console: Fix autocomplete / autocd support git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@857 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Console/Executor.cc b/Console/Executor.cc index d5f56fb..5000104 100644 --- a/Console/Executor.cc +++ b/Console/Executor.cc @@ -249,7 +249,7 @@ senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path boost::prior(path.end())), dir); DirectoryNode & base (*dir.back().lock()); - std::string const & name (boost::prior(path.end())->value()); + std::string const & name (complete(base, boost::prior(path.end())->value())); if (policy_) policy_( base, name ); return dir.back().lock()->get(name); @@ -281,9 +281,10 @@ senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & ; else { DirectoryNode & base (*dir.back().lock()); + std::string name (complete(base, i->value())); if (policy_) - policy_( base, i->value() ); - dir.push_back(base[i->value()].thisptr()); + policy_( base, name ); + dir.push_back(base[name].thisptr()); } } } @@ -295,6 +296,17 @@ senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & } } +prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir, + std::string const & name) +{ + if (! dir.hasChild(name)) { + DirectoryNode::ChildrenRange completions (dir.completions(name)); + if (completions.size() == 1) + return completions.begin()->first; + } + return name; +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "Executor.mpp" diff --git a/Console/Executor.hh b/Console/Executor.hh index 81049b1..02cff18 100644 --- a/Console/Executor.hh +++ b/Console/Executor.hh @@ -145,6 +145,7 @@ namespace console { GenericNode & traverseNode(ParseCommandInfo::TokensRange const & path); void traverseDirectory(ParseCommandInfo::TokensRange const & path, Path & dir); + std::string complete(DirectoryNode & dir, std::string const & name); struct InvalidPathException {}; struct InvalidDirectoryException {}; diff --git a/Console/Executor.test.cc b/Console/Executor.test.cc index 36f9dcf..1266ae0 100644 --- a/Console/Executor.test.cc +++ b/Console/Executor.test.cc @@ -229,6 +229,38 @@ BOOST_AUTO_UNIT_TEST(executorPolicy) senf::console::root().remove("dir2"); } +BOOST_AUTO_UNIT_TEST(executorAuto) +{ + senf::console::root().mkdir("dir1").mkdir("dir3"); + senf::console::root().mkdir("dir2").doc("Helptext").add("test",&testCommand); + + senf::console::Executor executor; + executor + .autocomplete(true) + .autocd(true); + senf::console::CommandParser parser; + + { + std::stringstream os; + parser.parse("dir2", &setCommand); + executor(os, commands.back()); + BOOST_CHECK_EQUAL( executor.cwdPath(), "/dir2" ); + BOOST_CHECK_EQUAL( os.str(), "" ); + } + + { + std::stringstream os; + parser.parse("t", &setCommand); + executor(os, commands.back()); + BOOST_CHECK_EQUAL( os.str(), "testCommand\n" ); + } + + + commands.clear(); + senf::console::root().remove("dir1"); + senf::console::root().remove("dir2"); +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_