MCPcopy
hub / github.com/Textualize/rich / _calculate_column_widths

Method _calculate_column_widths

rich/table.py:523–586  ·  view source on GitHub ↗

Calculate the widths of each column, including padding, not including borders.

(
        self, console: "Console", options: "ConsoleOptions"
    )

Source from the content-addressed store, hash-verified

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

Callers 2

__rich_measure__Method · 0.95
__rich_console__Method · 0.95

Calls 5

_measure_columnMethod · 0.95
_collapse_widthsMethod · 0.95
ratio_distributeFunction · 0.85
ratio_reduceFunction · 0.85
update_widthMethod · 0.80

Tested by

no test coverage detected