1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2012 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
14 #include "TextTable.h"
18 void TextTable::define_column(const string &heading,
19 enum TextTable::Align hd_align,
20 enum TextTable::Align col_align)
22 TextTableColumn def(heading, heading.length(), hd_align, col_align);
26 void TextTable::clear() {
30 // reset widths to heading widths
31 for (unsigned int i = 0; i < col.size(); i++)
32 col[i].width = col[i].heading.size();
36 * Pad s with space to appropriate alignment
38 * @param s string to pad
39 * @param width width of field to contain padded string
40 * @param align desired alignment (LEFT, CENTER, RIGHT)
42 * @return padded string
45 pad(string s, int width, TextTable::Align align)
51 rpad = width - s.length();
53 case TextTable::CENTER:
54 lpad = width / 2 - s.length() / 2;
55 rpad = width - lpad - s.length();
57 case TextTable::RIGHT:
58 lpad = width - s.length();
62 return string(lpad, ' ') + s + string(rpad, ' ');
65 std::ostream &operator<<(std::ostream &out, const TextTable &t)
67 for (unsigned int i = 0; i < t.col.size(); i++) {
68 TextTable::TextTableColumn col = t.col[i];
69 out << string(t.indent, ' ')
70 << pad(col.heading, col.width, col.hd_align)
75 for (unsigned int i = 0; i < t.row.size(); i++) {
76 for (unsigned int j = 0; j < t.row[i].size(); j++) {
77 TextTable::TextTableColumn col = t.col[j];
78 out << string(t.indent, ' ')
79 << pad(t.row[i][j], col.width, col.col_align)