flexablelib 2.1.1__tar.gz → 2.2.1__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 (65) hide show
  1. {flexablelib-2.1.1 → flexablelib-2.2.1}/CHANGELOG.md +58 -0
  2. {flexablelib-2.1.1/flexablelib.egg-info → flexablelib-2.2.1}/PKG-INFO +23 -3
  3. {flexablelib-2.1.1 → flexablelib-2.2.1}/README.md +22 -2
  4. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/merges.py +68 -5
  5. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/table.py +174 -5
  6. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/writers.py +209 -74
  7. {flexablelib-2.1.1 → flexablelib-2.2.1/flexablelib.egg-info}/PKG-INFO +23 -3
  8. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib.egg-info/SOURCES.txt +2 -0
  9. {flexablelib-2.1.1 → flexablelib-2.2.1}/package.json +1 -1
  10. flexablelib-2.2.1/tests/test_v220_exports.py +230 -0
  11. flexablelib-2.2.1/tests/test_v221_fixes.py +125 -0
  12. {flexablelib-2.1.1 → flexablelib-2.2.1}/LICENSE +0 -0
  13. {flexablelib-2.1.1 → flexablelib-2.2.1}/MANIFEST.in +0 -0
  14. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/__init__.py +0 -0
  15. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/_speedups.c +0 -0
  16. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/_width_tables.h +0 -0
  17. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/cells.py +0 -0
  18. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/colors.py +0 -0
  19. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/columns.py +0 -0
  20. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/fast.py +0 -0
  21. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/options.py +0 -0
  22. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/py.typed +0 -0
  23. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/readers.py +0 -0
  24. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/style_compositions.py +0 -0
  25. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/table_strings.py +0 -0
  26. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/text_width.py +0 -0
  27. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib/utils.py +0 -0
  28. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib.egg-info/dependency_links.txt +0 -0
  29. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib.egg-info/requires.txt +0 -0
  30. {flexablelib-2.1.1 → flexablelib-2.2.1}/flexablelib.egg-info/top_level.txt +0 -0
  31. {flexablelib-2.1.1 → flexablelib-2.2.1}/pytest.ini +0 -0
  32. {flexablelib-2.1.1 → flexablelib-2.2.1}/setup.cfg +0 -0
  33. {flexablelib-2.1.1 → flexablelib-2.2.1}/setup.py +0 -0
  34. {flexablelib-2.1.1 → flexablelib-2.2.1}/style_examples.md +0 -0
  35. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/conftest.py +0 -0
  36. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/helpers.py +0 -0
  37. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_cells.py +0 -0
  38. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_columns.py +0 -0
  39. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_excel.py +0 -0
  40. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_features.py +0 -0
  41. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_issues.py +0 -0
  42. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_merge_edge_matrix.py +0 -0
  43. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_merges.py +0 -0
  44. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_readme_examples.py +0 -0
  45. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_style_line_widths.py +0 -0
  46. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_styles.py +0 -0
  47. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_text_width.py +0 -0
  48. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_two_pass_measurement.py +0 -0
  49. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_upstream_issues.py +0 -0
  50. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v2021_fixes.py +0 -0
  51. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v210_rows_copy.py +0 -0
  52. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v2110_fixes.py +0 -0
  53. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v211_fixes.py +0 -0
  54. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v213_features.py +0 -0
  55. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v214_features.py +0 -0
  56. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v215_features.py +0 -0
  57. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v216_features.py +0 -0
  58. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v217_features.py +0 -0
  59. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v218_features.py +0 -0
  60. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v219_coverage.py +0 -0
  61. {flexablelib-2.1.1 → flexablelib-2.2.1}/tests/test_v219_fixes.py +0 -0
  62. {flexablelib-2.1.1 → flexablelib-2.2.1}/tools/benchmark.py +0 -0
  63. {flexablelib-2.1.1 → flexablelib-2.2.1}/tools/generate_width_tables.py +0 -0
  64. {flexablelib-2.1.1 → flexablelib-2.2.1}/tools/readme_gallery.py +0 -0
  65. {flexablelib-2.1.1 → flexablelib-2.2.1}/tools/text_to_png.py +0 -0
@@ -2,6 +2,64 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [2.2.1] - 2026-11-04
6
+
7
+ ### Fixed
8
+ - A merge on the first or last drawn row -- often where ``sort_by`` or
9
+ ``row_filter`` puts it -- rewrote the whole border next to it: the top
10
+ border under a ``title`` (with ``show_headers=False``) came out as an inner
11
+ rule (``├───┴───┼───┤``), the bottom border above a ``caption`` lost its
12
+ corners, ``markdown``/``orgtbl`` header separators turned into ``+---+``,
13
+ and ``*_eheader`` header separators kept a junction over the merged span.
14
+ Only the junctions over the merge change now, and each keeps the arms the
15
+ lines around it really have.
16
+ - A merge covering a column in ``hide_columns`` (or a hidden empty column)
17
+ that is left with a single drawn column printed ``.``: the column was sized
18
+ without the merge text. It now shrinks to that column and the column fits
19
+ the text, as the exports already did.
20
+
21
+ ### CI
22
+ - Wheels: one path to PyPI. Only the ``vX.Y.Z`` tag push builds and
23
+ publishes; publishing the GitHub release no longer starts a second, duplicate
24
+ build that competed for the macOS runner. Publishing waits for all 28 wheels
25
+ (Linux manylinux + musllinux, Windows, macOS arm64; CPython 3.8-3.14) and
26
+ the sdist, checks the count and that the tag matches ``package.json``, and
27
+ is safe to re-run. One run per ref (``concurrency``).
28
+ - Actions moved off Node 20: ``checkout@v5``, ``setup-python@v6``,
29
+ ``upload-artifact@v6``, ``download-artifact@v7``.
30
+
31
+ ## [2.2.0] - 2026-11-04
32
+
33
+ ### Changed (breaking)
34
+ - Exports now write the table as ``compose()`` shows it. ``to_csv``,
35
+ ``to_markdown``, ``to_html``, ``to_rst``, ``to_asciidoc``, ``to_latex``,
36
+ ``to_excel``, ``to_records`` and ``_repr_html_`` follow ``sort_by``
37
+ (``sort_reverse``, callables) and ``row_filter``, leave out
38
+ ``hide_columns`` (and empty rows/columns when ``show_empty_rows`` /
39
+ ``show_empty_columns`` are off), and number the index by display position.
40
+ Before, they wrote every stored row in insertion order.
41
+ - The text exports (CSV, Markdown, HTML, RST, AsciiDoc, LaTeX) hold the
42
+ formatted strings ``compose()`` prints -- ``float_format``,
43
+ ``int_format``, ``leading_zeros``, ``custom_format``, ``strip_cells`` --
44
+ without padding or ANSI codes. Before, they wrote ``str()`` of the raw
45
+ value. Gaps stay empty strings. Reading such an export back gives the
46
+ printed text (``'1,200'`` stays a string).
47
+ - ``to_records()`` keeps the stored Python values (numbers stay numbers, gaps
48
+ ``None``) but in display order, filtered, without hidden columns.
49
+ - ``to_excel()`` keeps numbers numeric; other cells hold the displayed text.
50
+ - Merges keep their display position, as in ``compose()``; a merge split by
51
+ ``show_empty_rows=False`` is dropped and one over hidden columns shrinks.
52
+ - ``Table.rows`` is unchanged: the stored data in insertion order.
53
+
54
+ ### Added
55
+ - ``to_records(formatted=True)`` returns the displayed strings.
56
+ - ``to_excel()`` turns simple ``int_format`` / ``float_format`` /
57
+ ``custom_format`` specs and ``leading_zeros`` into Excel number formats
58
+ (``',d'`` -> ``#,##0``, ``'.2f'`` -> ``0.00``, ``'.1%'`` -> ``0.0%``,
59
+ ``'$ {:,.2f}'`` -> ``"$ "#,##0.00``).
60
+ - ``to_html()`` sorts numeric columns on the stored number, so formatted
61
+ values such as ``1,200`` sort correctly in the browser.
62
+
5
63
  ## [2.1.1] - 2026-11-04
6
64
 
7
65
  ### Fixed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.1.1
3
+ Version: 2.2.1
4
4
  Summary: Fast, dependency-free terminal tables for Python: 56 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
@@ -425,6 +425,25 @@ table.to_html('report.html', paginate=25)
425
425
  table.to_excel('report.xlsx')
426
426
  ```
427
427
 
428
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
429
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
430
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
431
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
432
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
433
+ without padding or colour. Empty cells are empty strings.
434
+
435
+ ```python
436
+ table.sort_by, table.sort_reverse = 'Uptime', True
437
+ table.row_filter = lambda row: row[1] < 99.9
438
+ table.float_format = '.1f'
439
+ table.to_markdown() # same rows, order and numbers as print(table)
440
+ ```
441
+
442
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
443
+ `None`) in display order; `to_records(formatted=True)` gives the printed
444
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
445
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
446
+
428
447
  `to_html` writes one self-contained file — the stylesheet and the sorting,
429
448
  filtering and pagination script are inline — so it works offline and from a
430
449
  `file://` URL.
@@ -439,8 +458,9 @@ table.parse_str_numbers = True
439
458
 
440
459
  Values with leading zeros are left alone, so `'007'` stays a string.
441
460
 
442
- `table.rows` is a copy of the data, with `None` in empty cells (the same values
443
- as `to_records()`), so editing it does not change the table. Use `add_row` and
461
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
462
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
463
+ does not change the table. Use `add_row` and
444
464
  `set_cell` instead:
445
465
 
446
466
  ```python
@@ -360,6 +360,25 @@ table.to_html('report.html', paginate=25)
360
360
  table.to_excel('report.xlsx')
361
361
  ```
362
362
 
363
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
364
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
365
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
366
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
367
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
368
+ without padding or colour. Empty cells are empty strings.
369
+
370
+ ```python
371
+ table.sort_by, table.sort_reverse = 'Uptime', True
372
+ table.row_filter = lambda row: row[1] < 99.9
373
+ table.float_format = '.1f'
374
+ table.to_markdown() # same rows, order and numbers as print(table)
375
+ ```
376
+
377
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
378
+ `None`) in display order; `to_records(formatted=True)` gives the printed
379
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
380
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
381
+
363
382
  `to_html` writes one self-contained file — the stylesheet and the sorting,
364
383
  filtering and pagination script are inline — so it works offline and from a
365
384
  `file://` URL.
@@ -374,8 +393,9 @@ table.parse_str_numbers = True
374
393
 
375
394
  Values with leading zeros are left alone, so `'007'` stays a string.
376
395
 
377
- `table.rows` is a copy of the data, with `None` in empty cells (the same values
378
- as `to_records()`), so editing it does not change the table. Use `add_row` and
396
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
397
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
398
+ does not change the table. Use `add_row` and
379
399
  `set_cell` instead:
380
400
 
381
401
  ```python
@@ -509,8 +509,44 @@ def _looks_like_rule(line: str, h_glyph: str = None) -> bool:
509
509
  )
510
510
 
511
511
 
512
+ # Glyphs that carry a vertical arm out of their bottom / top edge.
513
+ _VERTICALS = set('|¦:│┃║┊┋┆┇╎╏')
514
+ _NO_DOWN = set('┴└┘╰╯╨╩╧╙╜╚╝╘╛┸┷┻┖┚┗┛')
515
+ _NO_UP = set('┬┌┐╭╮╥╦╤╓╖╔╗╒╕┰┯┳┎┒┏┓')
516
+
517
+
518
+ # Light verticals meeting a heavy dash ('╍', '━'): keep the rule's weight.
519
+ _HEAVY_H = set('╍━┅┉')
520
+ _LIGHT_TO_HEAVY_H = {
521
+ '┼': '┿', '├': '┝', '┤': '┥', '┬': '┯', '┴': '┷',
522
+ '┌': '┍', '┐': '┑', '└': '┕', '┘': '┙',
523
+ }
524
+
525
+
526
+ def _has_arm(line, x, down):
527
+ """
528
+ True when the glyph at ``x`` in a neighbouring line reaches this rule.
529
+
530
+ ``down`` asks about the line above (does its glyph continue downwards);
531
+ otherwise about the line below. Text, spaces and dashes do not reach it,
532
+ which is what tells a top border under a title, or a bottom border over
533
+ a caption, that it has no arm on that side.
534
+ """
535
+ if line is None:
536
+ return False
537
+ g = _glyph(_char_at(line, x))
538
+ if not g or g.isspace() or g in _H_CHARS:
539
+ return False
540
+ if g in _VERTICALS or g == '+':
541
+ return True
542
+ if g in _JUNCTIONS:
543
+ return g not in (_NO_DOWN if down else _NO_UP)
544
+ return False
545
+
546
+
512
547
  def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=None, missing_north=None,
513
- no_south=False, no_north=False):
548
+ no_south=False, no_north=False, above=False, below=False,
549
+ only=None):
514
550
  """
515
551
  Rebuild every junction on a horizontal rule from the arms that remain.
516
552
 
@@ -519,6 +555,16 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
519
555
 
520
556
  ``no_south`` / ``no_north`` force every junction to drop that arm — used for
521
557
  the table's outer end/start rules, which never continue past the border.
558
+
559
+ ``above`` / ``below``, when not False, are the lines next to this rule (None
560
+ past either end of the table). An arm is then kept only where that line
561
+ really reaches the rule, so a top border under a title, a bottom border
562
+ over a caption, or a header separator under a header drawn without
563
+ column rules keep their own shape. A junction drawn as a plain vertical
564
+ (markdown's ``|---|``) stays as it is unless it becomes a pure dash.
565
+
566
+ ``only``, when given, limits the rewrite to those x positions; every other
567
+ junction keeps the style's own glyph (corners such as ``O`` or ``╔``).
522
568
  """
523
569
  missing_south = missing_south or set()
524
570
  missing_north = missing_north or set()
@@ -531,6 +577,8 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
531
577
  for x, v_glyph in _junction_positions(layout, composition):
532
578
  if visible_width(rewritten) <= x:
533
579
  continue
580
+ if only is not None and x not in only:
581
+ continue
534
582
  here = _char_at(rewritten, x)
535
583
  # Interior of a multi-column vertical blank: junction was wiped to space.
536
584
  if _glyph(here) in ('', ' '):
@@ -541,10 +589,23 @@ def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=Non
541
589
  east = _is_h(right)
542
590
  north = (not no_north) and (x not in missing_north)
543
591
  south = (not no_south) and (x not in missing_south)
592
+ if above is not False:
593
+ north = north and _has_arm(above, x, down=True)
594
+ if below is not False:
595
+ south = south and _has_arm(below, x, down=False)
544
596
  glyph = _pick_junction(
545
597
  v_glyph, h_glyph, north, south, east, west,
546
598
  round_corners=round_corners,
547
599
  )
600
+ if h_glyph in _HEAVY_H and glyph in _LIGHT_TO_HEAVY_H:
601
+ glyph = _LIGHT_TO_HEAVY_H[glyph]
602
+ if (above is not False and _glyph(here) in _VERTICALS
603
+ and (north or south)):
604
+ glyph = here
605
+ if only is not None and south and not north and _glyph(here) in _NO_UP:
606
+ glyph = here # already has no arm upwards, e.g. a header box top
607
+ if only is not None and north and not south and _glyph(here) in _NO_DOWN:
608
+ glyph = here # already closed below, e.g. ╚═══╩═══╝
548
609
  rewritten = _repaint(rewritten, x, x + 1, glyph)
549
610
  return rewritten
550
611
 
@@ -769,8 +830,10 @@ def apply(lines, row_line_map, regions, column_widths, composition,
769
830
  line, layout, composition, line_h,
770
831
  missing_south=missing_south_by_line.get(line_index),
771
832
  missing_north=missing_north_by_line.get(line_index),
772
- no_south=(line_index >= len(lines) - 1),
773
- no_north=(line_index == 0),
833
+ above=lines[line_index - 1] if line_index > 0 else None,
834
+ below=lines[line_index + 1] if line_index + 1 < len(lines) else None,
835
+ only=(missing_south_by_line.get(line_index, set())
836
+ | missing_north_by_line.get(line_index, set())),
774
837
  )
775
838
 
776
839
  # --- Pass 4: synthetic caps for column-only styles ----------------------
@@ -798,8 +861,8 @@ def apply(lines, row_line_map, regions, column_widths, composition,
798
861
  line, layout, composition, line_h,
799
862
  missing_south=plan['s'] or None,
800
863
  missing_north=plan['n'] or None,
801
- no_south=(existing >= len(lines) - 1),
802
- no_north=(existing == 0),
864
+ above=lines[existing - 1] if existing > 0 else None,
865
+ below=lines[existing + 1] if existing + 1 < len(lines) else None,
803
866
  )
804
867
  continue
805
868
 
@@ -3617,7 +3617,7 @@ class Table(object):
3617
3617
  )
3618
3618
  self.__get_column_widths(semi=False)
3619
3619
  # Raise floors from column_min_width and re-assert max budgets.
3620
- self.__clamp_column_widths()
3620
+ self.__clamp_column_widths(merge_floors=True)
3621
3621
  table_width = self.__get_string_table_width()
3622
3622
  # Grow first when asked, then shrink if still over budget.
3623
3623
  if self.__expand_to_window:
@@ -4164,13 +4164,51 @@ class Table(object):
4164
4164
  return None
4165
4165
  return int(bounds)
4166
4166
 
4167
- def __clamp_column_widths(self):
4167
+ def __collapsed_merge_floors(self):
4168
+ """
4169
+ Width each drawn column needs for merges left with only that column.
4170
+
4171
+ A merge across several columns, with all but one of them in
4172
+ ``hide_columns`` (or hidden as empty), shrinks to the column still
4173
+ drawn -- the same thing the exports do. Its text then has that one
4174
+ column to itself, so the column has to fit it, as it would an
4175
+ ordinary cell; sized by its own content alone it printed ``.``.
4176
+ Merges still spanning two or more drawn columns are untouched: a merge
4177
+ never widens the table.
4178
+ """
4179
+ if not self.__merged_regions:
4180
+ return {}
4181
+ offset = 1 if self.__show_index else 0
4182
+ hidden = set(self.__effective_hidden_columns(self.__show_index))
4183
+ if not hidden:
4184
+ return {}
4185
+ floors = {}
4186
+ for region in self.__merged_regions:
4187
+ if region.value is None:
4188
+ continue
4189
+ columns = range(region.first_column + offset,
4190
+ region.last_column + offset + 1)
4191
+ drawn = [c for c in columns if c not in hidden]
4192
+ if len(drawn) != 1 or len(drawn) == len(columns):
4193
+ continue
4194
+ needed = max(
4195
+ visible_width(strip_ansi(part))
4196
+ for part in str(region.value).split('\n')
4197
+ )
4198
+ floors[drawn[0]] = max(floors.get(drawn[0], 0), needed)
4199
+ return floors
4200
+
4201
+ def __clamp_column_widths(self, merge_floors=False):
4168
4202
  """
4169
4203
  Enforce column_min_width / column_max_width on the measured widths.
4170
4204
 
4171
4205
  Only the width numbers are changed here. Content that still exceeds a
4172
4206
  max is reduced by :meth:`__enforce_column_max_widths`, which walks the
4173
4207
  same wrap/trim path as a terminal fit.
4208
+
4209
+ ``merge_floors`` also widens a column holding a merge that hiding left
4210
+ with that one column (:meth:`__collapsed_merge_floors`). Only the
4211
+ first clamp does it, so a terminal fit can still narrow it afterwards.
4174
4212
  """
4175
4213
  if self.__show_index:
4176
4214
  headers = list(self.__headers_with_i)
@@ -4184,6 +4222,7 @@ class Table(object):
4184
4222
  if not widths:
4185
4223
  return
4186
4224
 
4225
+ collapsed = self.__collapsed_merge_floors() if merge_floors else {}
4187
4226
  for column_i, header in enumerate(headers):
4188
4227
  # Index column (position 0 with show_index) is not user-facing.
4189
4228
  user_i = None if (self.__show_index and column_i == 0) else (
@@ -4203,6 +4242,8 @@ class Table(object):
4203
4242
  # clamping the number anyway drew a frame narrower than the text.
4204
4243
  content = widths[column_i]
4205
4244
  width = content
4245
+ if merge_floors and column_i in collapsed:
4246
+ width = max(width, collapsed[column_i])
4206
4247
  if lo is not None:
4207
4248
  width = max(width, int(lo))
4208
4249
  if hi is not None:
@@ -6150,6 +6191,127 @@ class Table(object):
6150
6191
  """Alias of :meth:`transpose` (tabled naming)."""
6151
6192
  return self.transpose(**options)
6152
6193
 
6194
+ def _export_view(self, include_index=False):
6195
+ """
6196
+ What the writers export: the rows ``compose()`` shows, as data.
6197
+
6198
+ Rows come in display order -- ``row_filter`` and ``sort_by`` (with
6199
+ ``sort_reverse`` and callables) applied, empty rows left out when
6200
+ ``show_empty_rows`` is off. Columns in ``hide_columns`` are left out,
6201
+ and so are empty columns when ``show_empty_columns`` is off.
6202
+
6203
+ Returns a namespace with:
6204
+
6205
+ - ``headers``: header names (``index_title`` first with the index).
6206
+ - ``raw``: cell values as stored, ``None`` in the gaps.
6207
+ - ``text``: the strings ``compose()`` prints for each cell --
6208
+ ``int_format``, ``float_format``, ``leading_zeros``,
6209
+ ``custom_format`` and ``strip_cells`` applied, colour stripped, no
6210
+ padding -- ``''`` in the gaps.
6211
+ - ``merges``: ``merged_regions`` in export coordinates (data columns,
6212
+ index excluded); merges broken up by the filter, or covering only
6213
+ hidden columns, are dropped.
6214
+ - ``index``: whether the first column is the index.
6215
+
6216
+ ``Table.rows`` stays the stored copy; this is only for writers.
6217
+ """
6218
+ from types import SimpleNamespace
6219
+
6220
+ headers = list(self.__headers)
6221
+ n_rows = self.__real_row_count
6222
+ stored = [self.__stored_row(row_i, headers) for row_i in range(n_rows)]
6223
+
6224
+ def is_gap(cell):
6225
+ return cell is None or isinstance(cell, ValuePlacer)
6226
+
6227
+ # Raw values, gaps as None -- both the padding marker and the cells
6228
+ # filled with ``missing_value`` above.
6229
+ raw_rows = []
6230
+ for row_i in range(n_rows):
6231
+ row = []
6232
+ for header in headers:
6233
+ col = self.__columns.get(header, [])
6234
+ cell = col[row_i] if row_i < len(col) else None
6235
+ row.append(None if is_gap(cell) else cell)
6236
+ raw_rows.append(row)
6237
+
6238
+ order = self.__display_row_order(stored)
6239
+ hidden_rows = (
6240
+ set() if self.__show_empty_rows else set(self.__empty_row_indexes)
6241
+ )
6242
+ # Display position (what merges are keyed on) -> exported row.
6243
+ position_to_row = {}
6244
+ kept = []
6245
+ for position, row_i in enumerate(order):
6246
+ if row_i in hidden_rows:
6247
+ continue
6248
+ position_to_row[position] = len(kept)
6249
+ kept.append(row_i)
6250
+
6251
+ hidden_cols = set(self.__user_hidden_column_indexes(False))
6252
+ if not self.__show_empty_columns:
6253
+ for column_i in range(len(headers)):
6254
+ if all(
6255
+ is_empty_cell(raw_rows[row_i][column_i], self.__value_placer)
6256
+ for row_i in range(n_rows)
6257
+ ):
6258
+ hidden_cols.add(column_i)
6259
+ visible = [i for i in range(len(headers)) if i not in hidden_cols]
6260
+ column_to_export = {c: j for j, c in enumerate(visible)}
6261
+
6262
+ out_headers = [str(headers[c]) for c in visible]
6263
+ out_raw = []
6264
+ out_text = []
6265
+ for row_i in kept:
6266
+ raw_row = raw_rows[row_i]
6267
+ out_raw.append([raw_row[c] for c in visible])
6268
+ texts = []
6269
+ for c in visible:
6270
+ value = raw_row[c]
6271
+ if value is None:
6272
+ texts.append('')
6273
+ continue
6274
+ shown = self.__format_display_value(value, headers[c], c)
6275
+ texts.append('' if shown is None else strip_ansi(str(shown)))
6276
+ out_text.append(texts)
6277
+
6278
+ merges = []
6279
+ for region in self.__merged_regions:
6280
+ rows_span = range(region.first_row, region.last_row + 1)
6281
+ mapped_rows = [position_to_row.get(p) for p in rows_span]
6282
+ if any(r is None for r in mapped_rows):
6283
+ continue
6284
+ if mapped_rows[-1] - mapped_rows[0] != len(mapped_rows) - 1:
6285
+ continue
6286
+ cols = [
6287
+ column_to_export[c]
6288
+ for c in range(region.first_column, region.last_column + 1)
6289
+ if c in column_to_export
6290
+ ]
6291
+ if not cols:
6292
+ continue
6293
+ merges.append(region._replace(
6294
+ first_row=mapped_rows[0], last_row=mapped_rows[-1],
6295
+ first_column=cols[0], last_column=cols[-1],
6296
+ ))
6297
+
6298
+ if include_index:
6299
+ start, step = self.index_start, self.index_step
6300
+ labels = []
6301
+ for export_i in range(len(kept)):
6302
+ label = start + export_i * step
6303
+ labels.append(to_roman(label) if self.roman_index else label)
6304
+ out_headers = [str(self.index_title)] + out_headers
6305
+ out_raw = [[label] + row for label, row in zip(labels, out_raw)]
6306
+ out_text = [
6307
+ [str(label)] + row for label, row in zip(labels, out_text)
6308
+ ]
6309
+
6310
+ return SimpleNamespace(
6311
+ headers=out_headers, raw=out_raw, text=out_text,
6312
+ merges=merges, index=bool(include_index),
6313
+ )
6314
+
6153
6315
  def paginate(self, page_length=20, freeze_header=True, page_label=False):
6154
6316
  """
6155
6317
  Split the table into composed string pages of ``page_length`` body rows.
@@ -6271,10 +6433,17 @@ class Table(object):
6271
6433
  from .readers import from_markdown
6272
6434
  return from_markdown(cls, source, parse_numbers=parse_numbers, **options)
6273
6435
 
6274
- def to_records(self, include_index=False):
6275
- """The table as a list of dicts, one per row."""
6436
+ def to_records(self, include_index=False, formatted=False):
6437
+ """
6438
+ The table as a list of dicts, one per row, in display order.
6439
+
6440
+ ``sort_by`` and ``row_filter`` are applied and ``hide_columns`` left
6441
+ out, like every export since 2.2.0. Values stay the stored Python
6442
+ objects (numbers remain numbers); ``formatted=True`` returns the
6443
+ strings ``compose()`` shows instead. Gaps are ``None``.
6444
+ """
6276
6445
  from .writers import to_records
6277
- return to_records(self, include_index)
6446
+ return to_records(self, include_index, formatted)
6278
6447
 
6279
6448
  def to_csv(self, target=None, include_index=False, delimiter=',',
6280
6449
  encoding='utf-8', dialect=None, quoting=None, quotechar=None,