mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-27 10:07:19 +02:00
CLI2: Generalize method to add context to support writeable context
This commit is contained in:
parent
5c3cf0f438
commit
5716f5abb7
2 changed files with 42 additions and 19 deletions
59
src/CLI2.cpp
59
src/CLI2.cpp
|
@ -575,15 +575,13 @@ void CLI2::addModifications (const std::string& arg)
|
||||||
while (lex.token (lexeme, type))
|
while (lex.token (lexeme, type))
|
||||||
mods.push_back (lexeme);
|
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;
|
unsigned int cmdIndex = 0;
|
||||||
for (; cmdIndex < _args.size(); ++cmdIndex)
|
for (; cmdIndex < _args.size(); ++cmdIndex)
|
||||||
{
|
{
|
||||||
if (a._lextype == Lexer::Type::separator)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Command found, stop iterating.
|
// Command found, stop iterating.
|
||||||
if (a.hasTag ("CMD"))
|
if (_args[cmdIndex].hasTag ("CMD"))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -597,7 +595,7 @@ void CLI2::addModifications (const std::string& arg)
|
||||||
// There are situations where a context filter is applied. This method
|
// There are situations where a context filter is applied. This method
|
||||||
// determines whether one applies, and if so, applies it. Disqualifiers include:
|
// determines whether one applies, and if so, applies it. Disqualifiers include:
|
||||||
// - filter contains ID or UUID
|
// - filter contains ID or UUID
|
||||||
void CLI2::addContextFilter ()
|
void CLI2::addContext (bool readable, bool writeable)
|
||||||
{
|
{
|
||||||
// Recursion block.
|
// Recursion block.
|
||||||
if (_context_added)
|
if (_context_added)
|
||||||
|
@ -623,18 +621,43 @@ void CLI2::addContextFilter ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply context
|
// Determine whether we're using readable or writeable context. Readable
|
||||||
Context::getContext ().debug ("Applying context: " + contextName);
|
// (filtering) takes precedence.
|
||||||
std::string contextFilter = Context::getContext ().config.get ("context." + contextName);
|
if (readable) {
|
||||||
|
Context::getContext ().debug ("Applying context: " + contextName);
|
||||||
|
std::string contextFilter = Context::getContext ().config.get ("context." + contextName + ".read");
|
||||||
|
|
||||||
if (contextFilter == "")
|
if (contextFilter.empty ())
|
||||||
Context::getContext ().debug ("Context '" + contextName + "' not defined.");
|
{
|
||||||
else
|
Context::getContext ().debug ("Specific readable context for '" + contextName + "' not defined. Falling back on generic.");
|
||||||
{
|
contextFilter = Context::getContext ().config.get ("context." + contextName);
|
||||||
_context_added = true;
|
}
|
||||||
addFilter (contextFilter);
|
|
||||||
if (Context::getContext ().verbose ("context"))
|
if (! contextFilter.empty ())
|
||||||
Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName));
|
{
|
||||||
|
_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 ();
|
std::string command = getCommand ();
|
||||||
Command* cmd = Context::getContext ().commands[command];
|
Command* cmd = Context::getContext ().commands[command];
|
||||||
if (cmd && cmd->uses_context ())
|
if (cmd && cmd->uses_context ())
|
||||||
addContextFilter ();
|
addContext (cmd->accepts_filter (), cmd->accepts_modifications ());
|
||||||
|
|
||||||
bool changes = false;
|
bool changes = false;
|
||||||
bool afterCommand = false;
|
bool afterCommand = false;
|
||||||
|
|
|
@ -74,7 +74,7 @@ public:
|
||||||
void analyze ();
|
void analyze ();
|
||||||
void addFilter (const std::string& arg);
|
void addFilter (const std::string& arg);
|
||||||
void addModifications (const std::string& arg);
|
void addModifications (const std::string& arg);
|
||||||
void addContextFilter ();
|
void addContext (bool readable, bool writeable);
|
||||||
void prepareFilter ();
|
void prepareFilter ();
|
||||||
const std::vector <std::string> getWords ();
|
const std::vector <std::string> getWords ();
|
||||||
bool canonicalize (std::string&, const std::string&, const std::string&) const;
|
bool canonicalize (std::string&, const std::string&, const std::string&) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue