diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/messagebus/key_value_printer.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/messagebus/key_value_printer.cpp')
-rw-r--r-- | library/cpp/messagebus/key_value_printer.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/library/cpp/messagebus/key_value_printer.cpp b/library/cpp/messagebus/key_value_printer.cpp new file mode 100644 index 0000000000..c8592145c7 --- /dev/null +++ b/library/cpp/messagebus/key_value_printer.cpp @@ -0,0 +1,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(); +} |