mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 10:08:40 +00:00
columns fixes
This commit is contained in:
parent
ad8523ebaf
commit
966ebdaae5
8 changed files with 22 additions and 17 deletions
|
@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.3.1] - Unreleased
|
||||
## [1.3.1] - 2020-06-01
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
@ -21,5 +21,5 @@ def get_content(user):
|
|||
|
||||
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"]
|
||||
print(users)
|
||||
user_renderables = [Panel(get_content(user), expand=False) for user in users]
|
||||
user_renderables = [Panel(get_content(user), expand=True) for user in users]
|
||||
print(Columns(user_renderables))
|
||||
|
|
|
@ -31,5 +31,5 @@ else:
|
|||
]
|
||||
filenames.sort(key=lambda filename: filename.lower())
|
||||
filename_text = [make_filename_text(filename) for filename in filenames]
|
||||
columns = Columns(filename_text, equal=True, column_first=True, padding=(0, 1))
|
||||
columns = Columns(filename_text, equal=True, column_first=True)
|
||||
print(columns)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "rich"
|
||||
homepage = "https://github.com/willmcgugan/rich"
|
||||
documentation = "https://rich.readthedocs.io/en/latest/"
|
||||
version = "1.3.0"
|
||||
version = "1.3.1"
|
||||
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
|
||||
authors = ["Will McGugan <willmcgugan@gmail.com>"]
|
||||
license = "MIT"
|
||||
|
|
|
@ -4,6 +4,7 @@ from operator import itemgetter
|
|||
from typing import Dict, Iterable, List, Optional, Tuple
|
||||
|
||||
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
||||
from .constrain import Constrain
|
||||
from .measure import Measurement
|
||||
from .padding import Padding, PaddingDimensions
|
||||
from .table import Table
|
||||
|
@ -107,11 +108,6 @@ class Columns(JupyterMixin):
|
|||
for _ in range(column_count):
|
||||
table.add_column(width=self.width)
|
||||
else:
|
||||
if self.equal:
|
||||
table.expand = True
|
||||
# for _ in range(column_count):
|
||||
# table.add_column(ratio=1)
|
||||
|
||||
while column_count > 1:
|
||||
widths.clear()
|
||||
column_no = 0
|
||||
|
@ -120,7 +116,6 @@ class Columns(JupyterMixin):
|
|||
total_width = sum(widths.values()) + width_padding * (
|
||||
len(widths) - 1
|
||||
)
|
||||
table.width = total_width
|
||||
if total_width > max_width:
|
||||
column_count = len(widths) - 1
|
||||
break
|
||||
|
@ -129,16 +124,21 @@ class Columns(JupyterMixin):
|
|||
else:
|
||||
break
|
||||
|
||||
column_count = max(column_count, 1)
|
||||
|
||||
add_row = table.add_row
|
||||
get_renderable = itemgetter(1)
|
||||
_renderables = [
|
||||
get_renderable(_renderable)
|
||||
for _renderable in iter_renderables(column_count)
|
||||
]
|
||||
if self.equal:
|
||||
_renderables = [
|
||||
None
|
||||
if renderable is None
|
||||
else Constrain(renderable, renderable_widths[0])
|
||||
for renderable in _renderables
|
||||
]
|
||||
|
||||
right_to_left = self.right_to_left
|
||||
add_row = table.add_row
|
||||
for start in range(0, len(_renderables), column_count):
|
||||
row = _renderables[start : start + column_count]
|
||||
if right_to_left:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from typing import Optional
|
||||
|
||||
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
||||
from .measure import Measurement
|
||||
|
||||
|
||||
class Constrain:
|
||||
|
@ -23,3 +24,10 @@ class Constrain:
|
|||
else:
|
||||
child_options = options.update(width=min(self.width, options.max_width))
|
||||
yield from console.render(self.renderable, child_options)
|
||||
|
||||
def __rich_measure__(self, console: Console, max_width: int) -> Measurement:
|
||||
if self.width is None:
|
||||
return Measurement.get(console, self.renderable, max_width)
|
||||
else:
|
||||
width = min(self.width, max_width)
|
||||
return Measurement(width, width)
|
||||
|
|
|
@ -333,7 +333,6 @@ class Table(JupyterMixin):
|
|||
max_width = options.max_width
|
||||
if self.width is not None:
|
||||
max_width = min(self.width, max_width)
|
||||
|
||||
if self.box:
|
||||
max_width -= len(self.columns) - 1
|
||||
if self.show_edge:
|
||||
|
@ -377,7 +376,6 @@ class Table(JupyterMixin):
|
|||
|
||||
padding_width = self.padding[1] + self.padding[3]
|
||||
if self.expand:
|
||||
|
||||
ratios = [col.ratio or 0 for col in columns if col.flexible]
|
||||
if any(ratios):
|
||||
fixed_widths = [
|
||||
|
@ -390,7 +388,6 @@ class Table(JupyterMixin):
|
|||
if column.flexible
|
||||
]
|
||||
flexible_width = max_width - sum(fixed_widths)
|
||||
|
||||
flex_widths = ratio_divide(flexible_width, ratios, flex_minimum)
|
||||
iter_flex_widths = iter(flex_widths)
|
||||
for index, column in enumerate(columns):
|
||||
|
|
|
@ -51,7 +51,7 @@ def render():
|
|||
|
||||
|
||||
def test_render():
|
||||
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"
|
||||
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"
|
||||
assert render() == expected
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue