summaryrefslogtreecommitdiff
path: root/src/formatter/formatter.h
blob: 5dfa184dcf03689a9187127198953da421aa361d (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-License-Identifier: BSD-3-Clause

#ifndef INVERTER_TOOLS_PRINT_H
#define INVERTER_TOOLS_PRINT_H

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <nlohmann/json.hpp>

#include "src/util.h"

namespace formatter {

using nlohmann::json;
using nlohmann::ordered_json;


/**
 * Enumerations
 */

enum class Unit {
    None = 0,
    V,
    A,
    Wh,
    VA,
    Hz,
    Percentage,
    Celsius,
};

enum class Format {
    Table,
    SimpleTable,
    JSON,
};
std::ostream& operator<<(std::ostream& os, Unit val);


/**
 * Helper functions
 */

template <typename T>
std::string to_str(T& v) {
    std::ostringstream buf;
    buf << v;
    return buf.str();
}


/**
 * Items
 */

template <typename T>
struct TableItem {
    explicit TableItem(std::string key, std::string title, T value, Unit unit = Unit::None, unsigned precision = 0) :
        key(std::move(key)),
        title(std::move(title)),
        value(value),
        unit(unit) {}

    std::string key;
    std::string title;
    T value;
    Unit unit;
};

template <typename T>
struct ListItem {
    explicit ListItem(T value) : value(value) {}
    T value;
};


/**
 * Items holders
 */

class Formattable {
protected:
    Format format_;

public:
    explicit Formattable(Format format) : format_(format) {}
    virtual ~Formattable() = default;

    virtual std::ostream& writeJSON(std::ostream& os) const = 0;
    virtual std::ostream& writeTable(std::ostream& os) const = 0;
    virtual std::ostream& writeSimpleTable(std::ostream& os) const = 0;

    friend std::ostream& operator<<(std::ostream& os, Formattable const& ref) {
        switch (ref.format_) {
            case Format::Table:
                return ref.writeTable(os);

            case Format::SimpleTable:
                return ref.writeSimpleTable(os);

            case Format::JSON:
                return ref.writeJSON(os);
        }

        return os;
    }
};


// T must have `operator<<` and `basic_json toJSON()` methods
template <typename T>
class Table : public Formattable {
protected:
    std::vector<TableItem<T>> v_;

public:
    explicit Table(Format format, std::vector<TableItem<T>> v)
        : Formattable(format), v_(v) {}

    std::ostream& writeSimpleTable(std::ostream& os) const override {
        for (const auto& item: v_) {
            os << item.key << " ";

            std::string value = to_str(item.value);
            bool space = string_has(value, ' ');
            if (space)
                os << "\"";
            os << value;
            if (space)
                os << "\"";

            if (item.unit != Unit::None)
                os << " " << item.unit;

            if (&item != &v_.back())
                os << std::endl;
        }
        return os;
    }

    std::ostream& writeTable(std::ostream& os) const override {
        int maxWidth = 0;
        for (const auto& item: v_) {
            int width = item.title.size()+1 /* colon */;
            if (width > maxWidth)
                maxWidth = width;
        }

        std::ios_base::fmtflags f(os.flags());
        os << std::left;
        for (const auto &item: v_) {
            os << std::setw(maxWidth) << (item.title+":") << " " << item.value;

            if (item.unit != Unit::None)
                os << " " << item.unit;

            if (&item != &v_.back())
                os << std::endl;
        }

        os.flags(f);
        return os;
    }

    std::ostream& writeJSON(std::ostream& os) const override {
        ordered_json j = {
            {"result", "ok"},
            {"data", {}}
        };
        for (const auto &item: v_) {
            if (item.unit != Unit::None) {
                json jval = json::object();
                jval["value"] = item.value.toJSON();
                jval["unit"] = to_str(item.unit);
                j["data"][item.key] = jval;
            } else {
                j["data"][item.key] = item.value.toJSON();
            }
        }
        return os << j.dump();
    }
};

template <typename T>
class List : public Formattable {
protected:
    std::vector<ListItem<T>> v_;

public:
    explicit List(Format format, std::vector<ListItem<T>> v)
        : Formattable(format), v_(v) {}

    std::ostream& writeSimpleTable(std::ostream& os) const override {
        return writeTable(os);
    }

    std::ostream& writeTable(std::ostream& os) const override {
        for (const auto &item: v_) {
            os << item.value;
            if (&item != &v_.back())
                os << std::endl;
        }
        return os;
    }

    std::ostream& writeJSON(std::ostream& os) const override {
        json data = {};
        ordered_json j;

        j["result"] = "ok";

        for (const auto &item: v_)
            data.push_back(item.value.toJSON());
        j["data"] = data;

        return os << j.dump();
    }
};

class Status : public Formattable {
protected:
    bool value_;
    std::string message_;

public:
    explicit Status(Format format, bool value, std::string message)
        : Formattable(format), value_(value), message_(std::move(message)) {}

    std::ostream& writeSimpleTable(std::ostream& os) const override {
        return writeTable(os);
    }

    std::ostream& writeTable(std::ostream& os) const override {
        os << (value_ ? "ok" : "error");
        if (!message_.empty())
            os << ": " << message_;
        return os;
    }

    std::ostream& writeJSON(std::ostream& os) const override {
        ordered_json j = {
            {"result", (value_ ? "ok" : "error")}
        };
        if (!message_.empty())
            j["message"] = message_;
        return os << j.dump();
    }
};

}

#endif