void PrettyCompactBlockOutputStream::write(const Block & block)
{
    UInt64 max_rows = format_settings.pretty.max_rows;

    if (total_rows >= max_rows)
    {
        total_rows += block.rows();
        return;
    }

    size_t rows = block.rows();

    WidthsPerColumn widths;
    Widths max_widths;
    Widths name_widths;
    calculateWidths(block, widths, max_widths, name_widths, format_settings);

    writeHeader(block, max_widths, name_widths);

    for (size_t i = 0; i < rows && total_rows + i < max_rows; ++i)
        writeRow(i, block, widths, max_widths);

    writeBottom(max_widths);

    total_rows += rows;
}
void PrettyCompactMonoBlockOutputStream::writeSuffix()
{
	if (blocks.empty())
		return;

	Widths_t max_widths;
	Widths_t name_widths;

	for (size_t i = 0; i < blocks.size(); ++i)
		calculateWidths(blocks[i], max_widths, name_widths);

	writeHeader(blocks.front(), max_widths, name_widths);

	size_t row_count = 0;

	for (size_t block_id = 0; block_id < blocks.size() && row_count < max_rows; ++block_id)
	{
		const Block & block = blocks[block_id];
		size_t rows = block.rows();

		for (size_t i = 0; i < rows && row_count < max_rows; ++i)
		{
			writeRow(i, block, max_widths, name_widths);
			++row_count;
		}
	}

	writeBottom(max_widths);

	if (total_rows >= max_rows)
	{
		writeCString("  Showed first ", ostr);
		writeIntText(max_rows, ostr);
		writeCString(".\n", ostr);
	}

	total_rows = 0;

	if (totals)
	{
		writeCString("\nTotals:\n", ostr);
		PrettyCompactBlockOutputStream::write(totals);
	}

	if (extremes)
	{
		writeCString("\nExtremes:\n", ostr);
		PrettyCompactBlockOutputStream::write(extremes);
	}
}
void PrettySpaceBlockOutputStream::write(const Block & block)
{
    if (total_rows >= max_rows)
    {
        total_rows += block.rows();
        return;
    }

    size_t rows = block.rows();
    size_t columns = block.columns();

    WidthsPerColumn widths;
    Widths max_widths;
    Widths name_widths;
    calculateWidths(block, widths, max_widths, name_widths);

    /// Do not align on too long values.
    if (terminal_width > 80)
        for (size_t i = 0; i < columns; ++i)
            if (max_widths[i] > terminal_width / 2)
                max_widths[i] = terminal_width / 2;

    /// Names
    for (size_t i = 0; i < columns; ++i)
    {
        if (i != 0)
            writeCString("   ", ostr);

        const ColumnWithTypeAndName & col = block.getByPosition(i);

        if (col.type->shouldAlignRightInPrettyFormats())
        {
            for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
                writeChar(' ', ostr);

            if (!no_escapes)
                writeCString("\033[1m", ostr);
            writeEscapedString(col.name, ostr);
            if (!no_escapes)
                writeCString("\033[0m", ostr);
        }
        else
        {
            if (!no_escapes)
                writeCString("\033[1m", ostr);
            writeEscapedString(col.name, ostr);
            if (!no_escapes)
                writeCString("\033[0m", ostr);

            for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
                writeChar(' ', ostr);
        }
    }
    writeCString("\n\n", ostr);

    for (size_t i = 0; i < rows && total_rows + i < max_rows; ++i)
    {
        for (size_t j = 0; j < columns; ++j)
        {
            if (j != 0)
                writeCString("   ", ostr);

            writeValueWithPadding(block.getByPosition(j), i, widths[j].empty() ? max_widths[j] : widths[j][i], max_widths[j]);
        }

        writeChar('\n', ostr);
    }

    total_rows += rows;
}