deltaFlow
Display.H
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
26#ifndef DISPLAY_H
27#define DISPLAY_H
28
29#include <fmt/color.h>
30#include <fmt/core.h>
31#include <string>
32#include <vector>
33
34#ifdef _WIN32
35 #include <windows.h>
36#else
37 #include <unistd.h>
38#endif
39
40#include "Version.H"
41
49namespace Display {
50 constexpr fmt::rgb LOGO_COLOR{153, 0, 204};
51 constexpr int pageWidth = 80;
52 constexpr int licenseBoxWidth = 67;
53 constexpr int productBoxWidth = 59;
54
61 inline std::string padRight(const std::string& text, int width) {
62 int pad = width - static_cast<int>(text.size());
63 return (pad > 0) ? text + std::string(pad, ' ') : text;
64 }
65
72 inline std::string centerIn(const std::string& text, int width) {
73 int left = (width - static_cast<int>(text.size())) / 2;
74 return (left > 0) ? std::string(left, ' ') + text : text;
75 }
76
82 inline std::string separator(char ch = '=') {
83 return std::string(pageWidth, ch);
84 }
85
91 inline std::string center(const std::string& text) {
92 return centerIn(text, pageWidth);
93 }
94
99 inline std::string hostname() {
100 char buf[256];
101 #ifdef _WIN32
102 DWORD len = sizeof(buf);
103 if (GetComputerNameA(buf, &len)) return std::string(buf);
104 #else
105 if (gethostname(buf, sizeof(buf)) == 0) return std::string(buf);
106 #endif
107 return "unknown";
108 }
109
111 struct LicenseLine {
112 std::string text;
113 bool isLogo;
114 };
115
121 inline std::vector<LicenseLine> licenseContent() {
122 constexpr int inner = licenseBoxWidth - 2;
123 auto mid = [inner](const std::string& t) { return centerIn(t, inner); };
124
125 return {
126 {"", false},
127 {mid(R"( /\\ +)"), true},
128 {mid(R"( / \\ )"), true},
129 {mid(R"( / \\ )"), true},
130 {mid(R"(/ \\ )"), true},
131 {mid(R"(========= -)"), true},
132 {mid("deltaFlow v" + std::string(deltaFlow_VERSION)), true},
133 {"", false},
134 {" Copyright (c) 2024 Saud Zahir.", false},
135 {" All rights reserved.", false},
136 {"", false},
137 {" deltaFlow is free software: you can redistribute it", false},
138 {" and/or modify it under the terms of the GNU General", false},
139 {" Public License as published by the Free Software", false},
140 {" Foundation, either version 3 of the License, or", false},
141 {" (at your option) any later version.", false},
142 {"", false},
143 {" deltaFlow is distributed in the hope that it will be useful,", false},
144 {" but WITHOUT ANY WARRANTY; without even the implied warranty", false},
145 {" of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.", false},
146 {" See the GNU General Public License for more details.", false},
147 {"", false},
148 {" <https://www.gnu.org/licenses/>", false},
149 {"", false},
150 };
151 }
152
154 struct ProductLine {
155 std::string key;
156 std::string value;
157 fmt::rgb keyColor;
158 fmt::rgb valColor;
159 };
160
166 inline std::vector<ProductLine> productContent() {
167 constexpr fmt::rgb INFO{100, 200, 255};
168 constexpr fmt::rgb VAL{255, 255, 100};
169 constexpr fmt::rgb DIM{180, 180, 180};
170
171 return {
172 {"", "", LOGO_COLOR, {}},
173 {" deltaFlow -- Power Flow Analysis Software", "", LOGO_COLOR, {}},
174 {" A Program for Steady-State Power System Analysis", "", DIM, {}},
175 {" Version : ", deltaFlow_VERSION, INFO, VAL},
176 {" CMake : ", CMake_VERSION, INFO, VAL},
177 {" GCC : ", gcc_VERSION, INFO, VAL},
178 {"", "", LOGO_COLOR, {}},
179 {" Copyright (c) 2024 ", "Saud Zahir", DIM, fmt::rgb{255, 165, 0}},
180 {" Licensed under GNU GPL v3.0", "", fmt::rgb{100,255,100},{}},
181 {"", "", LOGO_COLOR, {}},
182 {" Hostname : ", hostname(), INFO, fmt::rgb{255, 200, 100}},
183 };
184 }
185
189 inline std::string licenseNotice() {
190 constexpr int inner = licenseBoxWidth - 2;
191 std::string rule = " " + std::string(licenseBoxWidth, '*') + "\n";
192
193 std::string s = rule;
194 for (const auto& line : licenseContent())
195 s += " *" + padRight(line.text, inner) + "*\n";
196 s += rule;
197 return s;
198 }
199
203 inline std::string productBox() {
204 constexpr int inner = productBoxWidth - 2;
205
206 std::string s;
207 s += " " + std::string(inner, '_') + "\n";
208 for (const auto& p : productContent())
209 s += " |" + padRight(p.key + p.value, inner) + "|\n";
210 s += " |" + std::string(inner, '_') + "|\n";
211 return s;
212 }
213
217 inline std::string fileBanner() {
218 return licenseNotice() + "\n" + productBox();
219 }
220
226 inline std::string sectionHeader(const std::string& title) {
227 std::string s;
228 s += "\n";
229 s += separator('*') + "\n";
230 s += "\n";
231 s += center(title) + "\n";
232 s += center(std::string(title.size() + 4, '-')) + "\n";
233 s += "\n";
234 s += separator('*') + "\n";
235 return s;
236 }
237
243 inline std::string subSectionHeader(const std::string& title) {
244 std::string s;
245 s += "\n";
246 s += " " + std::string(title.size() + 4, '-') + "\n";
247 s += " " + title + "\n";
248 s += " " + std::string(title.size() + 4, '-') + "\n";
249 return s;
250 }
251
255 inline void printLicenseNotice() {
256 constexpr int inner = licenseBoxWidth - 2;
257 constexpr fmt::rgb BORDER{0x5a, 0x71, 0x79};
258
259 auto rule = [&]() {
260 fmt::print(fg(BORDER), " {}\n", std::string(licenseBoxWidth, '*'));
261 };
262
263 rule();
264 for (const auto& line : licenseContent()) {
265 fmt::rgb color = line.isLogo ? LOGO_COLOR : BORDER;
266 fmt::print(fg(BORDER), " *");
267 fmt::print(fg(color) | fmt::emphasis::bold, "{}", line.text);
268 fmt::print(fg(BORDER), "{}*\n", padRight("", inner - static_cast<int>(line.text.size())));
269 }
270 rule();
271 }
272
276 inline void printProductBox() {
277 constexpr int inner = productBoxWidth - 2;
278
279 auto printLine = [&](const std::string& text, fmt::rgb color) {
280 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold, " |");
281 fmt::print(fg(color), "{}", padRight(text, inner));
282 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold, "|\n");
283 };
284
285 auto printKV = [&](const std::string& key, const std::string& val,
286 fmt::rgb keyColor, fmt::rgb valColor) {
287 int pad = inner - static_cast<int>(key.size()) - static_cast<int>(val.size());
288 if (pad < 0) pad = 0;
289 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold, " |");
290 fmt::print(fg(keyColor), "{}", key);
291 fmt::print(fg(valColor) | fmt::emphasis::bold, "{}", val);
292 fmt::print("{}", std::string(pad, ' '));
293 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold, "|\n");
294 };
295
296 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold,
297 " {}\n", std::string(inner, '_'));
298
299 for (const auto& p : productContent()) {
300 if (p.value.empty())
301 printLine(p.key, p.keyColor);
302 else
303 printKV(p.key, p.value, p.keyColor, p.valColor);
304 }
305
306 fmt::print(fg(fmt::color::white) | fmt::emphasis::bold,
307 " |{}|\n", std::string(inner, '_'));
308 }
309
313 inline void printTerminalBanner() {
314 fmt::print("\n");
316 fmt::print("\n");
318 fmt::print("\n");
319 }
320
325 inline void printSectionHeader(const std::string& title) {
326 fmt::print("\n");
327 fmt::print(fg(LOGO_COLOR), "{}\n", separator('*'));
328 fmt::print("\n");
329 fmt::print(fg(LOGO_COLOR) | fmt::emphasis::bold, "{}\n", center(title));
330 fmt::print(fg(LOGO_COLOR), "{}\n", center(std::string(title.size() + 4, '-')));
331 fmt::print("\n");
332 fmt::print(fg(LOGO_COLOR), "{}\n", separator('*'));
333 fmt::print("\n");
334 }
335
336}
337
338#endif
@ INFO
Informational messages.
Terminal and file output formatting for deltaFlow.
void printProductBox()
Prints the colored product info box to terminal.
Definition Display.H:276
constexpr fmt::rgb LOGO_COLOR
deltaFlow logo color
Definition Display.H:50
std::string center(const std::string &text)
Returns a centered string within the page width.
Definition Display.H:91
void printSectionHeader(const std::string &title)
Prints a colored section header to terminal.
Definition Display.H:325
constexpr int pageWidth
Standard output page width.
Definition Display.H:51
std::string padRight(const std::string &text, int width)
Right-pads a string with spaces to the given width.
Definition Display.H:61
constexpr int licenseBoxWidth
License notice box width.
Definition Display.H:52
void printLicenseNotice()
Prints the colored license notice box to terminal.
Definition Display.H:255
void printTerminalBanner()
Prints the full colored banner to terminal.
Definition Display.H:313
std::string licenseNotice()
Returns the plain-text license notice box.
Definition Display.H:189
std::string centerIn(const std::string &text, int width)
Centers a string within a given width.
Definition Display.H:72
std::string hostname()
Returns the machine hostname.
Definition Display.H:99
std::vector< LicenseLine > licenseContent()
Returns the license notice content lines.
Definition Display.H:121
std::string separator(char ch='=')
Returns a separator line of the given character.
Definition Display.H:82
constexpr int productBoxWidth
Product info box width.
Definition Display.H:53
std::string subSectionHeader(const std::string &title)
Returns a sub-section header for output files.
Definition Display.H:243
std::string sectionHeader(const std::string &title)
Returns a section header for output files.
Definition Display.H:226
std::string fileBanner()
Returns a full plain-text banner for output/log files.
Definition Display.H:217
std::vector< ProductLine > productContent()
Returns the product info box content lines.
Definition Display.H:166
std::string productBox()
Returns the plain-text product info box.
Definition Display.H:203
A single line in the license notice box.
Definition Display.H:111
bool isLogo
true for logo/brand lines
Definition Display.H:113
std::string text
Line content.
Definition Display.H:112
A single line in the product info box.
Definition Display.H:154
fmt::rgb valColor
Value color (terminal only, ignored when value is empty)
Definition Display.H:158
std::string key
Label or full text.
Definition Display.H:155
fmt::rgb keyColor
Key/text color (terminal only)
Definition Display.H:157
std::string value
Value (empty for non-KV lines)
Definition Display.H:156