flexablelib 2.2.2__tar.gz → 2.2.4__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 (70) hide show
  1. {flexablelib-2.2.2 → flexablelib-2.2.4}/CHANGELOG.md +56 -0
  2. {flexablelib-2.2.2/flexablelib.egg-info → flexablelib-2.2.4}/PKG-INFO +1 -1
  3. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/merges.py +62 -3
  4. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/readers.py +9 -1
  5. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/table.py +231 -54
  6. {flexablelib-2.2.2 → flexablelib-2.2.4/flexablelib.egg-info}/PKG-INFO +1 -1
  7. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib.egg-info/SOURCES.txt +4 -0
  8. {flexablelib-2.2.2 → flexablelib-2.2.4}/package.json +1 -1
  9. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_merges.py +3 -1
  10. flexablelib-2.2.4/tests/test_stress_matrix.py +47 -0
  11. flexablelib-2.2.4/tests/test_v223_fixes.py +66 -0
  12. flexablelib-2.2.4/tests/test_v224_fixes.py +235 -0
  13. flexablelib-2.2.4/tools/stress_matrix.py +602 -0
  14. {flexablelib-2.2.2 → flexablelib-2.2.4}/LICENSE +0 -0
  15. {flexablelib-2.2.2 → flexablelib-2.2.4}/MANIFEST.in +0 -0
  16. {flexablelib-2.2.2 → flexablelib-2.2.4}/README.md +0 -0
  17. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/__init__.py +0 -0
  18. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/_speedups.c +0 -0
  19. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/_width_tables.h +0 -0
  20. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/cells.py +0 -0
  21. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/colors.py +0 -0
  22. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/columns.py +0 -0
  23. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/fast.py +0 -0
  24. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/options.py +0 -0
  25. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/py.typed +0 -0
  26. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/style_compositions.py +0 -0
  27. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/table_strings.py +0 -0
  28. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/text_width.py +0 -0
  29. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/utils.py +0 -0
  30. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib/writers.py +0 -0
  31. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib.egg-info/dependency_links.txt +0 -0
  32. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib.egg-info/requires.txt +0 -0
  33. {flexablelib-2.2.2 → flexablelib-2.2.4}/flexablelib.egg-info/top_level.txt +0 -0
  34. {flexablelib-2.2.2 → flexablelib-2.2.4}/pytest.ini +0 -0
  35. {flexablelib-2.2.2 → flexablelib-2.2.4}/setup.cfg +0 -0
  36. {flexablelib-2.2.2 → flexablelib-2.2.4}/setup.py +0 -0
  37. {flexablelib-2.2.2 → flexablelib-2.2.4}/style_examples.md +0 -0
  38. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/conftest.py +0 -0
  39. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/helpers.py +0 -0
  40. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_cells.py +0 -0
  41. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_columns.py +0 -0
  42. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_excel.py +0 -0
  43. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_features.py +0 -0
  44. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_issues.py +0 -0
  45. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_merge_edge_matrix.py +0 -0
  46. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_readme_examples.py +0 -0
  47. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_style_line_widths.py +0 -0
  48. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_styles.py +0 -0
  49. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_text_width.py +0 -0
  50. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_two_pass_measurement.py +0 -0
  51. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_upstream_issues.py +0 -0
  52. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v2021_fixes.py +0 -0
  53. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v210_rows_copy.py +0 -0
  54. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v2110_fixes.py +0 -0
  55. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v211_fixes.py +0 -0
  56. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v213_features.py +0 -0
  57. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v214_features.py +0 -0
  58. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v215_features.py +0 -0
  59. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v216_features.py +0 -0
  60. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v217_features.py +0 -0
  61. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v218_features.py +0 -0
  62. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v219_coverage.py +0 -0
  63. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v219_fixes.py +0 -0
  64. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v220_exports.py +0 -0
  65. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v221_fixes.py +0 -0
  66. {flexablelib-2.2.2 → flexablelib-2.2.4}/tests/test_v222_fixes.py +0 -0
  67. {flexablelib-2.2.2 → flexablelib-2.2.4}/tools/benchmark.py +0 -0
  68. {flexablelib-2.2.2 → flexablelib-2.2.4}/tools/generate_width_tables.py +0 -0
  69. {flexablelib-2.2.2 → flexablelib-2.2.4}/tools/readme_gallery.py +0 -0
  70. {flexablelib-2.2.2 → flexablelib-2.2.4}/tools/text_to_png.py +0 -0
@@ -2,6 +2,62 @@
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.4] - 2026-11-07
6
+
7
+ Found by a new stress matrix (``tools/stress_matrix.py``): every style and
8
+ theme on a 30x50 table of mixed data (CJK, emoji, multi-line, ``None``),
9
+ each feature alone, a pairwise covering set of all feature options, and
10
+ "everything at once" combos, checking line widths, border junctions,
11
+ visible rows, export order and determinism. A reduced version runs in the
12
+ test suite (``tests/test_stress_matrix.py``).
13
+
14
+ ### Fixed
15
+ - Fitting a table to the terminal (``max_width``) or capping one column
16
+ (``column_max_width``) left-aligned every other int column and the index,
17
+ even ones that were not shortened. Only a column that really loses
18
+ characters is turned into text now.
19
+ - ``add_divider`` / ``divide_every`` in a style without inter-row rules drew
20
+ a run of ASCII ``-`` across the column lines (``│ a │`` / ``-------``). The
21
+ divider now uses the style's glyphs (``├───┼───┤``, ``╞═══╪═══╡``,
22
+ ``+---+``, ``|---|`` for markdown/pipes/orgtbl, edge to edge for
23
+ ``*_ebody`` styles), skipping hidden columns; styles with no column lines
24
+ keep the dashed line.
25
+ - Merges in ``tilg_columns``, ``dots``, ``psql``/``presto`` and other
26
+ non-box styles got light box glyphs (``│ ├ ┴``) for their caps. ASCII
27
+ column lines now join with ``+`` and ``dots`` with ``:`` (dividers in
28
+ ``dots`` too).
29
+ - ``expand_to_window`` with ``column_max_width`` pulled a column that was
30
+ already wider than its cap (numbers are never cut) back to the cap, so its
31
+ cells overflowed the rules and the frame broke (also on ``paginate`` pages).
32
+ - ``stream()``:
33
+ - with ``show_index`` and ``column_min_width`` or ``column_max_width``
34
+ raised ``IndexError``; with ``show_index`` alone nothing was frozen and
35
+ the lines grew as rows arrived. The index width is now reserved for a
36
+ sized input (``len(rows)``), including roman numerals.
37
+ - with ``zebra`` or cell backgrounds lost every row and printed the bottom
38
+ border after the header and after each row.
39
+ - with ``show_headers=False`` on an empty table raised ``ValueError``
40
+ (``column_max_width must be >= 1``), and with ``panel`` / ``shadow`` on a
41
+ borderless style closed the panel after the first row.
42
+ - ``title`` and ``caption`` were not centred on a table with no rows.
43
+ - ``Table.from_csv`` accepts CSV text, so ``from_csv(table.to_csv())``
44
+ round-trips like ``from_markdown`` and ``from_html`` already did. Paths
45
+ and open handles work as before.
46
+
47
+ ## [2.2.3] - 2026-11-06
48
+
49
+ ### Fixed
50
+ - ``Table(headers=..., rows=...)`` with a short first row and a wider later
51
+ row no longer invents a ghost ``column N``. Padding now uses the live
52
+ column count after every row is loaded, so later cells that already fill
53
+ the remaining headers are enough.
54
+ - ``add_row`` / ``add_column`` reject bare ``str`` / ``bytes`` data with
55
+ ``TypeError`` instead of expanding them into one cell per character.
56
+ ``Table(rows=...)`` does the same for a string ``rows`` value or string
57
+ row entries. Wrap a single string cell in a list.
58
+ - ``show_empty_columns`` coerces with ``bool(...)``, matching
59
+ ``show_empty_rows``.
60
+
5
61
  ## [2.2.2] - 2026-11-05
6
62
 
7
63
  ### Fixed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.2.2
3
+ Version: 2.2.4
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
@@ -349,9 +349,13 @@ def _pick_junction(v_glyph: str, h_glyph: str, n: bool, s: bool, e: bool, w: boo
349
349
  if v_glyph in '|+' or (h_glyph and h_glyph in '-=~'):
350
350
  return h_glyph if h_glyph in '-=~' else '-'
351
351
  return h_glyph or '─'
352
- if v_glyph in '|+' or (h_glyph and h_glyph in '-=') :
353
- if v_glyph in '|+' and (h_glyph in '-=+' or not h_glyph):
354
- return _ASCII.get(key, '+')
352
+ if v_glyph in '|+':
353
+ # ASCII column lines join with '+' whatever the dash ('-', '=', '~').
354
+ return _ASCII.get(key, '+')
355
+ if v_glyph and not ('\u2500' <= v_glyph <= '\u257f'):
356
+ # Other non-box column glyphs (``dots`` draws ':') are their own
357
+ # junction; a light box tee next to them is a foreign glyph.
358
+ return v_glyph
355
359
  if v_glyph == '║' and h_glyph == '═':
356
360
  return _DOUBLE.get(key, '║')
357
361
  if v_glyph == '║':
@@ -383,6 +387,14 @@ def _synthesize_body_rule(composition):
383
387
  h = end.middle
384
388
  elif header is not None and header.middle:
385
389
  h = header.middle
390
+ if not ('\u2500' <= v.middle <= '\u257f') and v.middle not in '|+':
391
+ # Non-box column glyphs (``dots`` draws ':') are their own junction.
392
+ return SeparatorLine(
393
+ left=v.left or None,
394
+ middle=h if h and not ('\u2500' <= h <= '\u257f') else '-',
395
+ intersection=v.middle,
396
+ right=v.right or None,
397
+ )
386
398
  if v.middle in '|+':
387
399
  return SeparatorLine(
388
400
  left='+' if v.left else None,
@@ -412,6 +424,53 @@ def _synthesize_body_rule(composition):
412
424
  )
413
425
 
414
426
 
427
+ _FRAME_TEES = {
428
+ # vertical glyph -> (left tee, right tee) for a light horizontal
429
+ '│': ('├', '┤'), '┊': ('├', '┤'), '┆': ('├', '┤'), '╎': ('├', '┤'),
430
+ '┃': ('┠', '┨'), '║': ('╟', '╢'), '|': ('+', '+'),
431
+ }
432
+ _FRAME_TEES_DOUBLE_H = {
433
+ '│': ('╞', '╡'), '║': ('╠', '╣'), '|': ('+', '+'),
434
+ }
435
+
436
+
437
+ def _frame_only_body_rule(composition):
438
+ """Body rule for styles with outer verticals but no column lines.
439
+
440
+ ``*_ebody`` / ``*_empty`` styles frame the body without separating its
441
+ columns, so the rule runs edge to edge and only meets the frame.
442
+ Returns None when the body has no outer verticals either.
443
+ """
444
+ from .style_compositions import SeparatorLine
445
+ v = composition.vertical_table_body_lines
446
+ if v is None:
447
+ return None
448
+ left_v = _glyph(v.left or '')
449
+ right_v = _glyph(v.right or '')
450
+ if not (left_v.strip() or right_v.strip()):
451
+ return None
452
+ end = composition.table_end_line
453
+ h = _glyph(end.middle) if end is not None and end.middle else ''
454
+ if not h or h.isspace():
455
+ h = '-' if '|' in (left_v, right_v) else '─'
456
+ tees = _FRAME_TEES_DOUBLE_H if h == '═' else _FRAME_TEES
457
+ if '|' in (left_v, right_v) and h not in '-=~':
458
+ h = '-'
459
+
460
+ def tee(glyph, side):
461
+ if not glyph or glyph.isspace():
462
+ return None
463
+ pair = tees.get(glyph) or _FRAME_TEES.get(glyph)
464
+ return pair[side] if pair else glyph
465
+
466
+ return SeparatorLine(
467
+ left=tee(left_v, 0),
468
+ middle=h,
469
+ intersection=h,
470
+ right=tee(right_v, 1),
471
+ )
472
+
473
+
415
474
  def _build_rule_line(layout, composition, column_widths, hidden, rule=None):
416
475
  """Assemble one full horizontal rule matching the column layout."""
417
476
  rule = rule or _synthesize_body_rule(composition)
@@ -177,7 +177,11 @@ def from_csv(table_class, source, parse_numbers: bool = True,
177
177
  lineterminator=None, skipinitialspace=None, strict=None,
178
178
  **options):
179
179
  """
180
- Build from a CSV file path, or any open text handle.
180
+ Build from a CSV file path, an open text handle, or CSV text.
181
+
182
+ A string holding a line break that is not an existing file is read as
183
+ CSV text, so ``Table.from_csv(table.to_csv())`` round-trips the way
184
+ ``from_markdown`` and ``from_html`` already did with their writers.
181
185
 
182
186
  With ``has_header`` false, columns are named automatically.
183
187
 
@@ -192,6 +196,10 @@ def from_csv(table_class, source, parse_numbers: bool = True,
192
196
  doublequote=doublequote, lineterminator=lineterminator,
193
197
  skipinitialspace=skipinitialspace, strict=strict,
194
198
  )
199
+ if isinstance(source, str) and ('\n' in source or '\r' in source):
200
+ import os
201
+ if not os.path.isfile(source):
202
+ source = _io.StringIO(source, newline='')
195
203
  if hasattr(source, 'read'):
196
204
  reader = _csv.reader(source, **reader_kwargs)
197
205
  rows = [row for row in reader]
@@ -952,19 +952,39 @@ class Table(object):
952
952
  self.__check_add_header_on_init(header)
953
953
  except TypeError:
954
954
  pass
955
+ if isinstance(rows, (str, bytes)):
956
+ raise TypeError(
957
+ 'Table() rows must be a sequence of rows, not '
958
+ f'{type(rows).__name__}'
959
+ )
955
960
  try:
956
- for row in rows:
961
+ rows_iter = iter(rows)
962
+ except TypeError:
963
+ rows_iter = None
964
+ if rows_iter is not None:
965
+ for row in rows_iter:
966
+ if isinstance(row, (str, bytes)):
967
+ raise TypeError(
968
+ 'Table() row entries must be sequences of cells, not '
969
+ f'{type(row).__name__}; wrap a single value in a list'
970
+ )
957
971
  try:
958
972
  row.__iter__
959
- self.add_row(data=list(row))
960
973
  except AttributeError:
961
- pass
962
- empty_to_add = len(headers) - len(rows[0])
963
- for _ in range(empty_to_add):
974
+ continue
975
+ self.add_row(data=list(row))
976
+ # Activate any headers the rows did not already fill. Use the
977
+ # live column count, not len(rows[0]): a short first row followed
978
+ # by a wider one used to spawn a ghost "column N" because the
979
+ # padding arithmetic ignored later rows that had already claimed
980
+ # the remaining headers.
981
+ try:
982
+ n_headers = len(headers)
983
+ except TypeError:
984
+ n_headers = 0
985
+ for _ in range(max(0, n_headers - self.__real_column_count)):
964
986
  self.add_column()
965
987
  return None
966
- except (TypeError, IndexError):
967
- pass
968
988
  try:
969
989
  for column in columns:
970
990
  try:
@@ -2292,7 +2312,7 @@ class Table(object):
2292
2312
 
2293
2313
  @show_empty_columns.setter
2294
2314
  def show_empty_columns(self, value: bool):
2295
- self.__show_empty_columns = value
2315
+ self.__show_empty_columns = bool(value)
2296
2316
 
2297
2317
  @property
2298
2318
  def hide_columns(self):
@@ -2640,8 +2660,20 @@ class Table(object):
2640
2660
  return value
2641
2661
 
2642
2662
  def __coerce_incoming_sequence(self, data):
2643
- """Copy ``data`` with ``__coerce_incoming_value`` applied to each cell."""
2644
- if data is None or not self.__parse_str_numbers:
2663
+ """Copy ``data`` with ``__coerce_incoming_value`` applied to each cell.
2664
+
2665
+ ``str`` and ``bytes`` are rejected: both are iterable, so a bare
2666
+ ``add_row('abc')`` or ``add_column('h', 'abc')`` used to expand into
2667
+ one cell per character. Pass a list/tuple of cells instead.
2668
+ """
2669
+ if data is None:
2670
+ return data
2671
+ if isinstance(data, (str, bytes)):
2672
+ raise TypeError(
2673
+ 'row/column data must be a sequence of cells, not '
2674
+ f'{type(data).__name__}; wrap a single value in a list'
2675
+ )
2676
+ if not self.__parse_str_numbers:
2645
2677
  return data
2646
2678
  return [self.__coerce_incoming_value(cell) for cell in data]
2647
2679
 
@@ -3359,7 +3391,7 @@ class Table(object):
3359
3391
  Add a row to the table. Can be left empty to add
3360
3392
  an empty row.
3361
3393
 
3362
- ``data``: Data provided as any iterable.
3394
+ ``data``: Sequence of cell values (list or tuple). Bare ``str``/``bytes`` are rejected.
3363
3395
 
3364
3396
  Example::
3365
3397
 
@@ -4015,6 +4047,19 @@ class Table(object):
4015
4047
 
4016
4048
  return header_to_trim, col_to_trim
4017
4049
 
4050
+ def __column_exceeds(self, column_i, columns, new_width) -> bool:
4051
+ """True when the header or any cell of the column is wider than
4052
+ ``new_width`` on some line, i.e. trimming would change it."""
4053
+ headers = self.__headers_with_i if self.show_index else self.__headers
4054
+ cells = [headers[column_i]] + list(columns[column_i])
4055
+ for cell in cells:
4056
+ if cell is None:
4057
+ continue
4058
+ for line in str(cell).split('\n'):
4059
+ if visible_width(line) > new_width:
4060
+ return True
4061
+ return False
4062
+
4018
4063
  def __wrap_or_trim_data(self,
4019
4064
  column_i: int,
4020
4065
  new_width: int,
@@ -4045,6 +4090,18 @@ class Table(object):
4045
4090
  new_width=new_width,
4046
4091
  )
4047
4092
  else:
4093
+ # Only a column that really loses characters turns into text.
4094
+ # Every column passes through here, including the ones a fit
4095
+ # or a column_max_width elsewhere left at full width, and
4096
+ # resetting those flipped right-aligned ints and the index to
4097
+ # the left.
4098
+ if not self.__column_exceeds(column_i, columns, new_width):
4099
+ return self.__trim_column(
4100
+ column_i=column_i,
4101
+ columns=columns,
4102
+ new_width=new_width,
4103
+ index=True
4104
+ )
4048
4105
  if index:
4049
4106
  self.__column_alignments_with_i[
4050
4107
  column_title
@@ -4388,7 +4445,10 @@ class Table(object):
4388
4445
  )
4389
4446
  new_width = widths[column_i] + extra
4390
4447
  if hi is not None:
4391
- new_width = min(new_width, int(hi))
4448
+ # Growing must never shrink: a column already past its cap
4449
+ # holds content that could not be cut (numbers), and pulling
4450
+ # it back to the cap leaves the cells wider than the rules.
4451
+ new_width = max(widths[column_i], min(new_width, int(hi)))
4392
4452
  widths[column_i] = new_width
4393
4453
  store[headers[column_i]] = new_width
4394
4454
 
@@ -4950,6 +5010,39 @@ class Table(object):
4950
5010
  header_row = data_rows.header_row
4951
5011
  body_rows = data_rows.body_rows
4952
5012
  gap_separator = None
5013
+ # Dividers in a style with no inter-row rule are drawn with that
5014
+ # style's own glyphs (the same rule merges synthesise), so they join
5015
+ # the column lines instead of crossing them as a run of ASCII dashes.
5016
+ divider_rule = None
5017
+ if self.__dividers and separators.table_body_line is None:
5018
+ from .merges import _frame_only_body_rule, _synthesize_body_rule
5019
+ composition = self.__style_composition
5020
+ under_header = composition.inferior_header_line
5021
+ if (under_header is not None and under_header.left == '|'
5022
+ and under_header.right == '|'):
5023
+ # Markdown-like styles (markdown, pipes, orgtbl): a rule in
5024
+ # the shape of their header rule, without alignment colons.
5025
+ synthesized = SeparatorLine(
5026
+ left='|', middle='-',
5027
+ intersection=under_header.intersection or '|',
5028
+ right='|',
5029
+ )
5030
+ else:
5031
+ synthesized = (
5032
+ _synthesize_body_rule(composition)
5033
+ or _frame_only_body_rule(composition)
5034
+ )
5035
+ if synthesized is not None:
5036
+ divider_rule = _get_separators(
5037
+ self.__style_composition._replace(
5038
+ table_body_line=synthesized
5039
+ ),
5040
+ column_widths_list,
5041
+ self.__empty_column_indexes,
5042
+ self.__show_empty_columns,
5043
+ column_alignments_list,
5044
+ user_hidden,
5045
+ ).table_body_line
4953
5046
  if colors_on:
4954
5047
  separators, gap_separator = self.__colour_horizontal_rules(
4955
5048
  separators,
@@ -4989,10 +5082,21 @@ class Table(object):
4989
5082
  # line through the middle of a table with no rows.
4990
5083
  body, body_line_map = self.__join_body_rows(
4991
5084
  body_rows, body_line, separators.table_body_line,
4992
- gap_separator=gap_separator,
5085
+ gap_separator=gap_separator, divider_rule=divider_rule,
4993
5086
  )
4994
- title_line = self.__title_line(column_widths_list, table_with_i)
4995
- caption_line = self.__caption_line(column_widths_list, table_with_i)
5087
+ # Centre title and caption on the lines actually drawn: the computed
5088
+ # width came out short for a table with no rows yet (an empty stream
5089
+ # or query), which pushed both to the left.
5090
+ drawn_width = max(
5091
+ (visible_width(line)
5092
+ for part in (superior_row, body, end_line) if part
5093
+ for line in part.split('\n')),
5094
+ default=None,
5095
+ )
5096
+ title_line = self.__title_line(column_widths_list, table_with_i,
5097
+ drawn_width)
5098
+ caption_line = self.__caption_line(column_widths_list, table_with_i,
5099
+ drawn_width)
4996
5100
 
4997
5101
  # Where the body starts depends on which of the parts before it were
4998
5102
  # drawn at all: a title, a top rule, a header. Counting them off the
@@ -5033,7 +5137,7 @@ class Table(object):
5033
5137
  return self.__apply_table_align(table_string)
5034
5138
 
5035
5139
  def __join_body_rows(self, body_rows, body_line, separator_line,
5036
- gap_separator=None):
5140
+ gap_separator=None, divider_rule=None):
5037
5141
  """
5038
5142
  Join body rows, inserting extra divider rules where requested.
5039
5143
 
@@ -5061,8 +5165,9 @@ class Table(object):
5061
5165
  cursor = 0
5062
5166
 
5063
5167
  # A style with no inter-row rule still gets the dividers ``add_divider``
5064
- # asked for, drawn as a dashed line. A style that rules between every
5065
- # row already shows them.
5168
+ # asked for: ``divider_rule`` in the style's glyphs when it has column
5169
+ # lines to join, otherwise a dashed line. A style that rules between
5170
+ # every row already shows them.
5066
5171
  explicit_dividers = (
5067
5172
  self.__dividers if (self.__dividers and separator_line is None)
5068
5173
  else set()
@@ -5072,10 +5177,14 @@ class Table(object):
5072
5177
  # Measure one physical line: a wrapped first row holds several,
5073
5178
  # and measuring the whole string drew a rule as wide as all of
5074
5179
  # them put together.
5075
- rule = '-' * max(
5180
+ row_width = max(
5076
5181
  max(visible_width(line) for line in body_rows[0].split('\n')),
5077
5182
  1,
5078
5183
  )
5184
+ if divider_rule and visible_width(divider_rule) == row_width:
5185
+ rule = divider_rule
5186
+ else:
5187
+ rule = '-' * row_width
5079
5188
 
5080
5189
  separator_lines = body_line.count('\n') if separator_line is not None else 0
5081
5190
 
@@ -5098,11 +5207,11 @@ class Table(object):
5098
5207
 
5099
5208
  return '\n'.join(pieces), line_map
5100
5209
 
5101
- def __title_line(self, column_widths_list, table_with_i):
5210
+ def __title_line(self, column_widths_list, table_with_i, drawn_width=None):
5102
5211
  """Centre the title over the full table width, if any."""
5103
5212
  if not self.__title:
5104
5213
  return None
5105
- table_width = self.__get_string_table_width()
5214
+ table_width = drawn_width or self.__get_string_table_width()
5106
5215
  title = self.__title
5107
5216
  title_width = visible_width(title)
5108
5217
  if title_width >= table_width:
@@ -5237,11 +5346,11 @@ class Table(object):
5237
5346
  return chr(10).join(out)
5238
5347
 
5239
5348
 
5240
- def __caption_line(self, column_widths_list, table_with_i):
5349
+ def __caption_line(self, column_widths_list, table_with_i, drawn_width=None):
5241
5350
  """Centre the caption under the full table width, if any."""
5242
5351
  if not self.__caption:
5243
5352
  return None
5244
- table_width = self.__get_string_table_width()
5353
+ table_width = drawn_width or self.__get_string_table_width()
5245
5354
  caption = self.__caption
5246
5355
  caption_width = visible_width(caption)
5247
5356
  if caption_width >= table_width:
@@ -5980,7 +6089,9 @@ class Table(object):
5980
6089
  ``overflow`` (truncate/wrap).
5981
6090
 
5982
6091
  Requires headers (or existing columns) so widths can be measured
5983
- before the first streamed row.
6092
+ before the first streamed row. With ``show_index`` the index column
6093
+ is sized for every row when ``rows`` has a length (a list, say); a
6094
+ plain generator sizes it from the rows already in the table.
5984
6095
 
5985
6096
  Lines look as ``compose()`` would draw them: formats, alignment,
5986
6097
  colours, ``row_filter``, dividers and merges carry over. ``sort_by``
@@ -6011,7 +6122,17 @@ class Table(object):
6011
6122
  # Measure natural widths, then pin min=max so later rows cannot
6012
6123
  # widen the table (beautifultable stream contract).
6013
6124
  work.compose()
6014
- widths = list(work.__column_widths_as_list)
6125
+ # By header, not the positional list: with show_index the data-only
6126
+ # list is left empty (the index layout fills the *_with_i one), and
6127
+ # the floors below indexed it by data column -- IndexError.
6128
+ width_map = (
6129
+ work.__column_widths_with_i if work.show_index
6130
+ else work.__column_widths
6131
+ )
6132
+ # An empty table with show_headers off measures 0; a cap of 0 is
6133
+ # invalid (ValueError), and a column needs at least one cell.
6134
+ widths = [max(1, int(width_map.get(header, 0) or 0))
6135
+ for header in work.headers]
6015
6136
  # Honour caller floors already set on self.
6016
6137
  if self.__column_min_width is not None:
6017
6138
  for i, header in enumerate(work.headers):
@@ -6027,13 +6148,44 @@ class Table(object):
6027
6148
  )
6028
6149
  if ceil is not None:
6029
6150
  widths[i] = min(widths[i], int(ceil))
6030
- work.column_min_width = list(widths)
6031
- work.column_max_width = list(widths)
6151
+ bounds = list(widths)
6152
+ if work.show_index:
6153
+ # The index column grows as rows arrive (9 -> 10 -> 100), so
6154
+ # reserve the widest label when the number of rows is known.
6155
+ # Unpinned it widened the lines after the first nine rows.
6156
+ index_width = max(1, int(width_map.get(work.__index_title, 0) or 0))
6157
+ try:
6158
+ total = work.__real_row_count + len(rows)
6159
+ except TypeError:
6160
+ total = None
6161
+ if total:
6162
+ labels = [work.index_start + work.index_step * k
6163
+ for k in (0, total - 1)]
6164
+ if work.roman_index:
6165
+ labels = [work.index_start + work.index_step * k
6166
+ for k in range(total)]
6167
+ labels = [to_roman(label) for label in labels]
6168
+ index_width = max(
6169
+ [index_width] + [visible_width(str(label))
6170
+ for label in labels]
6171
+ )
6172
+ bounds = dict(zip(work.headers, widths))
6173
+ bounds[work.__index_title] = index_width
6174
+ work.column_min_width = bounds
6175
+ work.column_max_width = bounds
6032
6176
 
6033
- n_close = self.__stream_closing_line_count(work, widths)
6177
+ n_close, closing = self.__stream_closing_lines(bounds)
6034
6178
  before = work.compose().splitlines()
6035
- open_part = before[:-n_close] if n_close else before
6036
- for line in open_part:
6179
+ # The open part of the empty table is yielded first, but only when
6180
+ # it ends in the same closing lines as a table with rows. A style or
6181
+ # setting that draws nothing for an empty body (no headers, no
6182
+ # frame) renders '' -- and its closing lines must not be assumed.
6183
+ tail = [strip_ansi(line) for line in before[len(before) - n_close:]]
6184
+ if n_close and (len(before) < n_close or tail != closing):
6185
+ yielded = 0
6186
+ else:
6187
+ yielded = len(before) - n_close
6188
+ for line in before[:yielded]:
6037
6189
  yield line
6038
6190
 
6039
6191
  streamed = []
@@ -6042,41 +6194,66 @@ class Table(object):
6042
6194
  streamed.append(row)
6043
6195
  work.add_row(row)
6044
6196
  after = work.compose().splitlines()
6045
- open_after = after[:-n_close] if n_close else after
6046
- open_before_len = len(before) - n_close if n_close else len(before)
6047
- for line in open_after[open_before_len:]:
6197
+ open_end = max(0, len(after) - n_close)
6198
+ for line in after[yielded:open_end]:
6048
6199
  yield line
6200
+ yielded = max(yielded, open_end)
6049
6201
  before = after
6050
6202
 
6051
- for line in (before[-n_close:] if n_close else []):
6203
+ for line in before[yielded:]:
6052
6204
  yield line
6053
6205
 
6054
6206
  if append and streamed:
6055
6207
  for row in streamed:
6056
6208
  self.add_row(row)
6057
6209
 
6058
- def __stream_closing_line_count(self, template, widths):
6059
- """
6060
- How many trailing lines are shared by empty-body and one-row tables
6061
- (bottom border + caption). Those are deferred until stream ends.
6062
- """
6063
- empty = self.__display_clone(include_rows=False)
6064
- one = self.__display_clone(include_rows=False)
6065
- empty.column_min_width = list(widths)
6066
- empty.column_max_width = list(widths)
6067
- one.column_min_width = list(widths)
6068
- one.column_max_width = list(widths)
6069
- placeholders = ['' for _ in empty.headers] or ['']
6070
- if empty.headers:
6071
- one.add_row(placeholders)
6072
- else:
6073
- return 0
6074
- la = empty.compose().splitlines()
6075
- lb = one.compose().splitlines()
6210
+ def __stream_closing_lines(self, widths):
6211
+ """
6212
+ ``(n, lines)``: the trailing lines a one-row and a two-row table
6213
+ share (bottom border, caption, panel and shadow), compared without
6214
+ colour. They are deferred until the stream ends.
6215
+
6216
+ Measured on tables that have rows: an empty one can render nothing
6217
+ at all, and zebra or cell backgrounds paint the rule under the last
6218
+ row, so comparing against the empty table (as this did) deferred
6219
+ either every line or none -- the bottom border then came out after
6220
+ the header and again after every row.
6221
+ """
6222
+ def remap(table):
6223
+ # Dict bounds name the index column by its title object, which
6224
+ # is per table: translate it to this clone's own.
6225
+ if not isinstance(widths, dict):
6226
+ return list(widths)
6227
+ out = {}
6228
+ for key, value in widths.items():
6229
+ if isinstance(key, IndexColumnTitle):
6230
+ key = table.__index_title
6231
+ out[key] = value
6232
+ return out
6233
+
6234
+ renders = []
6235
+ for count in (1, 2):
6236
+ probe = self.__display_clone(include_rows=False)
6237
+ if not probe.headers:
6238
+ return 0, []
6239
+ probe.column_min_width = remap(probe)
6240
+ probe.column_max_width = remap(probe)
6241
+ # Callables written for the caller's data may not accept the
6242
+ # placeholder; they do not change the frame being measured.
6243
+ probe.row_filter = None
6244
+ probe.color_rule = None
6245
+ probe.custom_format = None
6246
+ for _ in range(count):
6247
+ probe.add_row(['x' for _ in probe.headers])
6248
+ renders.append(
6249
+ [strip_ansi(line) for line in probe.compose().splitlines()]
6250
+ )
6251
+ one, two = renders
6076
6252
  n = 0
6077
- while n < len(la) and n < len(lb) and la[-(n + 1)] == lb[-(n + 1)]:
6253
+ while (n < len(one) - 1 and n < len(two)
6254
+ and one[-(n + 1)] == two[-(n + 1)]):
6078
6255
  n += 1
6079
- return n
6256
+ return n, one[len(one) - n:]
6080
6257
 
6081
6258
  # Display settings a derived table (stream, paginate) takes from its
6082
6259
  # parent. Each is global or keyed by column, so it means the same thing
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.2.2
3
+ Version: 2.2.4
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
@@ -39,6 +39,7 @@ tests/test_issues.py
39
39
  tests/test_merge_edge_matrix.py
40
40
  tests/test_merges.py
41
41
  tests/test_readme_examples.py
42
+ tests/test_stress_matrix.py
42
43
  tests/test_style_line_widths.py
43
44
  tests/test_styles.py
44
45
  tests/test_text_width.py
@@ -59,7 +60,10 @@ tests/test_v219_fixes.py
59
60
  tests/test_v220_exports.py
60
61
  tests/test_v221_fixes.py
61
62
  tests/test_v222_fixes.py
63
+ tests/test_v223_fixes.py
64
+ tests/test_v224_fixes.py
62
65
  tools/benchmark.py
63
66
  tools/generate_width_tables.py
64
67
  tools/readme_gallery.py
68
+ tools/stress_matrix.py
65
69
  tools/text_to_png.py
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexablelib",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "private": true,
5
5
  "description": "Flexible console and HTML tables for Python: styles, merges, colors, widths, decimal alignment.",
6
6
  "repository": {
@@ -416,8 +416,10 @@ def test_a_divider_above_a_merge_does_not_shift_it():
416
416
  table.merge_cells(1, 0, last_row=2, value='Down')
417
417
 
418
418
  lines = str(table).splitlines()
419
+ # Since 2.2.4 the divider is drawn in the style's glyphs (+---+---+);
420
+ # skip the top frame, which looks the same.
419
421
  divider_index = next(i for i, line in enumerate(lines)
420
- if set(line) == {'-'})
422
+ if i > 0 and set(line) == {'+', '-'})
421
423
 
422
424
  assert 'Down' in lines[divider_index + 2]
423
425
  assert 'Norte' in lines[divider_index - 1]