flexablelib 3.0.0__tar.gz → 3.0.2__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 (78) hide show
  1. {flexablelib-3.0.0 → flexablelib-3.0.2}/CHANGELOG.md +38 -0
  2. {flexablelib-3.0.0/flexablelib.egg-info → flexablelib-3.0.2}/PKG-INFO +1 -1
  3. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/merges.py +130 -128
  4. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/table.py +25 -108
  5. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/text_width.py +19 -1
  6. {flexablelib-3.0.0 → flexablelib-3.0.2/flexablelib.egg-info}/PKG-INFO +1 -1
  7. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib.egg-info/SOURCES.txt +2 -0
  8. {flexablelib-3.0.0 → flexablelib-3.0.2}/package.json +1 -1
  9. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_features.py +13 -9
  10. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_issues.py +1 -1
  11. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_merge_edge_matrix.py +7 -8
  12. flexablelib-3.0.2/tests/test_merge_fill.py +244 -0
  13. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_merges.py +91 -100
  14. flexablelib-3.0.2/tests/test_v232_merge_ansi.py +113 -0
  15. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/stress_matrix.py +5 -0
  16. {flexablelib-3.0.0 → flexablelib-3.0.2}/LICENSE +0 -0
  17. {flexablelib-3.0.0 → flexablelib-3.0.2}/MANIFEST.in +0 -0
  18. {flexablelib-3.0.0 → flexablelib-3.0.2}/README.md +0 -0
  19. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/__init__.py +0 -0
  20. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/_speedups.c +0 -0
  21. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/_width_tables.h +0 -0
  22. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/cells.py +0 -0
  23. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/colors.py +0 -0
  24. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/columns.py +0 -0
  25. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/fast.py +0 -0
  26. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/options.py +0 -0
  27. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/py.typed +0 -0
  28. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/readers.py +0 -0
  29. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/style_compositions.py +0 -0
  30. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/table_strings.py +0 -0
  31. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/utils.py +0 -0
  32. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib/writers.py +0 -0
  33. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib.egg-info/dependency_links.txt +0 -0
  34. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib.egg-info/requires.txt +0 -0
  35. {flexablelib-3.0.0 → flexablelib-3.0.2}/flexablelib.egg-info/top_level.txt +0 -0
  36. {flexablelib-3.0.0 → flexablelib-3.0.2}/pytest.ini +0 -0
  37. {flexablelib-3.0.0 → flexablelib-3.0.2}/setup.cfg +0 -0
  38. {flexablelib-3.0.0 → flexablelib-3.0.2}/setup.py +0 -0
  39. {flexablelib-3.0.0 → flexablelib-3.0.2}/style_examples.md +0 -0
  40. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/conftest.py +0 -0
  41. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/helpers.py +0 -0
  42. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_align_indicators.py +0 -0
  43. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_cells.py +0 -0
  44. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_columns.py +0 -0
  45. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_excel.py +0 -0
  46. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_public_api_edges.py +0 -0
  47. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_readme_examples.py +0 -0
  48. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_stress_matrix.py +0 -0
  49. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_style_line_widths.py +0 -0
  50. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_styles.py +0 -0
  51. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_text_width.py +0 -0
  52. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_two_pass_measurement.py +0 -0
  53. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_upstream_issues.py +0 -0
  54. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v2021_fixes.py +0 -0
  55. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v210_rows_copy.py +0 -0
  56. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v2110_fixes.py +0 -0
  57. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v211_fixes.py +0 -0
  58. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v213_features.py +0 -0
  59. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v214_features.py +0 -0
  60. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v215_features.py +0 -0
  61. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v216_features.py +0 -0
  62. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v217_features.py +0 -0
  63. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v218_features.py +0 -0
  64. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v219_coverage.py +0 -0
  65. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v219_fixes.py +0 -0
  66. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v220_exports.py +0 -0
  67. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v221_fixes.py +0 -0
  68. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v222_fixes.py +0 -0
  69. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v223_fixes.py +0 -0
  70. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v224_fixes.py +0 -0
  71. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v230_none.py +0 -0
  72. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v231_fixes.py +0 -0
  73. {flexablelib-3.0.0 → flexablelib-3.0.2}/tests/test_v300_style_names.py +0 -0
  74. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/benchmark.py +0 -0
  75. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/generate_width_tables.py +0 -0
  76. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/readme_gallery.py +0 -0
  77. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/style_examples.py +0 -0
  78. {flexablelib-3.0.0 → flexablelib-3.0.2}/tools/text_to_png.py +0 -0
@@ -2,6 +2,44 @@
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.2] - 2026-11-11
6
+
7
+ ### Fixed
8
+ - Merge and zebra backgrounds now fill a single rectangle: the interior of
9
+ the cell or merge, including any separators absorbed between its rows.
10
+ The fill no longer stairsteps at the corners, leaks onto neighbouring
11
+ cells, or paints the outline glyphs (`─`, `═`, tees and crossings).
12
+ - Horizontal rules keep `border_color` (or the default foreground) and do
13
+ not inherit a cell or zebra background, so a `_rows` style no longer
14
+ loses its border colour or leaves an uncoloured gap on a junction.
15
+ - A leftover `ESC[0m` after a merge that already closed its colour is
16
+ dropped, so `column_colors` / zebra next to a coloured merge no longer
17
+ emit a doubled reset.
18
+ - Theme `slate` (psql + zebra) and other themes paint a 2D merge as a
19
+ solid rectangle; `border_color` no longer cuts through the label.
20
+ - Column-only styles (verticals but no rule between rows) no longer
21
+ insert a synthetic cap around a multi-column merge. That cap blanked
22
+ neighbour cells on a new line and made them look merged. The span is
23
+ shown by dropping the internal vertical; when the merge has a colour,
24
+ the rectangular fill marks it. Existing header and end rules still
25
+ get their tees repaired.
26
+
27
+ ## [3.0.1] - 2026-11-11
28
+
29
+ ### Fixed
30
+ - A merged region drawn over a cell with ANSI colour no longer leaves a
31
+ stray `ESC[0m` in the output. The reset is now added only when a colour is
32
+ actually still open where the merge starts.
33
+ - Slicing coloured text (`slice_by_width`, and merges that use it) no longer
34
+ appends a second reset after text that already closed its colour.
35
+
36
+ ### Added
37
+ - `text_width.sgr_is_open(text)` reports whether text leaves a colour or
38
+ attribute switched on.
39
+ - Tests for merge/slice escape handling and for `from_markdown` / `from_html`
40
+ edge cases (prose around a table, corrupt or malformed embedded payloads,
41
+ rowspans in the body and across the header).
42
+
5
43
  ## [3.0.0] - 2026-11-10
6
44
 
7
45
  ### Changed (breaking)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 3.0.0
3
+ Version: 3.0.2
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
@@ -20,7 +20,8 @@ import operator
20
20
  import re
21
21
  from typing import List, NamedTuple, Optional
22
22
 
23
- from .colors import background_hex, colorize
23
+ from .colors import colorize
24
+ from .text_width import sgr_is_open
24
25
  from .fast import (
25
26
  has_ansi,
26
27
  pad_to_width,
@@ -43,8 +44,17 @@ _RESET = '\x1b[0m'
43
44
  def _repaint(line, start, end, replacement):
44
45
  """Replace the visible columns [start, end) of a line, ending any colour."""
45
46
  before, removed, after = partition_by_width(line, start, end)
46
- if has_ansi(removed) and not replacement.startswith(_RESET):
47
+ if (has_ansi(removed) and sgr_is_open(before)
48
+ and not replacement.startswith(_RESET)):
47
49
  replacement = _RESET + replacement
50
+ # The removed span often keeps its trailing RESET, which partition leaves
51
+ # at the start of `after`. If the replacement already closed its colour,
52
+ # that leftover is a doubled reset (`ESC[0mESC[0m`).
53
+ if replacement.endswith(_RESET):
54
+ while after.startswith(_RESET):
55
+ after = after[len(_RESET):]
56
+ elif after.startswith(_RESET) and not sgr_is_open(before + replacement):
57
+ after = after[len(_RESET):]
48
58
  return before + replacement + after
49
59
 
50
60
 
@@ -672,44 +682,97 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
672
682
 
673
683
  def paint_rule_cell_backgrounds(line, layout, column_specs, border_color=None):
674
684
  """
675
- Paint each column's dash run on a horizontal rule with that column's cell
676
- background.
677
-
678
- Terminal grid tables put H-rules on their own row. Without a background,
679
- that row is a dark band between coloured cells (the ─ glyph is only a thin
680
- stroke). Colouring the dash run with the cell above makes the fill meet the
681
- rule. Junctions and outer corners stay outside the cell fill (same contract
682
- as vertical borders in ``flush``).
683
-
684
- ``column_specs`` is aligned with ``layout.spans`` (drawn columns). Specs
685
- without a background are skipped. Paint right-to-left so ANSI insertion
686
- does not shift later visible offsets.
685
+ Colour a horizontal rule with ``border_color`` only.
686
+
687
+ Cell, zebra and merge backgrounds belong on the interior rectangle of
688
+ the cell or merge (text lines and any rule segments absorbed inside a
689
+ merge). Rule glyphs on the outline stay chrome: they keep
690
+ ``border_color`` (or the default foreground) and never inherit a cell
691
+ fill, so a zebra stripe cannot leak onto ``─`` and a junction cannot
692
+ be left as an uncoloured gap.
693
+
694
+ ``layout`` and ``column_specs`` are accepted for compatibility and
695
+ ignored. Paint lives in ``apply`` for merge interiors and in the
696
+ table's rule colouring for chrome.
687
697
  """
688
698
  if not line:
689
699
  return line
690
- if not column_specs or not any(
691
- spec and background_hex(spec) is not None for spec in column_specs):
692
- return colorize(line, border_color) if border_color else line
700
+ return colorize(line, border_color) if border_color else line
693
701
 
694
- rewritten = line
695
- for (start, end), spec in reversed(list(zip(layout.spans, column_specs))):
696
- if end <= start:
697
- continue
698
- if not spec or background_hex(spec) is None:
699
- continue
700
- before, chunk, after = partition_by_width(rewritten, start, end)
701
- plain = strip_ansi(chunk) if has_ansi(chunk) else chunk
702
- if not plain:
703
- continue
704
- painted = colorize(plain, spec)
705
- if has_ansi(chunk) and not painted.startswith(_RESET):
706
- painted = _RESET + painted
707
- rewritten = before + painted + after
708
- return rewritten
702
+
703
+ def _join_sgr_parts(parts):
704
+ """Concatenate SGR spans, collapsing a reset that is immediately doubled."""
705
+ out = ''
706
+ for part in parts:
707
+ if out.endswith(_RESET) and part.startswith(_RESET):
708
+ part = part[len(_RESET):]
709
+ out += part
710
+ return out
711
+
712
+
713
+ def _apply_border_chrome(line, border_color, protected_ranges=()):
714
+ """
715
+ Recolour every visible column of a rule with ``border_color``, leaving
716
+ merge interiors untouched.
717
+
718
+ ``protected_ranges`` is a sequence of ``[start, end)`` visible-column
719
+ spans (the merge rectangle, including absorbed internal separators).
720
+ Those columns already hold the merge fill (spaces or text) and must
721
+ not pick up chrome. Everything else -- outline dashes, tees, outer
722
+ corners, and the rule on either side of a merge -- is stripped and
723
+ wrapped in ``border_color`` so a later ``_repaint`` cannot leave an
724
+ uncoloured hole where a colour was closed early.
725
+ """
726
+ if not line or not border_color:
727
+ return line
728
+ width = visible_width(line)
729
+ ranges = []
730
+ for start, end in protected_ranges:
731
+ start = max(0, start)
732
+ end = min(width, end)
733
+ if end > start:
734
+ ranges.append((start, end))
735
+ ranges.sort()
736
+ merged = []
737
+ for start, end in ranges:
738
+ if merged and start <= merged[-1][1]:
739
+ merged[-1] = (merged[-1][0], max(merged[-1][1], end))
740
+ else:
741
+ merged.append((start, end))
742
+
743
+ segments = []
744
+ cursor = 0
745
+ for start, end in merged:
746
+ if start > cursor:
747
+ segments.append((cursor, start, False))
748
+ segments.append((start, end, True))
749
+ cursor = max(cursor, end)
750
+ if cursor < width:
751
+ segments.append((cursor, width, False))
752
+
753
+ if not merged:
754
+ return colorize(strip_ansi(line), border_color)
755
+
756
+ parts = []
757
+ for start, end, kept in segments:
758
+ _, chunk, _ = partition_by_width(line, start, end)
759
+ if kept:
760
+ parts.append(chunk)
761
+ else:
762
+ painted = colorize(strip_ansi(chunk), border_color)
763
+ # The reset that closed the previous (merge) span often lands at
764
+ # the start of this chunk and would be stripped with the rest of
765
+ # its ANSI, so a 'red' prefix (fg only) would inherit the merge
766
+ # background. Re-open chrome with a reset when this is not the
767
+ # start of the line; _join_sgr_parts collapses a true double.
768
+ if start > 0 and not painted.startswith(_RESET):
769
+ painted = _RESET + painted
770
+ parts.append(painted)
771
+ return _join_sgr_parts(parts)
709
772
 
710
773
 
711
774
  def apply(lines, row_line_map, regions, column_widths, composition,
712
- index_offset, hidden_columns=(), filler=' '):
775
+ index_offset, hidden_columns=(), filler=' ', border_color=None):
713
776
  """
714
777
  Rewrite assembled lines so each region renders as one cell.
715
778
 
@@ -723,6 +786,22 @@ def apply(lines, row_line_map, regions, column_widths, composition,
723
786
  coordinates ignore it. ``hidden_columns`` lists the columns the table is
724
787
  not drawing, which take up no room in a line.
725
788
 
789
+ The merge interior is a single rectangle: every physical line from the
790
+ first content line to the last, including separators absorbed between
791
+ those rows, is painted from the first column's left edge to the last
792
+ column's right edge. Internal rule glyphs (including crossings) become
793
+ spaces and, when the region has a colour, carry that background.
794
+ Outline glyphs immediately outside the rectangle -- the rule above and
795
+ below, and the verticals to either side -- stay chrome: they use
796
+ ``border_color`` (or the default foreground) and never pick up the
797
+ merge fill.
798
+
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.
804
+
726
805
  Returns the rewritten lines.
727
806
  """
728
807
  if not regions:
@@ -739,32 +818,13 @@ def apply(lines, row_line_map, regions, column_widths, composition,
739
818
  # their data lines; caps above/below need ┴/┬ repair.
740
819
  # (above_line, below_line, inner_separator_xs, first_row, last_row)
741
820
  horiz_caps = []
742
- # Column-only gaps keyed by logical row *after which* a rule sits
743
- # (gap after row G = between G and G+1). Values: miss_s, miss_n, insert_at.
744
- gap_plans = {}
745
-
746
- synthesized = _synthesize_body_rule(composition)
747
- col_only = (
748
- composition.table_body_line is None
749
- and synthesized is not None
750
- and composition.vertical_table_body_lines is not None
751
- and composition.vertical_table_body_lines.middle not in (None, '', ' ')
752
- )
753
- syn_h = synthesized.middle if synthesized is not None else None
754
-
755
- def _blank_crossings(rule_line, gap_after_row):
756
- """Erase horizontal through merges that continue across this gap."""
757
- for region in regions:
758
- if region.first_row <= gap_after_row and region.last_row >= gap_after_row + 1:
759
- bounds = _region_bounds(region, layout, index_offset)
760
- if bounds is None:
761
- continue
762
- (start, end), _, _, _ = bounds
763
- if end > start:
764
- rule_line = _repaint(
765
- rule_line, start, end, filler * (end - start)
766
- )
767
- return rule_line
821
+ # Visible-column ranges of every merge interior, per physical line, so
822
+ # the chrome pass can recolour outline glyphs without touching the fill.
823
+ interior_ranges = {}
824
+
825
+ def _mark_interior(line_index, start, end):
826
+ if end > start:
827
+ interior_ranges.setdefault(line_index, []).append((start, end))
768
828
 
769
829
  for region in regions:
770
830
  bounds = _region_bounds(region, layout, index_offset)
@@ -808,6 +868,7 @@ def apply(lines, row_line_map, regions, column_widths, composition,
808
868
  continue
809
869
  replacement = painted if line_index == text_line else blank
810
870
  lines[line_index] = _repaint(line, start, end, replacement)
871
+ _mark_interior(line_index, start, end)
811
872
 
812
873
  # Blank separator lines inside a vertical (or 2D) span. Junctions are
813
874
  # repaired in a second pass once every region has painted, so adjacent
@@ -820,6 +881,7 @@ def apply(lines, row_line_map, regions, column_widths, composition,
820
881
  if visible_width(line) < end:
821
882
  continue
822
883
  lines[line_index] = _repaint(line, start, end, blank)
884
+ _mark_interior(line_index, start, end)
823
885
  touched_seps.add(line_index)
824
886
 
825
887
  inner_seps = [
@@ -835,31 +897,6 @@ def apply(lines, row_line_map, regions, column_widths, composition,
835
897
  (above, below, set(inner_seps), region.first_row, region.last_row)
836
898
  )
837
899
 
838
- if col_only and synthesized is not None:
839
- # Cap above: gap after first_row-1 (if any).
840
- if region.first_row > 0:
841
- g = region.first_row - 1
842
- plan = gap_plans.setdefault(
843
- g, {'s': set(), 'n': set(), 'at': min(target_lines),
844
- 'h': []}
845
- )
846
- plan['s'] |= set(inner_seps)
847
- plan['at'] = min(plan['at'], min(target_lines))
848
- plan.setdefault('h', []).append((start, end))
849
- # Cap below: gap after last_row (unless last table row — end
850
- # line is repaired in pass 3).
851
- max_row = max(row_line_map) if row_line_map else region.last_row
852
- if region.last_row < max_row:
853
- g = region.last_row
854
- plan = gap_plans.setdefault(
855
- g, {'s': set(), 'n': set(), 'at': max(target_lines) + 1,
856
- 'h': []}
857
- )
858
- plan['n'] |= set(inner_seps)
859
- plan['at'] = min(plan.get('at', max(target_lines) + 1),
860
- max(target_lines) + 1)
861
- plan.setdefault('h', []).append((start, end))
862
-
863
900
  # --- Pass 2: repair junctions on body rules touched by vertical blanks ---
864
901
  if h_glyph:
865
902
  for sep_index in sorted(touched_seps):
@@ -895,52 +932,17 @@ def apply(lines, row_line_map, regions, column_widths, composition,
895
932
  | missing_north_by_line.get(line_index, set())),
896
933
  )
897
934
 
898
- # --- Pass 4: synthetic caps for column-only styles ----------------------
899
- # Insert a rule in each planned gap that does not already hold a real
900
- # separator. Blank any merge that continues across the gap so taller
901
- # vertical/2D merges are not crossed by ghost edges.
902
- if col_only and gap_plans and synthesized is not None:
903
- for g in sorted(gap_plans.keys(), reverse=True):
904
- plan = gap_plans[g]
905
- at = plan['at']
906
- # If the line already at `at` (or just above the lower row) is a
907
- # real rule, repair it in place instead of inserting.
908
- existing = None
909
- if 0 <= at < len(lines) and _looks_like_rule(lines[at], syn_h):
910
- existing = at
911
- elif 0 <= at - 1 < len(lines) and _looks_like_rule(lines[at - 1], syn_h):
912
- # Header/end sitting where we wanted a gap.
913
- existing = at - 1
914
-
915
- if existing is not None:
916
- line = lines[existing]
917
- line = _blank_crossings(line, g)
918
- line_h = next((ch for ch in line if _is_h(ch)), syn_h or '─')
919
- lines[existing] = _repair_rule_junctions(
920
- line, layout, composition, line_h,
921
- missing_south=plan['s'] or None,
922
- missing_north=plan['n'] or None,
923
- above=lines[existing - 1] if existing > 0 else None,
924
- below=lines[existing + 1] if existing + 1 < len(lines) else None,
925
- )
926
- continue
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).
927
938
 
928
- rule = _build_rule_line(
929
- layout, composition, column_widths, hidden_columns, synthesized
930
- )
931
- if rule is None:
939
+ # --- Pass 4: restore border chrome on remaining rule glyphs --------------
940
+ if border_color:
941
+ for line_index, line in enumerate(lines):
942
+ if not _looks_like_rule(line, h_glyph):
932
943
  continue
933
- # H only across the merges that asked for this cap — not the
934
- # whole table — then punch through taller crossings.
935
- rule = _clamp_rule_to_h_ranges(
936
- rule, layout, composition, plan.get('h') or []
937
- )
938
- rule = _blank_crossings(rule, g)
939
- rule = _repair_rule_junctions(
940
- rule, layout, composition, syn_h,
941
- missing_south=plan['s'] or None,
942
- missing_north=plan['n'] or None,
944
+ lines[line_index] = _apply_border_chrome(
945
+ line, border_color, interior_ranges.get(line_index, ()),
943
946
  )
944
- lines.insert(max(0, min(at, len(lines))), rule)
945
947
 
946
948
  return lines
@@ -4747,6 +4747,9 @@ class Table(object):
4747
4747
  hidden_columns = self.__effective_hidden_columns(table_with_i)
4748
4748
 
4749
4749
  regions = self.__regions_with_colors()
4750
+ border_color = (
4751
+ self.__border_color if self.__colors_active else None
4752
+ )
4750
4753
  merged = apply_merges(
4751
4754
  table_string.split('\n'),
4752
4755
  row_line_map,
@@ -4755,44 +4758,11 @@ class Table(object):
4755
4758
  self.__style_composition,
4756
4759
  1 if table_with_i else 0,
4757
4760
  hidden_columns,
4761
+ border_color=border_color,
4758
4762
  )
4759
4763
  return '\n'.join(merged)
4760
4764
 
4761
4765
 
4762
- def __rule_spec_for_cell(self, row_index, column_index, column_titles):
4763
- """
4764
- Colour that should back the H-rule segment under a cell.
4765
-
4766
- Explicit merge colours win (so a wide merge paints one continuous
4767
- band under its span). Otherwise the cell's resolved colour is used.
4768
- """
4769
- for region in self.__regions_with_colors():
4770
- if (region.first_row <= row_index <= region.last_row
4771
- and region.first_column <= column_index <= region.last_column
4772
- and region.color):
4773
- return region.color
4774
- name = (
4775
- column_titles[column_index]
4776
- if column_index < len(column_titles) else ''
4777
- )
4778
- try:
4779
- raw = self.__raw_value_at(column_titles, column_index, row_index)
4780
- except Exception:
4781
- raw = None
4782
- return self.__color_for_cell(raw, row_index, name)
4783
-
4784
- def __rule_specs_for_row(self, row_index, column_titles, column_widths):
4785
- """One colour spec per *drawn* column for the rule under ``row_index``."""
4786
- hidden = set(self.__effective_hidden_columns(self.__show_index))
4787
- specs = []
4788
- for col_i, _width in enumerate(column_widths):
4789
- if col_i in hidden:
4790
- continue
4791
- specs.append(
4792
- self.__rule_spec_for_cell(row_index, col_i, column_titles)
4793
- )
4794
- return specs
4795
-
4796
4766
  @staticmethod
4797
4767
  def _border_text_span(length, text_len, offset, align, lo, hi):
4798
4768
  """
@@ -5056,92 +5026,39 @@ class Table(object):
5056
5026
  def __colour_horizontal_rules(self, separators, column_widths,
5057
5027
  column_titles, body_row_count):
5058
5028
  """
5059
- Paint cell backgrounds onto horizontal rules.
5029
+ Colour horizontal rules with ``border_color`` only (no cell fill).
5060
5030
 
5061
- Body gaps use the row above. The header/body rule uses the first body
5062
- row (fill meets the header separator from below). The end rule uses
5063
- the last body row. Superior header keeps ``border_color`` only unless
5064
- ``header_color`` carries a background.
5065
- Returns the updated HorizontalComposition and a gap painter callable
5066
- (or None when the style has no body rule).
5067
- """
5068
- from .merges import compute_layout, paint_rule_cell_backgrounds
5031
+ Cell, zebra and merge backgrounds live on the interior rectangle
5032
+ of the cell or merge. Rule glyphs stay chrome so a fill cannot
5033
+ leak onto ``─`` / ``═`` / junctions, and a missing ``border_color``
5034
+ on a junction cannot open an uncoloured gap in the middle of a
5035
+ rule.
5069
5036
 
5070
- hidden = self.__effective_hidden_columns(self.__show_index)
5071
- layout = compute_layout(
5072
- column_widths, self.__style_composition, hidden
5073
- )
5037
+ Returns the updated HorizontalComposition and a gap painter
5038
+ callable (or None when the style has no body rule).
5039
+ """
5074
5040
  border = self.__border_color
5075
5041
 
5076
- def paint(line, specs):
5042
+ def paint(line):
5077
5043
  if line is None:
5078
5044
  return None
5079
- return paint_rule_cell_backgrounds(line, layout, specs, border)
5080
-
5081
- first_specs = (
5082
- self.__rule_specs_for_row(0, column_titles, column_widths)
5083
- if body_row_count else []
5084
- )
5085
- last_specs = (
5086
- self.__rule_specs_for_row(
5087
- body_row_count - 1, column_titles, column_widths
5088
- )
5089
- if body_row_count else []
5090
- )
5091
- header_specs = []
5092
- if self.__header_color and background_hex(self.__header_color):
5093
- drawn = sum(
5094
- 1 for i, _ in enumerate(column_widths)
5095
- if i not in set(hidden)
5096
- )
5097
- header_specs = [self.__header_color] * drawn
5098
-
5099
- superior = separators.superior_header_line
5100
- if header_specs:
5101
- superior = paint(superior, header_specs)
5102
- elif border and superior is not None:
5103
- superior = colorize(superior, border)
5104
-
5105
- inferior = separators.inferior_header_line
5106
- if first_specs:
5107
- inferior = paint(inferior, first_specs)
5108
- elif border and inferior is not None:
5109
- inferior = colorize(inferior, border)
5110
-
5111
- superior_nh = separators.superior_header_line_no_header
5112
- if first_specs:
5113
- superior_nh = paint(superior_nh, first_specs)
5114
- elif border and superior_nh is not None:
5115
- superior_nh = colorize(superior_nh, border)
5116
-
5117
- end_line = separators.table_end_line
5118
- if last_specs:
5119
- end_line = paint(end_line, last_specs)
5120
- elif border and end_line is not None:
5121
- end_line = colorize(end_line, border)
5122
-
5123
- plain_body = separators.table_body_line
5124
- body_template = plain_body
5125
- if border and body_template is not None and not first_specs:
5126
- body_template = colorize(body_template, border)
5045
+ return colorize(line, border) if border else line
5127
5046
 
5128
5047
  painted = HorizontalComposition(
5129
- superior,
5130
- inferior,
5131
- superior_nh,
5132
- body_template if not first_specs else plain_body,
5133
- end_line,
5048
+ paint(separators.superior_header_line),
5049
+ paint(separators.inferior_header_line),
5050
+ paint(separators.superior_header_line_no_header),
5051
+ paint(separators.table_body_line),
5052
+ paint(separators.table_end_line),
5134
5053
  )
5135
5054
 
5055
+ plain_body = separators.table_body_line
5136
5056
  gap_separator = None
5137
5057
  if plain_body is not None and body_row_count:
5138
- def gap_separator(after_row, _plain=plain_body):
5139
- specs = self.__rule_specs_for_row(
5140
- after_row, column_titles, column_widths
5141
- )
5142
- return paint_rule_cell_backgrounds(
5143
- _plain, layout, specs, border
5144
- )
5058
+ chrome = paint(plain_body)
5059
+
5060
+ def gap_separator(after_row, _line=chrome):
5061
+ return _line
5145
5062
 
5146
5063
  return painted, gap_separator
5147
5064
 
@@ -45,6 +45,24 @@ _ANSI_RE = re.compile(
45
45
 
46
46
  _RESET = '\x1b[0m'
47
47
 
48
+ # Select Graphic Rendition: the CSI escapes (final byte 'm') that set colour
49
+ # and attributes. Only these leave state open that a reset has to close.
50
+ _SGR_RE = re.compile(r'\x1b\[([0-9;:]*)m')
51
+
52
+
53
+ def sgr_is_open(text: str) -> bool:
54
+ """
55
+ Whether `text` leaves a colour or attribute switched on at its end.
56
+
57
+ True when the last SGR escape in the text is anything other than a full
58
+ reset (``ESC[0m`` or ``ESC[m``). Partial resets such as ``ESC[39m`` count
59
+ as open: closing them again is harmless, leaving them open is not.
60
+ """
61
+ last = None
62
+ for last in _SGR_RE.finditer(text):
63
+ pass
64
+ return last is not None and last.group(1) not in ('', '0')
65
+
48
66
  # U+FE0F asks for the emoji presentation of the character before it. It has no
49
67
  # width of its own; what it does is make that character two columns wide.
50
68
  VARIATION_SELECTOR_16 = '\ufe0f'
@@ -269,7 +287,7 @@ def slice_by_width(text: str, start: int, end: int) -> str:
269
287
  next.
270
288
  """
271
289
  _, inside, _ = partition_by_width(text, start, end)
272
- if inside and _ANSI_RE.search(inside) and not inside.endswith(_RESET):
290
+ if inside and sgr_is_open(inside):
273
291
  inside += _RESET
274
292
  return inside
275
293
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 3.0.0
3
+ Version: 3.0.2
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
@@ -38,6 +38,7 @@ tests/test_excel.py
38
38
  tests/test_features.py
39
39
  tests/test_issues.py
40
40
  tests/test_merge_edge_matrix.py
41
+ tests/test_merge_fill.py
41
42
  tests/test_merges.py
42
43
  tests/test_public_api_edges.py
43
44
  tests/test_readme_examples.py
@@ -66,6 +67,7 @@ tests/test_v223_fixes.py
66
67
  tests/test_v224_fixes.py
67
68
  tests/test_v230_none.py
68
69
  tests/test_v231_fixes.py
70
+ tests/test_v232_merge_ansi.py
69
71
  tests/test_v300_style_names.py
70
72
  tools/benchmark.py
71
73
  tools/generate_width_tables.py
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexablelib",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "private": true,
5
5
  "description": "Flexible console and HTML tables for Python: styles, merges, colors, widths, decimal alignment.",
6
6
  "repository": {