Fri, 31 Jan 2025 23:00:00 +0100
fix log not being captured from git
5 | 1 | /* Copyright 2025 Mike Becker. All rights reserved. |
2 | * | |
3 | * Redistribution and use in source and binary forms, with or without | |
4 | * modification, are permitted provided that the following conditions are met: | |
5 | * | |
6 | * 1. Redistributions of source code must retain the above copyright | |
7 | * notice, this list of conditions and the following disclaimer. | |
8 | * | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
16 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
20 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
21 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
22 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "html.h" | |
26 | ||
27 | #include <cstdio> | |
28 | ||
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
29 | namespace chrono = std::chrono; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
30 | |
5 | 31 | namespace html { |
7 | 32 | static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; |
33 | ||
5 | 34 | static unsigned indentation; |
35 | static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; | |
36 | static void indent(int change = 0) { | |
37 | indentation += change; | |
38 | fwrite(tabs, 1, indentation, stdout); | |
39 | } | |
40 | ||
41 | static std::string encode(const std::string &data) { | |
42 | std::string buffer; | |
43 | buffer.reserve(data.size()+16); | |
44 | for (const char &pos: data) { | |
45 | switch (pos) { | |
46 | case '&': | |
47 | buffer.append("&"); | |
48 | break; | |
49 | case '\"': | |
50 | buffer.append("""); | |
51 | break; | |
52 | case '\'': | |
53 | buffer.append("'"); | |
54 | break; | |
55 | case '<': | |
56 | buffer.append("<"); | |
57 | break; | |
58 | case '>': | |
59 | buffer.append(">"); | |
60 | break; | |
61 | default: | |
62 | buffer.append(&pos, 1); | |
63 | break; | |
64 | } | |
65 | } | |
66 | return buffer; | |
67 | } | |
68 | } | |
69 | ||
70 | void html::open() { | |
71 | puts( | |
72 | R"(<html> | |
73 | <head> | |
74 | <style> | |
75 | table.heatmap { | |
76 | table-layout: fixed; | |
77 | border-collapse: collapse; | |
78 | font-family: sans-serif; | |
79 | } | |
80 | table.heatmap td, table.heatmap th { | |
81 | text-align: center; | |
82 | border: solid 1px lightgray; | |
83 | height: 1.5em; | |
84 | } | |
85 | table.heatmap td { | |
86 | border: solid 1px lightgray; | |
87 | width: 1.5em; | |
88 | height: 1.5em; | |
89 | } | |
90 | table.heatmap td.out-of-range { | |
91 | background-color: gray; | |
92 | } | |
7 | 93 | |
94 | table.heatmap td.zero-commits { | |
95 | background-color: white; | |
96 | } | |
97 | ||
98 | table.heatmap td.one-commit { | |
99 | background-color: #80E7A0; | |
100 | } | |
101 | ||
102 | table.heatmap td.up-to-5-commits { | |
103 | background-color: #30D350; | |
104 | } | |
105 | ||
106 | table.heatmap td.up-to-10-commits { | |
107 | background-color: #00BF00; | |
108 | } | |
109 | ||
110 | table.heatmap td.up-to-20-commits { | |
111 | background-color: #00A300; | |
112 | } | |
113 | ||
114 | table.heatmap td.commit-spam { | |
115 | background-color: #008000; | |
116 | } | |
5 | 117 | </style> |
118 | </head> | |
119 | <body>)"); | |
120 | indentation = 2; | |
121 | } | |
122 | ||
123 | void html::close() { | |
124 | puts("\t</body>\n</html>"); | |
125 | } | |
126 | ||
127 | void html::h1(const std::string& heading) { | |
128 | indent(); | |
129 | printf("<h1>%s</h1>\n", encode(heading).c_str()); | |
130 | } | |
131 | ||
132 | void html::h2(const std::string& heading) { | |
133 | indent(); | |
134 | printf("<h2>%s</h2>\n", encode(heading).c_str()); | |
135 | } | |
136 | ||
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
137 | void html::table_begin(int year) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
138 | static constexpr const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
139 | // compute the column spans, first |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
140 | unsigned colspans[12] = {}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
141 | { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
142 | unsigned total_cols = 0; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
143 | chrono::sys_days day{chrono::year_month_day{chrono::year{year}, chrono::month{1}, chrono::day{1}}}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
144 | if (chrono::weekday{day}.iso_encoding() != 1) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
145 | colspans[0] = 1; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
146 | total_cols = 1; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
147 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
148 | for (unsigned col = 0; col < 12; ++col) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
149 | while (total_cols < html::columns && chrono::year_month_day{day}.month() <= chrono::month{col + 1}) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
150 | ++total_cols; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
151 | ++colspans[col]; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
152 | day += chrono::days{7}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
153 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
154 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
155 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
156 | |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
157 | // now render the table heading |
5 | 158 | indent(); |
159 | puts("<table class=\"heatmap\">"); | |
160 | indent(1); | |
161 | puts("<tr>"); | |
162 | indent(1); | |
163 | puts("<th></th>"); | |
164 | for (unsigned i = 0 ; i < 12 ; i++) { | |
165 | indent(); | |
166 | printf("<th scope=\"col\" colspan=\"%d\">%s</th>\n", colspans[i], months[i]); | |
167 | } | |
168 | indent(-1); | |
169 | puts("</tr>"); | |
170 | } | |
171 | ||
172 | void html::table_end() { | |
173 | indentation--; | |
174 | indent(); | |
175 | puts("</table>"); | |
176 | } | |
177 | ||
178 | void html::row_begin(unsigned int row) { | |
179 | indent(); | |
180 | puts("<tr>"); | |
181 | indent(1); | |
182 | printf("<th scope=\"row\">%s</th>\n", weekdays[row]); | |
183 | } | |
184 | ||
185 | void html::row_end() { | |
186 | indent(-1); | |
187 | puts("</tr>"); | |
188 | } | |
189 | ||
190 | void html::cell_out_of_range() { | |
191 | indent(); | |
192 | puts("<td class=\"out-of-range\"></td>"); | |
193 | } | |
194 | ||
7 | 195 | void html::cell(std::chrono::year_month_day ymd, unsigned commits) { |
196 | const char *color_class; | |
197 | if (commits == 0) { | |
198 | color_class = "zero-commits"; | |
199 | } else if (commits == 1) { | |
200 | color_class = "one-commit"; | |
201 | } else if (commits <= 5) { | |
202 | color_class = "up-to-5-commits"; | |
203 | } else if (commits <= 10) { | |
204 | color_class = "up-to-10-commits"; | |
205 | } else if (commits <= 20) { | |
206 | color_class = "up-to-20-commits"; | |
207 | } else { | |
208 | color_class = "commit-spam"; | |
209 | } | |
5 | 210 | indent(); |
7 | 211 | printf("<td class=\"%s\" title=\"%s, %d-%u-%u: %u commits\"></td>\n", |
212 | color_class, | |
213 | weekdays[chrono::weekday(ymd).iso_encoding() - 1], | |
214 | static_cast<int>(ymd.year()), | |
215 | static_cast<unsigned>(ymd.month()), | |
216 | static_cast<unsigned>(ymd.day()), | |
217 | commits); | |
5 | 218 | } |