dropshell release 2025.0518.1355
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled

This commit is contained in:
Your Name
2025-05-18 13:55:39 +12:00
parent 314a5fe96a
commit 2b446f80a3
12 changed files with 205 additions and 133 deletions

View File

@ -1,10 +1,11 @@
#ifndef ASSERT_HPP
#define ASSERT_HPP
#include "output.hpp"
#define ASSERT(condition, message) \
if (!(condition)) { \
std::cerr << "Assertion failed: " << message << std::endl; \
dropshell::error << "Assertion failed: " << message << std::endl; \
std::exit(1); \
}

View File

@ -60,6 +60,56 @@ namespace dropshell
// execute_local_command
// ----------------------------------------------------------------------------------------------------------
class fancypinter
{
public:
fancypinter(sColour startColour) : startColour_(startColour), currentColour_(startColour) {}
void print_chunk(std::string chunk)
{
if (chunk.empty())
return;
if (newline_)
{
// sniff the mode... if the string starts with warning or warning: then set mode to WARNING. etc.
if (chunk.find("warning") == 0)
currentColour_ = sColour::WARNING;
else if (chunk.find("error") == 0)
currentColour_ = sColour::ERROR;
else if (chunk.find("debug") == 0)
currentColour_ = sColour::DEBUG;
else if (chunk.find("info") == 0)
currentColour_ = sColour::INFO;
else
currentColour_ = startColour_;
}
colourstream(currentColour_) << chunk;
newline_ = (chunk[chunk.size()-1] == '\n');
}
void print(const std::string& buffer) {
size_t start = 0;
while (start < buffer.size()) {
size_t newline_pos = buffer.find('\n', start);
if (newline_pos == std::string::npos) {
if (start < buffer.size()) {
print_chunk(buffer.substr(start));
}
break;
}
print_chunk(buffer.substr(start, newline_pos - start + 1)); // include the newline
start = newline_pos + 1;
}
}
private:
bool newline_ = true;
sColour startColour_;
sColour currentColour_;
};
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
{
sCommand command(directory_to_run_in, command_to_run, env_vars);
@ -86,15 +136,14 @@ namespace dropshell
return false;
}
char buffer[128];
fancypinter fancyprint(sColour::DEBUG);
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{
if (output != nullptr)
(*output) += buffer;
if (!silent)
{
std::cerr << buffer;
}
fancyprint.print(buffer);
}
int ret = pclose(pipe);
return EXITSTATUSCHECK(ret);

View File

@ -2,56 +2,63 @@
#include <iostream>
#include <mutex>
namespace {
namespace dropshell
{
// Mutex to synchronize output
std::mutex output_mutex;
// ANSI colour codes
constexpr const char* GREY = "\033[90m";
constexpr const char* RESET = "\033[0m";
constexpr const char* DEBUG_COLOUR = "\033[36m"; // Cyan
constexpr const char* INFO_COLOUR = "\033[32m"; // Green
constexpr const char* WARNING_COLOUR = "\033[33m"; // Yellow
constexpr const char* ERROR_COLOUR = "\033[31m"; // Red
constexpr const char *GREY = "\033[90m";
constexpr const char *RESET = "\033[0m";
constexpr const char *DEBUG_COLOUR = "\033[36m"; // Cyan
constexpr const char *INFO_COLOUR = "\033[32m"; // Green
constexpr const char *WARNING_COLOUR = "\033[33m"; // Yellow
constexpr const char *ERROR_COLOUR = "\033[31m"; // Red
// Tag and colour for each stream
struct StreamInfo {
const char* tag;
const char* colour;
struct StreamInfo
{
const char *tag;
const char *colour;
};
const StreamInfo stream_infos[] = {
{"[DBG]", DEBUG_COLOUR},
{"[INF]", INFO_COLOUR},
{"[WRN]", WARNING_COLOUR},
{"[ERR]", ERROR_COLOUR}
};
{"[ERR]", ERROR_COLOUR}};
// Custom streambuf to prefix and colour each line
class PrefixStreambuf : public std::streambuf {
class PrefixStreambuf : public std::streambuf
{
public:
PrefixStreambuf(std::ostream& dest, const char* tag, const char* colour)
PrefixStreambuf(std::ostream &dest, const char *tag, const char *colour)
: dest_(dest), tag_(tag), colour_(colour), at_line_start_(true) {}
protected:
int overflow(int c) override {
int overflow(int c) override
{
std::lock_guard<std::mutex> lock(output_mutex);
if (c == EOF) return !EOF;
if (at_line_start_ && c != '\n') {
if (c == EOF)
return !EOF;
if (at_line_start_ && c != '\n')
{
dest_ << GREY << tag_ << RESET << ' ' << colour_;
at_line_start_ = false;
}
dest_.put(static_cast<char>(c));
if (c == '\n') {
if (c == '\n')
{
dest_ << RESET;
at_line_start_ = true;
}
return c;
}
private:
std::ostream& dest_;
const char* tag_;
const char* colour_;
std::ostream &dest_;
const char *tag_;
const char *colour_;
bool at_line_start_;
};
@ -64,15 +71,33 @@ namespace {
std::ostream info_stream(&info_buf);
std::ostream warning_stream(&warning_buf);
std::ostream error_stream(&error_buf);
}
std::ostream& debug = debug_stream;
std::ostream& info = info_stream;
std::ostream& warning = warning_stream;
std::ostream& error = error_stream;
std::ostream &debug = debug_stream;
std::ostream &info = info_stream;
std::ostream &warning = warning_stream;
std::ostream &error = error_stream;
void SetColour(sColour colour, std::ostream& os) {
switch (colour) {
std::ostream &colourstream(sColour colour)
{
switch (colour)
{
case sColour::DEBUG:
return debug_stream;
case sColour::INFO:
return info_stream;
case sColour::WARNING:
return warning_stream;
case sColour::ERROR:
return error_stream;
default:
return info_stream;
}
}
void SetColour(sColour colour, std::ostream &os)
{
switch (colour)
{
case sColour::RESET:
os << RESET;
break;
@ -88,32 +113,17 @@ void SetColour(sColour colour, std::ostream& os) {
case sColour::ERROR:
os << ERROR_COLOUR;
break;
}
}
}
void PrintDebug(const std::string& msg) {
std::lock_guard<std::mutex> lock(output_mutex);
debug << msg << '\n';
}
void PrintInfo(const std::string& msg) {
std::lock_guard<std::mutex> lock(output_mutex);
info << msg << '\n';
}
void PrintWarning(const std::string& msg) {
std::lock_guard<std::mutex> lock(output_mutex);
warning << msg << '\n';
}
void PrintError(const std::string& msg) {
std::lock_guard<std::mutex> lock(output_mutex);
error << msg << '\n';
}
SwitchColour::SwitchColour(sColour colour, std::ostream &os) : os_(os), colour_(colour)
{
SetColour(colour_, os_);
}
SwitchColour::SwitchColour(sColour colour, std::ostream& os) : os_(os), colour_(colour)
{
SetColour(colour_, os_);
}
SwitchColour::~SwitchColour()
{
SetColour(sColour::RESET, os_);
}
SwitchColour::~SwitchColour()
{
SetColour(sColour::RESET, os_);
}
} // namespace dropshell

View File

@ -6,6 +6,8 @@
#include <vector>
#include <ostream>
namespace dropshell {
/*
output.hpp and output.cpp - simple output helpers.
@ -62,16 +64,11 @@ enum class sColour {
WARNING,
ERROR
};
std::ostream& colourstream(sColour colour);
// Set colour for a stream
void SetColour(sColour colour, std::ostream& os = std::cerr);
// Helper print functions
void PrintDebug(const std::string& msg);
void PrintInfo(const std::string& msg);
void PrintWarning(const std::string& msg);
void PrintError(const std::string& msg);
class SwitchColour
{
public:
@ -81,4 +78,7 @@ class SwitchColour
std::ostream& os_;
sColour colour_;
};
} // namespace dropshell
#endif // OUTPUT_HPP

View File

@ -10,10 +10,10 @@
namespace dropshell {
void maketitle(const std::string& title) {
std::cout << std::string(title.length() + 4, '-') << std::endl;
std::cout << "| " << title << " |" << std::endl;
std::cout << std::string(title.length() + 4, '-') << std::endl;
void maketitle(const std::string& title, sColour colour) {
colourstream(colour) << std::string(title.length() + 4, '-') << std::endl;
colourstream(colour) << "| " << title << " |" << std::endl;
colourstream(colour) << std::string(title.length() + 4, '-') << std::endl;
}
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line) {

View File

@ -3,6 +3,8 @@
#include <string>
#include <vector>
#include "output.hpp"
namespace dropshell {
/**
@ -10,7 +12,7 @@ namespace dropshell {
*
* @param title The title string to display
*/
void maketitle(const std::string& title);
void maketitle(const std::string& title, sColour colour=sColour::INFO);
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);