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

@ -922,6 +922,35 @@ int Context::getHeight ()
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 ()
{