From 3dc004d27a6549a0098b969fa791ade43b0fedfb Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Sat, 25 Mar 2017 00:25:20 -0700 Subject: [PATCH] improvements to the help command, including outputting detailed descriptions --- include/help.hpp | 3 +++ src/bottles/commands.cpp.cog | 13 +++++++++++ src/commands.cpp.cog | 12 ++++++++++ src/help/details.cpp | 26 +++++++++++++++++++++ src/help/help.cpp | 45 ++++++++++++++++++++++++++++-------- src/help/version | 1 + src/launch/commands.cpp.cog | 13 +++++++++++ src/launch/commands.txt | 6 ++--- src/launch/help/regedit | 1 + src/launch/help/winecfg | 1 + 10 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 src/help/details.cpp create mode 100644 src/help/version create mode 100644 src/launch/help/regedit create mode 100644 src/launch/help/winecfg diff --git a/include/help.hpp b/include/help.hpp index 0bfbab8..df76785 100644 --- a/include/help.hpp +++ b/include/help.hpp @@ -10,6 +10,9 @@ namespace cellar { namespace help { extern void set_description(string,string); extern string get_description(string); + + extern void set_details(string,string); + extern string get_details(string); } } diff --git a/src/bottles/commands.cpp.cog b/src/bottles/commands.cpp.cog index 5630eab..cd4e605 100644 --- a/src/bottles/commands.cpp.cog +++ b/src/bottles/commands.cpp.cog @@ -5,6 +5,7 @@ #include "internal/bottles.hpp" #include "commands.hpp" #include "dll.hpp" +#include "help.hpp" using namespace std; using namespace cellar::bottles; @@ -14,6 +15,7 @@ DLL_PUBLIC map cellar::commands::bottles_commands() { map result; /*[[[cog import cog + import os with open("src/bottles/commands.txt") as commandfile: for line in commandfile: @@ -30,6 +32,17 @@ DLL_PUBLIC map cellar::commands::bottles_commands() { .replace("\"", "\\\"") .replace("\\", "\\\\"))) # the replace methods escape " and \ characters + else: + print("-- No description is available for the {0} command.".format(name)) + + if os.path.exists("src/bottles/help/" + name): + cog.out("cellar::help::set_details(\"{0}\", R\"(".format(name)) + with open("src/bottles/help/" + name) as detailsfile: + for detail in detailsfile: + cog.out(detail) + cog.out(")\");") + else: + print("-- No details are available for the {0} command.".format(name)) ]]]*/ //[[[end]]] return result; diff --git a/src/commands.cpp.cog b/src/commands.cpp.cog index 5899ea0..05dd1f1 100644 --- a/src/commands.cpp.cog +++ b/src/commands.cpp.cog @@ -25,6 +25,7 @@ map cellar::commands::core_commands() { map result; /*[[[cog import cog + import os.path with open("src/commands.txt") as commandfile: for line in commandfile: @@ -41,6 +42,17 @@ map cellar::commands::core_commands() { .replace("\"", "\\\"") .replace("\\", "\\\\"))) # the replace methods escape " and \ characters + else: + print("-- No description is available for the {0} command.".format(name)) + + if os.path.exists("src/help/" + name): + cog.out("cellar::help::set_details(\"{0}\", R\"(".format(name)) + with open("src/help/" + name) as detailsfile: + for detail in detailsfile: + cog.out(detail) + cog.out(")\");") + else: + print("-- No details are available for the {0} command.".format(name)) ]]]*/ //[[[end]]] return result; diff --git a/src/help/details.cpp b/src/help/details.cpp new file mode 100644 index 0000000..cb851f2 --- /dev/null +++ b/src/help/details.cpp @@ -0,0 +1,26 @@ +#include +#include + +#include "help.hpp" + +map details_map; + +void cellar::help::set_details(string command, string details) { + if (details.empty()) { + if (details_map.find(command) == details_map.end()) { // not found + return; // no details no cry + } else { + details_map.erase(command); + } + } else { // setting details + details_map[command] = details; + } +} + +string cellar::help::get_details(string command) { + if (details_map.find(command) == details_map.end()) { // not found + return ""; + } else { + return details_map.at(command); + } +} diff --git a/src/help/help.cpp b/src/help/help.cpp index b21a26e..ab4e52c 100644 --- a/src/help/help.cpp +++ b/src/help/help.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include "cellar.hpp" #include "internal/core.hpp" #include "help.hpp" @@ -17,16 +19,41 @@ void cellar::commands::help_command(int argc, vector argv) { cout << endl; // TODO: cellar::output function for code clarity - output::statement("You have these commands:"); - stringstream sstr; - for (string command : commands) { - sstr << "\t" << command; - - string desc = help::get_description(command); - if (!desc.empty()) { - sstr << " - " << desc; + if (argc == 1) { + output::statement("You have these commands:"); + stringstream sstr; + for (string command : commands) { + sstr << "\t" << command; + + string desc = help::get_description(command); + if (!desc.empty()) { + sstr << " - " << desc; + } + output::statement(sstr.str()); + sstr.str(""); } + } else { + string command = argv[1]; + stringstream sstr; + sstr << "cellar " << command << " - "; + + string desc = help::get_description(command); + if (desc.empty()) { sstr << "no description is available."; } + else { sstr << desc; } output::statement(sstr.str()); - sstr.str(""); + + cout << endl; + + string details = help::get_details(command); + if (details.empty()) { output::statement("no details available."); } + else { + vector detaillines; + boost::split(detaillines, details, boost::is_any_of("\n")); + + for (string line : detaillines) { + if (!line.empty()) { output::statement(line); } + else { cout << endl; } + } + } } } diff --git a/src/help/version b/src/help/version new file mode 100644 index 0000000..b7e4f68 --- /dev/null +++ b/src/help/version @@ -0,0 +1 @@ +No, really, that's all it does. diff --git a/src/launch/commands.cpp.cog b/src/launch/commands.cpp.cog index 402213c..3cee31a 100644 --- a/src/launch/commands.cpp.cog +++ b/src/launch/commands.cpp.cog @@ -5,6 +5,7 @@ #include "internal/launch.hpp" #include "commands.hpp" #include "dll.hpp" +#include "help.hpp" using namespace std; using namespace cellar::launch; @@ -14,6 +15,7 @@ DLL_PUBLIC map cellar::commands::launch_commands() { map result; /*[[[cog import cog + import os with open("src/launch/commands.txt") as commandfile: for line in commandfile: @@ -30,6 +32,17 @@ DLL_PUBLIC map cellar::commands::launch_commands() { .replace("\"", "\\\"") .replace("\\", "\\\\"))) # the replace methods escape " and \ characters + else: + print("-- No description is available for the {0} command.".format(name)) + + if os.path.exists("src/launch/help/" + name): + cog.out("cellar::help::set_details(\"{0}\", R\"(".format(name)) + with open("src/launch/help/" + name) as detailsfile: + for detail in detailsfile: + cog.out(detail) + cog.out(")\");") + else: + print("-- No details are available for the {0} command.".format(name)) ]]]*/ //[[[end]]] return result; diff --git a/src/launch/commands.txt b/src/launch/commands.txt index b066958..8b6b208 100644 --- a/src/launch/commands.txt +++ b/src/launch/commands.txt @@ -1,3 +1,3 @@ -launch launch_command -winecfg launch_winecfg -regedit launch_regedit +launch launch_command Launch a program in WINE. +winecfg launch_winecfg Launch winecfg. +regedit launch_regedit Launch regedit. diff --git a/src/launch/help/regedit b/src/launch/help/regedit new file mode 100644 index 0000000..01e88ad --- /dev/null +++ b/src/launch/help/regedit @@ -0,0 +1 @@ +This is an alias for "cellar launch regedit". diff --git a/src/launch/help/winecfg b/src/launch/help/winecfg new file mode 100644 index 0000000..933d123 --- /dev/null +++ b/src/launch/help/winecfg @@ -0,0 +1 @@ +This is an alias for "cellar launch winecfg".