From b67b2ccc963327beaa6c029aef73b034d429408b Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 27 Jun 2015 15:53:24 -0400 Subject: [PATCH] CLI2: Added ::addContextFilter, and supporting processing --- src/CLI2.cpp | 94 ++++++++++++++++++++------------------ src/CLI2.h | 4 +- src/Filter.cpp | 5 +- src/commands/CmdCustom.cpp | 3 -- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/CLI2.cpp b/src/CLI2.cpp index 0f2f36b99..0487280c2 100644 --- a/src/CLI2.cpp +++ b/src/CLI2.cpp @@ -414,51 +414,6 @@ void CLI2::analyze () context.debug (dump ("CLI2::analyze end")); } -/* -//////////////////////////////////////////////////////////////////////////////// -// 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 () -{ - // Detect if any context is set, and bail out if not - std::string contextName = context.config.get ("context"); - if (contextName == "") - { - context.debug ("No context applied."); - return; - } - - // Detect if UUID or ID is set, and bail out - for (auto& a : _args) - { - // TODO This looks wrong. - if (a.hasTag ("FILTER") && - a.hasTag ("ATTRIBUTE") && - ! a.hasTag ("TERMINATED") && - ! a.hasTag ("WORD") && - (a.attribute ("raw") == "id" || a.attribute ("raw") == "uuid")) - { - context.debug (format ("UUID/ID lexeme found '{1}', not applying context.", a.attribute ("raw"))); - return; - } - } - - // Apply context - context.debug ("Applying context: " + contextName); - std::string contextFilter = context.config.get ("context." + contextName); - - if (contextFilter == "") - context.debug ("Context '" + contextName + "' not defined."); - else - { - addRawFilter ("( " + contextFilter + " )"); - if (context.verbose ("context")) - context.footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); - } -} -*/ - //////////////////////////////////////////////////////////////////////////////// // Process raw string. void CLI2::addFilter (const std::string& arg) @@ -480,6 +435,52 @@ void CLI2::addFilter (const std::string& arg) analyze (); } +//////////////////////////////////////////////////////////////////////////////// +// 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 () +{ + // Detect if any context is set, and bail out if not + std::string contextName = context.config.get ("context"); + if (contextName == "") + { + context.debug ("No context applied."); + return; + } + +/* + // Detect if UUID or ID is set, and bail out + for (auto& a : _args) + { + // TODO This is needed, but the parsing is not yet complete, so the logic + // below is not valid. + if (a.hasTag ("FILTER") && + a.hasTag ("ATTRIBUTE") && + ! a.hasTag ("TERMINATED") && + ! a.hasTag ("WORD") && + (a.attribute ("raw") == "id" || a.attribute ("raw") == "uuid")) + { + context.debug (format ("UUID/ID lexeme found '{1}', not applying context.", a.attribute ("raw"))); + return; + } + } +*/ + + // Apply context + context.debug ("Applying context: " + contextName); + std::string contextFilter = context.config.get ("context." + contextName); + + if (contextFilter == "") + context.debug ("Context '" + contextName + "' not defined."); + else + { + addFilter (contextFilter); + if (context.verbose ("context")) + context.footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); + } +} + //////////////////////////////////////////////////////////////////////////////// // Parse the command line, identifiying filter components, expanding syntactic // sugar as necessary. @@ -489,6 +490,9 @@ void CLI2::prepareFilter (bool applyContext) _id_ranges.clear (); _uuid_list.clear (); + if (applyContext) + addContextFilter (); + // Classify FILTER and MODIFICATION args, based on CMD and READCMD/WRITECMD. bool changes = false; bool foundCommand = false; diff --git a/src/CLI2.h b/src/CLI2.h index 3948ec5ea..4e768fb4c 100644 --- a/src/CLI2.h +++ b/src/CLI2.h @@ -73,10 +73,8 @@ public: void add (const std::string&); void analyze (); -/* - void addContextFilter (); -*/ void addFilter (const std::string& arg); + void addContextFilter (); void prepareFilter (bool applyContext = true); const std::vector getWords (bool filtered = true); bool canonicalize (std::string&, const std::string&, const std::string&) const; diff --git a/src/Filter.cpp b/src/Filter.cpp index a83aacbec..c2221f6a2 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -73,7 +73,8 @@ void Filter::subset (const std::vector & input, std::vector & output context.timer_filter.start (); _startCount = (int) input.size (); -// context.cli2.prepareFilter (applyContext); + context.cli2.prepareFilter (applyContext); + std::vector > precompiled; for (auto& a : context.cli2._args) if (a.hasTag ("FILTER")) @@ -118,6 +119,8 @@ void Filter::subset (std::vector & output, bool applyContext /* = true */) { context.timer_filter.start (); + context.cli2.prepareFilter (applyContext); + std::vector > precompiled; for (auto& a : context.cli2._args) if (a.hasTag ("FILTER")) diff --git a/src/commands/CmdCustom.cpp b/src/commands/CmdCustom.cpp index da672e099..e2b9f63ce 100644 --- a/src/commands/CmdCustom.cpp +++ b/src/commands/CmdCustom.cpp @@ -81,10 +81,7 @@ int CmdCustom::execute (std::string& output) // Add the report filter to any existing filter. if (reportFilter != "") - { context.cli2.addFilter (reportFilter); - context.cli2.prepareFilter (); - } // Apply filter. handleRecurrence ();