add setting output_format_pretty_row_numbers

This commit is contained in:
feng lv 2020-09-29 20:30:36 +08:00
parent b4f1eb07ff
commit 4f000388a7
6 changed files with 39 additions and 0 deletions

View File

@ -468,6 +468,7 @@ class IColumn;
M(Bool, output_format_enable_streaming, false, "Enable streaming in output formats that support it.", 0) \
M(Bool, output_format_write_statistics, true, "Write statistics about read rows, bytes, time elapsed in suitable output formats.", 0) \
M(Bool, allow_non_metadata_alters, true, "Allow to execute alters which affects not only tables metadata, but also data on disk", 0) \
M(Bool, output_format_pretty_row_numbers, false, "Add row numbers before each row for pretty output format", 0) \
#define LIST_OF_SETTINGS(M) \
COMMON_SETTINGS(M) \

View File

@ -107,6 +107,7 @@ static FormatSettings getOutputFormatSetting(const Settings & settings, const Co
format_settings.pretty.charset = settings.output_format_pretty_grid_charset.toString() == "ASCII" ?
FormatSettings::Pretty::Charset::ASCII :
FormatSettings::Pretty::Charset::UTF8;
format_settings.pretty.output_format_pretty_row_numbers = settings.output_format_pretty_row_numbers;
format_settings.template_settings.resultset_format = settings.format_template_resultset;
format_settings.template_settings.row_format = settings.format_template_row;
format_settings.template_settings.row_between_delimiter = settings.format_template_rows_between_delimiter;

View File

@ -45,6 +45,8 @@ struct FormatSettings
UInt64 max_value_width = 10000;
bool color = true;
bool output_format_pretty_row_numbers = false;
enum class Charset
{
UTF8,

View File

@ -33,6 +33,10 @@ void PrettyBlockOutputFormat::calculateWidths(
WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths)
{
size_t num_rows = std::min(chunk.getNumRows(), format_settings.pretty.max_rows);
auto max_row_number = std::to_string(num_rows);
row_number_width = max_row_number.size() + 2;
size_t num_columns = chunk.getNumColumns();
const auto & columns = chunk.getColumns();

View File

@ -33,6 +33,8 @@ protected:
size_t terminal_width = 0;
bool suffix_written = false;
size_t row_number_width = 7; // "10000. "
const FormatSettings format_settings;
using Widths = PODArray<size_t>;

View File

@ -69,6 +69,15 @@ void PrettyCompactBlockOutputFormat::writeHeader(
const Widths & max_widths,
const Widths & name_widths)
{
if (format_settings.pretty.output_format_pretty_row_numbers)
{
/// Write left blank
for (size_t i = 0; i < row_number_width; ++i)
{
writeCString(" ", out);
}
}
const GridSymbols & grid_symbols = format_settings.pretty.charset == FormatSettings::Pretty::Charset::UTF8 ?
utf8_grid_symbols :
ascii_grid_symbols;
@ -117,6 +126,15 @@ void PrettyCompactBlockOutputFormat::writeHeader(
void PrettyCompactBlockOutputFormat::writeBottom(const Widths & max_widths)
{
if (format_settings.pretty.output_format_pretty_row_numbers)
{
/// Write left blank
for (size_t i = 0; i < row_number_width; ++i)
{
writeCString(" ", out);
}
}
const GridSymbols & grid_symbols = format_settings.pretty.charset == FormatSettings::Pretty::Charset::UTF8 ?
utf8_grid_symbols :
ascii_grid_symbols;
@ -144,6 +162,17 @@ void PrettyCompactBlockOutputFormat::writeRow(
const WidthsPerColumn & widths,
const Widths & max_widths)
{
if (format_settings.pretty.output_format_pretty_row_numbers)
{
// Write row number;
auto row_num_string = std::to_string(row_num + 1) + ". ";
for (size_t i = 0; i < row_number_width - row_num_string.size(); ++i)
{
writeCString(" ", out);
}
writeString(row_num_string, out);
}
const GridSymbols & grid_symbols = format_settings.pretty.charset == FormatSettings::Pretty::Charset::UTF8 ?
utf8_grid_symbols :
ascii_grid_symbols;