Skip to content

Commit

Permalink
ClickHouse#58363 - removed switch from PrettyBlockOutputFormat and mo…
Browse files Browse the repository at this point in the history
…dified BlockOutputFormats to use color variable. Updated english and russian documentation. Updated test 00405 reference file.
  • Loading branch information
Blargian committed Jan 12, 2024
1 parent aa8876a commit 72b5cf5
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 52 deletions.
8 changes: 7 additions & 1 deletion docs/en/operations/settings/settings-formats.md
Expand Up @@ -1569,7 +1569,13 @@ Result:

Use ANSI escape sequences to paint colors in Pretty formats.

Enabled by default.
possible values:

- `0` — Disabled. Pretty formats do not use ANSI escape sequences.
- `1` — Enabled. Pretty formats will use ANSI escape sequences except for `NoEscapes` formats.
- `auto` - Enabled if `stdout` is a terminal except for `NoEscapes` formats.

Default value is `auto`.

### output_format_pretty_grid_charset {#output_format_pretty_grid_charset}

Expand Down
11 changes: 11 additions & 0 deletions docs/ru/operations/settings/settings.md
Expand Up @@ -2796,6 +2796,17 @@ SELECT TOP 3 name, value FROM system.settings;
3. │ max_block_size │ 65505 │
└─────────────────────────┴─────────┘
```
### output_format_pretty_color {#output_format_pretty_color}

Включает/выключает управляющие последовательности ANSI в форматах Pretty.

Возможные значения:

- `0` — выключена. Не исползует ANSI последовательности в форматах Pretty.
- `1` — включена. Исползует ANSI последовательности с исключением форматов `NoEscapes`.
- `auto` - включена если `stdout` является терминалом с исключением форматов `NoEscapes`.

Значение по умолчанию: `auto`

## system_events_show_zero_values {#system_events_show_zero_values}

Expand Down
2 changes: 1 addition & 1 deletion src/Formats/FormatFactory.cpp
Expand Up @@ -143,7 +143,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings)
format_settings.parquet.write_batch_size = settings.output_format_parquet_batch_size;
format_settings.parquet.local_read_min_bytes_for_seek = settings.input_format_parquet_local_file_min_bytes_for_seek;
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_color = settings.output_format_pretty_color;
format_settings.pretty.color = settings.output_format_pretty_color;
format_settings.pretty.max_column_pad_width = settings.output_format_pretty_max_column_pad_width;
format_settings.pretty.max_rows = settings.output_format_pretty_max_rows;
format_settings.pretty.max_value_width = settings.output_format_pretty_max_value_width;
Expand Down
4 changes: 1 addition & 3 deletions src/Formats/FormatSettings.h
Expand Up @@ -269,9 +269,7 @@ struct FormatSettings
UInt64 max_rows = 10000;
UInt64 max_column_pad_width = 250;
UInt64 max_value_width = 10000;
bool color = true;

SettingFieldUInt64Auto output_format_pretty_color{"auto"};
SettingFieldUInt64Auto color{"auto"};

bool output_format_pretty_row_numbers = false;

Expand Down
10 changes: 5 additions & 5 deletions src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp
Expand Up @@ -12,8 +12,8 @@ namespace DB
{

PrettyBlockOutputFormat::PrettyBlockOutputFormat(
WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool mono_block_)
: IOutputFormat(header_, out_), format_settings(format_settings_), serializations(header_.getSerializations()), mono_block(mono_block_)
WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool mono_block_, bool color_)
: IOutputFormat(header_, out_), format_settings(format_settings_), serializations(header_.getSerializations()), color(color_), mono_block(mono_block_)
{
}

Expand Down Expand Up @@ -237,7 +237,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind

const auto & col = header.getByPosition(i);

if (format_settings.pretty.color)
if (color)
writeCString("\033[1m", out);

if (col.type->shouldAlignRightInPrettyFormats())
Expand All @@ -255,7 +255,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
writeChar(' ', out);
}

if (format_settings.pretty.color)
if (color)
writeCString("\033[0m", out);
}
writeCString(" ", out);
Expand Down Expand Up @@ -335,7 +335,7 @@ void PrettyBlockOutputFormat::writeValueWithPadding(
reinterpret_cast<const UInt8 *>(serialized_value.data()), serialized_value.size(), 0, 1 + format_settings.pretty.max_value_width));

const char * ellipsis = format_settings.pretty.charset == FormatSettings::Pretty::Charset::UTF8 ? "" : "~";
if (format_settings.pretty.color)
if (color)
{
serialized_value += "\033[31;1m";
serialized_value += ellipsis;
Expand Down
29 changes: 7 additions & 22 deletions src/Processors/Formats/Impl/PrettyBlockOutputFormat.h
Expand Up @@ -19,10 +19,10 @@ class PrettyBlockOutputFormat : public IOutputFormat
{
public:
/// no_escapes - do not use ANSI escape sequences - to display in the browser, not in the console.
PrettyBlockOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool mono_block_);
PrettyBlockOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool mono_block_, bool color_);

String getName() const override { return "PrettyBlockOutputFormat"; }

protected:
void consume(Chunk) override;
void consumeTotals(Chunk) override;
Expand Down Expand Up @@ -57,7 +57,9 @@ class PrettyBlockOutputFormat : public IOutputFormat
total_rows = 0;
}

private:
bool color;

private:
bool mono_block;
/// For mono_block == true only
Chunk mono_chunk;
Expand All @@ -73,25 +75,8 @@ void registerPrettyFormatWithNoEscapesAndMonoBlock(FormatFactory & factory, cons
const Block & sample,
const FormatSettings & format_settings)
{
FormatSettings changed_settings = format_settings;
auto value = format_settings.pretty.output_format_pretty_color.valueOr(2);
switch (value)
{
case 0:
changed_settings.pretty.color = false;
break;
case 1:
changed_settings.pretty.color = no_escapes ? false : true;
break;
case 2:
changed_settings.pretty.color = isWritingToTerminal(buf) || no_escapes ? false : true;
break;
}
if (!changed_settings.pretty.color)
{
return std::make_shared<OutputFormat>(buf, sample, changed_settings, mono_block);
}
return std::make_shared<OutputFormat>(buf, sample, format_settings, mono_block);
bool color = !no_escapes && format_settings.pretty.color.valueOr(isWritingToTerminal(buf));
return std::make_shared<OutputFormat>(buf, sample, format_settings, mono_block, color);
});
if (!mono_block)
factory.markOutputFormatSupportsParallelFormatting(name);
Expand Down
12 changes: 6 additions & 6 deletions src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp
Expand Up @@ -48,8 +48,8 @@ GridSymbols ascii_grid_symbols {

}

PrettyCompactBlockOutputFormat::PrettyCompactBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_)
: PrettyBlockOutputFormat(out_, header, format_settings_, mono_block_)
PrettyCompactBlockOutputFormat::PrettyCompactBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_, bool color_)
: PrettyBlockOutputFormat(out_, header, format_settings_, mono_block_, color_)
{
}

Expand Down Expand Up @@ -87,18 +87,18 @@ void PrettyCompactBlockOutputFormat::writeHeader(
for (size_t k = 0; k < max_widths[i] - name_widths[i]; ++k)
writeCString(grid_symbols.dash, out);

if (format_settings.pretty.color)
if (color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
if (color)
writeCString("\033[0m", out);
}
else
{
if (format_settings.pretty.color)
if (color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
if (color)
writeCString("\033[0m", out);

for (size_t k = 0; k < max_widths[i] - name_widths[i]; ++k)
Expand Down
Expand Up @@ -13,7 +13,7 @@ namespace DB
class PrettyCompactBlockOutputFormat : public PrettyBlockOutputFormat
{
public:
PrettyCompactBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_);
PrettyCompactBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_, bool color);
String getName() const override { return "PrettyCompactBlockOutputFormat"; }

private:
Expand Down
8 changes: 4 additions & 4 deletions src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp
Expand Up @@ -48,18 +48,18 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port
for (ssize_t k = 0; k < std::max(0z, static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
writeChar(' ', out);

if (format_settings.pretty.color)
if (color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
if (color)
writeCString("\033[0m", out);
}
else
{
if (format_settings.pretty.color)
if (color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
if (color)
writeCString("\033[0m", out);

for (ssize_t k = 0; k < std::max(0z, static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
Expand Down
4 changes: 2 additions & 2 deletions src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.h
Expand Up @@ -11,8 +11,8 @@ namespace DB
class PrettySpaceBlockOutputFormat : public PrettyBlockOutputFormat
{
public:
PrettySpaceBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_)
: PrettyBlockOutputFormat(out_, header, format_settings_, mono_block_) {}
PrettySpaceBlockOutputFormat(WriteBuffer & out_, const Block & header, const FormatSettings & format_settings_, bool mono_block_, bool color_)
: PrettyBlockOutputFormat(out_, header, format_settings_, mono_block_, color_) {}

String getName() const override { return "PrettySpaceBlockOutputFormat"; }

Expand Down
Expand Up @@ -242,7 +242,7 @@
9 9 (9,'9') ᴺᵁᴸᴸ
auto
┏━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
helloworldtuple sometimes_nulls
helloworldtuple sometimes_nulls
┡━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ 0 │ 0 │ (0,'0') │ ᴺᵁᴸᴸ │
├───────┼───────┼─────────┼─────────────────┤
Expand All @@ -255,7 +255,7 @@ auto
│ 4 │ 4 │ (4,'4') │ 1 │
└───────┴───────┴─────────┴─────────────────┘
┏━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
helloworldtuple sometimes_nulls
helloworldtuple sometimes_nulls
┡━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ 5 │ 5 │ (5,'5') │ 2 │
├───────┼───────┼─────────┼─────────────────┤
Expand All @@ -267,35 +267,35 @@ auto
├───────┼───────┼─────────┼─────────────────┤
│ 9 │ 9 │ (9,'9') │ ᴺᵁᴸᴸ │
└───────┴───────┴─────────┴─────────────────┘
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
│ 0 │ 0 │ (0,'0') │ ᴺᵁᴸᴸ │
│ 1 │ 1 │ (1,'1') │ 1 │
│ 2 │ 2 │ (2,'2') │ 2 │
│ 3 │ 3 │ (3,'3') │ ᴺᵁᴸᴸ │
│ 4 │ 4 │ (4,'4') │ 1 │
└───────┴───────┴─────────┴─────────────────┘
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
│ 5 │ 5 │ (5,'5') │ 2 │
│ 6 │ 6 │ (6,'6') │ ᴺᵁᴸᴸ │
│ 7 │ 7 │ (7,'7') │ 1 │
│ 8 │ 8 │ (8,'8') │ 2 │
│ 9 │ 9 │ (9,'9') │ ᴺᵁᴸᴸ │
└───────┴───────┴─────────┴─────────────────┘
hello world tuple sometimes_nulls
hello world tuple sometimes_nulls

0 0 (0,'0') ᴺᵁᴸᴸ
1 1 (1,'1') 1
2 2 (2,'2') 2
3 3 (3,'3') ᴺᵁᴸᴸ
4 4 (4,'4') 1
hello world tuple sometimes_nulls
hello world tuple sometimes_nulls

5 5 (5,'5') 2
6 6 (6,'6') ᴺᵁᴸᴸ
7 7 (7,'7') 1
8 8 (8,'8') 2
9 9 (9,'9') ᴺᵁᴸᴸ
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
┌─hello─┬─world─┬─tuple───┬─sometimes_nulls─┐
│ 0 │ 0 │ (0,'0') │ ᴺᵁᴸᴸ │
│ 1 │ 1 │ (1,'1') │ 1 │
│ 2 │ 2 │ (2,'2') │ 2 │
Expand Down

0 comments on commit 72b5cf5

Please sign in to comment.