Adds the sdi_toolBox library (temporary version)
This commit is contained in:
84
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/ansi.h
Normal file
84
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/ansi.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
{{copyright}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{version}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{license}}
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace sdi_toolBox::console::ANSI
|
||||
{
|
||||
//--------------------------------------------------------------
|
||||
class EscapeCommand
|
||||
{
|
||||
public:
|
||||
enum class Color : uint8_t
|
||||
{
|
||||
Black = 0,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
};
|
||||
|
||||
static constexpr uint8_t LIGHT = 0x01 << 0; // 0x01
|
||||
static constexpr uint8_t BRIGHT = 0x01 << 1; // 0x02
|
||||
static constexpr uint8_t DIM = 0x01 << 2; // 0x04
|
||||
static constexpr uint8_t UNDERLINE = 0x01 << 3; // 0x08
|
||||
static constexpr uint8_t BLINK = 0x01 << 4; // 0x10
|
||||
static constexpr uint8_t REVERSE = 0x01 << 5; // 0x20
|
||||
|
||||
public:
|
||||
static std::string clear();
|
||||
static std::string get(const Color foregroundColor,
|
||||
const uint8_t foregroundStyle = 0,
|
||||
const Color backgroundColor = Color::Black,
|
||||
const uint8_t backgroundStyle = 0);
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
inline std::string EscapeCommand::clear()
|
||||
{
|
||||
return "\x1b[0m";
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
inline std::string EscapeCommand::get(const Color foregroundColor, const uint8_t foregroundStyle, const Color backgroundColor, const uint8_t backgroundStyle)
|
||||
{
|
||||
std::string command = "\x1b[";
|
||||
int foregroundColorValue = static_cast<int>(foregroundColor) + 30;
|
||||
if (foregroundStyle & LIGHT)
|
||||
foregroundColorValue += 60;
|
||||
command += std::to_string(foregroundColorValue) + ";";
|
||||
|
||||
int backgroundColorValue = static_cast<int>(backgroundColor) + 40;
|
||||
if (backgroundStyle & LIGHT)
|
||||
backgroundColorValue += 60;
|
||||
command += std::to_string(backgroundColorValue);
|
||||
|
||||
if (foregroundStyle & BRIGHT)
|
||||
command += ";1";
|
||||
if (foregroundStyle & DIM)
|
||||
command += ";2";
|
||||
if (foregroundStyle & UNDERLINE)
|
||||
command += ";4";
|
||||
if (foregroundStyle & BLINK)
|
||||
command += ";5";
|
||||
if (foregroundStyle & REVERSE)
|
||||
command += ";7";
|
||||
|
||||
command += "m";
|
||||
return command;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
} // namespace sdi_toolBox::console::ANSI
|
||||
152
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/consoleTable.h
Normal file
152
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/consoleTable.h
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
{{copyright}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{version}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{license}}
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace sdi_toolBox::console
|
||||
{
|
||||
//--------------------------------------------------------------
|
||||
class ConsoleTable
|
||||
{
|
||||
public:
|
||||
using Row = std::vector<std::string>;
|
||||
using ColumnWidths = std::vector<size_t>;
|
||||
|
||||
public:
|
||||
ConsoleTable() = default; // Constructor
|
||||
virtual ~ConsoleTable() = default; // Destructor
|
||||
ConsoleTable(const ConsoleTable &other) = delete; // Copy constructor
|
||||
ConsoleTable(ConsoleTable &&other) noexcept = delete; // Move constructor
|
||||
ConsoleTable &operator=(const ConsoleTable &other) = delete; // Copy assignment
|
||||
ConsoleTable &operator=(ConsoleTable &&other) noexcept = delete; // Move assignment
|
||||
|
||||
void setHeaders(const Row &headers); // Set table headers
|
||||
void addRow(const Row &row); // Add a row to the table
|
||||
|
||||
[[nodiscard]] std::string render() const; // Render the table as a string
|
||||
|
||||
protected:
|
||||
Row m_headers; // Table headers
|
||||
std::vector<Row> m_rows; // Table rows
|
||||
size_t m_padding = 2; // Padding between columns
|
||||
|
||||
private:
|
||||
[[nodiscard]] ColumnWidths calculateColumnWidths() const; // Calculate column widths
|
||||
[[nodiscard]] static std::string drawSeparator(const ColumnWidths &columnWidths); // Draw a separator line (+---+---+---+)
|
||||
[[nodiscard]] static std::string drawRow(const Row &row, // Draw a single row
|
||||
const ColumnWidths &columnWidths);
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
/* Set table headers */
|
||||
inline void ConsoleTable::setHeaders(const Row &headers)
|
||||
{
|
||||
m_headers = headers;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Add a row to the table */
|
||||
inline void ConsoleTable::addRow(const Row &row)
|
||||
{
|
||||
m_rows.push_back(row);
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Render the table as a string */
|
||||
inline std::string ConsoleTable::render() const
|
||||
{
|
||||
// Calculate column widths
|
||||
const auto columnWidths = calculateColumnWidths();
|
||||
|
||||
// Initialize the output string
|
||||
std::string output;
|
||||
|
||||
// Draw the top separator
|
||||
output += drawSeparator(columnWidths);
|
||||
|
||||
// Draw the headers
|
||||
output += drawRow(m_headers, columnWidths);
|
||||
|
||||
// Draw the separator between headers and data
|
||||
output += drawSeparator(columnWidths);
|
||||
|
||||
// Draw the data rows
|
||||
for (const auto &row : m_rows)
|
||||
{
|
||||
output += drawRow(row, columnWidths);
|
||||
}
|
||||
|
||||
// Draw the bottom separator
|
||||
output += drawSeparator(columnWidths);
|
||||
|
||||
return output;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Calculate column widths */
|
||||
inline ConsoleTable::ColumnWidths ConsoleTable::calculateColumnWidths() const
|
||||
{
|
||||
// Determine the number of columns (based on headers or first row)
|
||||
const auto numColumns = m_headers.empty() ? (m_rows.empty() ? 0 : m_rows[0].size()) : m_headers.size();
|
||||
|
||||
// Initialize column widths
|
||||
ColumnWidths widths(numColumns, 0);
|
||||
|
||||
// Calculate widths based on headers
|
||||
for (size_t i = 0; i < m_headers.size() && i < numColumns; ++i)
|
||||
{
|
||||
widths[i] = std::max(widths[i], m_headers[i].length() + m_padding);
|
||||
}
|
||||
|
||||
// Calculate widths based on rows
|
||||
for (const auto &row : m_rows)
|
||||
{
|
||||
for (size_t i = 0; i < row.size() && i < numColumns; ++i)
|
||||
{
|
||||
widths[i] = std::max(widths[i], row[i].length() + m_padding);
|
||||
}
|
||||
}
|
||||
|
||||
return widths;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Draw a separator line (+---+---+---+) */
|
||||
inline std::string ConsoleTable::drawSeparator(const ColumnWidths &columnWidths)
|
||||
{
|
||||
std::string output;
|
||||
output += "+";
|
||||
for (const auto &width : columnWidths)
|
||||
{
|
||||
output += std::string(width, '-') + "+";
|
||||
}
|
||||
output += "\n";
|
||||
return output;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Draw a single row */
|
||||
inline std::string ConsoleTable::drawRow(const Row &row, const ColumnWidths &columnWidths)
|
||||
{
|
||||
std::string output;
|
||||
output += "|";
|
||||
for (size_t i = 0; i < columnWidths.size(); ++i)
|
||||
{
|
||||
const auto cellContent = (i < row.size()) ? row[i] : "";
|
||||
output += " " + cellContent;
|
||||
// Add padding spaces
|
||||
const size_t paddingSpaces = columnWidths[i] - cellContent.length() - 1;
|
||||
output += std::string(paddingSpaces, ' ');
|
||||
output += "|";
|
||||
}
|
||||
output += "\n";
|
||||
return output;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
} // namespace sdi_toolBox::console
|
||||
124
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/msw/win32Console.h
Normal file
124
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/msw/win32Console.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
{{copyright}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{version}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{license}}
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <Windows.h>
|
||||
#endif // defined WIN32
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
namespace sdi_toolBox::logs
|
||||
{
|
||||
//--------------------------------------------------------------
|
||||
class Win32Console
|
||||
{
|
||||
public:
|
||||
static void initConsole(); // Init application console and attach debug console under MSW
|
||||
static void releaseConsole(); // Release application console
|
||||
|
||||
public:
|
||||
virtual ~Win32Console(); // Default destructor
|
||||
Win32Console(const Win32Console &obj) = delete; // Copy constructor
|
||||
Win32Console(Win32Console &&obj) noexcept = delete; // Move constructor
|
||||
Win32Console &operator=(const Win32Console &obj) = delete; // Copy assignment operator
|
||||
Win32Console &operator=(Win32Console &&obj) noexcept = delete; // Move assignment operator
|
||||
|
||||
static bool hasAttachedConsole(); // Returns true if console is attached, false otherwise
|
||||
|
||||
protected:
|
||||
static inline std::unique_ptr<Win32Console> m_singleton;
|
||||
Win32Console(); // Default constructor
|
||||
|
||||
FILE *m_stdoutFile; // Reopened stdout file pointer
|
||||
FILE *m_stderrFile; // Reopened stderr file pointer
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
/* Init application console and attach debug console under MSW */
|
||||
inline void Win32Console::initConsole()
|
||||
{
|
||||
#ifndef _WIN32
|
||||
# error This calss is only available under Windows systems
|
||||
#endif
|
||||
|
||||
if (!m_singleton)
|
||||
m_singleton = std::unique_ptr<Win32Console>(new Win32Console());
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Release application console */
|
||||
inline void Win32Console::releaseConsole()
|
||||
{
|
||||
if (m_singleton)
|
||||
m_singleton.reset();
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Default constructor */
|
||||
inline Win32Console::Win32Console()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
bool consoleIsCreated = false;
|
||||
|
||||
// Try to attach application to the current console
|
||||
AttachConsole(ATTACH_PARENT_PROCESS);
|
||||
|
||||
if (!GetConsoleWindow()) // No console was available
|
||||
{
|
||||
// Create console and attach application to it
|
||||
if (!AllocConsole())
|
||||
throw std::logic_error("Unable to attach application to debug console"); // Error during creating console
|
||||
consoleIsCreated = true;
|
||||
}
|
||||
|
||||
// Reopen stdout and stderr streams to console window
|
||||
if (freopen_s(&m_stdoutFile, "CONOUT$", "w", stdout) != 0)
|
||||
throw std::logic_error("Unable to reopen stdout"); // Error during reopen on stdout
|
||||
|
||||
if (freopen_s(&m_stderrFile, "CONOUT$", "w", stderr) != 0)
|
||||
throw std::logic_error("Unable to reopen stderr"); // Error during reopen on stderr
|
||||
|
||||
std::cout.clear();
|
||||
std::cerr.clear();
|
||||
|
||||
if (!consoleIsCreated)
|
||||
std::cout << std::endl; // Add a new line if console was already existing
|
||||
#endif
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Default destructor */
|
||||
inline Win32Console::~Win32Console()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
std::cout.clear();
|
||||
std::cerr.clear();
|
||||
|
||||
// Free console
|
||||
FreeConsole();
|
||||
|
||||
// Close reopened stdout and stderr streams
|
||||
(void)fclose(m_stdoutFile);
|
||||
(void)fclose(m_stderrFile);
|
||||
#endif
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Returns true if console is attached, false otherwise */
|
||||
inline bool Win32Console::hasAttachedConsole()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (GetConsoleWindow())
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
} // namespace sdi_toolBox::logs
|
||||
Reference in New Issue
Block a user