CLI2: Simplify context determination

Simlifies by offloading portion of the logic into the singleton Context
class (which has nothing to do with the task context per se, just a
implementation singleton).
This commit is contained in:
Tomas Babej 2021-04-03 00:02:11 -04:00
parent 985aab0541
commit 9a380887ee
3 changed files with 54 additions and 41 deletions

View file

@ -602,12 +602,17 @@ void CLI2::addContext (bool readable, bool writeable)
return; return;
// Detect if any context is set, and bail out if not // Detect if any context is set, and bail out if not
std::string contextName = Context::getContext ().config.get ("context"); std::string contextString;
if (contextName == "") if (readable)
{ contextString = Context::getContext ().getTaskContext("read");
Context::getContext ().debug ("No context."); else if (writeable)
contextString = Context::getContext ().getTaskContext("write");
else
return;
// If context is empty, bail out too
if (contextString.empty ())
return; return;
}
// Detect if UUID or ID is set, and bail out // Detect if UUID or ID is set, and bail out
for (auto& a : _args) for (auto& a : _args)
@ -621,44 +626,21 @@ void CLI2::addContext (bool readable, bool writeable)
} }
} }
// Determine whether we're using readable or writeable context. Readable // Apply the context. Readable (filtering) takes precedence.
// (filtering) takes precedence. if (readable)
if (readable) { addFilter (contextString);
Context::getContext ().debug ("Applying context: " + contextName); else if (writeable)
std::string contextFilter = Context::getContext ().config.get ("context." + contextName + ".read"); addModifications (contextString);
if (contextFilter.empty ()) // Inform the user about the application of context
{ if (Context::getContext ().verbose ("context"))
Context::getContext ().debug ("Specific readable context for '" + contextName + "' not defined. Falling back on generic."); Context::getContext ().footnote (format (
contextFilter = Context::getContext ().config.get ("context." + contextName); "Context '{1}' set. Use 'task context none' to remove.",
} Context::getContext ().config.get ("context")
));
if (! contextFilter.empty ()) // Set the block, we don't want to apply context multiple times by accident.
{ _context_added = true;
_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));
}
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -922,6 +922,35 @@ int Context::getHeight ()
return height; return height;
} }
////////////////////////////////////////////////////////////////////////////////
std::string Context::getTaskContext (const std::string& kind, bool fallback /* = true */)
{
// Detect if any context is set, and bail out if not
std::string contextName = config.get ("context");
if (! contextName.empty ())
debug (format ("Applying context '{1}'", contextName));
else
{
debug ("No context set");
return "";
}
// Figure out the context string for this kind (read/write)
std::string contextString = config.get ("context." + contextName + "." + kind);
if (contextString.empty ())
{
debug ("Specific " + kind + " context for '" + contextName + "' not defined. ");
if (fallback)
{
debug ("Falling back on generic.");
contextString = config.get ("context." + contextName);
}
}
debug (format ("Detected context string: {1}", contextString.empty() ? "(empty)" : contextString));
return contextString;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Context::color () bool Context::color ()
{ {

View file

@ -57,6 +57,8 @@ public:
int getWidth (); // determine terminal width int getWidth (); // determine terminal width
int getHeight (); // determine terminal height int getHeight (); // determine terminal height
std::string getTaskContext (const std::string&, bool fallback=true);
const std::vector <std::string> getColumns () const; const std::vector <std::string> getColumns () const;
void getLimits (int&, int&); void getLimits (int&, int&);