From 5716f5abb78005ee62abe9c247fe833ed46d222d Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Thu, 28 Jan 2021 01:09:43 -0500 Subject: [PATCH] CLI2: Generalize method to add context to support writeable context --- src/CLI2.cpp | 59 ++++++++++++++++++++++++++++++++++++---------------- src/CLI2.h | 2 +- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/CLI2.cpp b/src/CLI2.cpp index be572bbf2..0e52dcb08 100644 --- a/src/CLI2.cpp +++ b/src/CLI2.cpp @@ -575,15 +575,13 @@ void CLI2::addModifications (const std::string& arg) while (lex.token (lexeme, type)) mods.push_back (lexeme); - // Determine at which argument index does the task command reside + // Determine at which argument index does the task modification command + // reside unsigned int cmdIndex = 0; for (; cmdIndex < _args.size(); ++cmdIndex) { - if (a._lextype == Lexer::Type::separator) - continue; - // Command found, stop iterating. - if (a.hasTag ("CMD")) + if (_args[cmdIndex].hasTag ("CMD")) break; } @@ -597,7 +595,7 @@ void CLI2::addModifications (const std::string& arg) // There are situations where a context filter is applied. This method // determines whether one applies, and if so, applies it. Disqualifiers include: // - filter contains ID or UUID -void CLI2::addContextFilter () +void CLI2::addContext (bool readable, bool writeable) { // Recursion block. if (_context_added) @@ -623,18 +621,43 @@ void CLI2::addContextFilter () } } - // Apply context - Context::getContext ().debug ("Applying context: " + contextName); - std::string contextFilter = Context::getContext ().config.get ("context." + contextName); + // Determine whether we're using readable or writeable context. Readable + // (filtering) takes precedence. + if (readable) { + Context::getContext ().debug ("Applying context: " + contextName); + std::string contextFilter = Context::getContext ().config.get ("context." + contextName + ".read"); - if (contextFilter == "") - Context::getContext ().debug ("Context '" + contextName + "' not defined."); - else - { - _context_added = true; - addFilter (contextFilter); - if (Context::getContext ().verbose ("context")) - Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); + if (contextFilter.empty ()) + { + Context::getContext ().debug ("Specific readable context for '" + contextName + "' not defined. Falling back on generic."); + contextFilter = Context::getContext ().config.get ("context." + contextName); + } + + if (! contextFilter.empty ()) + { + _context_added = true; + addFilter (contextFilter); + if (Context::getContext ().verbose ("context")) + Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); + } + } + else if (writeable) { + Context::getContext ().debug ("Applying context: " + contextName); + std::string contextMods = Context::getContext ().config.get ("context." + contextName + ".write"); + + if (contextMods.empty ()) + { + Context::getContext ().debug ("Specific writeable context for '" + contextName + "' not defined. Falling back on generic."); + contextMods = Context::getContext ().config.get ("context." + contextName); + } + + if (! contextMods.empty ()) + { + _context_added = true; + addModifications (contextMods); + if (Context::getContext ().verbose ("context")) + Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); + } } } @@ -931,7 +954,7 @@ void CLI2::categorizeArgs () std::string command = getCommand (); Command* cmd = Context::getContext ().commands[command]; if (cmd && cmd->uses_context ()) - addContextFilter (); + addContext (cmd->accepts_filter (), cmd->accepts_modifications ()); bool changes = false; bool afterCommand = false; diff --git a/src/CLI2.h b/src/CLI2.h index ce9e386b0..6a276a758 100644 --- a/src/CLI2.h +++ b/src/CLI2.h @@ -74,7 +74,7 @@ public: void analyze (); void addFilter (const std::string& arg); void addModifications (const std::string& arg); - void addContextFilter (); + void addContext (bool readable, bool writeable); void prepareFilter (); const std::vector getWords (); bool canonicalize (std::string&, const std::string&, const std::string&) const;