Skip to content

The cell width should be calculated using the data when applying a formatter #2

@koffeinfrei

Description

@koffeinfrei

The documentation

formatter: a Proc to format the cells, while conserving the inferred alignment from the body contents. Default to "->(n : CellType) { n.to_s }"

suggests that the value from the fomatter does not influence the calculation / alignment of the cell.

When using a formatter to output the value with a color (using Crystal's Colorize) the cell width is not calculated based on the data value, but based on the value provided by the formatter.

Tablo::Table.new(table_data, connectors: Tablo::CONNECTORS_SINGLE_ROUNDED) do |t|
  t.add_column("Diff", width: COLUMN_WIDTH, formatter: ->(n : Tablo::CellType) { n.colorize(:red).to_s }) { |n| n[1] }
end

Expected output:
image

Actual output:
image

Tabulo also mentions this:

In most terminals, if you want to print text that is coloured, or has certain other styles such as underlining, you need to use ANSI escape sequences, either directly, or by means of a library such as Rainbow that uses them internally. Tabulo needs to properly account for escape sequences when performing the width calculations required to render tables. The styler option on the add_column method is intended to facilitate this.

Is this the intended behaviour for tablo as well? If so the width calculation should be based on the data value instead of the formatted value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions