1#include <Common/PODArray.h>
2#include <IO/WriteBuffer.h>
3#include <IO/WriteHelpers.h>
4#include <Formats/FormatFactory.h>
5#include <Processors/Formats/Impl/PrettySpaceBlockOutputFormat.h>
6
7
8namespace DB
9{
10
11
12void PrettySpaceBlockOutputFormat::write(const Chunk & chunk, PortKind port_kind)
13{
14 UInt64 max_rows = format_settings.pretty.max_rows;
15
16 if (total_rows >= max_rows)
17 {
18 total_rows += chunk.getNumRows();
19 return;
20 }
21
22 size_t num_rows = chunk.getNumRows();
23 size_t num_columns = chunk.getNumColumns();
24 auto & header = getPort(port_kind).getHeader();
25 auto & columns = chunk.getColumns();
26
27 WidthsPerColumn widths;
28 Widths max_widths;
29 Widths name_widths;
30 calculateWidths(header, chunk, widths, max_widths, name_widths);
31
32 /// Names
33 for (size_t i = 0; i < num_columns; ++i)
34 {
35 if (i != 0)
36 writeCString(" ", out);
37
38 const ColumnWithTypeAndName & col = header.getByPosition(i);
39
40 if (col.type->shouldAlignRightInPrettyFormats())
41 {
42 for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
43 writeChar(' ', out);
44
45 if (format_settings.pretty.color)
46 writeCString("\033[1m", out);
47 writeString(col.name, out);
48 if (format_settings.pretty.color)
49 writeCString("\033[0m", out);
50 }
51 else
52 {
53 if (format_settings.pretty.color)
54 writeCString("\033[1m", out);
55 writeString(col.name, out);
56 if (format_settings.pretty.color)
57 writeCString("\033[0m", out);
58
59 for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
60 writeChar(' ', out);
61 }
62 }
63 writeCString("\n\n", out);
64
65 for (size_t row = 0; row < num_rows && total_rows + row < max_rows; ++row)
66 {
67 for (size_t column = 0; column < num_columns; ++column)
68 {
69 if (column != 0)
70 writeCString(" ", out);
71
72 auto & type = *header.getByPosition(column).type;
73 auto & cur_width = widths[column].empty() ? max_widths[column] : widths[column][row];
74 writeValueWithPadding(*columns[column], type, row, cur_width, max_widths[column]);
75 }
76
77 writeChar('\n', out);
78 }
79
80 total_rows += num_rows;
81}
82
83
84void PrettySpaceBlockOutputFormat::writeSuffix()
85{
86 if (total_rows >= format_settings.pretty.max_rows)
87 {
88 writeCString("\nShowed first ", out);
89 writeIntText(format_settings.pretty.max_rows, out);
90 writeCString(".\n", out);
91 }
92}
93
94
95void registerOutputFormatProcessorPrettySpace(FormatFactory & factory)
96{
97 factory.registerOutputFormatProcessor("PrettySpace", [](
98 WriteBuffer & buf,
99 const Block & sample,
100 FormatFactory::WriteCallback,
101 const FormatSettings & format_settings)
102 {
103 return std::make_shared<PrettySpaceBlockOutputFormat>(buf, sample, format_settings);
104 });
105
106 factory.registerOutputFormatProcessor("PrettySpaceNoEscapes", [](
107 WriteBuffer & buf,
108 const Block & sample,
109 FormatFactory::WriteCallback,
110 const FormatSettings & format_settings)
111 {
112 FormatSettings changed_settings = format_settings;
113 changed_settings.pretty.color = false;
114 return std::make_shared<PrettySpaceBlockOutputFormat>(buf, sample, changed_settings);
115 });
116}
117
118}
119