Calculate the widths of each column, including padding, not including borders.
(
self, console: "Console", options: "ConsoleOptions"
)
| 521 | ) |
| 522 | |
| 523 | def _calculate_column_widths( |
| 524 | self, console: "Console", options: "ConsoleOptions" |
| 525 | ) -> List[int]: |
| 526 | """Calculate the widths of each column, including padding, not including borders.""" |
| 527 | max_width = options.max_width |
| 528 | columns = self.columns |
| 529 | width_ranges = [ |
| 530 | self._measure_column(console, options, column) for column in columns |
| 531 | ] |
| 532 | widths = [_range.maximum or 1 for _range in width_ranges] |
| 533 | |
| 534 | get_padding_width = self._get_padding_width |
| 535 | extra_width = self._extra_width |
| 536 | if self.expand: |
| 537 | ratios = [col.ratio or 0 for col in columns if col.flexible] |
| 538 | if any(ratios): |
| 539 | fixed_widths = [ |
| 540 | 0 if column.flexible else _range.maximum |
| 541 | for _range, column in zip(width_ranges, columns) |
| 542 | ] |
| 543 | flex_minimum = [ |
| 544 | (column.width or 1) + get_padding_width(column._index) |
| 545 | for column in columns |
| 546 | if column.flexible |
| 547 | ] |
| 548 | flexible_width = max_width - sum(fixed_widths) |
| 549 | flex_widths = ratio_distribute(flexible_width, ratios, flex_minimum) |
| 550 | iter_flex_widths = iter(flex_widths) |
| 551 | for index, column in enumerate(columns): |
| 552 | if column.flexible: |
| 553 | widths[index] = fixed_widths[index] + next(iter_flex_widths) |
| 554 | table_width = sum(widths) |
| 555 | |
| 556 | if table_width > max_width: |
| 557 | widths = self._collapse_widths( |
| 558 | widths, |
| 559 | [(column.width is None and not column.no_wrap) for column in columns], |
| 560 | max_width, |
| 561 | ) |
| 562 | table_width = sum(widths) |
| 563 | # last resort, reduce columns evenly |
| 564 | if table_width > max_width: |
| 565 | excess_width = table_width - max_width |
| 566 | widths = ratio_reduce(excess_width, [1] * len(widths), widths, widths) |
| 567 | table_width = sum(widths) |
| 568 | |
| 569 | width_ranges = [ |
| 570 | self._measure_column(console, options.update_width(width), column) |
| 571 | for width, column in zip(widths, columns) |
| 572 | ] |
| 573 | widths = [_range.maximum or 0 for _range in width_ranges] |
| 574 | |
| 575 | if (table_width < max_width and self.expand) or ( |
| 576 | self.min_width is not None and table_width < (self.min_width - extra_width) |
| 577 | ): |
| 578 | _max_width = ( |
| 579 | max_width |
| 580 | if self.min_width is None |
no test coverage detected