diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/common/TextTable.cc | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/common/TextTable.cc')
-rw-r--r-- | src/common/TextTable.cc | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/common/TextTable.cc b/src/common/TextTable.cc new file mode 100644 index 00000000..c94719a9 --- /dev/null +++ b/src/common/TextTable.cc @@ -0,0 +1,87 @@ +// -*- 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]; + 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]; + out << string(t.indent, ' ') + << pad(t.row[i][j], col.width, col.col_align) + << ' '; + } + out << endl; + } + return out; +} |