From de77c55df7f8e0750c7378de05229a6c96e0de0e Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Fri, 24 Mar 2017 23:37:49 -0700 Subject: [PATCH] description implementation! version already has one --- CMakeLists.txt | 14 ++++++++++++-- include/help.hpp | 1 + src/help/descriptions.cpp | 26 ++++++++++++++++++++++++++ src/help/help.cpp | 26 +++++++++++++++----------- 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/help/descriptions.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a3c8acd..7c47ed2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,8 +77,18 @@ endfunction(cellar_library) cellar_library(bottles) cellar_library(launch) -add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp - ${src}/version.cpp ${src}/output.cpp ${src}/help/help.cpp) +file(GLOB coresources RELATIVE "${CMAKE_SOURCE_DIR}" + "${CMAKE_SOURCE_DIR}/src/*.cpp") +file(GLOB corecoggedsources RELATIVE "${CMAKE_SOURCE_DIR}" + "${CMAKE_SOURCE_DIR}/src/*.cpp.cog") +foreach(corecog ${corecoggedsources}) + string(REGEX REPLACE ".cog\$" "" this "${corecog}") + set(coresources ${coresources} ${this}) +endforeach(corecog) + +get_sources("src/help/*.cpp" helpsources) + +add_executable(cellar ${coresources} ${helpsources}) target_link_libraries(cellar ${cellar_LIBRARIES} ${Boost_LIBRARIES}) add_dependencies(cellar cog) # effectively redundant but a couple of the bin's # files are cogged, so this is mostly for people diff --git a/include/help.hpp b/include/help.hpp index 7ba6a3e..0bfbab8 100644 --- a/include/help.hpp +++ b/include/help.hpp @@ -9,6 +9,7 @@ using namespace std; namespace cellar { namespace help { extern void set_description(string,string); + extern string get_description(string); } } diff --git a/src/help/descriptions.cpp b/src/help/descriptions.cpp new file mode 100644 index 0000000..bd2bf60 --- /dev/null +++ b/src/help/descriptions.cpp @@ -0,0 +1,26 @@ +#include +#include + +#include "help.hpp" + +map description_map; + +void cellar::help::set_description(string command, string description) { + if (description.empty()) { + if (description_map.find(command) == description_map.end()) { // not found + return; // no description no cry + } else { + description_map.erase(command); + } + } else { // setting description + description_map[command] = description; + } +} + +string cellar::help::get_description(string command) { + if (description_map.find(command) == description_map.end()) { // not found + return ""; + } else { + return description_map.at(command); + } +} diff --git a/src/help/help.cpp b/src/help/help.cpp index 6a2d1c1..b21a26e 100644 --- a/src/help/help.cpp +++ b/src/help/help.cpp @@ -1,28 +1,32 @@ +#include +#include #include #include #include "cellar.hpp" #include "internal/core.hpp" -#include "help.cpp" +#include "help.hpp" +#include "output.hpp" using namespace std; +using namespace cellar; void cellar::commands::help_command(int argc, vector argv) { vector commands = list_commands(); cellar::print_header(); - cout << "You have these commands:\n" << endl; + cout << endl; // TODO: cellar::output function for code clarity - int num_columns = 4; - int cur_column = 1; + output::statement("You have these commands:"); + stringstream sstr; for (string command : commands) { - cout << "\t" << command; - if (cur_column == num_columns) { - cout << endl; - cur_column = 1; - } else { - cur_column++; + sstr << "\t" << command; + + string desc = help::get_description(command); + if (!desc.empty()) { + sstr << " - " << desc; } + output::statement(sstr.str()); + sstr.str(""); } - cout << endl; }