key_value_printer.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "key_value_printer.h"
  2. #include <util/stream/format.h>
  3. TKeyValuePrinter::TKeyValuePrinter(const TString& sep)
  4. : Sep(sep)
  5. {
  6. }
  7. TKeyValuePrinter::~TKeyValuePrinter() {
  8. }
  9. void TKeyValuePrinter::AddRowImpl(const TString& key, const TString& value, bool alignLeft) {
  10. Keys.push_back(key);
  11. Values.push_back(value);
  12. AlignLefts.push_back(alignLeft);
  13. }
  14. TString TKeyValuePrinter::PrintToString() const {
  15. if (Keys.empty()) {
  16. return TString();
  17. }
  18. size_t keyWidth = 0;
  19. size_t valueWidth = 0;
  20. for (size_t i = 0; i < Keys.size(); ++i) {
  21. keyWidth = Max(keyWidth, Keys.at(i).size());
  22. valueWidth = Max(valueWidth, Values.at(i).size());
  23. }
  24. TStringStream ss;
  25. for (size_t i = 0; i < Keys.size(); ++i) {
  26. ss << RightPad(Keys.at(i), keyWidth);
  27. ss << Sep;
  28. if (AlignLefts.at(i)) {
  29. ss << Values.at(i);
  30. } else {
  31. ss << LeftPad(Values.at(i), valueWidth);
  32. }
  33. ss << Endl;
  34. }
  35. return ss.Str();
  36. }