diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/common/TextTable.cc | |
parent | Initial commit. (diff) | |
download | ceph-upstream/18.2.2.tar.xz ceph-upstream/18.2.2.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/common/TextTable.cc')
-rw-r--r-- | src/common/TextTable.cc | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/common/TextTable.cc b/src/common/TextTable.cc new file mode 100644 index 000000000..e17e7eb8d --- /dev/null +++ b/src/common/TextTable.cc @@ -0,0 +1,91 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2012 Inktank Storage, Inc. + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ +#include "TextTable.h" + +using namespace std; + +void TextTable::define_column(const string &heading, + enum TextTable::Align hd_align, + enum TextTable::Align col_align) +{ + TextTableColumn def(heading, heading.length(), hd_align, col_align); + col.push_back(def); +} + +void TextTable::clear() { + currow = 0; + curcol = 0; + indent = 0; + row.clear(); + // reset widths to heading widths + for (unsigned int i = 0; i < col.size(); i++) + col[i].width = col[i].heading.size(); +} + +/** + * Pad s with space to appropriate alignment + * + * @param s string to pad + * @param width width of field to contain padded string + * @param align desired alignment (LEFT, CENTER, RIGHT) + * + * @return padded string + */ +static string +pad(string s, int width, TextTable::Align align) +{ + int lpad, rpad; + lpad = 0; + rpad = 0; + switch (align) { + case TextTable::LEFT: + rpad = width - s.length(); + break; + case TextTable::CENTER: + lpad = width / 2 - s.length() / 2; + rpad = width - lpad - s.length(); + break; + case TextTable::RIGHT: + lpad = width - s.length(); + break; + } + + return string(lpad, ' ') + s + string(rpad, ' '); +} + +std::ostream &operator<<(std::ostream &out, const TextTable &t) +{ + for (unsigned int i = 0; i < t.col.size(); i++) { + TextTable::TextTableColumn col = t.col[i]; + if (i) { + out << t.column_separation; + } + out << string(t.indent, ' ') + << pad(col.heading, col.width, col.hd_align); + } + out << endl; + + for (unsigned int i = 0; i < t.row.size(); i++) { + for (unsigned int j = 0; j < t.row[i].size(); j++) { + TextTable::TextTableColumn col = t.col[j]; + if (j) { + out << t.column_separation; + } + out << string(t.indent, ' ') + << pad(t.row[i][j], col.width, col.col_align); + } + out << endl; + } + return out; +} |