aboutsummaryrefslogtreecommitdiff
path: root/src/shared/shared.hh
blob: 2d6481449e81c0fe5f059f3c55313314445fae59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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