flexablelib 3.0.1__tar.gz → 3.0.3__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-3.0.1 → flexablelib-3.0.3}/CHANGELOG.md +48 -0
- {flexablelib-3.0.1/flexablelib.egg-info → flexablelib-3.0.3}/PKG-INFO +1 -1
- flexablelib-3.0.3/flexablelib/box_topology.py +308 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/merges.py +217 -68
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/table.py +25 -108
- {flexablelib-3.0.1 → flexablelib-3.0.3/flexablelib.egg-info}/PKG-INFO +1 -1
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib.egg-info/SOURCES.txt +3 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/package.json +1 -1
- flexablelib-3.0.3/tests/test_box_topology.py +109 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_features.py +13 -9
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_merge_edge_matrix.py +8 -8
- flexablelib-3.0.3/tests/test_merge_fill.py +266 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_merges.py +78 -83
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/stress_matrix.py +5 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/LICENSE +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/MANIFEST.in +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/README.md +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/__init__.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/_speedups.c +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/_width_tables.h +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/cells.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/colors.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/columns.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/fast.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/options.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/py.typed +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/readers.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/style_compositions.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/table_strings.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/text_width.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/utils.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib/writers.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/pytest.ini +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/setup.cfg +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/setup.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/style_examples.md +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/conftest.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/helpers.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_align_indicators.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_cells.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_columns.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_excel.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_issues.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_public_api_edges.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_readme_examples.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_stress_matrix.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_style_line_widths.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_styles.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_text_width.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_upstream_issues.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v2021_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v210_rows_copy.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v2110_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v211_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v213_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v214_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v215_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v216_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v217_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v218_features.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v219_coverage.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v219_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v220_exports.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v221_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v222_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v223_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v224_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v230_none.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v231_fixes.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v232_merge_ansi.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tests/test_v300_style_names.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/benchmark.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/generate_width_tables.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/readme_gallery.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/style_examples.py +0 -0
- {flexablelib-3.0.1 → flexablelib-3.0.3}/tools/text_to_png.py +0 -0
|
@@ -2,6 +2,54 @@
|
|
|
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
|
+
## [3.0.3] - 2026-11-11
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
- A leftover `ESC[0m` kept by `strip_by_width` no longer lands in the
|
|
9
|
+
padded text of a coloured merge and closes the fill mid-cell. The
|
|
10
|
+
background now covers the whole interior rectangle, including internal
|
|
11
|
+
separators and padding (theme `dark` and other themes).
|
|
12
|
+
- Column-only styles again draw a top and bottom rule around a
|
|
13
|
+
multi-column merge, clamped to the merge width, with `├`/`┤` tees that
|
|
14
|
+
do not bleed into neighbouring cells. A vertical-only merge still
|
|
15
|
+
gets no cap.
|
|
16
|
+
- Junctions on a fully absorbed separator (side-by-side merges, or a
|
|
17
|
+
rule that is almost entirely merge interiors) are repaired even when
|
|
18
|
+
the line no longer looks like a rule, so leftover `┼`/`╟`/`╢` become
|
|
19
|
+
`│`/`║`.
|
|
20
|
+
- Dotted-row `:` junctions become `.` when a vertical arm is missing;
|
|
21
|
+
markdown header separators stay `|---|---|` so the format remains
|
|
22
|
+
valid; ascii-rounded outer frames are not rewritten.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
- A glyph-topology verifier (`flexablelib.box_topology`) and tests over
|
|
26
|
+
every style, with and without colour, covering horizontal, vertical
|
|
27
|
+
and 2D merges at corners, edges, and next to the header or footer.
|
|
28
|
+
Colour must not change the glyphs, and every interior column of a
|
|
29
|
+
coloured merge must carry that merge's background.
|
|
30
|
+
|
|
31
|
+
## [3.0.2] - 2026-11-11
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
- Merge and zebra backgrounds now fill a single rectangle: the interior of
|
|
35
|
+
the cell or merge, including any separators absorbed between its rows.
|
|
36
|
+
The fill no longer stairsteps at the corners, leaks onto neighbouring
|
|
37
|
+
cells, or paints the outline glyphs (`─`, `═`, tees and crossings).
|
|
38
|
+
- Horizontal rules keep `border_color` (or the default foreground) and do
|
|
39
|
+
not inherit a cell or zebra background, so a `_rows` style no longer
|
|
40
|
+
loses its border colour or leaves an uncoloured gap on a junction.
|
|
41
|
+
- A leftover `ESC[0m` after a merge that already closed its colour is
|
|
42
|
+
dropped, so `column_colors` / zebra next to a coloured merge no longer
|
|
43
|
+
emit a doubled reset.
|
|
44
|
+
- Theme `slate` (psql + zebra) and other themes paint a 2D merge as a
|
|
45
|
+
solid rectangle; `border_color` no longer cuts through the label.
|
|
46
|
+
- Column-only styles (verticals but no rule between rows) no longer
|
|
47
|
+
insert a synthetic cap around a multi-column merge. That cap blanked
|
|
48
|
+
neighbour cells on a new line and made them look merged. The span is
|
|
49
|
+
shown by dropping the internal vertical; when the merge has a colour,
|
|
50
|
+
the rectangular fill marks it. Existing header and end rules still
|
|
51
|
+
get their tees repaired.
|
|
52
|
+
|
|
5
53
|
## [3.0.1] - 2026-11-11
|
|
6
54
|
|
|
7
55
|
### Fixed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.3
|
|
4
4
|
Summary: Fast, dependency-free terminal tables for Python: 54 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Author: Benjamin Ramirez
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Box-drawing topology: every arm of a grid glyph must meet a matching arm.
|
|
3
|
+
|
|
4
|
+
Used to verify merged tables: a │ must not point into a space, a ┼ must
|
|
5
|
+
have lines on all four sides, and a merge cap must not send a horizontal
|
|
6
|
+
arm into a neighbour column that has no rule.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from typing import Iterable, List, Optional, Sequence, Tuple
|
|
13
|
+
|
|
14
|
+
from .fast import char_width, strip_ansi, visible_width
|
|
15
|
+
from .text_width import tokenize
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# (north, south, east, west) — 1 if the glyph carries an arm that way.
|
|
19
|
+
_ARMS = {}
|
|
20
|
+
# Glyphs that can stand in for any tee (ASCII has only '+').
|
|
21
|
+
_SOFT = set('+')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _reg(ch: str, n, s, e, w):
|
|
25
|
+
_ARMS[ch] = (bool(n), bool(s), bool(e), bool(w))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Light
|
|
29
|
+
for ch, key in {
|
|
30
|
+
'─': (0, 0, 1, 1), '│': (1, 1, 0, 0),
|
|
31
|
+
'┌': (0, 1, 1, 0), '┐': (0, 1, 0, 1), '└': (1, 0, 1, 0), '┘': (1, 0, 0, 1),
|
|
32
|
+
'├': (1, 1, 1, 0), '┤': (1, 1, 0, 1), '┬': (0, 1, 1, 1), '┴': (1, 0, 1, 1),
|
|
33
|
+
'┼': (1, 1, 1, 1),
|
|
34
|
+
'╭': (0, 1, 1, 0), '╮': (0, 1, 0, 1), '╰': (1, 0, 1, 0), '╯': (1, 0, 0, 1),
|
|
35
|
+
}.items():
|
|
36
|
+
_reg(ch, *key)
|
|
37
|
+
|
|
38
|
+
# Heavy
|
|
39
|
+
for ch, key in {
|
|
40
|
+
'━': (0, 0, 1, 1), '┃': (1, 1, 0, 0),
|
|
41
|
+
'┏': (0, 1, 1, 0), '┓': (0, 1, 0, 1), '┗': (1, 0, 1, 0), '┛': (1, 0, 0, 1),
|
|
42
|
+
'┣': (1, 1, 1, 0), '┫': (1, 1, 0, 1), '┳': (0, 1, 1, 1), '┻': (1, 0, 1, 1),
|
|
43
|
+
'╋': (1, 1, 1, 1),
|
|
44
|
+
'┝': (1, 1, 1, 0), '┥': (1, 1, 0, 1), '┯': (0, 1, 1, 1), '┷': (1, 0, 1, 1),
|
|
45
|
+
'┿': (1, 1, 1, 1),
|
|
46
|
+
'┠': (1, 1, 1, 0), '┨': (1, 1, 0, 1), '┯': (0, 1, 1, 1),
|
|
47
|
+
'┍': (0, 1, 1, 0), '┑': (0, 1, 0, 1), '┕': (1, 0, 1, 0), '┙': (1, 0, 0, 1),
|
|
48
|
+
}.items():
|
|
49
|
+
_reg(ch, *key)
|
|
50
|
+
|
|
51
|
+
# Double and mixed
|
|
52
|
+
for ch, key in {
|
|
53
|
+
'═': (0, 0, 1, 1), '║': (1, 1, 0, 0),
|
|
54
|
+
'╔': (0, 1, 1, 0), '╗': (0, 1, 0, 1), '╚': (1, 0, 1, 0), '╝': (1, 0, 0, 1),
|
|
55
|
+
'╠': (1, 1, 1, 0), '╣': (1, 1, 0, 1), '╦': (0, 1, 1, 1), '╩': (1, 0, 1, 1),
|
|
56
|
+
'╬': (1, 1, 1, 1),
|
|
57
|
+
'╒': (0, 1, 1, 0), '╓': (0, 1, 1, 0), '╕': (0, 1, 0, 1), '╖': (0, 1, 0, 1),
|
|
58
|
+
'╘': (1, 0, 1, 0), '╙': (1, 0, 1, 0), '╛': (1, 0, 0, 1), '╜': (1, 0, 0, 1),
|
|
59
|
+
'╞': (1, 1, 1, 0), '╟': (1, 1, 1, 0), '╡': (1, 1, 0, 1), '╢': (1, 1, 0, 1),
|
|
60
|
+
'╤': (0, 1, 1, 1), '╥': (0, 1, 1, 1), '╧': (1, 0, 1, 1), '╨': (1, 0, 1, 1),
|
|
61
|
+
'╪': (1, 1, 1, 1), '╫': (1, 1, 1, 1),
|
|
62
|
+
}.items():
|
|
63
|
+
_reg(ch, *key)
|
|
64
|
+
|
|
65
|
+
# Dashed / dotted / light variants used by shipped styles
|
|
66
|
+
for ch in '┄┅┈┉╌╍▬−':
|
|
67
|
+
_reg(ch, 0, 0, 1, 1)
|
|
68
|
+
for ch in '┊┋┆┇╎╏':
|
|
69
|
+
_reg(ch, 1, 1, 0, 0)
|
|
70
|
+
|
|
71
|
+
# ASCII
|
|
72
|
+
_reg('-', 0, 0, 1, 1)
|
|
73
|
+
_reg('=', 0, 0, 1, 1)
|
|
74
|
+
_reg('~', 0, 0, 1, 1)
|
|
75
|
+
_reg('|', 1, 1, 0, 0)
|
|
76
|
+
_reg('+', 1, 1, 1, 1)
|
|
77
|
+
|
|
78
|
+
# Extra light/heavy mixed tees often produced by repairs
|
|
79
|
+
for ch, key in {
|
|
80
|
+
'┞': (1, 1, 1, 0), '┟': (1, 1, 1, 0), '┡': (1, 1, 1, 0), '┢': (1, 1, 1, 0),
|
|
81
|
+
'┩': (1, 1, 0, 1), '┪': (1, 1, 0, 1), '┦': (1, 1, 0, 1), '┧': (1, 1, 0, 1),
|
|
82
|
+
'┱': (0, 1, 1, 1), '┲': (0, 1, 1, 1), '.items': (1, 0, 1, 1), '┺': (1, 0, 1, 1),
|
|
83
|
+
'╀': (1, 1, 1, 1), '╁': (1, 1, 1, 1), '╂': (1, 1, 1, 1),
|
|
84
|
+
'╃': (1, 1, 1, 1), '╄': (1, 1, 1, 1), '╅': (1, 1, 1, 1), '╆': (1, 1, 1, 1),
|
|
85
|
+
'╇': (1, 1, 1, 1), '╈': (1, 1, 1, 1), '╉': (1, 1, 1, 1), '╊': (1, 1, 1, 1),
|
|
86
|
+
}.items():
|
|
87
|
+
if ch != '.items':
|
|
88
|
+
_reg(ch, *key)
|
|
89
|
+
|
|
90
|
+
# I typo'd one key - fix ┹
|
|
91
|
+
_reg('┹', 1, 0, 1, 1)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
Break = Tuple[int, int, str, str, str] # row, col, glyph, direction, neighbor
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def arms(ch: str):
|
|
98
|
+
"""Return (n, s, e, w) or None if ``ch`` is not a grid glyph."""
|
|
99
|
+
if not ch:
|
|
100
|
+
return None
|
|
101
|
+
return _ARMS.get(ch)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def grid(line: str) -> List[str]:
|
|
105
|
+
"""Visible-column list; a width-2 character occupies two cells (second '')."""
|
|
106
|
+
out: List[str] = []
|
|
107
|
+
for ch in strip_ansi(line):
|
|
108
|
+
w = char_width(ch)
|
|
109
|
+
if w <= 0:
|
|
110
|
+
continue
|
|
111
|
+
out.append(ch)
|
|
112
|
+
if w == 2:
|
|
113
|
+
out.append('')
|
|
114
|
+
return out
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_SGR = re.compile(r'\x1b\[([0-9;]*)m')
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _apply_sgr(params, fg, bg):
|
|
121
|
+
if not params:
|
|
122
|
+
return None, None
|
|
123
|
+
i = 0
|
|
124
|
+
n = len(params)
|
|
125
|
+
while i < n:
|
|
126
|
+
p = params[i]
|
|
127
|
+
if p == 0:
|
|
128
|
+
fg, bg = None, None
|
|
129
|
+
elif p == 39:
|
|
130
|
+
fg = None
|
|
131
|
+
elif p == 49:
|
|
132
|
+
bg = None
|
|
133
|
+
elif p in (38, 48):
|
|
134
|
+
kind = 'fg' if p == 38 else 'bg'
|
|
135
|
+
if i + 1 < n and params[i + 1] == 2 and i + 4 < n:
|
|
136
|
+
val = ('2', params[i + 2], params[i + 3], params[i + 4])
|
|
137
|
+
i += 4
|
|
138
|
+
elif i + 1 < n and params[i + 1] == 5 and i + 2 < n:
|
|
139
|
+
val = ('5', params[i + 2])
|
|
140
|
+
i += 2
|
|
141
|
+
else:
|
|
142
|
+
val = ('raw', p)
|
|
143
|
+
if kind == 'fg':
|
|
144
|
+
fg = val
|
|
145
|
+
else:
|
|
146
|
+
bg = val
|
|
147
|
+
elif 40 <= p <= 47 or 100 <= p <= 107:
|
|
148
|
+
bg = p
|
|
149
|
+
elif 30 <= p <= 37 or 90 <= p <= 97:
|
|
150
|
+
fg = p
|
|
151
|
+
i += 1
|
|
152
|
+
return fg, bg
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def visible_cells(line: str):
|
|
156
|
+
"""List of (column, character, bg) for each visible column."""
|
|
157
|
+
out = []
|
|
158
|
+
i = 0
|
|
159
|
+
col = 0
|
|
160
|
+
fg = bg = None
|
|
161
|
+
while i < len(line):
|
|
162
|
+
match = _SGR.match(line, i)
|
|
163
|
+
if match:
|
|
164
|
+
raw = match.group(1)
|
|
165
|
+
params = [int(part) if part else 0 for part in raw.split(';')] if raw else [0]
|
|
166
|
+
fg, bg = _apply_sgr(params, fg, bg)
|
|
167
|
+
i = match.end()
|
|
168
|
+
continue
|
|
169
|
+
ch = line[i]
|
|
170
|
+
width = char_width(ch)
|
|
171
|
+
if width <= 0:
|
|
172
|
+
i += 1
|
|
173
|
+
continue
|
|
174
|
+
out.append((col, ch, bg))
|
|
175
|
+
for _ in range(width - 1):
|
|
176
|
+
out.append((col + 1, '', bg))
|
|
177
|
+
col += 1
|
|
178
|
+
col += width
|
|
179
|
+
i += 1
|
|
180
|
+
return out
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def bg_runs(line: str, wanted):
|
|
184
|
+
"""Contiguous [start, end) column ranges whose background is ``wanted``."""
|
|
185
|
+
runs = []
|
|
186
|
+
start = None
|
|
187
|
+
for col, _ch, bg in visible_cells(line):
|
|
188
|
+
hit = (
|
|
189
|
+
bg == ('2',) + wanted
|
|
190
|
+
if isinstance(wanted, tuple) and len(wanted) == 3
|
|
191
|
+
else bg == wanted
|
|
192
|
+
)
|
|
193
|
+
if hit:
|
|
194
|
+
if start is None:
|
|
195
|
+
start = col
|
|
196
|
+
elif start is not None:
|
|
197
|
+
runs.append((start, col))
|
|
198
|
+
start = None
|
|
199
|
+
if start is not None:
|
|
200
|
+
runs.append((start, visible_width(line)))
|
|
201
|
+
return runs
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def check_topology(lines: Sequence[str]) -> List[Break]:
|
|
205
|
+
"""
|
|
206
|
+
Return arm mismatches in ``lines``.
|
|
207
|
+
|
|
208
|
+
A glyph with an arm toward a neighbour must meet a glyph with the
|
|
209
|
+
opposite arm. A glyph with no arm that way must not meet an opposite
|
|
210
|
+
arm. Space, text and the edge of the table have no arms.
|
|
211
|
+
"""
|
|
212
|
+
rows = [grid(line) for line in lines]
|
|
213
|
+
breaks: List[Break] = []
|
|
214
|
+
# (direction, d_row, d_col, my_arm_index, neighbour_arm_index)
|
|
215
|
+
dirs = (
|
|
216
|
+
('up', -1, 0, 0, 1),
|
|
217
|
+
('down', 1, 0, 1, 0),
|
|
218
|
+
('right', 0, 1, 2, 3),
|
|
219
|
+
('left', 0, -1, 3, 2),
|
|
220
|
+
)
|
|
221
|
+
for r, row in enumerate(rows):
|
|
222
|
+
for c, ch in enumerate(row):
|
|
223
|
+
a = arms(ch)
|
|
224
|
+
if not a:
|
|
225
|
+
continue
|
|
226
|
+
for name, dr, dc, mine, theirs in dirs:
|
|
227
|
+
has = a[mine]
|
|
228
|
+
rr, cc = r + dr, c + dc
|
|
229
|
+
if 0 <= rr < len(rows) and 0 <= cc < len(rows[rr]):
|
|
230
|
+
nch = rows[rr][cc]
|
|
231
|
+
na = arms(nch)
|
|
232
|
+
n_has = bool(na and na[theirs])
|
|
233
|
+
neighbor = nch or ' '
|
|
234
|
+
else:
|
|
235
|
+
# Off the canvas (including a shorter neighbouring line):
|
|
236
|
+
# a dangling arm at the edge of the table is allowed.
|
|
237
|
+
continue
|
|
238
|
+
# ASCII '+' stands in for every tee, so unused arms do not
|
|
239
|
+
# have to meet a neighbour. Incoming arms still match.
|
|
240
|
+
if ch in _SOFT and has and not n_has:
|
|
241
|
+
continue
|
|
242
|
+
if has and not n_has:
|
|
243
|
+
breaks.append((r, c, ch, name, neighbor or ''))
|
|
244
|
+
elif n_has and not has:
|
|
245
|
+
breaks.append((r, c, ch, 'no-' + name, neighbor or ''))
|
|
246
|
+
return breaks
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def check_merge_fill(lines: Sequence[str], wanted, label: str = '') -> List[str]:
|
|
250
|
+
"""
|
|
251
|
+
The background ``wanted`` (24-bit r,g,b or an SGR code) must form a
|
|
252
|
+
single rectangle: every line that carries it has one run, all runs
|
|
253
|
+
share the same [start, end), and the rows are contiguous.
|
|
254
|
+
"""
|
|
255
|
+
problems = []
|
|
256
|
+
hits = []
|
|
257
|
+
for index, line in enumerate(lines):
|
|
258
|
+
runs = bg_runs(line, wanted)
|
|
259
|
+
if not runs:
|
|
260
|
+
continue
|
|
261
|
+
if len(runs) != 1:
|
|
262
|
+
problems.append(
|
|
263
|
+
f'{label}: line {index} has non-contiguous fill {runs}'
|
|
264
|
+
)
|
|
265
|
+
continue
|
|
266
|
+
hits.append((index, runs[0][0], runs[0][1]))
|
|
267
|
+
if not hits:
|
|
268
|
+
problems.append(f'{label}: background {wanted} never appeared')
|
|
269
|
+
return problems
|
|
270
|
+
indexes = [i for i, _, _ in hits]
|
|
271
|
+
if indexes != list(range(indexes[0], indexes[-1] + 1)):
|
|
272
|
+
problems.append(f'{label}: fill rows are not a solid block {indexes}')
|
|
273
|
+
spans = {(s, e) for _, s, e in hits}
|
|
274
|
+
if len(spans) != 1:
|
|
275
|
+
problems.append(f'{label}: fill width is not rectangular {spans}')
|
|
276
|
+
return problems
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def signatures(lines: Sequence[str]):
|
|
280
|
+
"""Set of (direction, glyph, neighbour) from ``check_topology``."""
|
|
281
|
+
return {(d, ch, n) for _r, _c, ch, d, n in check_topology(lines)}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def new_breaks(merged_lines: Sequence[str], baseline_lines: Sequence[str]) -> List[Break]:
|
|
285
|
+
"""Breaks in ``merged_lines`` whose signature is not in the unmerged table."""
|
|
286
|
+
allowed = signatures(baseline_lines)
|
|
287
|
+
out = []
|
|
288
|
+
for brk in check_topology(merged_lines):
|
|
289
|
+
if (brk[3], brk[2], brk[4]) in allowed:
|
|
290
|
+
continue
|
|
291
|
+
row, _c, ch, direction, _n = brk
|
|
292
|
+
if (
|
|
293
|
+
ch == '|'
|
|
294
|
+
and direction in ('up', 'down', 'no-up', 'no-down')
|
|
295
|
+
and 0 <= row < len(merged_lines)
|
|
296
|
+
and _is_markdown_separator(merged_lines[row])
|
|
297
|
+
):
|
|
298
|
+
continue
|
|
299
|
+
out.append(brk)
|
|
300
|
+
return out
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _is_markdown_separator(line: str) -> bool:
|
|
304
|
+
"""'|---|---:|' style header rules must keep '|', even over a merge."""
|
|
305
|
+
plain = strip_ansi(line).strip()
|
|
306
|
+
return bool(plain) and plain[0] == '|' and plain[-1] == '|' and all(
|
|
307
|
+
ch in '|-: ' for ch in plain
|
|
308
|
+
)
|