cli_tui_wrap.py 1.1 KB

1234567891011121314151617181920
  1. """Row-wrapping helpers for the classic CLI's prompt_toolkit panels (moved from ``cli_tui_mixin``)."""
  2. def _wrap_rows(wrap, items, width, indent) -> list[tuple[int, str]]:
  3. """``(index, wrapped_line)`` pairs so selected styling can be re-applied per row."""
  4. return [(i, w) for i, label in enumerate(items) for w in wrap(label, width, subsequent_indent=indent)]
  5. def _prefix_wrapped_rows(wrap, label, width, first_prefix, indent) -> list[str]:
  6. """Wrap ``label``, then prefix the rows with ``first_prefix`` / ``indent``.
  7. The prefix is applied *after* wrapping on purpose. Folding it into the
  8. string handed to the wrapper charges those columns against the label's own
  9. width budget — so a selected row wraps one line early and strands the ``❯``
  10. cursor on a row of its own — and whitespace-trimming wrappers drop the
  11. leading indent entirely, leaving long unselected labels flush against the
  12. panel border, out of alignment with every other row.
  13. """
  14. rows = wrap(label, width)
  15. return [(first_prefix if i == 0 else indent) + row for i, row in enumerate(rows)]