diff options
| author | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
|---|---|---|
| committer | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
| commit | 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch) | |
| tree | 222dfcd07a1e40716127a347bbfd7119ce3d0984 /src/shared/shared.hh | |
initial commit
Diffstat (limited to 'src/shared/shared.hh')
| -rw-r--r-- | src/shared/shared.hh | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/shared/shared.hh b/src/shared/shared.hh new file mode 100644 index 0000000..2d64814 --- /dev/null +++ b/src/shared/shared.hh @@ -0,0 +1,120 @@ +#ifndef SHARED_SHARED_HH_ +#define SHARED_SHARED_HH_ + +#include <algorithm> +#include <atomic> +#include <chrono> +#include <fstream> +#include <iomanip> +#include <iostream> +#include <mutex> +#include <ranges> +#include <string> + +#include <boost/iostreams/copy.hpp> +#include <boost/iostreams/filter/gzip.hpp> +#include <boost/iostreams/filtering_streambuf.hpp> + +#include "shared/math.hh" + +// Tiny thread safe printing. +namespace shared { + +std::string make_string_lower(std::string str) noexcept; +std::string read_file(const std::string& path); +void compress_string(std::string& str) noexcept; +void decompress_string(std::string& str) noexcept; +std::ofstream open_file(const std::string& dir, + const std::ios_base::openmode mode = std::ios::trunc); + +constexpr unsigned long MAX_SAY_LENGTH = 32ul; +constexpr unsigned long MAX_USER_PASS_LENGTH = 64; + +class print { +private: + static inline std::mutex lock; + +public: + enum class colour { + black = 30, + red = 31, + green = 32, + yellow = 33, + blue = 34, + magenta = 35, + cyan = 36, + white = 37, + none = 39, + grey = 90, + bright_red = 91, + bright_green = 92, + bright_yellow = 93, + bright_blue = 94, + bright_magenta = 95, + bright_cyan = 96, + bright_white = 97 + }; + +private: + static void print_message(const std::string_view message, + const bool print_time, + const std::string_view colour, + const bool cerr = false) noexcept { + const std::lock_guard<std::mutex> guard(lock); + + if (print_time) { + const auto epoch = std::time(nullptr); + struct tm* local = localtime(&epoch); + std::cout << std::put_time(local, "[%F %T] "); + } + static std::string none = make_colour(colour::none); + + if (cerr) { + std::cerr << colour << message << none; + return; + } + + std::cout << colour << message << none; + } + static std::string make_colour(const colour clr) noexcept { + return {"\033[" + std::to_string(static_cast<int>(clr)) + "m"}; + } + +public: + print() noexcept = delete; + static void message(const std::string_view msg, + const bool print_time = true) noexcept { + static const std::string grey = make_colour(colour::white); + print_message(msg, print_time, grey); + } + static void notify(const std::string_view msg, + const bool print_time = true) noexcept { + static const std::string cyan = make_colour(colour::cyan); + print_message(msg, print_time, cyan); + } + static void warn(const std::string_view msg, + const bool print_time = true) noexcept { + static const std::string b_yellow = make_colour(colour::bright_yellow); + print_message(msg, print_time, b_yellow); + } + static void fault(const std::string_view msg, + const bool print_time = true) noexcept { + static const std::string bright_red = make_colour(colour::bright_red); + print_message(msg, print_time, bright_red, true); + } + static void debug(const std::string_view msg, + const bool print_time = true) noexcept { + static const std::string magenta = make_colour(colour::magenta); + print_message(msg, print_time, magenta, true); + } + static void custom(const std::string_view msg, const enum colour colour, + const bool print_time = true) noexcept { + print_message(msg, print_time, make_colour(colour)); + } +}; + +inline std::atomic<bool> should_exit = false; + +}; // namespace shared + +#endif |
