CmdCalendar: Code cleanup

This commit is contained in:
Paul Beckingham 2017-01-28 20:31:52 -05:00
parent 5806cddfd3
commit 9e28c62730

View file

@ -64,11 +64,11 @@ int CmdCalendar::execute (std::string& output)
// Each month requires 28 text columns width. See how many will actually // Each month requires 28 text columns width. See how many will actually
// fit. But if a preference is specified, and it fits, use it. // fit. But if a preference is specified, and it fits, use it.
int width = context.getWidth (); auto width = context.getWidth ();
int preferredMonthsPerLine = (context.config.getInteger ("monthsperline")); auto preferredMonthsPerLine = context.config.getInteger ("monthsperline");
int monthsThatFit = width / 26; auto monthsThatFit = width / 26;
int monthsPerLine = monthsThatFit; auto monthsPerLine = monthsThatFit;
if (preferredMonthsPerLine != 0 && preferredMonthsPerLine < monthsThatFit) if (preferredMonthsPerLine != 0 && preferredMonthsPerLine < monthsThatFit)
monthsPerLine = preferredMonthsPerLine; monthsPerLine = preferredMonthsPerLine;
@ -77,12 +77,12 @@ int CmdCalendar::execute (std::string& output)
auto tasks = context.tdb2.pending.get_tasks (); auto tasks = context.tdb2.pending.get_tasks ();
Datetime today; Datetime today;
bool getpendingdate = false; auto getPendingDate = false;
int monthsToDisplay = 1; auto monthsToDisplay = 1;
int mFrom = today.month (); auto mFrom = today.month ();
int yFrom = today.year (); auto yFrom = today.year ();
int mTo = mFrom; auto mTo = mFrom;
int yTo = yFrom; auto yTo = yFrom;
// Defaults. // Defaults.
monthsToDisplay = monthsPerLine; monthsToDisplay = monthsPerLine;
@ -90,12 +90,10 @@ int CmdCalendar::execute (std::string& output)
yFrom = today.year (); yFrom = today.year ();
// Set up a vector of commands (1), for autoComplete. // Set up a vector of commands (1), for autoComplete.
std::vector <std::string> commandNames; std::vector <std::string> commandNames {"calendar"};
commandNames.push_back ("calendar");
// Set up a vector of keywords, for autoComplete. // Set up a vector of keywords, for autoComplete.
std::vector <std::string> keywordNames; std::vector <std::string> keywordNames {"due"};
keywordNames.push_back ("due");
// Set up a vector of months, for autoComplete. // Set up a vector of months, for autoComplete.
std::vector <std::string> monthNames; std::vector <std::string> monthNames;
@ -106,13 +104,11 @@ int CmdCalendar::execute (std::string& output)
std::vector <std::string> matches; std::vector <std::string> matches;
// Look at all args, regardless of sequence. // Look at all args, regardless of sequence.
int argMonth = 0; auto argMonth = 0;
int argYear = 0; auto argYear = 0;
bool argWholeYear = false; auto argWholeYear = false;
std::vector <std::string> words = context.cli2.getWords (); for (auto& arg : context.cli2.getWords ())
for (auto& arg : words)
{ {
// Some version of "calendar". // Some version of "calendar".
if (autoComplete (Lexer::lowerCase (arg), commandNames, matches, context.config.getInteger ("abbreviation.minimum")) == 1) if (autoComplete (Lexer::lowerCase (arg), commandNames, matches, context.config.getInteger ("abbreviation.minimum")) == 1)
@ -120,7 +116,7 @@ int CmdCalendar::execute (std::string& output)
// "due". // "due".
else if (autoComplete (Lexer::lowerCase (arg), keywordNames, matches, context.config.getInteger ("abbreviation.minimum")) == 1) else if (autoComplete (Lexer::lowerCase (arg), keywordNames, matches, context.config.getInteger ("abbreviation.minimum")) == 1)
getpendingdate = true; getPendingDate = true;
// "y". // "y".
else if (Lexer::lowerCase (arg) == "y") else if (Lexer::lowerCase (arg) == "y")
@ -152,7 +148,7 @@ int CmdCalendar::execute (std::string& output)
// Supported combinations: // Supported combinations:
// //
// Command line monthsToDisplay mFrom yFrom getpendingdate // Command line monthsToDisplay mFrom yFrom getPendingDate
// ------------ --------------- ----- ----- -------------- // ------------ --------------- ----- ----- --------------
// cal monthsPerLine today today false // cal monthsPerLine today today false
// cal y 12 today today false // cal y 12 today today false
@ -174,8 +170,8 @@ int CmdCalendar::execute (std::string& output)
yFrom = argYear; yFrom = argYear;
// Now begin the data subset and rendering. // Now begin the data subset and rendering.
int countDueDates = 0; auto countDueDates = 0;
if (getpendingdate == true) if (getPendingDate == true)
{ {
// Find the oldest pending due date. // Find the oldest pending due date.
Datetime oldest (2037, 12, 31); Datetime oldest (2037, 12, 31);
@ -198,8 +194,8 @@ int CmdCalendar::execute (std::string& output)
if (context.config.getBoolean ("calendar.offset")) if (context.config.getBoolean ("calendar.offset"))
{ {
int moffset = context.config.getInteger ("calendar.offset.value") % 12; auto moffset = context.config.getInteger ("calendar.offset.value") % 12;
int yoffset = context.config.getInteger ("calendar.offset.value") / 12; auto yoffset = context.config.getInteger ("calendar.offset.value") / 12;
mFrom += moffset; mFrom += moffset;
yFrom += yoffset; yFrom += yoffset;
if (mFrom < 1) if (mFrom < 1)
@ -222,21 +218,21 @@ int CmdCalendar::execute (std::string& output)
yTo++; yTo++;
} }
int details_yFrom = yFrom; auto details_yFrom = yFrom;
int details_mFrom = mFrom; auto details_mFrom = mFrom;
std::stringstream out; std::stringstream out;
out << '\n'; out << '\n';
while (yFrom < yTo || (yFrom == yTo && mFrom <= mTo)) while (yFrom < yTo || (yFrom == yTo && mFrom <= mTo))
{ {
int nextM = mFrom; auto nextM = mFrom;
int nextY = yFrom; auto nextY = yFrom;
// Print month headers (cheating on the width settings, yes) // Print month headers (cheating on the width settings, yes)
for (int i = 0 ; i < monthsPerLine ; i++) for (int i = 0 ; i < monthsPerLine ; i++)
{ {
std::string month = Datetime::monthName (nextM); auto month = Datetime::monthName (nextM);
// 12345678901234567890123456 = 26 chars wide // 12345678901234567890123456 = 26 chars wide
// ^^ = center // ^^ = center
@ -252,10 +248,10 @@ int CmdCalendar::execute (std::string& output)
// | 31 27 28 29 30 31 | // | 31 27 28 29 30 31 |
// +--------------------------+ // +--------------------------+
int totalWidth = 26; auto totalWidth = 26;
int labelWidth = month.length () + 5; // 5 = " 2009" auto labelWidth = month.length () + 5; // 5 = " 2009"
int leftGap = (totalWidth / 2) - (labelWidth / 2); auto leftGap = (totalWidth / 2) - (labelWidth / 2);
int rightGap = totalWidth - leftGap - labelWidth; auto rightGap = totalWidth - leftGap - labelWidth;
out << std::setw (leftGap) << ' ' out << std::setw (leftGap) << ' '
<< month << month
@ -329,16 +325,16 @@ int CmdCalendar::execute (std::string& output)
} }
Datetime date_after (details_yFrom, details_mFrom, details_dFrom); Datetime date_after (details_yFrom, details_mFrom, details_dFrom);
std::string after = date_after.toString (context.config.get ("dateformat")); auto after = date_after.toString (context.config.get ("dateformat"));
Datetime date_before (yTo, mTo, 1); Datetime date_before (yTo, mTo, 1);
std::string before = date_before.toString (context.config.get ("dateformat")); auto before = date_before.toString (context.config.get ("dateformat"));
// Table with due date information // Table with due date information
if (context.config.get ("calendar.details") == "full") if (context.config.get ("calendar.details") == "full")
{ {
// Assert that 'report' is a valid report. // Assert that 'report' is a valid report.
std::string report = context.config.get ("calendar.details.report"); auto report = context.config.get ("calendar.details.report");
if (context.commands.find (report) == context.commands.end ()) if (context.commands.find (report) == context.commands.end ())
throw std::string (STRING_ERROR_DETAILS); throw std::string (STRING_ERROR_DETAILS);
@ -346,7 +342,7 @@ int CmdCalendar::execute (std::string& output)
// calendar --> taskendar // calendar --> taskendar
// If the executable was "cal" or equivalent, replace it with "task". // If the executable was "cal" or equivalent, replace it with "task".
std::string executable = context.cli2._original_args[0].attribute ("raw"); auto executable = context.cli2._original_args[0].attribute ("raw");
auto cal = executable.find ("cal"); auto cal = executable.find ("cal");
if (cal != std::string::npos) if (cal != std::string::npos)
executable = executable.substr (0, cal) + PACKAGE; executable = executable.substr (0, cal) + PACKAGE;
@ -379,17 +375,17 @@ int CmdCalendar::execute (std::string& output)
if (it.first.substr (0, 8) == "holiday.") if (it.first.substr (0, 8) == "holiday.")
if (it.first.substr (it.first.size () - 4) == "name") if (it.first.substr (it.first.size () - 4) == "name")
{ {
std::string holName = context.config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".name"); auto holName = context.config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".name");
std::string holDate = context.config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date"); auto holDate = context.config.get ("holiday." + it.first.substr (8, it.first.size () - 13) + ".date");
Datetime hDate (holDate.c_str (), context.config.get ("dateformat.holiday")); Datetime hDate (holDate.c_str (), context.config.get ("dateformat.holiday"));
if (date_after < hDate && hDate < date_before) if (date_after < hDate && hDate < date_before)
hm[hDate.toEpoch()].push_back(holName); hm[hDate.toEpoch()].push_back (holName);
} }
std::string format = context.config.get ("report." + auto format = context.config.get ("report." +
context.config.get ("calendar.details.report") + context.config.get ("calendar.details.report") +
".dateformat"); ".dateformat");
if (format == "") if (format == "")
format = context.config.get ("dateformat.report"); format = context.config.get ("dateformat.report");
if (format == "") if (format == "")
@ -397,12 +393,12 @@ int CmdCalendar::execute (std::string& output)
for (auto& hm_it : hm) for (auto& hm_it : hm)
{ {
std::vector <std::string> v = hm_it.second; auto v = hm_it.second;
Datetime hDate (hm_it.first); Datetime hDate (hm_it.first);
std::string d = hDate.toString (format); auto d = hDate.toString (format);
for (size_t i = 0; i < v.size(); i++) for (size_t i = 0; i < v.size(); i++)
{ {
int row = holTable.addRow (); auto row = holTable.addRow ();
holTable.set (row, 0, d); holTable.set (row, 0, d);
holTable.set (row, 1, v[i]); holTable.set (row, 1, v[i]);
} }
@ -427,7 +423,7 @@ std::string CmdCalendar::renderMonths (
int monthsPerLine) int monthsPerLine)
{ {
// What day of the week does the user consider the first? // What day of the week does the user consider the first?
int weekStart = Datetime::dayOfWeek (context.config.get ("weekstart")); auto weekStart = Datetime::dayOfWeek (context.config.get ("weekstart"));
if (weekStart != 0 && weekStart != 1) if (weekStart != 0 && weekStart != 1)
throw std::string (STRING_CMD_CAL_SUN_MON); throw std::string (STRING_CMD_CAL_SUN_MON);
@ -492,7 +488,7 @@ std::string CmdCalendar::renderMonths (
daysInMonth.push_back (Datetime::daysInMonth (thisYear, thisMonth++)); daysInMonth.push_back (Datetime::daysInMonth (thisYear, thisMonth++));
} }
int row = 0; auto row = 0;
Color color_today (context.config.get ("color.calendar.today")); Color color_today (context.config.get ("color.calendar.today"));
Color color_due (context.config.get ("color.calendar.due")); Color color_due (context.config.get ("color.calendar.due"));
@ -513,8 +509,8 @@ std::string CmdCalendar::renderMonths (
for (int d = 1; d <= daysInMonth[mpl]; ++d) for (int d = 1; d <= daysInMonth[mpl]; ++d)
{ {
Datetime temp (years[mpl], months[mpl], d); Datetime temp (years[mpl], months[mpl], d);
int dow = temp.dayOfWeek (); auto dow = temp.dayOfWeek ();
int woy = temp.week (); auto woy = temp.week ();
if (context.config.getBoolean ("displayweeknumber")) if (context.config.getBoolean ("displayweeknumber"))
view.set (row, view.set (row,
@ -524,9 +520,9 @@ std::string CmdCalendar::renderMonths (
color_weeknumber); color_weeknumber);
// Calculate column id. // Calculate column id.
int thisCol = dow + // 0 = Sunday auto thisCol = dow + // 0 = Sunday
(weekStart == 1 ? 0 : 1) + // Offset for weekStart (weekStart == 1 ? 0 : 1) + // Offset for weekStart
(8 * mpl); // Columns in 1 month (8 * mpl); // Columns in 1 month
if (thisCol == (8 * mpl)) if (thisCol == (8 * mpl))
thisCol += 7; thisCol += 7;