flexablelib 2.0.12__tar.gz → 2.0.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flexablelib-2.0.12 → flexablelib-2.0.13}/CHANGELOG.md +17 -0
- {flexablelib-2.0.12/flexablelib.egg-info → flexablelib-2.0.13}/PKG-INFO +1 -1
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/colors.py +6 -1
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/readers.py +81 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/table.py +248 -6
- {flexablelib-2.0.12 → flexablelib-2.0.13/flexablelib.egg-info}/PKG-INFO +1 -1
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib.egg-info/SOURCES.txt +1 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/package.json +1 -1
- flexablelib-2.0.13/tests/test_v213_features.py +97 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/LICENSE +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/MANIFEST.in +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/README.md +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/__init__.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/cells.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/columns.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/fast.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/merges.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/options.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/style_compositions.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/table_strings.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/text_width.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/utils.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib/writers.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/setup.cfg +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/setup.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_cells.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_columns.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_excel.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_features.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_issues.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_merges.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_styles.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_text_width.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tools/benchmark.py +0 -0
- {flexablelib-2.0.12 → flexablelib-2.0.13}/tools/generate_width_tables.py +0 -0
|
@@ -2,6 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.0.13] - 2026-10-28
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- Research inventory of 89 terminal/pretty-print table libraries across languages
|
|
9
|
+
(see delivery demos / research notes); feature backlog mined into this release.
|
|
10
|
+
- ``Table.zebra`` / ``row_stripe``: alternating body-row colours
|
|
11
|
+
(``None``, odd-only spec, or ``(even, odd)``).
|
|
12
|
+
- ``Table.caption``: centred text below the table (``title`` stays above).
|
|
13
|
+
- ``Table.footer_rows`` + ``Table.footer_color``: style a trailing totals band.
|
|
14
|
+
- ``Table.cell_colors``: per-cell ``{(row, col): spec}`` highlights.
|
|
15
|
+
- ``Table.cell_padding``: override style cell margin; ``show_margin=False`` now
|
|
16
|
+
forces margin 0 (was stored but unused).
|
|
17
|
+
- ``Table.overflow``: ``'wrap'`` / ``'truncate'`` / ``'ellipsis'`` (keeps
|
|
18
|
+
``auto_wrap`` in sync).
|
|
19
|
+
- ``Table.from_markdown``: import GFM pipe tables (alignment row → col_alignment).
|
|
20
|
+
- Colour specs may be background-only: ``on #rrggbb`` / ``on white``.
|
|
21
|
+
|
|
5
22
|
## [2.0.12] - 2026-10-28
|
|
6
23
|
|
|
7
24
|
### Removed
|
|
@@ -116,7 +116,12 @@ def parse_spec(spec: Union[str, int, None]) -> str:
|
|
|
116
116
|
|
|
117
117
|
parameters = []
|
|
118
118
|
# 'on' separates foreground from background: 'bold red on white'.
|
|
119
|
-
|
|
119
|
+
# A bare 'on <bg>' (Rich-style background-only) is also accepted.
|
|
120
|
+
lowered = spec.lower().strip()
|
|
121
|
+
if lowered.startswith('on '):
|
|
122
|
+
foreground_part, background_part = '', lowered[3:]
|
|
123
|
+
else:
|
|
124
|
+
foreground_part, _, background_part = lowered.partition(' on ')
|
|
120
125
|
|
|
121
126
|
for token in foreground_part.split():
|
|
122
127
|
if token in ATTRIBUTES:
|
|
@@ -399,3 +399,84 @@ def from_excel(table_class, path, sheet=None, has_header: bool = True,
|
|
|
399
399
|
# Overlaps or single cells from odd sheets: skip rather than fail.
|
|
400
400
|
continue
|
|
401
401
|
return table
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def from_markdown(table_class, source, parse_numbers: bool = True, **options):
|
|
405
|
+
"""
|
|
406
|
+
Parse a GitHub-flavoured Markdown pipe table into a Table.
|
|
407
|
+
|
|
408
|
+
Accepts a string, a path, or a readable. The separator row
|
|
409
|
+
(``| --- | :---: | ---: |``) sets ``col_alignment`` when present and is
|
|
410
|
+
not stored as data. Round-trips with ``Table.to_markdown()``.
|
|
411
|
+
"""
|
|
412
|
+
import os
|
|
413
|
+
if hasattr(source, 'read'):
|
|
414
|
+
text = source.read()
|
|
415
|
+
if isinstance(text, bytes):
|
|
416
|
+
text = text.decode('utf-8')
|
|
417
|
+
elif isinstance(source, (str, bytes, os.PathLike)) and not (
|
|
418
|
+
isinstance(source, str) and '\n' in source
|
|
419
|
+
) and os.path.isfile(str(source)):
|
|
420
|
+
with open(source, encoding='utf-8') as fh:
|
|
421
|
+
text = fh.read()
|
|
422
|
+
else:
|
|
423
|
+
text = source.decode('utf-8') if isinstance(source, bytes) else str(source)
|
|
424
|
+
|
|
425
|
+
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
|
|
426
|
+
rows_raw = []
|
|
427
|
+
for ln in lines:
|
|
428
|
+
if '|' not in ln:
|
|
429
|
+
continue
|
|
430
|
+
body = ln[1:] if ln.startswith('|') else ln
|
|
431
|
+
if body.endswith('|'):
|
|
432
|
+
body = body[:-1]
|
|
433
|
+
rows_raw.append([c.strip() for c in body.split('|')])
|
|
434
|
+
|
|
435
|
+
if not rows_raw:
|
|
436
|
+
return _new_table(table_class, [], [], parse_numbers, **options)
|
|
437
|
+
|
|
438
|
+
def _is_sep_row(cells):
|
|
439
|
+
if not cells:
|
|
440
|
+
return False
|
|
441
|
+
for token in cells:
|
|
442
|
+
token = token.strip()
|
|
443
|
+
if not token or '-' not in token:
|
|
444
|
+
return False
|
|
445
|
+
if token.replace(':', '').replace('-', '').strip():
|
|
446
|
+
return False
|
|
447
|
+
return True
|
|
448
|
+
|
|
449
|
+
headers = rows_raw[0]
|
|
450
|
+
align_from_sep = None
|
|
451
|
+
data_start = 1
|
|
452
|
+
if len(rows_raw) > 1 and _is_sep_row(rows_raw[1]):
|
|
453
|
+
align_from_sep = []
|
|
454
|
+
for cell in rows_raw[1]:
|
|
455
|
+
token = cell.strip()
|
|
456
|
+
left = token.startswith(':')
|
|
457
|
+
right = token.endswith(':')
|
|
458
|
+
if left and right:
|
|
459
|
+
align_from_sep.append('c')
|
|
460
|
+
elif right:
|
|
461
|
+
align_from_sep.append('r')
|
|
462
|
+
else:
|
|
463
|
+
align_from_sep.append('l')
|
|
464
|
+
data_start = 2
|
|
465
|
+
|
|
466
|
+
width = len(headers)
|
|
467
|
+
normalized = []
|
|
468
|
+
for row in rows_raw[data_start:]:
|
|
469
|
+
row = list(row)
|
|
470
|
+
if len(row) < width:
|
|
471
|
+
row = row + [''] * (width - len(row))
|
|
472
|
+
elif len(row) > width:
|
|
473
|
+
row = row[:width]
|
|
474
|
+
normalized.append(row)
|
|
475
|
+
|
|
476
|
+
table = _new_table(table_class, headers, normalized, parse_numbers, **options)
|
|
477
|
+
if align_from_sep is not None:
|
|
478
|
+
try:
|
|
479
|
+
table.col_alignment = align_from_sep
|
|
480
|
+
except Exception:
|
|
481
|
+
pass
|
|
482
|
+
return table
|
|
@@ -806,6 +806,19 @@ class Table(object):
|
|
|
806
806
|
self.__header_align = None
|
|
807
807
|
# Optional title drawn above the table.
|
|
808
808
|
self.__title = None
|
|
809
|
+
# Optional caption drawn below the table (Rich/tablewriter style).
|
|
810
|
+
self.__caption = None
|
|
811
|
+
# Alternating body-row colours: None, a single odd-row spec, or (even, odd).
|
|
812
|
+
self.__zebra = None
|
|
813
|
+
# Last N rendered body rows treated as footer (Spectre/tablewriter).
|
|
814
|
+
self.__footer_rows = 0
|
|
815
|
+
self.__footer_color = None
|
|
816
|
+
# Per-cell colour map: keys (row, col_index|header) -> colour spec.
|
|
817
|
+
self.__cell_colors = {}
|
|
818
|
+
# Override style cell margin (spaces each side); None keeps style default.
|
|
819
|
+
self.__cell_padding = None
|
|
820
|
+
# 'wrap' | 'truncate' | None (None defers to auto_wrap bool).
|
|
821
|
+
self.__overflow = None
|
|
809
822
|
# Row indexes after which a horizontal rule is forced.
|
|
810
823
|
self.__dividers = set()
|
|
811
824
|
# Sort / filter applied to the display copy, not to stored data.
|
|
@@ -1373,6 +1386,75 @@ class Table(object):
|
|
|
1373
1386
|
"""
|
|
1374
1387
|
return self.__title
|
|
1375
1388
|
|
|
1389
|
+
@property
|
|
1390
|
+
def caption(self):
|
|
1391
|
+
"""
|
|
1392
|
+
Optional text drawn below the table, centred on the table width.
|
|
1393
|
+
|
|
1394
|
+
Distinct from :attr:`title` (above). Inspired by Rich captions and
|
|
1395
|
+
go ``tablewriter`` / Spectre footers' companion labels.
|
|
1396
|
+
"""
|
|
1397
|
+
return self.__caption
|
|
1398
|
+
|
|
1399
|
+
@property
|
|
1400
|
+
def zebra(self):
|
|
1401
|
+
"""
|
|
1402
|
+
Alternating body-row colours (Rich ``row_styles`` / zebra striping).
|
|
1403
|
+
|
|
1404
|
+
Accepts ``None`` (off), a single colour spec applied to odd rows
|
|
1405
|
+
(1, 3, …), or a two-item sequence ``(even_spec, odd_spec)``. Empty
|
|
1406
|
+
/ falsy specs leave that parity uncoloured. Alias: :attr:`row_stripe`.
|
|
1407
|
+
"""
|
|
1408
|
+
return self.__zebra
|
|
1409
|
+
|
|
1410
|
+
@property
|
|
1411
|
+
def row_stripe(self):
|
|
1412
|
+
"""Alias of :attr:`zebra`."""
|
|
1413
|
+
return self.__zebra
|
|
1414
|
+
|
|
1415
|
+
@property
|
|
1416
|
+
def footer_rows(self):
|
|
1417
|
+
"""
|
|
1418
|
+
Number of trailing body rows treated as a footer band.
|
|
1419
|
+
|
|
1420
|
+
Combined with :attr:`footer_color`. Does not change stored data.
|
|
1421
|
+
"""
|
|
1422
|
+
return self.__footer_rows
|
|
1423
|
+
|
|
1424
|
+
@property
|
|
1425
|
+
def footer_color(self):
|
|
1426
|
+
"""Colour spec applied to the trailing :attr:`footer_rows` band."""
|
|
1427
|
+
return self.__footer_color
|
|
1428
|
+
|
|
1429
|
+
@property
|
|
1430
|
+
def cell_colors(self):
|
|
1431
|
+
"""
|
|
1432
|
+
Per-cell colour map.
|
|
1433
|
+
|
|
1434
|
+
Keys are ``(row_index, column)`` where ``column`` is a header name or
|
|
1435
|
+
0-based column index (index column excluded, matching merges)::
|
|
1436
|
+
|
|
1437
|
+
table.cell_colors = {(0, 'Status'): 'bold green', (2, 1): 'reverse'}
|
|
1438
|
+
"""
|
|
1439
|
+
return self.__cell_colors
|
|
1440
|
+
|
|
1441
|
+
@property
|
|
1442
|
+
def cell_padding(self):
|
|
1443
|
+
"""
|
|
1444
|
+
Spaces on each side of every cell, overriding the style margin.
|
|
1445
|
+
|
|
1446
|
+
``None`` keeps the style default (and honours :attr:`show_margin`).
|
|
1447
|
+
"""
|
|
1448
|
+
return self.__cell_padding
|
|
1449
|
+
|
|
1450
|
+
@property
|
|
1451
|
+
def overflow(self):
|
|
1452
|
+
"""
|
|
1453
|
+
How cells behave when wider than their column: ``'wrap'``,
|
|
1454
|
+
``'truncate'``, or ``None`` to defer to :attr:`auto_wrap`.
|
|
1455
|
+
"""
|
|
1456
|
+
return self.__overflow
|
|
1457
|
+
|
|
1376
1458
|
@property
|
|
1377
1459
|
def sort_by(self):
|
|
1378
1460
|
"""
|
|
@@ -1655,6 +1737,68 @@ class Table(object):
|
|
|
1655
1737
|
def title(self, value):
|
|
1656
1738
|
self.__title = None if value is None else str(value)
|
|
1657
1739
|
|
|
1740
|
+
@caption.setter
|
|
1741
|
+
def caption(self, value):
|
|
1742
|
+
self.__caption = None if value is None else str(value)
|
|
1743
|
+
|
|
1744
|
+
@zebra.setter
|
|
1745
|
+
def zebra(self, value):
|
|
1746
|
+
self.__zebra = value
|
|
1747
|
+
|
|
1748
|
+
@row_stripe.setter
|
|
1749
|
+
def row_stripe(self, value):
|
|
1750
|
+
self.__zebra = value
|
|
1751
|
+
|
|
1752
|
+
@footer_rows.setter
|
|
1753
|
+
def footer_rows(self, value):
|
|
1754
|
+
value = int(value or 0)
|
|
1755
|
+
if value < 0:
|
|
1756
|
+
raise ValueError('footer_rows must be >= 0')
|
|
1757
|
+
self.__footer_rows = value
|
|
1758
|
+
|
|
1759
|
+
@footer_color.setter
|
|
1760
|
+
def footer_color(self, value):
|
|
1761
|
+
self.__footer_color = value
|
|
1762
|
+
|
|
1763
|
+
@cell_colors.setter
|
|
1764
|
+
def cell_colors(self, value):
|
|
1765
|
+
if not value:
|
|
1766
|
+
self.__cell_colors = {}
|
|
1767
|
+
return
|
|
1768
|
+
cleaned = {}
|
|
1769
|
+
for key, spec in dict(value).items():
|
|
1770
|
+
if not (isinstance(key, tuple) and len(key) == 2):
|
|
1771
|
+
raise TypeError(
|
|
1772
|
+
'cell_colors keys must be (row, column) tuples, got {0!r}'
|
|
1773
|
+
.format(key)
|
|
1774
|
+
)
|
|
1775
|
+
cleaned[(int(key[0]), key[1])] = spec
|
|
1776
|
+
self.__cell_colors = cleaned
|
|
1777
|
+
|
|
1778
|
+
@cell_padding.setter
|
|
1779
|
+
def cell_padding(self, value):
|
|
1780
|
+
if value is None:
|
|
1781
|
+
self.__cell_padding = None
|
|
1782
|
+
return
|
|
1783
|
+
value = int(value)
|
|
1784
|
+
if value < 0:
|
|
1785
|
+
raise ValueError('cell_padding must be >= 0 or None')
|
|
1786
|
+
self.__cell_padding = value
|
|
1787
|
+
|
|
1788
|
+
@overflow.setter
|
|
1789
|
+
def overflow(self, value):
|
|
1790
|
+
if value is None:
|
|
1791
|
+
self.__overflow = None
|
|
1792
|
+
return
|
|
1793
|
+
value = str(value).lower()
|
|
1794
|
+
if value not in ('wrap', 'truncate', 'ellipsis'):
|
|
1795
|
+
raise ValueError(
|
|
1796
|
+
"overflow must be 'wrap', 'truncate', 'ellipsis', or None"
|
|
1797
|
+
)
|
|
1798
|
+
# ellipsis is truncate with the default trimming sign
|
|
1799
|
+
self.__overflow = 'truncate' if value == 'ellipsis' else value
|
|
1800
|
+
self.__auto_wrap_table = self.__overflow == 'wrap'
|
|
1801
|
+
|
|
1658
1802
|
@sort_by.setter
|
|
1659
1803
|
def sort_by(self, value):
|
|
1660
1804
|
self.__sort_by = value
|
|
@@ -1716,6 +1860,9 @@ class Table(object):
|
|
|
1716
1860
|
@auto_wrap.setter
|
|
1717
1861
|
def auto_wrap(self, value: bool):
|
|
1718
1862
|
self.__auto_wrap_table = bool(value)
|
|
1863
|
+
if self.__overflow is not None:
|
|
1864
|
+
# Keep overflow in sync when callers flip the bool directly.
|
|
1865
|
+
self.__overflow = 'wrap' if self.__auto_wrap_table else 'truncate'
|
|
1719
1866
|
|
|
1720
1867
|
# +----------------------+ SHOW SETTERS +------------------------+
|
|
1721
1868
|
|
|
@@ -1746,8 +1893,20 @@ class Table(object):
|
|
|
1746
1893
|
def __style_composition(self) -> TableComposition:
|
|
1747
1894
|
"""
|
|
1748
1895
|
Gets the style composition using a checked name.
|
|
1896
|
+
|
|
1897
|
+
``cell_padding`` overrides the style's per-side margin; ``show_margin``
|
|
1898
|
+
False forces margin 0 when no explicit padding is set.
|
|
1749
1899
|
"""
|
|
1750
|
-
|
|
1900
|
+
base = style_catalogue.__getattribute__(self.__checked_style_name)
|
|
1901
|
+
if self.__cell_padding is not None:
|
|
1902
|
+
margin = max(0, int(self.__cell_padding))
|
|
1903
|
+
elif not self.__show_margin:
|
|
1904
|
+
margin = 0
|
|
1905
|
+
else:
|
|
1906
|
+
margin = base.margin
|
|
1907
|
+
if margin == base.margin:
|
|
1908
|
+
return base
|
|
1909
|
+
return base._replace(margin=margin)
|
|
1751
1910
|
|
|
1752
1911
|
@property
|
|
1753
1912
|
def __checked_style_name(self) -> str:
|
|
@@ -2211,20 +2370,71 @@ class Table(object):
|
|
|
2211
2370
|
return supports_color()
|
|
2212
2371
|
return bool(self.__use_colors)
|
|
2213
2372
|
|
|
2214
|
-
def __color_for_cell(self, value, row_index: int, column_name: str
|
|
2373
|
+
def __color_for_cell(self, value, row_index: int, column_name: str,
|
|
2374
|
+
column_index: int = None):
|
|
2215
2375
|
"""
|
|
2216
2376
|
Colour of a single cell, or None.
|
|
2217
2377
|
|
|
2218
|
-
Precedence:
|
|
2378
|
+
Precedence: ``color_rule``, then ``cell_colors``, then ``row_colors``,
|
|
2379
|
+
then footer colour, then zebra stripe, then ``column_colors``.
|
|
2219
2380
|
"""
|
|
2220
2381
|
if self.__color_rule is not None:
|
|
2221
2382
|
decided = self.__color_rule(value, row_index, column_name)
|
|
2222
2383
|
if decided:
|
|
2223
2384
|
return decided
|
|
2385
|
+
cell_spec = self.__lookup_cell_color(row_index, column_name, column_index)
|
|
2386
|
+
if cell_spec:
|
|
2387
|
+
return cell_spec
|
|
2224
2388
|
if row_index in self.__row_colors:
|
|
2225
2389
|
return self.__row_colors[row_index]
|
|
2390
|
+
footer_spec = self.__footer_color_for_row(row_index)
|
|
2391
|
+
if footer_spec:
|
|
2392
|
+
return footer_spec
|
|
2393
|
+
zebra_spec = self.__zebra_color_for_row(row_index)
|
|
2394
|
+
if zebra_spec:
|
|
2395
|
+
return zebra_spec
|
|
2226
2396
|
return self.__column_colors.get(column_name)
|
|
2227
2397
|
|
|
2398
|
+
def __lookup_cell_color(self, row_index, column_name, column_index):
|
|
2399
|
+
"""Resolve ``cell_colors`` keyed by (row, header) or (row, col index)."""
|
|
2400
|
+
if not self.__cell_colors:
|
|
2401
|
+
return None
|
|
2402
|
+
for key in (
|
|
2403
|
+
(row_index, column_name),
|
|
2404
|
+
(row_index, column_index) if column_index is not None else None,
|
|
2405
|
+
):
|
|
2406
|
+
if key is not None and key in self.__cell_colors:
|
|
2407
|
+
return self.__cell_colors[key]
|
|
2408
|
+
return None
|
|
2409
|
+
|
|
2410
|
+
def __footer_color_for_row(self, row_index: int):
|
|
2411
|
+
"""Colour for rows in the trailing footer band, if configured."""
|
|
2412
|
+
if not self.__footer_color or self.__footer_rows <= 0:
|
|
2413
|
+
return None
|
|
2414
|
+
# Body length for the current paint pass is stored when known;
|
|
2415
|
+
# fall back to internal row count.
|
|
2416
|
+
body_len = getattr(self, "_paint_body_len", None)
|
|
2417
|
+
if body_len is None:
|
|
2418
|
+
body_len = self.__real_row_count
|
|
2419
|
+
if body_len <= 0:
|
|
2420
|
+
return None
|
|
2421
|
+
threshold = body_len - int(self.__footer_rows)
|
|
2422
|
+
if row_index >= threshold:
|
|
2423
|
+
return self.__footer_color
|
|
2424
|
+
return None
|
|
2425
|
+
|
|
2426
|
+
def __zebra_color_for_row(self, row_index: int):
|
|
2427
|
+
"""Even/odd body stripe from ``zebra`` / ``row_stripe``."""
|
|
2428
|
+
if not self.__zebra:
|
|
2429
|
+
return None
|
|
2430
|
+
# Footer rows keep footer_color (already handled); still allow zebra
|
|
2431
|
+
# on non-footer rows.
|
|
2432
|
+
if isinstance(self.__zebra, (list, tuple)) and len(self.__zebra) >= 2:
|
|
2433
|
+
even_spec, odd_spec = self.__zebra[0], self.__zebra[1]
|
|
2434
|
+
else:
|
|
2435
|
+
even_spec, odd_spec = None, self.__zebra
|
|
2436
|
+
return odd_spec if (row_index % 2) else even_spec
|
|
2437
|
+
|
|
2228
2438
|
def __has_any_color(self) -> bool:
|
|
2229
2439
|
"""True if anything at all was configured to be coloured."""
|
|
2230
2440
|
return bool(
|
|
@@ -2233,6 +2443,9 @@ class Table(object):
|
|
|
2233
2443
|
or self.__column_colors
|
|
2234
2444
|
or self.__row_colors
|
|
2235
2445
|
or self.__color_rule
|
|
2446
|
+
or self.__zebra
|
|
2447
|
+
or self.__footer_color
|
|
2448
|
+
or self.__cell_colors
|
|
2236
2449
|
or any(getattr(r, 'color', None) for r in self.__merged_regions)
|
|
2237
2450
|
)
|
|
2238
2451
|
|
|
@@ -3884,7 +4097,7 @@ class Table(object):
|
|
|
3884
4097
|
aligned_header, self.__header_color
|
|
3885
4098
|
)
|
|
3886
4099
|
aligned_header = self.__zip_columns(aligned_header, headers=True)
|
|
3887
|
-
aligned_columns = _align_columns(
|
|
4100
|
+
aligned_columns = list(_align_columns(
|
|
3888
4101
|
self.__style_composition,
|
|
3889
4102
|
unaligned_columns,
|
|
3890
4103
|
column_titles, # here the header is used to know the title of the column
|
|
@@ -3893,14 +4106,19 @@ class Table(object):
|
|
|
3893
4106
|
self.__empty_column_indexes,
|
|
3894
4107
|
self.__show_empty_columns,
|
|
3895
4108
|
float_column_widths
|
|
3896
|
-
)
|
|
4109
|
+
))
|
|
3897
4110
|
if colors_on:
|
|
4111
|
+
# Stash body length so footer_rows can resolve during paint.
|
|
4112
|
+
self._paint_body_len = (
|
|
4113
|
+
len(aligned_columns[0]) if aligned_columns else 0
|
|
4114
|
+
)
|
|
3898
4115
|
aligned_columns = self.__paint_cells(
|
|
3899
4116
|
aligned_columns,
|
|
3900
4117
|
lambda cell_i, row_i: self.__color_for_cell(
|
|
3901
4118
|
self.__raw_value_at(column_titles, cell_i, row_i),
|
|
3902
4119
|
row_i,
|
|
3903
4120
|
column_titles[cell_i] if cell_i < len(column_titles) else '',
|
|
4121
|
+
column_index=cell_i,
|
|
3904
4122
|
),
|
|
3905
4123
|
)
|
|
3906
4124
|
aligned_columns = self.__zip_columns(aligned_columns)
|
|
@@ -3980,6 +4198,7 @@ class Table(object):
|
|
|
3980
4198
|
gap_separator=gap_separator,
|
|
3981
4199
|
)
|
|
3982
4200
|
title_line = self.__title_line(column_widths_list, table_with_i)
|
|
4201
|
+
caption_line = self.__caption_line(column_widths_list, table_with_i)
|
|
3983
4202
|
|
|
3984
4203
|
# Where the body starts depends on which of the parts before it were
|
|
3985
4204
|
# drawn at all: a title, a top rule, a header. Counting them off the
|
|
@@ -3988,7 +4207,7 @@ class Table(object):
|
|
|
3988
4207
|
# every merge one line up, into the header.
|
|
3989
4208
|
parts = []
|
|
3990
4209
|
body_offset = 0
|
|
3991
|
-
for part in (title_line, superior_row, body, end_line):
|
|
4210
|
+
for part in (title_line, superior_row, body, end_line, caption_line):
|
|
3992
4211
|
if not part:
|
|
3993
4212
|
continue
|
|
3994
4213
|
if part is body:
|
|
@@ -4079,6 +4298,18 @@ class Table(object):
|
|
|
4079
4298
|
left = (table_width - title_width) // 2
|
|
4080
4299
|
return (' ' * left) + title
|
|
4081
4300
|
|
|
4301
|
+
def __caption_line(self, column_widths_list, table_with_i):
|
|
4302
|
+
"""Centre the caption under the full table width, if any."""
|
|
4303
|
+
if not self.__caption:
|
|
4304
|
+
return None
|
|
4305
|
+
table_width = self.__get_string_table_width()
|
|
4306
|
+
caption = self.__caption
|
|
4307
|
+
caption_width = visible_width(caption)
|
|
4308
|
+
if caption_width >= table_width:
|
|
4309
|
+
return caption
|
|
4310
|
+
left = (table_width - caption_width) // 2
|
|
4311
|
+
return (' ' * left) + caption
|
|
4312
|
+
|
|
4082
4313
|
def __apply_table_align(self, table_string):
|
|
4083
4314
|
"""Pad every line so the table sits left, centre or right."""
|
|
4084
4315
|
if not table_string or not self.__table_align:
|
|
@@ -4607,6 +4838,17 @@ class Table(object):
|
|
|
4607
4838
|
# +-----------------------------------------------------------------------------+
|
|
4608
4839
|
# start +------------------------+ DATA WRITING +------------------------+ start
|
|
4609
4840
|
|
|
4841
|
+
@classmethod
|
|
4842
|
+
def from_markdown(cls, source, parse_numbers=True, **options):
|
|
4843
|
+
"""
|
|
4844
|
+
Build a table from a GitHub-flavoured Markdown pipe table string.
|
|
4845
|
+
|
|
4846
|
+
Round-trips with :meth:`to_markdown`. Alignment row (``---``, ``:--``)
|
|
4847
|
+
is detected and discarded; column align codes are applied when present.
|
|
4848
|
+
"""
|
|
4849
|
+
from .readers import from_markdown
|
|
4850
|
+
return from_markdown(cls, source, parse_numbers=parse_numbers, **options)
|
|
4851
|
+
|
|
4610
4852
|
def to_records(self, include_index=False):
|
|
4611
4853
|
"""The table as a list of dicts, one per row."""
|
|
4612
4854
|
from .writers import to_records
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Features shipped in 2.0.13 (zebra, caption, footer, padding, cell_colors, overflow, from_markdown)."""
|
|
2
|
+
import re
|
|
3
|
+
import pytest
|
|
4
|
+
from flexablelib.table import Table
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def bare(text: str) -> str:
|
|
8
|
+
return re.sub(r'\x1b\[[0-9;]*m', '', text)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _sample(**opts):
|
|
12
|
+
t = Table.from_records(
|
|
13
|
+
[['A', 10], ['B', 20], ['C', 30], ['Tot', 60]],
|
|
14
|
+
headers=['Name', 'N'],
|
|
15
|
+
style_name='rounded',
|
|
16
|
+
**opts,
|
|
17
|
+
)
|
|
18
|
+
return t
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_caption_below_title_above():
|
|
22
|
+
t = _sample()
|
|
23
|
+
t.title = 'TITLE'
|
|
24
|
+
t.caption = 'CAPTION'
|
|
25
|
+
out = bare(t.compose()).splitlines()
|
|
26
|
+
assert out[0].strip() == 'TITLE'
|
|
27
|
+
assert out[-1].strip() == 'CAPTION'
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_zebra_alias_row_stripe_emits_ansi():
|
|
31
|
+
t = _sample()
|
|
32
|
+
t.use_colors = True
|
|
33
|
+
t.zebra = ('', 'on #222233')
|
|
34
|
+
coloured = t.compose()
|
|
35
|
+
assert '\x1b[' in coloured
|
|
36
|
+
t.row_stripe = ('dim', 'bold')
|
|
37
|
+
assert t.zebra == ('dim', 'bold')
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_footer_rows_color():
|
|
41
|
+
t = _sample()
|
|
42
|
+
t.use_colors = True
|
|
43
|
+
t.footer_rows = 1
|
|
44
|
+
t.footer_color = 'bold yellow'
|
|
45
|
+
coloured = t.compose()
|
|
46
|
+
assert '\x1b[' in coloured
|
|
47
|
+
# Last data row Tot should be painted; first row text still present.
|
|
48
|
+
assert 'Tot' in bare(coloured)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_cell_padding_and_show_margin():
|
|
52
|
+
t = _sample()
|
|
53
|
+
assert t.style_composition.margin >= 1
|
|
54
|
+
t.cell_padding = 3
|
|
55
|
+
assert t.style_composition.margin == 3
|
|
56
|
+
t.cell_padding = None
|
|
57
|
+
t.show_margin = False
|
|
58
|
+
assert t.style_composition.margin == 0
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_cell_colors_by_header_and_index():
|
|
62
|
+
t = _sample()
|
|
63
|
+
t.use_colors = True
|
|
64
|
+
t.cell_colors = {(0, 'Name'): 'green', (1, 1): 'red'}
|
|
65
|
+
coloured = t.compose()
|
|
66
|
+
assert '\x1b[' in coloured
|
|
67
|
+
assert 'A' in bare(coloured)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_overflow_sets_auto_wrap():
|
|
71
|
+
t = _sample()
|
|
72
|
+
t.overflow = 'wrap'
|
|
73
|
+
assert t.auto_wrap is True
|
|
74
|
+
assert t.overflow == 'wrap'
|
|
75
|
+
t.overflow = 'truncate'
|
|
76
|
+
assert t.auto_wrap is False
|
|
77
|
+
t.overflow = 'ellipsis'
|
|
78
|
+
assert t.overflow == 'truncate'
|
|
79
|
+
with pytest.raises(ValueError):
|
|
80
|
+
t.overflow = 'explode'
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_from_markdown_roundtrip_alignment():
|
|
84
|
+
md = '| Name | N |\n| :--- | ---: |\n| A | 10 |\n| B | 20 |\n'
|
|
85
|
+
t = Table.from_markdown(md, style_name='markdown')
|
|
86
|
+
assert t.col_alignment == ['l', 'r']
|
|
87
|
+
assert t.columns['Name'] == ['A', 'B']
|
|
88
|
+
assert t.columns['N'] == [10, 20]
|
|
89
|
+
out = t.to_markdown()
|
|
90
|
+
assert 'Name' in out and 'N' in out
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_on_background_only_colour_spec():
|
|
94
|
+
from flexablelib.colors import parse_spec
|
|
95
|
+
seq = parse_spec('on #112233')
|
|
96
|
+
assert seq.startswith('\x1b[')
|
|
97
|
+
assert '48;2;17;34;51' in seq
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|