Made CmdCalendar.cpp less redundant by only calling

Context::getContext().config once per method.
This commit is contained in:
DanielMowitz 2021-04-21 12:54:34 +02:00 committed by Tomas Babej
parent 45c67fef6e
commit f703bd5a74

View file

@ -59,10 +59,12 @@ int CmdCalendar::execute (std::string& output)
{
int rc = 0;
auto& config = Context::getContext ().config;
// Each month requires 28 text columns width. See how many will actually
// fit. But if a preference is specified, and it fits, use it.
auto width = Context::getContext ().getWidth ();
auto preferredMonthsPerLine = Context::getContext ().config.getInteger ("monthsperline");
auto preferredMonthsPerLine = config.getInteger ("monthsperline");
auto monthsThatFit = width / 26;
auto monthsPerLine = monthsThatFit;
@ -109,11 +111,11 @@ int CmdCalendar::execute (std::string& output)
for (auto& arg : Context::getContext ().cli2.getWords ())
{
// Some version of "calendar".
if (autoComplete (Lexer::lowerCase (arg), commandNames, matches, Context::getContext ().config.getInteger ("abbreviation.minimum")) == 1)
if (autoComplete (Lexer::lowerCase (arg), commandNames, matches, config.getInteger ("abbreviation.minimum")) == 1)
continue;
// "due".
else if (autoComplete (Lexer::lowerCase (arg), keywordNames, matches, Context::getContext ().config.getInteger ("abbreviation.minimum")) == 1)
else if (autoComplete (Lexer::lowerCase (arg), keywordNames, matches, config.getInteger ("abbreviation.minimum")) == 1)
getPendingDate = true;
// "y".
@ -133,7 +135,7 @@ int CmdCalendar::execute (std::string& output)
}
// "January" etc.
else if (autoComplete (Lexer::lowerCase (arg), monthNames, matches, Context::getContext ().config.getInteger ("abbreviation.minimum")) == 1)
else if (autoComplete (Lexer::lowerCase (arg), monthNames, matches, config.getInteger ("abbreviation.minimum")) == 1)
{
argMonth = Datetime::monthOfYear (matches[0]);
if (argMonth == -1)
@ -195,10 +197,10 @@ int CmdCalendar::execute (std::string& output)
}
}
if (Context::getContext ().config.getBoolean ("calendar.offset"))
if (config.getBoolean ("calendar.offset"))
{
auto moffset = Context::getContext ().config.getInteger ("calendar.offset.value") % 12;
auto yoffset = Context::getContext ().config.getInteger ("calendar.offset.value") / 12;
auto moffset = config.getInteger ("calendar.offset.value") % 12;
auto yoffset = config.getInteger ("calendar.offset.value") / 12;
mFrom += moffset;
yFrom += yoffset;
if (mFrom < 1)
@ -282,15 +284,15 @@ int CmdCalendar::execute (std::string& output)
}
}
Color color_today (Context::getContext ().config.get ("color.calendar.today"));
Color color_due (Context::getContext ().config.get ("color.calendar.due"));
Color color_duetoday (Context::getContext ().config.get ("color.calendar.due.today"));
Color color_overdue (Context::getContext ().config.get ("color.calendar.overdue"));
Color color_weekend (Context::getContext ().config.get ("color.calendar.weekend"));
Color color_holiday (Context::getContext ().config.get ("color.calendar.holiday"));
Color color_weeknumber (Context::getContext ().config.get ("color.calendar.weeknumber"));
Color color_today (config.get ("color.calendar.today"));
Color color_due (config.get ("color.calendar.due"));
Color color_duetoday (config.get ("color.calendar.due.today"));
Color color_overdue (config.get ("color.calendar.overdue"));
Color color_weekend (config.get ("color.calendar.weekend"));
Color color_holiday (config.get ("color.calendar.holiday"));
Color color_weeknumber (config.get ("color.calendar.weeknumber"));
if (Context::getContext ().color () && Context::getContext ().config.getBoolean ("calendar.legend"))
if (Context::getContext ().color () && config.getBoolean ("calendar.legend"))
out << "Legend: "
<< color_today.colorize ("today")
<< ", "
@ -309,7 +311,7 @@ int CmdCalendar::execute (std::string& output)
<< optionalBlankLine ()
<< '\n';
if (Context::getContext ().config.get ("calendar.details") == "full" || Context::getContext ().config.get ("calendar.holidays") == "full")
if (config.get ("calendar.details") == "full" || config.get ("calendar.holidays") == "full")
{
--details_mFrom;
if (details_mFrom == 0)
@ -327,16 +329,16 @@ int CmdCalendar::execute (std::string& output)
}
Datetime date_after (details_yFrom, details_mFrom, details_dFrom);
auto after = date_after.toString (Context::getContext ().config.get ("dateformat"));
auto after = date_after.toString (config.get ("dateformat"));
Datetime date_before (yTo, mTo, 1);
auto before = date_before.toString (Context::getContext ().config.get ("dateformat"));
auto before = date_before.toString (config.get ("dateformat"));
// Table with due date information
if (Context::getContext ().config.get ("calendar.details") == "full")
if (config.get ("calendar.details") == "full")
{
// Assert that 'report' is a valid report.
auto report = Context::getContext ().config.get ("calendar.details.report");
auto report = config.get ("calendar.details.report");
if (Context::getContext ().commands.find (report) == Context::getContext ().commands.end ())
throw std::string ("The setting 'calendar.details.report' must contain a single report name.");
@ -366,7 +368,7 @@ int CmdCalendar::execute (std::string& output)
}
// Table with holiday information
if (Context::getContext ().config.get ("calendar.holidays") == "full")
if (config.get ("calendar.holidays") == "full")
{
Table holTable;
holTable.width (Context::getContext ().getWidth ());
@ -375,26 +377,26 @@ int CmdCalendar::execute (std::string& output)
setHeaderUnderline (holTable);
std::map <time_t, std::vector<std::string>> hm; // we need to store multiple holidays per day
for (auto& it : Context::getContext ().config)
for (auto& it : config)
if (it.first.substr (0, 8) == "holiday.")
if (it.first.substr (it.first.size () - 4) == "name")
{
auto holName = Context::getContext ().config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".name");
if (Context::getContext ().config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date"))
auto holName = config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".name");
if (config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date"))
{
auto holDate = Context::getContext ().config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date");
Datetime hDate (holDate.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
auto holDate = config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date");
Datetime hDate (holDate.c_str (), config.get ("dateformat.holiday"));
if (date_after < hDate && hDate < date_before)
hm[hDate.toEpoch()].push_back (holName);
}
if (Context::getContext ().config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".start") &&
Context::getContext ().config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".end"))
if (config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".start") &&
config.has ("holiday." + it.first.substr (8, it.first.size () - 13) + ".end"))
{
auto holStart = Context::getContext ().config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".start");
auto holEnd = Context::getContext ().config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".end");
Datetime hStart (holStart.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
Datetime hEnd (holEnd.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
auto holStart = config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".start");
auto holEnd = config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".end");
Datetime hStart (holStart.c_str (), config.get ("dateformat.holiday"));
Datetime hEnd (holEnd.c_str (), config.get ("dateformat.holiday"));
if (date_after < hStart && hStart < date_before)
hm[hStart.toEpoch()].push_back ("Start of " + holName);
@ -403,13 +405,13 @@ int CmdCalendar::execute (std::string& output)
}
}
auto format = Context::getContext ().config.get ("report." +
Context::getContext ().config.get ("calendar.details.report") +
auto format = config.get ("report." +
config.get ("calendar.details.report") +
".dateformat");
if (format == "")
format = Context::getContext ().config.get ("dateformat.report");
format = config.get ("dateformat.report");
if (format == "")
format = Context::getContext ().config.get ("dateformat");
format = config.get ("dateformat");
for (auto& hm_it : hm)
{
@ -442,8 +444,11 @@ std::string CmdCalendar::renderMonths (
std::vector <Task>& all,
int monthsPerLine)
{
auto& config = Context::getContext ().config;
// What day of the week does the user consider the first?
auto weekStart = Datetime::dayOfWeek (Context::getContext ().config.get ("weekstart"));
auto weekStart = Datetime::dayOfWeek (config.get ("weekstart"));
if (weekStart != 0 && weekStart != 1)
throw std::string ("The 'weekstart' configuration variable may only contain 'Sunday' or 'Monday'.");
@ -508,13 +513,13 @@ std::string CmdCalendar::renderMonths (
auto row = 0;
Color color_today (Context::getContext ().config.get ("color.calendar.today"));
Color color_due (Context::getContext ().config.get ("color.calendar.due"));
Color color_duetoday (Context::getContext ().config.get ("color.calendar.due.today"));
Color color_overdue (Context::getContext ().config.get ("color.calendar.overdue"));
Color color_weekend (Context::getContext ().config.get ("color.calendar.weekend"));
Color color_holiday (Context::getContext ().config.get ("color.calendar.holiday"));
Color color_weeknumber (Context::getContext ().config.get ("color.calendar.weeknumber"));
Color color_today (config.get ("color.calendar.today"));
Color color_due (config.get ("color.calendar.due"));
Color color_duetoday (config.get ("color.calendar.due.today"));
Color color_overdue (config.get ("color.calendar.overdue"));
Color color_weekend (config.get ("color.calendar.weekend"));
Color color_holiday (config.get ("color.calendar.holiday"));
Color color_weeknumber (config.get ("color.calendar.weeknumber"));
// Loop through months to be added on this line.
for (int mpl = 0; mpl < monthsPerLine ; mpl++)
@ -530,7 +535,7 @@ std::string CmdCalendar::renderMonths (
auto dow = date.dayOfWeek ();
auto woy = date.week ();
if (Context::getContext ().config.getBoolean ("displayweeknumber"))
if (config.getBoolean ("displayweeknumber"))
view.set (row,
(8 * mpl),
// Make sure the week number is always 4 columns, space-padded.
@ -556,27 +561,27 @@ std::string CmdCalendar::renderMonths (
cellColor.blend (color_weekend);
// colorize holidays
if (Context::getContext ().config.get ("calendar.holidays") != "none")
if (config.get ("calendar.holidays") != "none")
{
for (auto& hol : Context::getContext ().config)
for (auto& hol : config)
{
if (hol.first.substr (0, 8) == "holiday.")
{
if (hol.first.substr (hol.first.size () - 4) == "date")
{
auto value = hol.second;
Datetime holDate (value.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
Datetime holDate (value.c_str (), config.get ("dateformat.holiday"));
if (holDate.sameDay (date))
cellColor.blend (color_holiday);
}
if (hol.first.substr (hol.first.size () - 5) == "start" &&
Context::getContext ().config.has ("holiday." + hol.first.substr (8, hol.first.size () - 14) + ".end"))
config.has ("holiday." + hol.first.substr (8, hol.first.size () - 14) + ".end"))
{
auto start = hol.second;
auto end = Context::getContext ().config.get ("holiday." + hol.first.substr (8, hol.first.size () - 14) + ".end");
Datetime holStart (start.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
Datetime holEnd (end.c_str (), Context::getContext ().config.get ("dateformat.holiday"));
auto end = config.get ("holiday." + hol.first.substr (8, hol.first.size () - 14) + ".end");
Datetime holStart (start.c_str (), config.get ("dateformat.holiday"));
Datetime holEnd (end.c_str (), config.get ("dateformat.holiday"));
if (holStart <= date &&
date <= holEnd)
cellColor.blend (color_holiday);
@ -590,9 +595,9 @@ std::string CmdCalendar::renderMonths (
cellColor.blend (color_today);
// colorize due tasks
if (Context::getContext ().config.get ("calendar.details") != "none")
if (config.get ("calendar.details") != "none")
{
Context::getContext ().config.set ("due", 0);
config.set ("due", 0);
for (auto& task : all)
{
auto status = task.getStatus ();