]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/TextTable.h
60c982a7e5ff656d2f8e6653686946205eef4642
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.
22 #include "include/assert.h"
26 * Manage tabular output of data. Caller defines heading of each column
27 * and alignment of heading and column data,
28 * then inserts rows of data including tuples of
29 * length (ncolumns) terminated by TextTable::endrow. When all rows
30 * are inserted, caller asks for output with ostream <<
31 * which sizes/pads/dumps the table to ostream.
33 * Columns autosize to largest heading or datum. One space is printed
40 enum Align
{LEFT
= 1, CENTER
, RIGHT
};
43 struct TextTableColumn
{
50 TextTableColumn(std::string h
, int w
, Align ha
, Align ca
) :
51 heading(h
), width(w
), hd_align(ha
), col_align(ca
) { }
55 std::vector
<TextTableColumn
> col
; // column definitions
56 unsigned int curcol
, currow
; // col, row being inserted into
57 unsigned int indent
; // indent width when rendering
60 std::vector
<std::vector
<std::string
> > row
; // row data array
63 TextTable(): curcol(0), currow(0), indent(0) {}
67 * Define a column in the table.
69 * @param heading Column heading string (or "")
70 * @param hd_align Alignment for heading in column
71 * @param col_align Data alignment
73 * @note alignment is of type TextTable::Align; values are
74 * TextTable::LEFT, TextTable::CENTER, or TextTable::RIGHT
77 void define_column(const std::string
& heading
, Align hd_align
,
81 * Set indent for table. Only affects table output.
83 * @param i Number of spaces to indent
85 void set_indent(int i
) { indent
= i
; }
88 * Add item to table, perhaps on new row.
89 * table << val1 << val2 << TextTable::endrow;
91 * @param: value to output.
93 * @note: Numerics are output in decimal; strings are not truncated.
94 * Output formatting choice is limited to alignment in define_column().
96 * @return TextTable& for chaining.
99 template<typename T
> TextTable
& operator<<(const T
& item
)
101 if (row
.size() < currow
+ 1)
102 row
.resize(currow
+ 1);
105 * col.size() is a good guess for how big row[currow] needs to be,
106 * so just expand it out now
108 if (row
[currow
].size() < col
.size()) {
109 row
[currow
].resize(col
.size());
112 // inserting more items than defined columns is a coding error
113 assert(curcol
+ 1 <= col
.size());
115 // get rendered width of item alone
116 std::ostringstream oss
;
118 int width
= oss
.str().length();
121 // expand column width if necessary
122 if (width
> col
[curcol
].width
) {
123 col
[curcol
].width
= width
;
126 // now store the rendered item with its proper width
127 row
[currow
][curcol
] = oss
.str();
134 * Degenerate type/variable here is just to allow selection of the
135 * following operator<< for "<< TextTable::endrow"
139 static endrow_t endrow
;
142 * Implements TextTable::endrow
145 TextTable
&operator<<(endrow_t
)
153 * Render table to ostream (i.e. cout << table)
156 friend std::ostream
&operator<<(std::ostream
&out
, const TextTable
&t
);
159 * clear: Reset everything in a TextTable except column defs
160 * resize cols to heading widths, clear indent