aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/key_value_printer.cpp
blob: 75a82a10e24e9e82e42593f30f61e84ec4f1decc (plain) (blame)
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
#include "key_value_printer.h"

#include <util/stream/format.h>

TKeyValuePrinter::TKeyValuePrinter(const TString& sep)
    : Sep(sep)
{ 
} 

TKeyValuePrinter::~TKeyValuePrinter() { 
} 

void TKeyValuePrinter::AddRowImpl(const TString& key, const TString& value, bool alignLeft) {
    Keys.push_back(key);
    Values.push_back(value);
    AlignLefts.push_back(alignLeft);
}

TString TKeyValuePrinter::PrintToString() const {
    if (Keys.empty()) {
        return TString();
    }

    size_t keyWidth = 0;
    size_t valueWidth = 0;

    for (size_t i = 0; i < Keys.size(); ++i) {
        keyWidth = Max(keyWidth, Keys.at(i).size());
        valueWidth = Max(valueWidth, Values.at(i).size());
    }

    TStringStream ss;

    for (size_t i = 0; i < Keys.size(); ++i) {
        ss << RightPad(Keys.at(i), keyWidth);
        ss << Sep;
        if (AlignLefts.at(i)) {
            ss << Values.at(i);
        } else {
            ss << LeftPad(Values.at(i), valueWidth);
        }
        ss << Endl;
    }

    return ss.Str();
}