flexablelib 3.0.2__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.
Files changed (80) hide show
  1. {flexablelib-3.0.2 → flexablelib-3.0.3}/CHANGELOG.md +26 -0
  2. {flexablelib-3.0.2/flexablelib.egg-info → flexablelib-3.0.3}/PKG-INFO +1 -1
  3. flexablelib-3.0.3/flexablelib/box_topology.py +308 -0
  4. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/merges.py +167 -17
  5. {flexablelib-3.0.2 → flexablelib-3.0.3/flexablelib.egg-info}/PKG-INFO +1 -1
  6. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib.egg-info/SOURCES.txt +2 -0
  7. {flexablelib-3.0.2 → flexablelib-3.0.3}/package.json +1 -1
  8. flexablelib-3.0.3/tests/test_box_topology.py +109 -0
  9. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_merge_edge_matrix.py +3 -2
  10. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_merge_fill.py +26 -4
  11. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_merges.py +43 -39
  12. {flexablelib-3.0.2 → flexablelib-3.0.3}/LICENSE +0 -0
  13. {flexablelib-3.0.2 → flexablelib-3.0.3}/MANIFEST.in +0 -0
  14. {flexablelib-3.0.2 → flexablelib-3.0.3}/README.md +0 -0
  15. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/__init__.py +0 -0
  16. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/_speedups.c +0 -0
  17. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/_width_tables.h +0 -0
  18. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/cells.py +0 -0
  19. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/colors.py +0 -0
  20. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/columns.py +0 -0
  21. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/fast.py +0 -0
  22. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/options.py +0 -0
  23. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/py.typed +0 -0
  24. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/readers.py +0 -0
  25. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/style_compositions.py +0 -0
  26. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/table.py +0 -0
  27. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/table_strings.py +0 -0
  28. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/text_width.py +0 -0
  29. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/utils.py +0 -0
  30. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib/writers.py +0 -0
  31. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib.egg-info/dependency_links.txt +0 -0
  32. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib.egg-info/requires.txt +0 -0
  33. {flexablelib-3.0.2 → flexablelib-3.0.3}/flexablelib.egg-info/top_level.txt +0 -0
  34. {flexablelib-3.0.2 → flexablelib-3.0.3}/pytest.ini +0 -0
  35. {flexablelib-3.0.2 → flexablelib-3.0.3}/setup.cfg +0 -0
  36. {flexablelib-3.0.2 → flexablelib-3.0.3}/setup.py +0 -0
  37. {flexablelib-3.0.2 → flexablelib-3.0.3}/style_examples.md +0 -0
  38. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/conftest.py +0 -0
  39. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/helpers.py +0 -0
  40. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_align_indicators.py +0 -0
  41. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_cells.py +0 -0
  42. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_columns.py +0 -0
  43. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_excel.py +0 -0
  44. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_features.py +0 -0
  45. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_issues.py +0 -0
  46. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_public_api_edges.py +0 -0
  47. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_readme_examples.py +0 -0
  48. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_stress_matrix.py +0 -0
  49. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_style_line_widths.py +0 -0
  50. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_styles.py +0 -0
  51. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_text_width.py +0 -0
  52. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_two_pass_measurement.py +0 -0
  53. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_upstream_issues.py +0 -0
  54. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v2021_fixes.py +0 -0
  55. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v210_rows_copy.py +0 -0
  56. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v2110_fixes.py +0 -0
  57. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v211_fixes.py +0 -0
  58. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v213_features.py +0 -0
  59. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v214_features.py +0 -0
  60. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v215_features.py +0 -0
  61. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v216_features.py +0 -0
  62. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v217_features.py +0 -0
  63. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v218_features.py +0 -0
  64. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v219_coverage.py +0 -0
  65. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v219_fixes.py +0 -0
  66. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v220_exports.py +0 -0
  67. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v221_fixes.py +0 -0
  68. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v222_fixes.py +0 -0
  69. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v223_fixes.py +0 -0
  70. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v224_fixes.py +0 -0
  71. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v230_none.py +0 -0
  72. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v231_fixes.py +0 -0
  73. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v232_merge_ansi.py +0 -0
  74. {flexablelib-3.0.2 → flexablelib-3.0.3}/tests/test_v300_style_names.py +0 -0
  75. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/benchmark.py +0 -0
  76. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/generate_width_tables.py +0 -0
  77. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/readme_gallery.py +0 -0
  78. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/stress_matrix.py +0 -0
  79. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/style_examples.py +0 -0
  80. {flexablelib-3.0.2 → flexablelib-3.0.3}/tools/text_to_png.py +0 -0
@@ -2,6 +2,32 @@
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
+
5
31
  ## [3.0.2] - 2026-11-11
6
32
 
7
33
  ### Fixed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 3.0.2
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
+ )
@@ -362,9 +362,14 @@ def _pick_junction(v_glyph: str, h_glyph: str, n: bool, s: bool, e: bool, w: boo
362
362
  if v_glyph in '|+':
363
363
  # ASCII column lines join with '+' whatever the dash ('-', '=', '~').
364
364
  return _ASCII.get(key, '+')
365
+ if v_glyph == ':':
366
+ # dotted_rows: ':' is a through vertical. When a vertical arm is
367
+ # missing, fall back to the dotted dash so the junction does not
368
+ # point into a merge interior.
369
+ return ':' if (n and s) else (h_glyph if h_glyph and h_glyph != ':' else '.')
365
370
  if v_glyph and not ('\u2500' <= v_glyph <= '\u257f'):
366
- # Other non-box column glyphs (``dotted_rows`` draws ':') are their own
367
- # junction; a light box tee next to them is a foreign glyph.
371
+ # Other non-box column glyphs are their own junction; a light box
372
+ # tee next to them is a foreign glyph.
368
373
  return v_glyph
369
374
  if v_glyph == '║' and h_glyph == '═':
370
375
  return _DOUBLE.get(key, '║')
@@ -557,6 +562,23 @@ def _clamp_rule_to_h_ranges(line, layout, composition, h_ranges, filler=' '):
557
562
  return rewritten
558
563
 
559
564
 
565
+ _OUTER_FRAME_CORNERS = set(".'`")
566
+
567
+
568
+ def _is_outer_frame(line: str) -> bool:
569
+ """True for a top/bottom box or ascii-rounded frame, not a grid rule."""
570
+ if not line:
571
+ return False
572
+ plain = strip_ansi(line)
573
+ if not plain or _re_search_word(plain):
574
+ return False
575
+ return plain[0] in _OUTER_FRAME_CORNERS and plain[-1] in _OUTER_FRAME_CORNERS
576
+
577
+
578
+ def _re_search_word(plain: str) -> bool:
579
+ return any(ch.isalnum() for ch in plain)
580
+
581
+
560
582
  def _looks_like_rule(line: str, h_glyph: str = None) -> bool:
561
583
  """True only for real separator lines, not cell text that contains '-'."""
562
584
  if not line:
@@ -666,10 +688,19 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
666
688
  v_glyph, h_glyph, north, south, east, west,
667
689
  round_corners=round_corners,
668
690
  )
691
+ # Markdown/org header rules are '|---|---|'. Rewriting '|' to '+'
692
+ # would stop the line being a valid markdown separator.
693
+ if v_glyph == '|' and glyph == '+':
694
+ plain = strip_ansi(rewritten)
695
+ if plain.startswith('|') and plain.endswith('|') and '+' not in plain:
696
+ glyph = '|'
669
697
  if h_glyph in _HEAVY_H and glyph in _LIGHT_TO_HEAVY_H:
670
698
  glyph = _LIGHT_TO_HEAVY_H[glyph]
699
+ # Keep a plain vertical only when it still runs through the rule
700
+ # (both arms). A '|' or ':' that has lost the south (or north) arm
701
+ # must become a tee or a dash; otherwise it points into the merge.
671
702
  if (above is not False and _glyph(here) in _VERTICALS
672
- and (north or south)):
703
+ and north and south):
673
704
  glyph = here
674
705
  if only is not None and south and not north and _glyph(here) in _NO_UP:
675
706
  glyph = here # already has no arm upwards, e.g. a header box top
@@ -796,11 +827,11 @@ def apply(lines, row_line_map, regions, column_widths, composition,
796
827
  ``border_color`` (or the default foreground) and never pick up the
797
828
  merge fill.
798
829
 
799
- Column-only styles (verticals but no body rule) do not grow a synthetic
800
- cap around a multi-column merge. An inserted line would blank neighbour
801
- cells and make them look merged; without a real rule there is no outline
802
- to repair, and the rectangular fill (when coloured) already shows the
803
- span.
830
+ Column-only styles (verticals but no body rule) insert a synthetic cap
831
+ above and below a multi-column merge, drawn only across the merge
832
+ width, with tees that do not send a horizontal arm into neighbour
833
+ columns. A vertical-only merge does not get a cap: the table has no
834
+ row rules, and the left/right verticals already outline the span.
804
835
 
805
836
  Returns the rewritten lines.
806
837
  """
@@ -818,10 +849,42 @@ def apply(lines, row_line_map, regions, column_widths, composition,
818
849
  # their data lines; caps above/below need ┴/┬ repair.
819
850
  # (above_line, below_line, inner_separator_xs, first_row, last_row)
820
851
  horiz_caps = []
852
+ # Column-only gaps keyed by logical row *after which* a rule sits
853
+ # (gap after row G = between G and G+1). Values: miss_s, miss_n, insert_at.
854
+ gap_plans = {}
821
855
  # Visible-column ranges of every merge interior, per physical line, so
822
856
  # the chrome pass can recolour outline glyphs without touching the fill.
823
857
  interior_ranges = {}
824
858
 
859
+ synthesized = _synthesize_body_rule(composition)
860
+ col_only = (
861
+ composition.table_body_line is None
862
+ and synthesized is not None
863
+ and composition.vertical_table_body_lines is not None
864
+ and composition.vertical_table_body_lines.middle not in (None, '', ' ')
865
+ )
866
+ syn_h = synthesized.middle if synthesized is not None else None
867
+
868
+ def _blank_crossings(rule_line, gap_after_row):
869
+ """Erase horizontal through merges that continue across this gap."""
870
+ for crossing in regions:
871
+ if crossing.first_row <= gap_after_row and crossing.last_row >= gap_after_row + 1:
872
+ cross_bounds = _region_bounds(crossing, layout, index_offset)
873
+ if cross_bounds is None:
874
+ continue
875
+ (c_start, c_end), _, _, _ = cross_bounds
876
+ if c_end > c_start:
877
+ rule_line = _repaint(
878
+ rule_line, c_start, c_end, filler * (c_end - c_start)
879
+ )
880
+ return rule_line
881
+
882
+ def _shift_interiors(at):
883
+ return {
884
+ (k + 1 if k >= at else k): v
885
+ for k, v in interior_ranges.items()
886
+ }
887
+
825
888
  def _mark_interior(line_index, start, end):
826
889
  if end > start:
827
890
  interior_ranges.setdefault(line_index, []).append((start, end))
@@ -846,6 +909,14 @@ def apply(lines, row_line_map, regions, column_widths, composition,
846
909
  first_line = lines[target_lines[0]]
847
910
  text = strip_by_width(slice_by_width(first_line, cell_start, cell_end))
848
911
  text = str(text)
912
+ # strip_by_width keeps leading/trailing escapes so a cell can keep
913
+ # its colour when the merge has no colour of its own. If we are
914
+ # about to paint a region colour, those escapes must go: a leftover
915
+ # RESET would sit after the left pad and close the fill mid-span
916
+ # ("Mérida" falling out of the background).
917
+ spec = getattr(region, 'color', None)
918
+ if spec:
919
+ text = strip_ansi(text)
849
920
 
850
921
  if visible_width(text) > span_width:
851
922
  text = truncate_to_width(text, span_width)
@@ -856,7 +927,6 @@ def apply(lines, row_line_map, regions, column_widths, composition,
856
927
  # Colour the entire span (text line + blank continuations) so a merge
857
928
  # fill is flush across its rectangle -- same pad-then-paint contract
858
929
  # as ordinary cells. Borders stay outside _repaint's range.
859
- spec = getattr(region, 'color', None)
860
930
  if spec:
861
931
  painted = colorize(painted, spec)
862
932
  blank = colorize(blank, spec)
@@ -897,14 +967,46 @@ def apply(lines, row_line_map, regions, column_widths, composition,
897
967
  (above, below, set(inner_seps), region.first_row, region.last_row)
898
968
  )
899
969
 
970
+ if col_only and synthesized is not None:
971
+ # Cap above, including a merge on the first body row when
972
+ # the style has no header/body separator (ascii_rounded).
973
+ # If the line above is already a rule, pass 4 repairs it
974
+ # in place; otherwise a cap is inserted there.
975
+ g = region.first_row - 1
976
+ plan = gap_plans.setdefault(
977
+ g, {'s': set(), 'n': set(), 'at': min(target_lines),
978
+ 'h': []}
979
+ )
980
+ plan['s'] |= set(inner_seps)
981
+ plan['at'] = min(plan['at'], min(target_lines))
982
+ plan.setdefault('h', []).append((start, end))
983
+ # Cap below: gap after last_row (unless last table row — end
984
+ # line is repaired in pass 3).
985
+ max_row = max(row_line_map) if row_line_map else region.last_row
986
+ if region.last_row < max_row:
987
+ g = region.last_row
988
+ plan = gap_plans.setdefault(
989
+ g, {'s': set(), 'n': set(), 'at': max(target_lines) + 1,
990
+ 'h': []}
991
+ )
992
+ plan['n'] |= set(inner_seps)
993
+ plan['at'] = min(plan.get('at', max(target_lines) + 1),
994
+ max(target_lines) + 1)
995
+ plan.setdefault('h', []).append((start, end))
996
+
900
997
  # --- Pass 2: repair junctions on body rules touched by vertical blanks ---
901
- if h_glyph:
998
+ # Always run, even when the dash run was fully absorbed: leftover ┼
999
+ # between side-by-side merges must become │ (no H arms into spaces).
1000
+ if composition.vertical_table_body_lines is not None:
902
1001
  for sep_index in sorted(touched_seps):
903
1002
  line = lines[sep_index]
904
- if not _looks_like_rule(line, h_glyph):
905
- continue
1003
+ line_h = h_glyph or next(
1004
+ (ch for ch in strip_ansi(line) if _is_h(ch)), '─'
1005
+ )
906
1006
  lines[sep_index] = _repair_rule_junctions(
907
- line, layout, composition, h_glyph
1007
+ line, layout, composition, line_h,
1008
+ above=lines[sep_index - 1] if sep_index > 0 else None,
1009
+ below=lines[sep_index + 1] if sep_index + 1 < len(lines) else None,
908
1010
  )
909
1011
 
910
1012
  # --- Pass 3: ┴ / ┬ on existing rules above and below H / 2D merges -------
@@ -932,11 +1034,59 @@ def apply(lines, row_line_map, regions, column_widths, composition,
932
1034
  | missing_north_by_line.get(line_index, set())),
933
1035
  )
934
1036
 
935
- # No synthetic caps. Inserting a rule in a column-only style blanks the
936
- # neighbour columns on that new line and makes them look merged. Outline
937
- # tees are only repaired on rules that already exist (pass 3).
1037
+ # --- Pass 4: synthetic caps for column-only styles ----------------------
1038
+ # A multi-column merge in a style with no body rule has no top/bottom
1039
+ # outline, so a │ on the neighbouring row points into the merge interior
1040
+ # (wrong vertex) and the block is easy to misread as continuing. Insert a
1041
+ # rule only across the merge width, with ├/┤ (or ┌/┐) at the edges so
1042
+ # horizontal arms do not bleed into neighbour columns. Neighbour columns
1043
+ # keep their verticals through the new line; they are not given a
1044
+ # horizontal cap of their own.
1045
+ if col_only and gap_plans and synthesized is not None:
1046
+ for g in sorted(gap_plans.keys(), reverse=True):
1047
+ plan = gap_plans[g]
1048
+ at = plan['at']
1049
+ existing = None
1050
+ if 0 <= at < len(lines) and _looks_like_rule(lines[at], syn_h):
1051
+ existing = at
1052
+ elif 0 <= at - 1 < len(lines) and _looks_like_rule(lines[at - 1], syn_h):
1053
+ existing = at - 1
1054
+
1055
+ if existing is not None and _is_outer_frame(lines[existing]):
1056
+ existing = None
1057
+
1058
+ if existing is not None:
1059
+ line = lines[existing]
1060
+ line = _blank_crossings(line, g)
1061
+ line_h = next((ch for ch in strip_ansi(line) if _is_h(ch)), syn_h or '─')
1062
+ lines[existing] = _repair_rule_junctions(
1063
+ line, layout, composition, line_h,
1064
+ missing_south=plan['s'] or None,
1065
+ missing_north=plan['n'] or None,
1066
+ above=lines[existing - 1] if existing > 0 else None,
1067
+ below=lines[existing + 1] if existing + 1 < len(lines) else None,
1068
+ )
1069
+ continue
1070
+
1071
+ rule = _build_rule_line(
1072
+ layout, composition, column_widths, hidden_columns, synthesized
1073
+ )
1074
+ if rule is None:
1075
+ continue
1076
+ rule = _clamp_rule_to_h_ranges(
1077
+ rule, layout, composition, plan.get('h') or []
1078
+ )
1079
+ rule = _blank_crossings(rule, g)
1080
+ rule = _repair_rule_junctions(
1081
+ rule, layout, composition, syn_h,
1082
+ missing_south=plan['s'] or None,
1083
+ missing_north=plan['n'] or None,
1084
+ )
1085
+ at = max(0, min(at, len(lines)))
1086
+ lines.insert(at, rule)
1087
+ interior_ranges = _shift_interiors(at)
938
1088
 
939
- # --- Pass 4: restore border chrome on remaining rule glyphs --------------
1089
+ # --- Pass 5: restore border chrome on remaining rule glyphs --------------
940
1090
  if border_color:
941
1091
  for line_index, line in enumerate(lines):
942
1092
  if not _looks_like_rule(line, h_glyph):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 3.0.2
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
@@ -10,6 +10,7 @@ style_examples.md
10
10
  flexablelib/__init__.py
11
11
  flexablelib/_speedups.c
12
12
  flexablelib/_width_tables.h
13
+ flexablelib/box_topology.py
13
14
  flexablelib/cells.py
14
15
  flexablelib/colors.py
15
16
  flexablelib/columns.py
@@ -32,6 +33,7 @@ flexablelib.egg-info/top_level.txt
32
33
  tests/conftest.py
33
34
  tests/helpers.py
34
35
  tests/test_align_indicators.py
36
+ tests/test_box_topology.py
35
37
  tests/test_cells.py
36
38
  tests/test_columns.py
37
39
  tests/test_excel.py
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexablelib",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "private": true,
5
5
  "description": "Flexible console and HTML tables for Python: styles, merges, colors, widths, decimal alignment.",
6
6
  "repository": {
@@ -0,0 +1,109 @@
1
+ """Topology of box-drawing glyphs around merges, for every border style."""
2
+
3
+ import os
4
+
5
+ import pytest
6
+
7
+ from flexablelib import Table
8
+ from flexablelib.box_topology import check_merge_fill, new_breaks
9
+ from flexablelib.fast import strip_ansi, visible_width
10
+
11
+
12
+ RESET = '\x1b[0m'
13
+
14
+ H_BG = (255, 0, 170)
15
+ V_BG = (0, 255, 170)
16
+ BLOCK_BG = (0, 170, 255)
17
+
18
+
19
+ def _styles():
20
+ return list(Table().possible_styles)
21
+
22
+
23
+ def _base(style, colored):
24
+ table = Table(style_name=style)
25
+ table.add_column('A', ['a0', 'a1', 'a2', 'a3'])
26
+ table.add_column('B', ['b0', 'b1', 'b2', 'b3'])
27
+ table.add_column('C', ['c0', 'c1', 'c2', 'c3'])
28
+ table.add_column('D', ['d0', 'd1', 'd2', 'd3'])
29
+ if colored:
30
+ os.environ['FORCE_COLOR'] = '1'
31
+ table.use_colors = True
32
+ table.border_color = 'red'
33
+ table.zebra = 'black on #222222'
34
+ else:
35
+ table.use_colors = False
36
+ return table
37
+
38
+
39
+ def _apply(table, merges, colored):
40
+ colors = {
41
+ 'h': 'black on #ff00aa',
42
+ 'v': 'black on #00ffaa',
43
+ 'b': 'black on #00aaff',
44
+ }
45
+ for kind, coords, value in merges:
46
+ kw = dict(
47
+ last_row=coords[2] if len(coords) > 2 else coords[0],
48
+ last_column=coords[3] if len(coords) > 3 else coords[1],
49
+ value=value,
50
+ align='c',
51
+ )
52
+ if colored:
53
+ kw['color'] = colors[kind]
54
+ table.merge_cells(coords[0], coords[1], **kw)
55
+ return table
56
+
57
+
58
+ # Battery: H, V, 2D, corners, edges, adjacent, against header and footer.
59
+ _BATTERY = [
60
+ ('H-mid', [('h', (1, 1, 1, 2), 'HSPAN')]),
61
+ ('V-mid', [('v', (1, 1, 2, 1), 'VSPAN')]),
62
+ ('2D-mid', [('b', (1, 1, 2, 2), 'BLOCK')]),
63
+ ('H-corner-tl', [('h', (0, 0, 0, 1), 'TL')]),
64
+ ('H-corner-br', [('h', (3, 2, 3, 3), 'BR')]),
65
+ ('V-edge-left', [('v', (0, 0, 2, 0), 'LEFT')]),
66
+ ('V-edge-right', [('v', (1, 3, 3, 3), 'RIGHT')]),
67
+ ('2D-first-row', [('b', (0, 1, 1, 2), 'HEAD')]),
68
+ ('2D-last-row', [('b', (2, 1, 3, 2), 'FOOT')]),
69
+ ('adjacent-side', [('v', (1, 0, 2, 0), 'L'), ('v', (1, 1, 2, 1), 'R')]),
70
+ ('adjacent-stacked', [('h', (1, 1, 1, 2), 'TOP'), ('h', (2, 1, 2, 2), 'BOT')]),
71
+ ('adjacent-L', [('v', (0, 0, 2, 0), 'TALL'), ('h', (0, 1, 0, 2), 'WIDE')]),
72
+ ]
73
+
74
+
75
+ @pytest.mark.parametrize('style', _styles())
76
+ @pytest.mark.parametrize('name,merges', _BATTERY, ids=[n for n, _ in _BATTERY])
77
+ def test_battery_plain_topology(style, name, merges):
78
+ plain = _base(style, colored=False)
79
+ baseline = str(plain)
80
+ _apply(plain, merges, colored=False)
81
+ rendered = str(plain)
82
+ lines = rendered.splitlines()
83
+ if len({visible_width(l) for l in baseline.splitlines()}) == 1:
84
+ assert len({visible_width(l) for l in lines}) == 1
85
+ assert new_breaks(lines, baseline.splitlines()) == []
86
+
87
+
88
+ @pytest.mark.parametrize('style', _styles())
89
+ def test_battery_colored_matches_plain_and_fills(style):
90
+ os.environ['FORCE_COLOR'] = '1'
91
+ # One table with H + V + 2D together (they do not overlap).
92
+ merges = [
93
+ ('h', (0, 0, 0, 1), 'HSPAN'),
94
+ ('v', (1, 0, 2, 0), 'VSPAN'),
95
+ ('b', (1, 2, 2, 3), 'BLOCK'),
96
+ ]
97
+ plain = _apply(_base(style, colored=False), merges, colored=False)
98
+ colored = _apply(_base(style, colored=True), merges, colored=True)
99
+ plain_s = str(plain)
100
+ color_s = str(colored)
101
+ assert strip_ansi(color_s) == plain_s
102
+ assert RESET * 2 not in color_s
103
+ assert new_breaks(plain_s.splitlines(), str(_base(style, False)).splitlines()) == []
104
+ assert new_breaks(color_s.splitlines(), plain_s.splitlines()) == []
105
+ problems = []
106
+ problems += check_merge_fill(color_s.splitlines(), H_BG, 'HSPAN')
107
+ problems += check_merge_fill(color_s.splitlines(), V_BG, 'VSPAN')
108
+ problems += check_merge_fill(color_s.splitlines(), BLOCK_BG, 'BLOCK')
109
+ assert problems == []
@@ -86,15 +86,16 @@ def test_last_row_horizontal_merge_bottom_is_not_header_tee(style):
86
86
 
87
87
 
88
88
  @pytest.mark.parametrize('style', ['double_horizontal', 'rounded_double_underline', 'dashed'])
89
- def test_column_only_multi_col_merge_stays_square_without_cap(style):
89
+ def test_column_only_multi_col_merge_stays_square_with_clamped_cap(style):
90
+ from flexablelib.box_topology import new_breaks
90
91
  table = _build_base(style)
91
92
  before = str(table)
92
93
  table.merge_cells(1, 0, last_column=1, value='AB')
93
94
  rendered = str(table)
94
95
  lines = rendered.splitlines()
95
96
  assert any('AB' in line for line in lines)
96
- assert len(lines) == len(before.splitlines())
97
97
  _square(rendered)
98
+ assert new_breaks(lines, before.splitlines()) == []
98
99
 
99
100
 
100
101
  @pytest.mark.parametrize('style', ['double_horizontal', 'rounded_double_underline', 'dashed'])
@@ -193,8 +193,9 @@ def test_coloured_merges_and_zebra_paint_cleanly(style):
193
193
 
194
194
 
195
195
  @pytest.mark.parametrize('style', _styles())
196
- def test_column_only_styles_do_not_insert_synthetic_caps(style):
197
- """A multi-column merge must not grow the table by a synthetic rule."""
196
+ def test_multi_column_merge_keeps_topology_and_width(style):
197
+ """A multi-column merge may grow a width-clamped cap; topology stays clean."""
198
+ from flexablelib.box_topology import new_breaks
198
199
  table = Table(style_name=style)
199
200
  table.add_column('A', ['a1', 'a2', 'a3'])
200
201
  table.add_column('B', ['b1', 'b2', 'b3'])
@@ -203,11 +204,10 @@ def test_column_only_styles_do_not_insert_synthetic_caps(style):
203
204
  table.merge_cells(1, 0, last_column=1, value='AB')
204
205
  after = str(table)
205
206
  assert 'AB' in after
206
- # No synthetic cap: the merged table does not gain a line.
207
- assert len(after.splitlines()) == len(before.splitlines())
208
207
  widths = {visible_width(line) for line in after.splitlines()}
209
208
  if len({visible_width(line) for line in before.splitlines()}) == 1:
210
209
  assert len(widths) == 1
210
+ assert new_breaks(after.splitlines(), before.splitlines()) == []
211
211
 
212
212
 
213
213
  def test_zebra_plus_border_color_on_rows_keeps_rule_chrome():
@@ -242,3 +242,25 @@ def test_theme_slate_2d_merge_is_rectangular():
242
242
  assert _RESET * 2 not in rendered
243
243
  assert 'Centro' in strip_ansi(rendered)
244
244
  _assert_rectangular(rendered, 'Centro', (250, 204, 21))
245
+
246
+
247
+ def test_merge_fill_covers_inherited_cell_text():
248
+ """A leftover RESET from a dim border must not close the fill mid-label."""
249
+ os.environ['FORCE_COLOR'] = '1'
250
+ table = Table(
251
+ rows=[
252
+ ['Norte', 'Ana', 28, 'Guadalajara', 12],
253
+ ['Norte', 'José', 35, 'Monterrey', 18],
254
+ ['Centro', 'Luna', 22, 'Mérida', 15],
255
+ ['Centro', 'Sofía', 41, 'Puebla', 9],
256
+ ],
257
+ headers=['Grupo', 'Nombre', 'Edad', 'Ciudad', 'Pts'],
258
+ style_name='rounded',
259
+ )
260
+ table.apply_theme('dark')
261
+ table.use_colors = True
262
+ table.merge_cells(2, 3, 3, 4, color='black on #22d3ee', align='c')
263
+ rendered = str(table)
264
+ assert _RESET * 2 not in rendered
265
+ _assert_rectangular(rendered, 'Mérida', (34, 211, 238))
266
+ assert '\x1b[0mMérida' not in rendered
@@ -295,12 +295,11 @@ def test_a_merge_works_across_styles(style):
295
295
  rendered = str(table)
296
296
 
297
297
  assert 'span' in rendered
298
- # Column-only styles no longer grow a synthetic cap (that blanked
299
- # neighbour cells). Every line must still share the width, and the
300
- # table must not gain a line.
298
+ # Column-only styles may grow a width-clamped cap; every line must
299
+ # still share the width.
301
300
  after = [len(line) for line in rendered.splitlines()]
302
301
  assert set(after) == set(before)
303
- assert len(after) == len(before)
302
+ assert len(after) >= len(before)
304
303
 
305
304
 
306
305
  # +-------------------------------------------------------------------------+
@@ -564,13 +563,14 @@ def test_horizontal_merge_caps_become_tees_above_and_below():
564
563
  assert '├────┬────┼────┤' in below
565
564
 
566
565
 
567
- def test_column_only_style_does_not_insert_synthetic_cap():
566
+ def test_column_only_style_caps_are_clamped_to_the_merge():
568
567
  """double_horizontal has column lines but no row lines.
569
568
 
570
- A synthetic cap would blank neighbour cells on a new line and make
571
- them look merged. The multi-column span is shown by dropping the
572
- internal vertical; the table stays the same height and stays square.
569
+ A multi-column merge gets a cap only across its width, with tees that
570
+ do not send a horizontal arm through neighbour C. The neighbour stays
571
+ visible on the merge row and the table stays square.
573
572
  """
573
+ from flexablelib.box_topology import new_breaks
574
574
  table = Table(style_name='double_horizontal')
575
575
  table.add_column('A', ['a1', 'a2', 'a3'])
576
576
  table.add_column('B', ['b1', 'b2', 'b3'])
@@ -580,11 +580,10 @@ def test_column_only_style_does_not_insert_synthetic_cap():
580
580
  rendered = str(table)
581
581
  lines = rendered.splitlines()
582
582
  assert any('AB' in line for line in lines)
583
- assert len(lines) == len(before.splitlines())
584
583
  assert len({visible_width(line) for line in lines}) == 1
585
- # Neighbour C stays a normal cell on the merged row, not a blank.
586
584
  ab_line = next(line for line in lines if 'AB' in line)
587
585
  assert 'c2' in ab_line
586
+ assert new_breaks(lines, before.splitlines()) == []
588
587
 
589
588
 
590
589
  def test_block_merge_header_and_footer_caps():
@@ -678,12 +677,13 @@ def _looks_like_rule_line(line: str) -> bool:
678
677
  return re.search(r'[─━═┄┅┈┉╌╍▬−\-~=]{2,}', line) is not None
679
678
 
680
679
 
681
- def test_multi_column_merge_in_column_only_style_does_not_add_a_cap():
682
- """A 2D merge in a column-only style keeps the original line count.
680
+ def test_multi_column_merge_in_column_only_style_clamps_the_cap():
681
+ """A 2D merge in a column-only style gets a width-clamped cap.
683
682
 
684
- Neighbour cells on the same rows stay visible; no synthetic rule is
685
- inserted between them. Existing header/end rules keep their own dash.
683
+ Neighbour cells on the same rows stay visible; the cap does not send
684
+ H through column A. Existing header/end rules keep their own dash.
686
685
  """
686
+ from flexablelib.box_topology import new_breaks
687
687
  table = Table(style_name='rounded_double_underline')
688
688
  table.add_column('A', ['a1', 'a2', 'a3', 'a4'])
689
689
  table.add_column('B', ['b1', 'b2', 'b3', 'b4'])
@@ -694,14 +694,12 @@ def test_multi_column_merge_in_column_only_style_does_not_add_a_cap():
694
694
  rendered = str(table)
695
695
  lines = rendered.splitlines()
696
696
  assert any('BC' in line for line in lines)
697
- assert len(lines) == len(before.splitlines())
698
697
  assert len({visible_width(line) for line in lines}) == 1
699
- # Neighbour A stays a normal cell beside the merge, not a blank.
700
698
  bc_line = next(line for line in lines if 'BC' in line)
701
699
  assert 'a2' in bc_line or 'a3' in bc_line
702
- # Existing outer rules keep ─ (or ═ under the header), never a foreign dash.
703
700
  bottom = lines[-1]
704
701
  assert '┄' not in bottom
702
+ assert new_breaks(lines, before.splitlines()) == []
705
703
 
706
704
 
707
705
  def test_dashes_style_bottom_under_merge_keeps_dotted_glyph_not_solid():
@@ -748,8 +746,9 @@ def test_round_edges_end_corners_survive_last_row_horizontal_merge():
748
746
 
749
747
 
750
748
  def test_crossing_vertical_merge_is_not_cut_by_a_synthetic_cap():
751
- """A 2D block beside a continuing vertical merge must not insert a cap
752
- that draws a horizontal through the vertical merge."""
749
+ """A 2D block beside a continuing vertical merge must not draw a
750
+ horizontal through the vertical merge. Caps stay inside the 2D span."""
751
+ from flexablelib.box_topology import new_breaks
753
752
  table = Table(style_name='rounded_double_underline')
754
753
  table.add_column('Cat', [''] * 6)
755
754
  table.add_column('Item', [f'i{r}' for r in range(6)])
@@ -762,21 +761,22 @@ def test_crossing_vertical_merge_is_not_cut_by_a_synthetic_cap():
762
761
  rendered = str(table)
763
762
  lines = rendered.splitlines()
764
763
  assert 'ALL' in rendered and 'BOX' in rendered
765
- assert len(lines) == len(before.splitlines())
766
- # No body rule is introduced; existing header/end stay the only rules
767
- # with a horizontal run besides the header underline.
768
- body_caps = [
769
- line for line in lines
770
- if _looks_like_rule_line(line)
771
- and '═' not in line
772
- and not line.startswith('╭')
773
- and not line.startswith('╰')
774
- ]
775
- assert body_caps == []
764
+ # Body caps flanking BOX must start with │ (the ALL column continues)
765
+ # and must not run H through ALL.
766
+ for line in lines:
767
+ if not _looks_like_rule_line(line):
768
+ continue
769
+ if '═' in line or line.startswith('╭') or line.startswith('╰'):
770
+ continue
771
+ if '┬' in line or '┴' in line:
772
+ assert line.startswith('│'), line
773
+ assert '├──────┼' not in line, line
774
+ assert new_breaks(lines, before.splitlines()) == []
776
775
 
777
776
  def test_column_only_merge_does_not_draw_a_cap_into_neighbours():
778
- """A multi-column merge in a column-only style must not insert a rule
779
- that blanks or draws H through neighbour columns."""
777
+ """A multi-column merge in a column-only style must not draw H through
778
+ neighbour columns. A width-clamped cap is allowed."""
779
+ from flexablelib.box_topology import new_breaks
780
780
  table = Table(style_name='ascii_double_underline_open_header')
781
781
  for c in range(4):
782
782
  table.add_column(f'C{c}', [f'r{r}c{c}' for r in range(4)])
@@ -787,17 +787,15 @@ def test_column_only_merge_does_not_draw_a_cap_into_neighbours():
787
787
  rendered = str(table)
788
788
  lines = rendered.splitlines()
789
789
  assert 'HSPAN' in rendered
790
- assert len(lines) == len(before.splitlines())
791
790
  hspan = next(line for line in lines if 'HSPAN' in line)
792
- # The unmerged last column stays visible on the merge row.
793
791
  assert 'r0c3' in hspan
794
- # No mid-table cap line after the merge text.
795
- below = lines[lines.index(hspan) + 1]
796
- assert not _looks_like_rule_line(below), below
792
+ assert len({visible_width(line) for line in lines}) == 1
793
+ assert new_breaks(lines, before.splitlines()) == []
797
794
 
798
795
 
799
796
  def test_column_only_merge_does_not_bleed_h_through_unrelated_column():
800
797
  """Item column next to a V|N merge must not gain a horizontal run."""
798
+ from flexablelib.box_topology import new_breaks
801
799
  table = Table(style_name='rounded_double_underline')
802
800
  table.add_column('Cat', [''] * 6)
803
801
  table.add_column('Item', [f'i{r}' for r in range(6)])
@@ -810,10 +808,16 @@ def test_column_only_merge_does_not_bleed_h_through_unrelated_column():
810
808
  rendered = str(table)
811
809
  lines = rendered.splitlines()
812
810
  assert 'ALL' in rendered and 'BOX' in rendered
813
- assert len(lines) == len(before.splitlines())
814
811
  box_line = next(line for line in lines if 'BOX' in line)
815
- # The Item column beside BOX stays body text, not a blank cap.
816
812
  assert 'i1' in box_line or 'i2' in box_line or 'i3' in box_line
813
+ for line in lines:
814
+ if not _looks_like_rule_line(line):
815
+ continue
816
+ if '═' in line or line.startswith('╭') or line.startswith('╰'):
817
+ continue
818
+ if '┬' in line or '┴' in line:
819
+ assert '├──────┼' not in line, line
820
+ assert new_breaks(lines, before.splitlines()) == []
817
821
 
818
822
 
819
823
  def test_invisible_vertical_style_keeps_plain_dash_rules():
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes