Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
table-printer.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/table-printer.h"
16 #include "common/logging.h"
17 
18 #include <iomanip>
19 #include <sstream>
20 
21 #include "common/names.h"
22 
23 using namespace impala;
24 
25 // Number of spaces to pad adjacent columns.
26 const int COLUMN_PAD = 2;
27 
28 TablePrinter::TablePrinter() : max_output_width_(-1) {
29 }
30 
31 void TablePrinter::AddColumn(const string& label, bool left_align) {
32  labels_.push_back(label);
33  left_align_.push_back(left_align);
34  max_col_widths_.push_back(label.size());
35 }
36 
38  max_output_width_ = width;
39 }
40 
41 // Add a row to the table. This must have the same width as labels.
42 void TablePrinter::AddRow(const vector<string>& row) {
43  DCHECK_EQ(row.size(), labels_.size());
44  rows_.push_back(row);
45  for (int i = 0; i < row.size(); ++i) {
46  if (row[i].size() > max_col_widths_[i]) max_col_widths_[i] = row[i].size();
47  }
48 }
49 
50 void TablePrinter::PrintRow(stringstream* s, const vector<string>& row,
51  const vector<int>& widths) const {
52  DCHECK_EQ(row.size(), labels_.size());
53  stringstream& ss = *s;
54  for (int i = 0; i < row.size(); ++i) {
55  if (left_align_[i]) {
56  ss << std::left;
57  } else {
58  ss << std::right;
59  }
60  ss << setw(widths[i]);
61  stringstream tmp;
62  if (i != 0) tmp << " ";
63  if (row[i].size() > widths[i] - COLUMN_PAD) {
64  tmp << string(row[i].data(), widths[i] - COLUMN_PAD - 3) << "...";
65  } else {
66  tmp << row[i];
67  }
68  if (i != row.size() - 1) tmp << " ";
69  ss << tmp.str();
70  }
71 }
72 
73 string TablePrinter::ToString(const string& prefix) const {
74  vector<int> output_widths = max_col_widths_;
75  int total_width = 0;
76  for (int i = 0; i < output_widths.size(); ++i) {
77  if (max_output_width_ >= 0) {
78  output_widths[i] = std::min(output_widths[i], max_output_width_);
79  }
80  output_widths[i] += COLUMN_PAD;
81  total_width += output_widths[i];
82  }
83 
84  stringstream ss;
85  ss << prefix;
86 
87  // Print the labels and line after.
88  PrintRow(&ss, labels_, output_widths);
89  ss << endl;
90 
91  // Print line after labels
92  for (int i = 0; i < total_width; ++i) {
93  ss << "-";
94  }
95  ss << endl;
96 
97  // Print the rows
98  for (int i = 0; i < rows_.size(); ++i) {
99  PrintRow(&ss, rows_[i], output_widths);
100  if (i != rows_.size() - 1) ss << endl;
101  }
102 
103  return ss.str();
104 }
std::string ToString(const std::string &prefix="") const
Print to a table with prefix coming before the output.
void AddColumn(const std::string &label, bool left_align)
std::vector< std::vector< std::string > > rows_
Definition: table-printer.h:53
std::vector< bool > left_align_
For each column, true if the value should be left aligned, right aligned otherwise.
Definition: table-printer.h:48
std::vector< std::string > labels_
Definition: table-printer.h:46
int max_output_width_
-1 to indicate unlimited.
Definition: table-printer.h:51
std::vector< int > max_col_widths_
Definition: table-printer.h:54
void AddRow(const std::vector< std::string > &row)
Add a row to the table. This must have the same width as labels.
void set_max_output_width(int width)
const int COLUMN_PAD
void PrintRow(std::stringstream *ss, const std::vector< std::string > &row, const std::vector< int > &widths) const
Helper function to print one row to ss.