From 8a617edb10c79671255ad8cb7d757ab0d0faec64 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 29 Aug 2012 19:03:53 +0400 Subject: [PATCH] Support "ls -a" to list scripts in subdirs. --- library/Core.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 6a0dea7c2..735359a7f 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -219,28 +219,30 @@ static std::string getScriptHelp(std::string path, std::string helpprefix) return "No help available."; } -static std::map listScripts(PluginManager *plug_mgr, std::string path) +static void listScripts(PluginManager *plug_mgr, std::map &pset, std::string path, bool all, std::string prefix = "") { std::vector files; getdir(path, files); - std::map pset; for (size_t i = 0; i < files.size(); i++) { if (hasEnding(files[i], ".lua")) { std::string help = getScriptHelp(path + files[i], "-- "); - pset[files[i].substr(0, files[i].size()-4)] = help; + pset[prefix + files[i].substr(0, files[i].size()-4)] = help; } else if (plug_mgr->eval_ruby && hasEnding(files[i], ".rb")) { std::string help = getScriptHelp(path + files[i], "# "); - pset[files[i].substr(0, files[i].size()-3)] = help; + pset[prefix + files[i].substr(0, files[i].size()-3)] = help; + } + else if (all && !files[i].empty() && files[i][0] != '.') + { + listScripts(plug_mgr, pset, path+files[i]+"/", all, prefix+files[i]+"/"); } } - return pset; } static bool fileExists(std::string path) @@ -335,7 +337,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve con.print("Basic commands:\n" " help|?|man - This text.\n" " help COMMAND - Usage help for the given command.\n" - " ls|dir [PLUGIN] - List available commands. Optionally for single plugin.\n" + " ls|dir [-a] [PLUGIN] - List available commands. Optionally for single plugin.\n" " cls - Clear the console.\n" " fpause - Force DF to pause.\n" " die - Force DF to close immediately\n" @@ -469,6 +471,12 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve } else if(first == "ls" || first == "dir") { + bool all = false; + if (parts.size() && parts[0] == "-a") + { + all = true; + vector_erase_at(parts, 0); + } if(parts.size()) { string & plugname = parts[0]; @@ -491,7 +499,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve con.print( "builtin:\n" " help|?|man - This text or help specific to a plugin.\n" - " ls [PLUGIN] - List available commands. Optionally for single plugin.\n" + " ls [-a] [PLUGIN] - List available commands. Optionally for single plugin.\n" " cls - Clear the console.\n" " fpause - Force DF to pause.\n" " die - Force DF to close immediately\n" @@ -523,7 +531,8 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str()); con.reset_color(); } - auto scripts = listScripts(plug_mgr, getHackPath() + "scripts/"); + std::map scripts; + listScripts(plug_mgr, scripts, getHackPath() + "scripts/", all); if (!scripts.empty()) { con.print("\nscripts:\n");