From 7f712d0926677d8054032d886e91d8061ec8f791 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 23 May 2015 22:42:03 -0400 Subject: [PATCH] Review: Added framework - Added 'review' command, which does nothing. - Removed 'context' command, which now passes to Taskwarrior. - Removed 'leave' command. - Removed 'clear' command. --- src/CMakeLists.txt | 4 +- src/main.cpp | 28 +++++-------- src/prompt.cpp | 73 +++++++++++++++++++++++++++++++-- src/{context.cpp => review.cpp} | 67 +----------------------------- 4 files changed, 83 insertions(+), 89 deletions(-) rename src/{context.cpp => review.cpp} (51%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3bdf07..a333d04 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,10 +3,10 @@ include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${TASKSH_INCLUDE_DIRS}) -set (tasksh_SRCS context.cpp - diag.cpp +set (tasksh_SRCS diag.cpp help.cpp prompt.cpp + review.cpp Color.cpp Color.h Path.cpp Path.h File.cpp File.h diff --git a/src/main.cpp b/src/main.cpp index 693e061..94f8c91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,18 +45,15 @@ // tasksh commands. int cmdHelp (const std::vector &); int cmdDiagnostics (const std::vector &); -int cmdContext (const std::vector &, std::vector &); -int cmdClear (std::vector &); -int cmdLeave (std::vector &); -std::string composePrompt (std::vector &); -std::string composeContexts (std::vector &, bool pretty = false); +int cmdReview (const std::vector &); +std::string promptCompose (); std::string findTaskwarrior (); //////////////////////////////////////////////////////////////////////////////// -static int commandLoop (std::vector & contexts) +static int commandLoop () { // Compose the prompt. - std::string prompt = composePrompt (contexts); + std::string prompt = promptCompose (); // Display prompt, get input. #ifdef HAVE_READLINE @@ -90,18 +87,16 @@ static int commandLoop (std::vector & contexts) std::vector args; split (args, command, ' '); - // Dispatch command + // Dispatch command. if (closeEnough ("exit", args[0], 3)) status = -1; else if (closeEnough ("quit", args[0], 3)) status = -1; - else if (closeEnough ("help", args[0], 3)) status = cmdHelp (args ); - else if (closeEnough ("diagnostics", args[0], 3)) status = cmdDiagnostics (args ); - else if (closeEnough ("context", args[0], 3)) status = cmdContext (args, contexts); - else if (closeEnough ("clear", args[0], 3)) status = cmdClear ( contexts); - else if (closeEnough ("leave", args[0], 3)) status = cmdLeave ( contexts); + else if (closeEnough ("help", args[0], 3)) status = cmdHelp (args); + else if (closeEnough ("diagnostics", args[0], 3)) status = cmdDiagnostics (args); + else if (closeEnough ("review", args[0], 3)) status = cmdReview (args); else if (command != "") { - command = "task " + composeContexts (contexts) + command; - std::cout << "[task " << command << "]\n"; + command = "task " + command; + std::cout << "[" << command << "]\n"; system (command.c_str ()); // Deliberately ignoreѕ taskwarrior exit status, otherwise empty filters @@ -126,8 +121,7 @@ int main (int argc, const char** argv) { try { - std::vector contexts; - while ((status = commandLoop (contexts)) == 0) + while ((status = commandLoop ()) == 0) ; } diff --git a/src/prompt.cpp b/src/prompt.cpp index bf784ac..1d3d6fe 100644 --- a/src/prompt.cpp +++ b/src/prompt.cpp @@ -27,20 +27,85 @@ #include #include #include +#include -std::string composeContexts (std::vector &, bool plain = true); +static const char* contextColors[] = +{ + "bold white on red", + "bold white on blue", + "bold white on green", + "bold white on magenta", + "black on cyan", + "black on yellow", + "black on white", +}; + +#define NUM_COLORS (sizeof (contextColors) / sizeof (contextColors[0])) + +static std::vector contexts; + +std::string composeContexts (bool pretty = false); //////////////////////////////////////////////////////////////////////////////// -std::string composePrompt (std::vector & contexts) +int promptClear () +{ + contexts.clear (); + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +int promptRemove () +{ + if (contexts.size ()) + contexts.pop_back (); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +int promptAdd (const std::string& context) +{ + contexts.push_back (context); + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string composeContexts (bool pretty /* = false */) +{ + std::string combined; + for (int i = 0; i < contexts.size (); ++i) + { + if (pretty) + { + combined += (i ? " " : "") + + std::string ("\001") + + Color::colorize ("\002 " + contexts[i] + " \001", contextColors[i % NUM_COLORS]) + + "\002"; + + } + else + { + combined += (i ? " " : "") + contexts[i]; + } + } + + if (combined != "") + combined += ' '; + + return combined; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string promptCompose () { // TODO The prompt may be composed of different elements: // TODO - The configurable text // TODO - The accumulated context, as colored tokens. // TODO - sync status // TODO - time - std::string decoration = composeContexts (contexts, true); + std::string decoration = composeContexts (true); if (decoration.length ()) - return "task " + composeContexts (contexts, true) + "> "; + return "task " + composeContexts (true) + "> "; return "task> "; } diff --git a/src/context.cpp b/src/review.cpp similarity index 51% rename from src/context.cpp rename to src/review.cpp index b6eae8c..eaa4f9a 100644 --- a/src/context.cpp +++ b/src/review.cpp @@ -25,78 +25,13 @@ //////////////////////////////////////////////////////////////////////////////// #include -#include // TODO Remove. #include #include -#include - -static const char* contextColors[] = -{ - "bold white on red", - "bold white on blue", - "bold white on green", - "bold white on magenta", - "black on cyan", - "black on yellow", - "black on white", -}; - -#define NUM_COLORS (sizeof (contextColors) / sizeof (contextColors[0])) //////////////////////////////////////////////////////////////////////////////// -int cmdClear (std::vector & contexts) +int cmdReview (const std::vector & args) { - contexts.clear (); return 0; } //////////////////////////////////////////////////////////////////////////////// -int cmdLeave (std::vector & contexts) -{ - if (contexts.size ()) - contexts.pop_back (); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -int cmdContext ( - const std::vector & args, - std::vector & contexts) -{ - // TODO Verify context not already in use. - if (args.size () > 1) - contexts.push_back (args[1]); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -std::string composeContexts ( - std::vector & contexts, - bool pretty /* = false */) -{ - std::string combined; - for (int i = 0; i < contexts.size (); ++i) - { - if (pretty) - { - combined += (i ? " " : "") - + std::string ("\001") - + Color::colorize ("\002 " + contexts[i] + " \001", contextColors[i % NUM_COLORS]) - + "\002"; - - } - else - { - combined += (i ? " " : "") + contexts[i]; - } - } - - if (combined != "") - combined += ' '; - - return combined; -} - -////////////////////////////////////////////////////////////////////////////////