1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
}
|