add total commits counters default tip

Tue, 25 Feb 2025 18:46:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 25 Feb 2025 18:46:17 +0100
changeset 44
de22ded6d50a
parent 43
f58fd8461e10

add total commits counters

fixes #605

src/heatmap.cpp file | annotate | diff | comparison | revisions
src/heatmap.h file | annotate | diff | comparison | revisions
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/heatmap.cpp	Tue Feb 25 18:22:55 2025 +0100
+++ b/src/heatmap.cpp	Tue Feb 25 18:46:17 2025 +0100
@@ -51,3 +51,16 @@
         }]++;
     }
 }
+
+std::array<unsigned int, 12> fm::heatmap::commits_per_month(
+    const std::string& repo,
+    const std::string& author,
+    chrono::year year
+) const {
+    std::array<unsigned int, 12> result{};
+    for (auto&& [ymd, commits] : m_heatmap.at(repo).at(author)) {
+        if (ymd.year() != year) continue;
+        result[static_cast<unsigned int>(ymd.month())-1] += commits;
+    }
+    return result;
+}
--- a/src/heatmap.h	Tue Feb 25 18:22:55 2025 +0100
+++ b/src/heatmap.h	Tue Feb 25 18:46:17 2025 +0100
@@ -26,6 +26,7 @@
 #define HEATMAP_H
 
 #include <map>
+#include <array>
 #include <string>
 #include <chrono>
 
@@ -53,6 +54,12 @@
     [[nodiscard]] const auto& data() const {
         return m_heatmap;
     }
+
+    [[nodiscard]] std::array<unsigned int, 12> commits_per_month(
+        const std::string &repo,
+        const std::string& author,
+        std::chrono::year year
+    ) const;
 };
 
 }
--- a/src/html.cpp	Tue Feb 25 18:22:55 2025 +0100
+++ b/src/html.cpp	Tue Feb 25 18:46:17 2025 +0100
@@ -134,17 +134,19 @@
     }
 }
 
-void html::h1(const std::string& heading) {
+void html::heading_repo(const std::string& repo) {
     indent();
-    printf("<h1>%s</h1>\n", encode(heading).c_str());
+    printf("<h1>%s</h1>\n", encode(repo).c_str());
 }
 
-void html::h2(const std::string& heading) {
+void html::heading_author(const std::string& author, unsigned int total_commits) {
     indent();
-    printf("<h2>%s</h2>\n", encode(heading).c_str());
+    printf("<h2 title=\"Total commits: %u\">%s</h2>\n",
+        total_commits,
+        encode(author).c_str());
 }
 
-void html::table_begin(year y) {
+void html::table_begin(year y, const std::array<unsigned int, 12> &commits_per_month) {
     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] = {};
@@ -169,7 +171,8 @@
     puts("<th></th>");
     for (unsigned i = 0 ; i < 12 ; i++) {
         indent();
-        printf("<th scope=\"col\" colspan=\"%d\">%s</th>\n", colspans[i], months[i]);
+        printf("<th scope=\"col\" title=\"Total commits: %u\" colspan=\"%d\">%s</th>\n",
+            commits_per_month[i], colspans[i], months[i]);
     }
     indent(-1);
     puts("</tr>");
--- a/src/html.h	Tue Feb 25 18:22:55 2025 +0100
+++ b/src/html.h	Tue Feb 25 18:46:17 2025 +0100
@@ -27,6 +27,7 @@
 
 #include <string>
 #include <chrono>
+#include <array>
 
 namespace html {
 
@@ -35,9 +36,9 @@
     void open(bool fragment);
     void close(bool fragment);
 
-    void h1(const std::string& heading);
-    void h2(const std::string& heading);
-    void table_begin(std::chrono::year y);
+    void heading_repo(const std::string& repo);
+    void heading_author(const std::string& author, unsigned int total_commits);
+    void table_begin(std::chrono::year y, const std::array<unsigned int, 12> &commits_per_month);
     void table_end();
     void row_begin(unsigned int row);
     void row_end();
--- a/src/main.cpp	Tue Feb 25 18:22:55 2025 +0100
+++ b/src/main.cpp	Tue Feb 25 18:46:17 2025 +0100
@@ -34,6 +34,8 @@
 #include <cstring>
 #include <cerrno>
 
+#include <numeric>
+
 using namespace std::chrono;
 
 static constexpr auto program_version = "1.1.0 (dev)";
@@ -266,11 +268,14 @@
         for (const auto &[author, entries] : authors) {
             if (settings.exclude_author(author)) continue;
             if (!h1_rendered) {
-                html::h1(repo);
+                html::heading_repo(repo);
                 h1_rendered = true;
             }
-            html::h2(author);
-            html::table_begin(report_year);
+
+            const auto commits_per_month = heatmap.commits_per_month(repo, author, report_year);
+            const auto total_commits = std::accumulate(commits_per_month.begin(), commits_per_month.end(), 0u);
+            html::heading_author(author, total_commits);
+            html::table_begin(report_year, commits_per_month);
 
             // initialize counters
             unsigned column = 0, row = 0;

mercurial