]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/TextTable.h
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.
20 #include "include/assert.h"
24 * Manage tabular output of data. Caller defines heading of each column
25 * and alignment of heading and column data,
26 * then inserts rows of data including tuples of
27 * length (ncolumns) terminated by TextTable::endrow. When all rows
28 * are inserted, caller asks for output with ostream <<
29 * which sizes/pads/dumps the table to ostream.
31 * Columns autosize to largest heading or datum. One space is printed
38 enum Align
{LEFT
= 1, CENTER
, RIGHT
};
41 struct TextTableColumn
{
48 TextTableColumn(std::string h
, int w
, Align ha
, Align ca
) :
49 heading(h
), width(w
), hd_align(ha
), col_align(ca
) { }
53 std::vector
<TextTableColumn
> col
; // column definitions
54 unsigned int curcol
, currow
; // col, row being inserted into
55 unsigned int indent
; // indent width when rendering
58 std::vector
<std::vector
<std::string
> > row
; // row data array
61 TextTable(): curcol(0), currow(0), indent(0) {}
65 * Define a column in the table.
67 * @param heading Column heading string (or "")
68 * @param hd_align Alignment for heading in column
69 * @param col_align Data alignment
71 * @note alignment is of type TextTable::Align; values are
72 * TextTable::LEFT, TextTable::CENTER, or TextTable::RIGHT
75 void define_column(const std::string
& heading
, Align hd_align
,
79 * Set indent for table. Only affects table output.
81 * @param i Number of spaces to indent
83 void set_indent(int i
) { indent
= i
; }
86 * Add item to table, perhaps on new row.
87 * table << val1 << val2 << TextTable::endrow;
89 * @param: value to output.
91 * @note: Numerics are output in decimal; strings are not truncated.
92 * Output formatting choice is limited to alignment in define_column().
94 * @return TextTable& for chaining.
97 template<typename T
> TextTable
& operator<<(const T
& item
)
99 if (row
.size() < currow
+ 1)
100 row
.resize(currow
+ 1);
103 * col.size() is a good guess for how big row[currow] needs to be,
104 * so just expand it out now
106 if (row
[currow
].size() < col
.size()) {
107 row
[currow
].resize(col
.size());
110 // inserting more items than defined columns is a coding error
111 assert(curcol
+ 1 <= col
.size());
113 // get rendered width of item alone
114 std::ostringstream oss
;
116 int width
= oss
.str().length();
119 // expand column width if necessary
120 if (width
> col
[curcol
].width
) {
121 col
[curcol
].width
= width
;
124 // now store the rendered item with its proper width
125 row
[currow
][curcol
] = oss
.str();
132 * Degenerate type/variable here is just to allow selection of the
133 * following operator<< for "<< TextTable::endrow"
137 static endrow_t endrow
;
140 * Implements TextTable::endrow
143 TextTable
&operator<<(endrow_t
)
151 * Render table to ostream (i.e. cout << table)
154 friend std::ostream
&operator<<(std::ostream
&out
, const TextTable
&t
);
157 * clear: Reset everything in a TextTable except column defs
158 * resize cols to heading widths, clear indent