blob: 0a721e9f819a2fef1220d6b3a85ed3add5dc295a [file] [log] [blame]
Yifan Hong1bc1e9f2017-08-29 17:28:12 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <iomanip>
19
20#include "TextTable.h"
21
22namespace android {
23namespace lshal {
24
25void TextTable::computeWidth(const std::vector<std::string>& v) {
26 if (mWidths.size() < v.size()) {
27 mWidths.resize(v.size());
28 }
29 for (size_t i = 0; i < v.size() - 1; ++i) {
30 mWidths[i] = std::max(mWidths[i], v[i].length());
31 }
32 // last column has std::setw(0) to avoid printing unnecessary spaces.
33 mWidths[v.size() - 1] = 0;
34}
35
36void TextTable::dump(std::ostream& out) const {
37 out << std::left;
38 for (const auto& row : mTable) {
39 if (!row.isRow()) {
40 out << row.line() << std::endl;
41 continue;
42 }
43
44 for (size_t i = 0; i < row.fields().size(); ++i) {
45 if (i != 0) {
46 out << " ";
47 }
48 if (i < mWidths.size()) {
49 out << std::setw(mWidths[i]);
50 }
51 out << row.fields()[i];
52 }
53 out << std::endl;
54 }
55}
56
57} // namespace lshal
58} // namespace android