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

Method __rich_console__

rich/columns.py:62–171  ·  view source on GitHub ↗
(
        self, console: Console, options: ConsoleOptions
    )

Source from the content-addressed store, hash-verified

60 self.renderables.append(renderable)
61
62 def __rich_console__(
63 self, console: Console, options: ConsoleOptions
64 ) -> RenderResult:
65 render_str = console.render_str
66 renderables = [
67 render_str(renderable) if isinstance(renderable, str) else renderable
68 for renderable in self.renderables
69 ]
70 if not renderables:
71 return
72 _top, right, _bottom, left = Padding.unpack(self.padding)
73 width_padding = max(left, right)
74 max_width = options.max_width
75 widths: Dict[int, int] = defaultdict(int)
76 column_count = len(renderables)
77
78 get_measurement = Measurement.get
79 renderable_widths = [
80 get_measurement(console, options, renderable).maximum
81 for renderable in renderables
82 ]
83 if self.equal:
84 renderable_widths = [max(renderable_widths)] * len(renderable_widths)
85
86 def iter_renderables(
87 column_count: int,
88 ) -> Iterable[Tuple[int, Optional[RenderableType]]]:
89 item_count = len(renderables)
90 if self.column_first:
91 width_renderables = list(zip(renderable_widths, renderables))
92
93 column_lengths: List[int] = [item_count // column_count] * column_count
94 for col_no in range(item_count % column_count):
95 column_lengths[col_no] += 1
96
97 row_count = (item_count + column_count - 1) // column_count
98 cells = [[-1] * column_count for _ in range(row_count)]
99 row = col = 0
100 for index in range(item_count):
101 cells[row][col] = index
102 column_lengths[col] -= 1
103 if column_lengths[col]:
104 row += 1
105 else:
106 col += 1
107 row = 0
108 for index in chain.from_iterable(cells):
109 if index == -1:
110 break
111 yield width_renderables[index]
112 else:
113 yield from zip(renderable_widths, renderables)
114 # Pad odd elements with spaces
115 if item_count % column_count:
116 for _ in range(column_count - (item_count % column_count)):
117 yield 0, None
118
119 table = Table.grid(padding=self.padding, collapse_padding=True, pad_edge=False)

Callers

nothing calls this directly

Calls 5

ConstrainClass · 0.85
unpackMethod · 0.80
gridMethod · 0.80
add_columnMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected