Skip to content

Commit 966ebda

Browse files
committed
columns fixes
1 parent ad8523e commit 966ebda

File tree

8 files changed

+22
-17
lines changed

8 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [1.3.1] - Unreleased
8+
## [1.3.1] - 2020-06-01
99

1010
### Changed
1111

examples/columns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def get_content(user):
2121

2222
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"]
2323
print(users)
24-
user_renderables = [Panel(get_content(user), expand=False) for user in users]
24+
user_renderables = [Panel(get_content(user), expand=True) for user in users]
2525
print(Columns(user_renderables))

examples/listdir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ def make_filename_text(filename):
3131
]
3232
filenames.sort(key=lambda filename: filename.lower())
3333
filename_text = [make_filename_text(filename) for filename in filenames]
34-
columns = Columns(filename_text, equal=True, column_first=True, padding=(0, 1))
34+
columns = Columns(filename_text, equal=True, column_first=True)
3535
print(columns)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "1.3.0"
5+
version = "1.3.1"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <willmcgugan@gmail.com>"]
88
license = "MIT"

rich/columns.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Dict, Iterable, List, Optional, Tuple
55

66
from .console import Console, ConsoleOptions, RenderableType, RenderResult
7+
from .constrain import Constrain
78
from .measure import Measurement
89
from .padding import Padding, PaddingDimensions
910
from .table import Table
@@ -107,11 +108,6 @@ def iter_renderables(
107108
for _ in range(column_count):
108109
table.add_column(width=self.width)
109110
else:
110-
if self.equal:
111-
table.expand = True
112-
# for _ in range(column_count):
113-
# table.add_column(ratio=1)
114-
115111
while column_count > 1:
116112
widths.clear()
117113
column_no = 0
@@ -120,7 +116,6 @@ def iter_renderables(
120116
total_width = sum(widths.values()) + width_padding * (
121117
len(widths) - 1
122118
)
123-
table.width = total_width
124119
if total_width > max_width:
125120
column_count = len(widths) - 1
126121
break
@@ -129,16 +124,21 @@ def iter_renderables(
129124
else:
130125
break
131126

132-
column_count = max(column_count, 1)
133-
134-
add_row = table.add_row
135127
get_renderable = itemgetter(1)
136128
_renderables = [
137129
get_renderable(_renderable)
138130
for _renderable in iter_renderables(column_count)
139131
]
132+
if self.equal:
133+
_renderables = [
134+
None
135+
if renderable is None
136+
else Constrain(renderable, renderable_widths[0])
137+
for renderable in _renderables
138+
]
140139

141140
right_to_left = self.right_to_left
141+
add_row = table.add_row
142142
for start in range(0, len(_renderables), column_count):
143143
row = _renderables[start : start + column_count]
144144
if right_to_left:

rich/constrain.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
from .console import Console, ConsoleOptions, RenderableType, RenderResult
4+
from .measure import Measurement
45

56

67
class Constrain:
@@ -23,3 +24,10 @@ def __rich_console__(
2324
else:
2425
child_options = options.update(width=min(self.width, options.max_width))
2526
yield from console.render(self.renderable, child_options)
27+
28+
def __rich_measure__(self, console: Console, max_width: int) -> Measurement:
29+
if self.width is None:
30+
return Measurement.get(console, self.renderable, max_width)
31+
else:
32+
width = min(self.width, max_width)
33+
return Measurement(width, width)

rich/table.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ def __rich_console__(
333333
max_width = options.max_width
334334
if self.width is not None:
335335
max_width = min(self.width, max_width)
336-
337336
if self.box:
338337
max_width -= len(self.columns) - 1
339338
if self.show_edge:
@@ -377,7 +376,6 @@ def _calculate_column_widths(
377376

378377
padding_width = self.padding[1] + self.padding[3]
379378
if self.expand:
380-
381379
ratios = [col.ratio or 0 for col in columns if col.flexible]
382380
if any(ratios):
383381
fixed_widths = [
@@ -390,7 +388,6 @@ def _calculate_column_widths(
390388
if column.flexible
391389
]
392390
flexible_width = max_width - sum(fixed_widths)
393-
394391
flex_widths = ratio_divide(flexible_width, ratios, flex_minimum)
395392
iter_flex_widths = iter(flex_widths)
396393
for index, column in enumerate(columns):

tests/test_columns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def render():
5151

5252

5353
def test_render():
54-
expected = "Ursus americanus American buffalo Bison bison American crow \nCorvus brachyrhynchos American marten Martes americana American racer \nColuber constrictor American woodcock Scolopax minor Anaconda (unidentified)\nEunectes sp. Andean goose Chloephaga melanoptera Ant \nAnteater, australian spiny Tachyglossus aculeatus Anteater, giant Myrmecophaga tridactyla\n\nUrsus americanus American marten Scolopax minor Ant \nAmerican buffalo Martes americana Anaconda (unidentified) Anteater, australian spiny\nBison bison American racer Eunectes sp. Tachyglossus aculeatus \nAmerican crow Coluber constrictor Andean goose Anteater, giant \nCorvus brachyrhynchos American woodcock Chloephaga melanoptera Myrmecophaga tridactyla \n\nAnt Scolopax minor American marten Ursus americanus \nAnteater, australian spiny Anaconda (unidentified) Martes americana American buffalo \nTachyglossus aculeatus Eunectes sp. American racer Bison bison \nAnteater, giant Andean goose Coluber constrictor American crow \nMyrmecophaga tridactyla Chloephaga melanoptera American woodcock Corvus brachyrhynchos\n\nChloephaga melanoptera American racer Ursus americanus \nAnt Coluber constrictor American buffalo \nAnteater, australian spiny American woodcock Bison bison \nTachyglossus aculeatus Scolopax minor American crow \nAnteater, giant Anaconda (unidentified) Corvus brachyrhynchos \nMyrmecophaga tridactyla Eunectes sp. American marten \n Andean goose Martes americana \n\nTachyglossus Chloephaga Anaconda Coluber Corvus Ursus americanus\naculeatus melanoptera (unidentified) constrictor brachyrhynchos \nAnteater, giant Ant Eunectes sp. American American marten American buffalo\n woodcock \nMyrmecophaga Anteater, Andean goose Scolopax minor Martes americana Bison bison \ntridactyla australian spiny \n American racer American crow \n\n"
54+
expected = "Ursus americanus American buffalo Bison bison American crow \nCorvus brachyrhynchos American marten Martes americana American racer \nColuber constrictor American woodcock Scolopax minor Anaconda (unidentified)\nEunectes sp. Andean goose Chloephaga melanoptera Ant \nAnteater, australian spiny Tachyglossus aculeatus Anteater, giant Myrmecophaga tridactyla\n\nUrsus americanus American marten Scolopax minor Ant \nAmerican buffalo Martes americana Anaconda (unidentified) Anteater, australian spiny\nBison bison American racer Eunectes sp. Tachyglossus aculeatus \nAmerican crow Coluber constrictor Andean goose Anteater, giant \nCorvus brachyrhynchos American woodcock Chloephaga melanoptera Myrmecophaga tridactyla \n\nAnt Scolopax minor American marten Ursus americanus \nAnteater, australian spiny Anaconda (unidentified) Martes americana American buffalo \nTachyglossus aculeatus Eunectes sp. American racer Bison bison \nAnteater, giant Andean goose Coluber constrictor American crow \nMyrmecophaga tridactyla Chloephaga melanoptera American woodcock Corvus brachyrhynchos\n\nChloephaga melanoptera American racer Ursus americanus \nAnt Coluber constrictor American buffalo \nAnteater, australian spiny American woodcock Bison bison \nTachyglossus aculeatus Scolopax minor American crow \nAnteater, giant Anaconda (unidentified) Corvus brachyrhynchos \nMyrmecophaga tridactyla Eunectes sp. American marten \n Andean goose Martes americana \n\nTachyglossus Chloephaga Anaconda Coluber Corvus Ursus americanus\naculeatus melanoptera (unidentified) constrictor brachyrhynchos \nAnteater, giant Ant Eunectes sp. American American marten American buffalo\n woodcock \nMyrmecophaga Anteater, Andean goose Scolopax minor Martes americana Bison bison \ntridactyla australian spiny \n American racer American crow \n\n"
5555
assert render() == expected
5656

5757

0 commit comments

Comments
 (0)