@@ -24,7 +24,7 @@ PrettyBlockOutputFormat::PrettyBlockOutputFormat(
2424// / Note that number of code points is just a rough approximation of visible string width.
2525void PrettyBlockOutputFormat::calculateWidths (
2626 const Block & header, const Chunk & chunk,
27- WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths)
27+ WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths, size_t table_border_width )
2828{
2929 size_t num_rows = std::min (chunk.getNumRows (), format_settings.pretty .max_rows );
3030
@@ -42,7 +42,7 @@ void PrettyBlockOutputFormat::calculateWidths(
4242
4343 // / Calculate widths of all values.
4444 String serialized_value;
45- size_t prefix = 2 ; // Tab character adjustment
45+ size_t prefix = format_settings. pretty . output_format_pretty_row_numbers ? row_number_width + table_border_width : table_border_width ; // Tab character adjustment
4646 for (size_t i = 0 ; i < num_columns; ++i)
4747 {
4848 const auto & elem = header.getByPosition (i);
@@ -187,7 +187,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
187187 WidthsPerColumn widths;
188188 Widths max_widths;
189189 Widths name_widths;
190- calculateWidths (header, chunk, widths, max_widths, name_widths);
190+ calculateWidths (header, chunk, widths, max_widths, name_widths, 2 );
191191
192192 const GridSymbols & grid_symbols = format_settings.pretty .charset == FormatSettings::Pretty::Charset::UTF8 ?
193193 utf8_grid_symbols :
@@ -321,6 +321,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
321321
322322 std::vector<String> transferred_row (num_columns);
323323 bool has_transferred_row = false ;
324+ size_t prefix = format_settings.pretty .output_format_pretty_row_numbers ? row_number_width + 2 : 2 ;
324325
325326 for (size_t j = 0 ; j < num_columns; ++j)
326327 {
@@ -334,11 +335,13 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
334335 serializations[j]->serializeText (*columns[j], i, out_serialize, format_settings);
335336 }
336337 if (cut_to_width)
337- splitValueAtBreakLine (serialized_value, transferred_row[j], cur_width);
338- has_transferred_row |= !transferred_row[j].empty () && cur_width <= cut_to_width ;
338+ splitValueAtBreakLine (serialized_value, transferred_row[j], cur_width, cut_to_width, prefix );
339+ has_transferred_row |= !transferred_row[j].empty ();
339340
340341 writeValueWithPadding (serialized_value, cur_width, max_widths[j], cut_to_width,
341342 type.shouldAlignRightInPrettyFormats (), isNumber (type), !transferred_row[j].empty (), false );
343+
344+ prefix += max_widths[j] + 3 ;
342345 }
343346
344347 writeCString (grid_symbols.bar , out);
@@ -499,6 +502,7 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
499502
500503 std::vector<String> new_transferred_row (num_columns);
501504 bool has_transferred_row = false ;
505+ size_t prefix = format_settings.pretty .output_format_pretty_row_numbers ? row_number_width + 2 : 2 ;
502506
503507 for (size_t j = 0 ; j < num_columns; ++j)
504508 {
@@ -510,11 +514,13 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
510514 const auto & type = *header.getByPosition (j).type ;
511515 size_t cur_width = UTF8::computeWidth (reinterpret_cast <const UInt8 *>(transferred_row[j].data ()), transferred_row[j].size ());
512516 if (cut_to_width)
513- splitValueAtBreakLine (transferred_row[j], new_transferred_row[j], cur_width);
514- has_transferred_row |= !new_transferred_row[j].empty () && cur_width <= cut_to_width ;
517+ splitValueAtBreakLine (transferred_row[j], new_transferred_row[j], cur_width, cut_to_width, prefix );
518+ has_transferred_row |= !new_transferred_row[j].empty ();
515519
516520 writeValueWithPadding (transferred_row[j], cur_width, max_widths[j], cut_to_width,
517521 type.shouldAlignRightInPrettyFormats (), isNumber (type), !new_transferred_row[j].empty (), !transferred_row[j].empty ());
522+
523+ prefix += max_widths[j] + 3 ;
518524 }
519525
520526 if (!space_block)
@@ -525,13 +531,14 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
525531 writeTransferredRow (max_widths, header, new_transferred_row, cut_to_width, space_block);
526532}
527533
528- void PrettyBlockOutputFormat::splitValueAtBreakLine (String & value, String & transferred_value, size_t & value_width)
534+ void PrettyBlockOutputFormat::splitValueAtBreakLine (String & value, String & transferred_value, size_t & value_width, size_t cut_to_width, size_t prefix )
529535{
530536 if (size_t break_line_pos = value.find_first_of (' \n ' ); break_line_pos != String::npos)
531537 {
532- transferred_value = value.substr (break_line_pos + 1 );
538+ value_width = UTF8::computeWidth (reinterpret_cast <const UInt8 *>(value.data ()), break_line_pos, prefix);
539+ if (value_width <= cut_to_width)
540+ transferred_value = value.substr (break_line_pos + 1 );
533541 value = value.substr (0 , break_line_pos);
534- value_width = UTF8::computeWidth (reinterpret_cast <const UInt8 *>(value.data ()), value.size ());
535542 }
536543}
537544
0 commit comments