deltaFlow
Display Namespace Reference

Terminal and file output formatting for deltaFlow. More...

Classes

struct  LicenseLine
 A single line in the license notice box. More...
 
struct  ProductLine
 A single line in the product info box. More...
 

Functions

std::string padRight (const std::string &text, int width)
 Right-pads a string with spaces to the given width.
 
std::string centerIn (const std::string &text, int width)
 Centers a string within a given width.
 
std::string separator (char ch='=')
 Returns a separator line of the given character.
 
std::string center (const std::string &text)
 Returns a centered string within the page width.
 
std::string hostname ()
 Returns the machine hostname.
 
std::vector< LicenseLinelicenseContent ()
 Returns the license notice content lines.
 
std::vector< ProductLineproductContent ()
 Returns the product info box content lines.
 
std::string licenseNotice ()
 Returns the plain-text license notice box.
 
std::string productBox ()
 Returns the plain-text product info box.
 
std::string fileBanner ()
 Returns a full plain-text banner for output/log files.
 
std::string sectionHeader (const std::string &title)
 Returns a section header for output files.
 
std::string subSectionHeader (const std::string &title)
 Returns a sub-section header for output files.
 
void printLicenseNotice ()
 Prints the colored license notice box to terminal.
 
void printProductBox ()
 Prints the colored product info box to terminal.
 
void printTerminalBanner ()
 Prints the full colored banner to terminal.
 
void printSectionHeader (const std::string &title)
 Prints a colored section header to terminal.
 

Variables

constexpr fmt::rgb LOGO_COLOR {153, 0, 204}
 deltaFlow logo color
 
constexpr int pageWidth = 80
 Standard output page width.
 
constexpr int licenseBoxWidth = 67
 License notice box width.
 
constexpr int productBoxWidth = 59
 Product info box width.
 

Detailed Description

Terminal and file output formatting for deltaFlow.

Provides colored terminal banners, plain-text banners for log/output files, section headers, and common formatting constants.

Function Documentation

◆ center()

std::string Display::center ( const std::string &  text)
inline

Returns a centered string within the page width.

Parameters
textText to center.
Returns
Padded string.

Definition at line 91 of file Display.H.

91 {
92 return centerIn(text, pageWidth);
93 }
std::string centerIn(const std::string &text, int width)
Centers a string within a given width.
Definition Display.H:72

References centerIn(), and pageWidth.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ centerIn()

std::string Display::centerIn ( const std::string &  text,
int  width 
)
inline

Centers a string within a given width.

Parameters
textText to center.
widthTarget width.
Returns
Centered string.

Definition at line 72 of file Display.H.

72 {
73 int left = (width - static_cast<int>(text.size())) / 2;
74 return (left > 0) ? std::string(left, ' ') + text : text;
75 }
Here is the caller graph for this function:

◆ fileBanner()

std::string Display::fileBanner ( )
inline

Returns a full plain-text banner for output/log files.

Definition at line 217 of file Display.H.

217 {
218 return licenseNotice() + "\n" + productBox();
219 }
std::string licenseNotice()
Returns the plain-text license notice box.
Definition Display.H:189
std::string productBox()
Returns the plain-text product info box.
Definition Display.H:203

References licenseNotice(), and productBox().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hostname()

std::string Display::hostname ( )
inline

Returns the machine hostname.

Returns
Hostname string, or "unknown" on failure.

Definition at line 99 of file Display.H.

99 {
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 }
Here is the caller graph for this function:

◆ licenseContent()

std::vector< LicenseLine > Display::licenseContent ( )
inline

Returns the license notice content lines.

Shared by licenseNotice() (plain-text) and printLicenseNotice() (colored).

Definition at line 121 of file Display.H.

121 {
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 }
constexpr int licenseBoxWidth
License notice box width.
Definition Display.H:52

References centerIn(), and licenseBoxWidth.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ licenseNotice()

std::string Display::licenseNotice ( )
inline

Returns the plain-text license notice box.

Definition at line 189 of file Display.H.

189 {
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 }
std::string padRight(const std::string &text, int width)
Right-pads a string with spaces to the given width.
Definition Display.H:61
std::vector< LicenseLine > licenseContent()
Returns the license notice content lines.
Definition Display.H:121

References licenseBoxWidth, licenseContent(), and padRight().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ padRight()

std::string Display::padRight ( const std::string &  text,
int  width 
)
inline

Right-pads a string with spaces to the given width.

Parameters
textInput string.
widthTarget width.
Returns
Padded string (unchanged if text is already wider).

Definition at line 61 of file Display.H.

61 {
62 int pad = width - static_cast<int>(text.size());
63 return (pad > 0) ? text + std::string(pad, ' ') : text;
64 }
Here is the caller graph for this function:

◆ printLicenseNotice()

void Display::printLicenseNotice ( )
inline

Prints the colored license notice box to terminal.

Definition at line 255 of file Display.H.

255 {
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 }
constexpr fmt::rgb LOGO_COLOR
deltaFlow logo color
Definition Display.H:50

References licenseBoxWidth, licenseContent(), LOGO_COLOR, and padRight().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printProductBox()

void Display::printProductBox ( )
inline

Prints the colored product info box to terminal.

Definition at line 276 of file Display.H.

276 {
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 }
constexpr int productBoxWidth
Product info box width.
Definition Display.H:53
std::vector< ProductLine > productContent()
Returns the product info box content lines.
Definition Display.H:166

References padRight(), productBoxWidth, and productContent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printSectionHeader()

void Display::printSectionHeader ( const std::string &  title)
inline

Prints a colored section header to terminal.

Parameters
titleSection title.

Definition at line 325 of file Display.H.

325 {
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 }
std::string center(const std::string &text)
Returns a centered string within the page width.
Definition Display.H:91
std::string separator(char ch='=')
Returns a separator line of the given character.
Definition Display.H:82

References center(), LOGO_COLOR, and separator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printTerminalBanner()

void Display::printTerminalBanner ( )
inline

Prints the full colored banner to terminal.

Definition at line 313 of file Display.H.

313 {
314 fmt::print("\n");
316 fmt::print("\n");
318 fmt::print("\n");
319 }
void printProductBox()
Prints the colored product info box to terminal.
Definition Display.H:276
void printLicenseNotice()
Prints the colored license notice box to terminal.
Definition Display.H:255

References printLicenseNotice(), and printProductBox().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ productBox()

std::string Display::productBox ( )
inline

Returns the plain-text product info box.

Definition at line 203 of file Display.H.

203 {
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 }

References padRight(), productBoxWidth, and productContent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ productContent()

std::vector< ProductLine > Display::productContent ( )
inline

Returns the product info box content lines.

Shared by productBox() (plain-text) and printProductBox() (colored).

Definition at line 166 of file Display.H.

166 {
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 }
@ INFO
Informational messages.
std::string hostname()
Returns the current hostname.
Definition OutputFile.H:60

References hostname(), INFO, and LOGO_COLOR.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sectionHeader()

std::string Display::sectionHeader ( const std::string &  title)
inline

Returns a section header for output files.

Parameters
titleSection title.
Returns
Formatted section header string.

Definition at line 226 of file Display.H.

226 {
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 }

References center(), and separator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ separator()

std::string Display::separator ( char  ch = '=')
inline

Returns a separator line of the given character.

Parameters
chFill character (default: '=').
Returns
String of pageWidth characters.

Definition at line 82 of file Display.H.

82 {
83 return std::string(pageWidth, ch);
84 }

References pageWidth.

Here is the caller graph for this function:

◆ subSectionHeader()

std::string Display::subSectionHeader ( const std::string &  title)
inline

Returns a sub-section header for output files.

Parameters
titleSub-section title.
Returns
Formatted sub-section header string.

Definition at line 243 of file Display.H.

243 {
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 }

Variable Documentation

◆ licenseBoxWidth

constexpr int Display::licenseBoxWidth = 67
constexpr

License notice box width.

Definition at line 52 of file Display.H.

◆ LOGO_COLOR

constexpr fmt::rgb Display::LOGO_COLOR {153, 0, 204}
constexpr

deltaFlow logo color

Definition at line 50 of file Display.H.

50{153, 0, 204};

◆ pageWidth

constexpr int Display::pageWidth = 80
constexpr

Standard output page width.

Definition at line 51 of file Display.H.

◆ productBoxWidth

constexpr int Display::productBoxWidth = 59
constexpr

Product info box width.

Definition at line 53 of file Display.H.