add color coding

Fri, 31 Jan 2025 22:48:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 31 Jan 2025 22:48:43 +0100
changeset 7
d0f77dd2da42
parent 6
1040ba37d4c9
child 8
6a2e20a4a8ff

add color coding

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	Fri Jan 31 22:27:28 2025 +0100
+++ b/src/html.cpp	Fri Jan 31 22:48:43 2025 +0100
@@ -26,10 +26,11 @@
 
 #include <cstdio>
 
-#include <chrono>
 namespace chrono = std::chrono;
 
 namespace html {
+    static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
+
     static unsigned indentation;
     static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
     static void indent(int change = 0) {
@@ -89,6 +90,30 @@
             table.heatmap td.out-of-range {
                 background-color: gray;
             }
+
+            table.heatmap td.zero-commits {
+                background-color: white;
+            }
+
+            table.heatmap td.one-commit {
+                background-color: #80E7A0;
+            }
+
+            table.heatmap td.up-to-5-commits {
+                background-color: #30D350;
+            }
+
+            table.heatmap td.up-to-10-commits {
+                background-color: #00BF00;
+            }
+
+            table.heatmap td.up-to-20-commits {
+                background-color: #00A300;
+            }
+
+            table.heatmap td.commit-spam {
+                background-color: #008000;
+            }
         </style>
     </head>
     <body>)");
@@ -151,7 +176,6 @@
 }
 
 void html::row_begin(unsigned int row) {
-    static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
     indent();
     puts("<tr>");
     indent(1);
@@ -168,8 +192,27 @@
     puts("<td class=\"out-of-range\"></td>");
 }
 
-void html::cell(unsigned commits) {
+void html::cell(std::chrono::year_month_day ymd, unsigned commits) {
+    const char *color_class;
+    if (commits == 0) {
+        color_class = "zero-commits";
+    } else if (commits == 1) {
+        color_class = "one-commit";
+    } else if (commits <= 5) {
+        color_class = "up-to-5-commits";
+    } else if (commits <= 10) {
+        color_class = "up-to-10-commits";
+    } else if (commits <= 20) {
+        color_class = "up-to-20-commits";
+    } else {
+        color_class = "commit-spam";
+    }
     indent();
-    // TODO: use nice coloring and tooltips instead of printing the commits
-    printf("<td>%u</td>\n", commits);
+    printf("<td class=\"%s\" title=\"%s, %d-%u-%u: %u commits\"></td>\n",
+        color_class,
+        weekdays[chrono::weekday(ymd).iso_encoding() - 1],
+        static_cast<int>(ymd.year()),
+        static_cast<unsigned>(ymd.month()),
+        static_cast<unsigned>(ymd.day()),
+        commits);
 }
--- a/src/html.h	Fri Jan 31 22:27:28 2025 +0100
+++ b/src/html.h	Fri Jan 31 22:48:43 2025 +0100
@@ -26,6 +26,7 @@
 #define HTML_H
 
 #include <string>
+#include <chrono>
 
 namespace html {
 
@@ -41,7 +42,7 @@
     void row_begin(unsigned int weekday);
     void row_end();
     void cell_out_of_range();
-    void cell(unsigned commits);
+    void cell(std::chrono::year_month_day ymd, unsigned commits);
 
 }
 
--- a/src/main.cpp	Fri Jan 31 22:27:28 2025 +0100
+++ b/src/main.cpp	Fri Jan 31 22:48:43 2025 +0100
@@ -245,9 +245,9 @@
                     // get the entry from the heatmap
                     auto find_result = entries.find(day_to_check);
                     if (find_result == entries.end()) {
-                        html::cell(0);
+                        html::cell(day_to_check, 0);
                     } else {
-                        html::cell(find_result->second);
+                        html::cell(day_to_check, find_result->second);
                     }
                     // advance seven days and one column
                     day_to_check += chrono::days{7};

mercurial