]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/rich/columns.py
1 from collections
import defaultdict
2 from itertools
import chain
3 from operator
import itemgetter
4 from typing
import Dict
, Iterable
, List
, Optional
, Tuple
6 from .align
import Align
, AlignMethod
7 from .console
import Console
, ConsoleOptions
, RenderableType
, RenderResult
8 from .constrain
import Constrain
9 from .measure
import Measurement
10 from .padding
import Padding
, PaddingDimensions
11 from .table
import Table
12 from .text
import TextType
13 from .jupyter
import JupyterMixin
16 class Columns(JupyterMixin
):
17 """Display renderables in neat columns.
20 renderables (Iterable[RenderableType]): Any number of Rich renderables (including str).
21 width (int, optional): The desired width of the columns, or None to auto detect. Defaults to None.
22 padding (PaddingDimensions, optional): Optional padding around cells. Defaults to (0, 1).
23 expand (bool, optional): Expand columns to full width. Defaults to False.
24 equal (bool, optional): Arrange in to equal sized columns. Defaults to False.
25 column_first (bool, optional): Align items from top to bottom (rather than left to right). Defaults to False.
26 right_to_left (bool, optional): Start column from right hand side. Defaults to False.
27 align (str, optional): Align value ("left", "right", or "center") or None for default. Defaults to None.
28 title (TextType, optional): Optional title for Columns.
33 renderables
: Optional
[Iterable
[RenderableType
]] = None,
34 padding
: PaddingDimensions
= (0, 1),
36 width
: Optional
[int] = None,
39 column_first
: bool = False,
40 right_to_left
: bool = False,
41 align
: Optional
[AlignMethod
] = None,
42 title
: Optional
[TextType
] = None,
44 self
.renderables
= list(renderables
or [])
46 self
.padding
= padding
49 self
.column_first
= column_first
50 self
.right_to_left
= right_to_left
51 self
.align
: Optional
[AlignMethod
] = align
54 def add_renderable(self
, renderable
: RenderableType
) -> None:
55 """Add a renderable to the columns.
58 renderable (RenderableType): Any renderable object.
60 self
.renderables
.append(renderable
)
63 self
, console
: Console
, options
: ConsoleOptions
65 render_str
= console
.render_str
67 render_str(renderable
) if isinstance(renderable
, str) else renderable
68 for renderable
in self
.renderables
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
)
78 get_measurement
= Measurement
.get
80 get_measurement(console
, options
, renderable
).maximum
81 for renderable
in renderables
84 renderable_widths
= [max(renderable_widths
)] * len(renderable_widths
)
88 ) -> Iterable
[Tuple
[int, Optional
[RenderableType
]]]:
89 item_count
= len(renderables
)
91 width_renderables
= list(zip(renderable_widths
, renderables
))
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
97 row_count
= (item_count
+ column_count
- 1) // column_count
98 cells
= [[-1] * column_count
for _
in range(row_count
)]
100 for index
in range(item_count
):
101 cells
[row
][col
] = index
102 column_lengths
[col
] -= 1
103 if column_lengths
[col
]:
108 for index
in chain
.from_iterable(cells
):
111 yield width_renderables
[index
]
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
)):
119 table
= Table
.grid(padding
=self
.padding
, collapse_padding
=True, pad_edge
=False)
120 table
.expand
= self
.expand
121 table
.title
= self
.title
123 if self
.width
is not None:
124 column_count
= (max_width
) // (self
.width
+ width_padding
)
125 for _
in range(column_count
):
126 table
.add_column(width
=self
.width
)
128 while column_count
> 1:
131 for renderable_width
, _
in iter_renderables(column_count
):
132 widths
[column_no
] = max(widths
[column_no
], renderable_width
)
133 total_width
= sum(widths
.values()) + width_padding
* (
136 if total_width
> max_width
:
137 column_count
= len(widths
) - 1
140 column_no
= (column_no
+ 1) % column_count
144 get_renderable
= itemgetter(1)
146 get_renderable(_renderable
)
147 for _renderable
in iter_renderables(column_count
)
152 if renderable
is None
153 else Constrain(renderable
, renderable_widths
[0])
154 for renderable
in _renderables
160 None if renderable
is None else _Align(renderable
, align
)
161 for renderable
in _renderables
164 right_to_left
= self
.right_to_left
165 add_row
= table
.add_row
166 for start
in range(0, len(_renderables
), column_count
):
167 row
= _renderables
[start
: start
+ column_count
]
174 if __name__
== "__main__": # pragma: no cover
179 files
= [f
"{i} {s}" for i
, s
in enumerate(sorted(os
.listdir()))]
180 columns
= Columns(files
, padding
=(0, 1), expand
=False, equal
=False)
181 console
.print(columns
)
183 columns
.column_first
= True
184 console
.print(columns
)
185 columns
.right_to_left
= True
187 console
.print(columns
)