Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Utilities #103

Closed
wants to merge 27 commits into from
Closed

Improvements to Utilities #103

wants to merge 27 commits into from

Conversation

AnonymouX47
Copy link
Owner

@AnonymouX47 AnonymouX47 commented Feb 1, 2024

  • Moves utility definitions into ._utils and exports only public ones from .utils.
  • Query results are now cached globally, not locally.
    • Adds ._utils.query_cache
    • Adds @._utils.cached_query
  • Query results are now cached only when queries are enabled.
  • Removes defunct (and unlikely to be used) utilities.
  • Tidies up active terminal derivation.
    • Adds NoActiveTerminalWarning, get_active_terminal().
    • NoActiveTerminalWarning is now issued instead of the more generic TermImageUserWarning, when there's no active terminal.
    • The active terminal is no longer determined at package load time.
  • Tidies up multi-process TTY access and cell size synchronization.
    • Adds TTYSyncProcess, NoMultiProcessSyncWarning.
  • Improves cell size computations
    • Adds CellSize.
    • Introduces fractional cell dimensions.
    • get_cell_size() no longer uses CSI 16 t to avoid premature rounding.
    • get_cell_size() now returns CellSize instead of .geometry.Size.
  • Tidies up terminal name & version derivation.
    • Adds NameVersion.
    • get_terminal_name_version() now returns NameVersion instead of a mere tuple.
  • Tidies up terminal FG & BG color derivation.
    • Adds .utils.get_fg_bg_colors() (to the public API).
    • get_fg_bg_colors() now returns Color instead of tuple | str.
    • Removes typing overloads.

- Add: `._utils` submodule.
- Change: Move definitions from `.utils` to `._utils`
- Change: Export only public utilities from `.utils`.
- Change: Update affected modules and tests.

  All modules now import from `._utils` instead of `.utils`.
- Add: `._utils.query_cache`.
- Add: `@._utils.cached_query`.
- Change: Decorate the following with `@cached_query` instead of
  `@cached`:

  - `._utils.get_terminal_name_version()`
  - `._utils.get_fg_bg_colors()`

- Change: Update query result cache invalidation in `.enable_queries()`.
- Fix: `UnboundLocalError` when ioctl fails and queries are disabled.
- Fix: Update `cell_size` when text area size is determined via query.
- Change: Compress query-based portion.
- Change: Cache query results only when queries are enabled.
- Change: Update `@cached_query` and `get_cell_size()` accordingly.
- Change: Remove no-longer-needed comment about invalidating cached
  query results.
- Change: Replace `@cached` with `@functools.lru_cache(maxsize=None)`.
- Change: Remove from `._utils`:

  - `@cached`
  - `@terminal_size_cached`
@AnonymouX47 AnonymouX47 added improvement Implementation of improvement to an existing feature utils Related to utilities labels Feb 1, 2024
@AnonymouX47 AnonymouX47 added this to the 0.8.0 milestone Feb 1, 2024
- Change: Remove long-commented code for active terminal
  synchronization workaround for `termios` functions.
- Add: `NoActiveTerminalWarning`, `get_active_terminal()`.
- Change: Move active terminal derivation code into
  `get_active_terminal()`.
- Change: `_tty_fd` is now initialized to `None` and updated by the
  first call to `get_active_terminal()`.
- Change: Update functions to use `get_active_terminal()` instead of
  `_tty_fd` directly.
- Change: `NoActiveTerminalWarning` is now issued instead of the more
  generic `TermImageUserWarning`, when there's no active terminal.
- Change: Update term description.
- Change: Update warning issued when there's no active terminal.
- Change: "terminal/TTY device" -> "PTY device".
- Add: `TTYSyncProcess`, `NoMultiProcessSyncWarning`.
- Change: Move multi-process TTY access and cell size synchronization
  into `TTYSyncProcess`.
- Change: The active terminal is no longer determined at module load
  time.
- Change: Update docstring of `@lock_tty`.
- Change: Update type hints.
- Change: Ignore `NoActiveTerminalWarning` directly since the
  active terminal is no longer determined at package load time.
- Fix: Eliminate race condition between threads for changing locks and
  cell size cache while starting a `TTYSyncProcess`.
- Change: Ensure the change of tty lock and cell size cache/lock are
  each attempted only once.
- Change: Clean up code.
- Add: Attempt to set the inheritable flag of active terminal file
  descriptor and pass on the file descriptor to the subprocess if
  successful.

  Useful for spawned (non-forked) processes.
- Add: `CellSize`.

  Uses `fractions.Fraction` for cell dimensions to allow and
  preserve possible fractional cell sizes.

- Change: `get_cell_size()` no longer uses `CSI 16 t`.

  This is to avoid early rounding as [@jerch](https://github.com/jerch)
  mentioned (on the chafa matrix chat) that cell sizes may be fractional
  in certain cases [on some TEs].

- Change: `get_cell_size()` now returns `CellSize` instead of `Size`.
- Change: `_cell_size_cache` is now an array of 6 integers, to allow for
  fractional cell sizes.
- Change: Update `term_image.set_cell_ratio()` and
  `term_image.get_cell_ratio()` to ensure they return the correct types.
- Change: Update `set_cell_size()` in the tests to use `CellSize`.
- Change: Update `GraphicsImage`'s size computation methods to:

  - keep return types consistent
  - round values late
  - remove redundant `math.ceil()`
- Add: `NameVersion` named tuple.
- Change: Now returns `NameVersion` instead of a mere tuple.
- Change: Use the return value as named tuple everywhere.
- Change: Update the docstring.
- Change: Correct the grammar in `arg_value_error_range()`'s message.
- Add: Make the function public; Export from `.utils`.
- Change: Now returns `Color` instead of `tuple | str`.
- Change: Remove typing overloads.
- Change: Update the use of the function all over.
- Change: Update the docstring.
Copy link

github-actions bot commented May 28, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/term_image
  __init__.py 29-30, 40
  _ctlseqs.py
  _utils.py 5-43, 52-80, 85-86, 92-97, 101-107, 114-136, 143, 157-208, 256-420, 428-436, 444, 452, 460-910
  color.py
  exceptions.py
  geometry.py
  padding.py
  utils.py 22
  src/term_image/image
  block.py
  common.py 1940
  iterm2.py
  kitty.py 302
  src/term_image/render
  _iterator.py
  src/term_image/renderable
  _renderable.py
  _types.py
  src/term_image/widget
  _urwid.py
Project Total  

This report was generated by python-coverage-comment-action

@AnonymouX47
Copy link
Owner Author

Superseded by #131.

My vision for this aspect of the API has changed drastically since ef094db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Implementation of improvement to an existing feature utils Related to utilities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant