Sat, 01 Feb 2025 14:19:36 +0100
improve readability
src/html.cpp | file | annotate | diff | comparison | revisions | |
src/html.h | file | annotate | diff | comparison | revisions | |
src/main.cpp | file | annotate | diff | comparison | revisions |
--- a/src/html.cpp Sat Feb 01 14:11:46 2025 +0100 +++ b/src/html.cpp Sat Feb 01 14:19:36 2025 +0100 @@ -26,8 +26,7 @@ #include <cstdio> -namespace chrono = std::chrono; -using chrono::operator ""d; +using namespace std::chrono; namespace html { static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; @@ -135,22 +134,22 @@ printf("<h2>%s</h2>\n", encode(heading).c_str()); } -void html::table_begin(int year) { +void html::table_begin(year y) { static constexpr const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // compute the column spans, first unsigned colspans[12] = {}; { unsigned total_cols = 0; - chrono::sys_days day{chrono::year_month_day{chrono::year{year}, chrono::January, 1d}}; - if (chrono::weekday{day}.iso_encoding() != 1) { + sys_days day{year_month_day{y, January, 1d}}; + if (weekday{day}.iso_encoding() != 1) { colspans[0] = 1; total_cols = 1; } for (unsigned col = 0; col < 12; ++col) { - while (total_cols < html::columns && chrono::year_month_day{day}.month() <= chrono::month{col + 1}) { + while (total_cols < html::columns && year_month_day{day}.month() <= month{col + 1}) { ++total_cols; ++colspans[col]; - day += chrono::days{7}; + day += days{7}; } } } @@ -193,7 +192,7 @@ puts("<td class=\"out-of-range\"></td>"); } -void html::cell(std::chrono::year_month_day ymd, unsigned commits) { +void html::cell(year_month_day ymd, unsigned commits) { const char *color_class; if (commits == 0) { color_class = "zero-commits"; @@ -211,7 +210,7 @@ indent(); printf("<td class=\"%s\" title=\"%s, %d-%u-%u: %u commits\"></td>\n", color_class, - weekdays[chrono::weekday(ymd).iso_encoding() - 1], + weekdays[weekday(ymd).iso_encoding() - 1], static_cast<int>(ymd.year()), static_cast<unsigned>(ymd.month()), static_cast<unsigned>(ymd.day()),
--- a/src/html.h Sat Feb 01 14:11:46 2025 +0100 +++ b/src/html.h Sat Feb 01 14:19:36 2025 +0100 @@ -37,9 +37,9 @@ void h1(const std::string& heading); void h2(const std::string& heading); - void table_begin(int year); + void table_begin(std::chrono::year y); void table_end(); - void row_begin(unsigned int weekday); + void row_begin(unsigned int row); void row_end(); void cell_out_of_range(); void cell(std::chrono::year_month_day ymd, unsigned commits);
--- a/src/main.cpp Sat Feb 01 14:11:46 2025 +0100 +++ b/src/main.cpp Sat Feb 01 14:19:36 2025 +0100 @@ -34,8 +34,7 @@ #include <cstring> #include <cerrno> -namespace chrono = std::chrono; -using chrono::operator ""d; +using namespace std::chrono; static void print_help() { fputs( @@ -179,14 +178,13 @@ } // determine our reporting range - int year; - if (settings.year == fm::settings_current_year) { - year = static_cast<int>(chrono::year_month_day{chrono::floor<chrono::days>(chrono::system_clock::now())}.year()); - } else { - year = settings.year; - } - chrono::year_month_day report_begin{chrono::year{year}, chrono::January, 1d}; - chrono::year_month_day report_end{chrono::year{year}, chrono::December, 31d}; + year report_year{ + settings.year == fm::settings_current_year + ? year_month_day{floor<days>(system_clock::now())}.year() + : year{settings.year} + }; + year_month_day report_begin{report_year, January, 1d}; + year_month_day report_end{report_year, December, 31d}; // read the commit logs fm::heatmap heatmap; @@ -198,7 +196,7 @@ if (repo.type == fm::HG) { proc.setbin(settings.hg); if (proc.exec_log({"log", - "--date", std::format("{0}-01-01 to {0}-12-31", year), + "--date", std::format("{0}-01-01 to {0}-12-31", report_year), "--template", "{author}#{date|shortdate}\n"})) { fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); return EXIT_FAILURE; @@ -207,8 +205,8 @@ } else { proc.setbin(settings.git); if (proc.exec_log({"log", - "--since", std::format("{0}-01-01", year), - "--until", std::format("{0}-12-31", year), + "--since", std::format("{0}-01-01", report_year), + "--until", std::format("{0}-12-31", report_year), "--format=tformat:%an <%ae>#%cs"})) { fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); return EXIT_FAILURE; @@ -222,13 +220,13 @@ html::h1(repo); for (const auto &[author, entries] : authors) { html::h2(author); - html::table_begin(year); + html::table_begin(report_year); // initialize counters unsigned column = 0, row = 0; // initialize first day (which must be a Monday, possibly the year before) - chrono::sys_days day_to_check = chrono::January / chrono::Monday[1] / chrono::year{year}; + sys_days day_to_check = January / Monday[1] / report_year; // remember the starting point auto start = day_to_check; @@ -240,7 +238,7 @@ // check if we need to add blank cells while (day_to_check < report_begin) { html::cell_out_of_range(); - day_to_check += chrono::days{7}; + day_to_check += days{7}; column++; } @@ -253,7 +251,7 @@ html::cell(day_to_check, find_result->second); } // advance seven days and one column - day_to_check += chrono::days{7}; + day_to_check += days{7}; column++; } // fill remaining columns with blank cells @@ -268,7 +266,7 @@ if (++row == 7) break; // otherwise, advance the starting point by one day, reset, and begin a new row - start += chrono::days{1}; + start += days{1}; day_to_check = start; column =0; }