Skip to content

Conversation

@tony
Copy link
Member

@tony tony commented Feb 7, 2024

Extracted from #513

Warning

APIs below are subject to change (both params, return types, and structures)

Changes

New internal: OptionsMixin

  • show_options(): High level objects, e.g. preserving sparse-array data such as command-alias[1], command-alias[99], and inside of a dataclass object.

    command_alias: SparseArray({0: {'split-pane': 'split-window'})

  • _show_options(): Map of options split up by key, with raw values.

    command-alias[1] split-pane=split-window

  • _show_options_raw(): Raw stdout from tmux show-options

Individual options:

  • show_option(): High level objects, e.g. preserving sparse-array data such as command-alias[1], command-alias[99]

    Not sure if this will scale, but will try to get this:

    > show_option('command-alias')
    SparseArray({0: 'split-pane': 'split-window'})
    
    > show_option('command-alias')[0]
    'split-pane': 'split-window'
    
    > show_option('command-alias[0]')
    'split-pane': 'split-window'
    
  • _show_option():

  • _show_option_raw(): Raw stdout from tmux show-option [option name]

New internal: HooksMixin

New features

  • Window.set_option()

    Learned params:

    • format -> -F
    • unset -> -u
    • global -> -g
    • unset_panes -> -U: Also unset other panse in windows
    • prevent_overwrite: -o
    • suppress_warnings: -q
    • append: -a
  • Window.show_option()

  • Window.show_options()

Breaking changes

Deprecations

  • Deprecated Window.set_window_option() in favor of Window.set_option()
  • Deprecated Window.show_window_option() in favor of Window.show_option()
  • Deprecated Window.show_window_options() in favor of Window.show_options()

Summary by Sourcery

Refactor option and hook management, deprecating old methods.

New Features:

  • Add OptionsMixin to manage tmux options.
  • Add HooksMixin to manage tmux hooks.
  • Introduce new high-level option management methods: show_option(), show_options(), and set_option().
  • Add new high-level hook management methods: run_hook(), set_hook(), show_hooks(), show_hook(), and unset_hook().
  • Implement SparseArray to handle sparse arrays in options and hooks.

Tests:

  • Added tests for new option and hook management methods.

@tony tony force-pushed the improved-options branch from 6cd43f0 to 3399c7d Compare February 7, 2024 16:28
@codecov
Copy link

codecov bot commented Feb 7, 2024

Codecov Report

❌ Patch coverage is 54.65587% with 560 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.97%. Comparing base (0866211) to head (42add28).

Files with missing lines Patch % Lines
src/libtmux/_internal/constants.py 5.90% 237 Missing and 2 partials ⚠️
src/libtmux/options.py 58.54% 90 Missing and 24 partials ⚠️
src/libtmux/hooks.py 27.53% 80 Missing and 20 partials ⚠️
src/libtmux/_internal/waiter.py 85.75% 23 Missing and 23 partials ⚠️
src/libtmux/_internal/sparse_array.py 26.66% 11 Missing ⚠️
...examples/_internal/waiter/test_wait_until_ready.py 42.10% 11 Missing ⚠️
src/libtmux/window.py 40.00% 9 Missing ⚠️
src/libtmux/server.py 0.00% 8 Missing ⚠️
src/libtmux/session.py 0.00% 7 Missing ⚠️
tests/examples/_internal/waiter/helpers.py 36.36% 7 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #516      +/-   ##
==========================================
+ Coverage   46.24%   48.97%   +2.73%     
==========================================
  Files          19       38      +19     
  Lines        1875     3010    +1135     
  Branches      294      462     +168     
==========================================
+ Hits          867     1474     +607     
- Misses        893     1363     +470     
- Partials      115      173      +58     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tony tony mentioned this pull request Feb 7, 2024
@tony tony force-pushed the improved-options branch 4 times, most recently from e5c6186 to 37898a4 Compare February 8, 2024 12:35
@tony tony mentioned this pull request Feb 8, 2024
@tony tony force-pushed the improved-options branch 22 times, most recently from 97d74c1 to 36c5907 Compare February 8, 2024 18:54
tony added 21 commits October 25, 2025 09:51
…evel, "sparse array" objects and for those to set"

This reverts commit 9dc876d.
src/libtmux/window.py:848: error: Returning Any from function declared to return "dict[str, Any]"  [no-any-return]
- Update SparseArray type parameter to include all value types
- Add proper type checking for both dict and SparseArray cases
- Fix return type casting in _show_option method
- Resolve mypy errors related to string indexing of SparseArray
- Add version-specific assertions for terminal features
- Handle rxvt* features in tmux 3.4+
- Make test more robust by checking individual features
WHAT:
- Add comprehensive test coverage for complex option values and edge cases
- Add tests for terminal features edge cases including empty/malformed features
- Add tests for style validation and error handling
- Fix type safety issues in terminal features tests

WHY:
- Ensure proper handling of tmux's complex option types:
  * Empty feature lists (tmux returns [''] not [])
  * Malformed feature strings with colons
  * Features containing spaces and special chars
- Improve type safety by:
  * Adding proper type assertions for runtime checks
  * Using t.cast() to handle complex union types
  * Fixing string vs int indexing for SparseArray
- Make tests more maintainable by:
  * Breaking complex assertions into steps
  * Using descriptive variable names
  * Adding clear comments about tmux behavior
WHAT:
- Add type assertions for style option values
- Convert style values to strings before comparison
- Fix mypy errors in test_style_option_validation

WHY:
- Ensure type safety when handling style option values
- Prevent potential runtime errors from type mismatches
- Make test assertions more robust and explicit
WHAT:
- Add version check for status-format option (added in tmux 2.9)
- Update test assertions to handle version-specific behavior

WHY:
- Ensure tests pass on older tmux versions
- Properly handle version-specific options
- Improve test robustness across tmux versions
WHAT:
- Add version check for update-environment option (added in tmux 3.0)
- Update test assertions to handle version-specific behavior
- Add type checking for update-environment value

WHY:
- Ensure tests pass on older tmux versions
- Properly handle version-specific options
- Improve test robustness across tmux versions
@tony tony force-pushed the improved-options branch from 7bff126 to dd0438b Compare October 25, 2025 14:54
tony added 5 commits October 25, 2025 10:15
- Add command_error hook field (tmux 3.5+)
- Guard pane-colours test with version check (tmux 3.3+)
- Update terminal-overrides expectations for tmux 3.5
- Fix escape-time doctests to not rely on default value
Allow all matrix jobs to run even if one fails
Based on examination of tmux source code:

1. terminal-overrides defaults:
   - tmux 3.0a-3.1b: Have default values (xterm*, screen*)
   - tmux 3.2+: Defaults removed (commit 527f66ed, April 2020)

2. terminal-overrides parsing:
   - Fix split("=") to split("=", 1) to handle values containing "="
   - Example: "RGB=\E[...=%p1%d...]" was failing to parse

3. Style option behavior:
   - When bg=default is set, tmux stores bg as color 8 (default)
   - When converting to string, color 8 is omitted from output
   - Test was incorrectly expecting "bg=default" in output
   - This behavior is consistent across all tmux versions

Verification method:
- Created git worktrees for tmux 3.0a, 3.1b, 3.2a
- Examined options-table.c, style.c, colour.c in each version
- Tested terminal-overrides parsing with actual tmux commands

References:
- tmux-3.0a/options-table.c:219-223 (terminal-overrides defaults)
- tmux-3.2a/options-table.c:314-318 (empty defaults)
- tmux-3.0a/style.c:174-245 (style_tostring implementation)
- tmux-3.0a/style.c:231-235 (bg color 8 omitted)
Based on examination of tmux source code commit f03b6113 (May 2020):

In tmux 3.2, style options changed from OPTIONS_TABLE_STYLE to
OPTIONS_TABLE_STRING with OPTIONS_TABLE_IS_STYLE flag to support
format expansion in styles.

This changed how styles are stored and displayed:

**tmux ≤3.1 behavior:**
- Styles parsed and normalized when stored
- Output uses style_tostring() which normalizes:
  - "bold" → "bright"
  - bg=default (color 8) omitted from output
- Example: Input "fg=red,bg=default,bold" → Output "fg=red,bright"

**tmux ≥3.2 behavior:**
- Styles stored as literal strings (enables format expansion like #{...})
- Output shows the exact string that was set
- Example: Input "fg=red,bg=default,bold" → Output "fg=red,bg=default,bold"

Verification method:
- Created git worktrees for tmux 3.0a, 3.1b, 3.2a, 3.3a, 3.4, 3.5
- Examined source code changes across versions
- Tested directly with tmux commands to verify behavior

References:
- tmux commit f03b6113 (tmux 3.2 style type change)
- tmux-3.0a/options.c:126-127 (OPTIONS_IS_STYLE → style_tostring)
- tmux-3.0a/style.c:174-245 (style_tostring implementation)
- tmux-3.0a/attributes.c:26-50 (attributes_tostring: bold→bright)
tmux <3.2 normalizes 'bold' to 'bright' in style output.
Added version guard to expect correct output format.

Verification method:
- Created git worktrees for tmux versions 3.0a, 3.1b, 3.2a, 3.3a, 3.4, 3.5
- Examined source code to trace style handling across versions
- Tested directly with tmux commands to verify actual behavior

In tmux ≤3.1, styles are parsed and normalized when stored:
- attributes_fromstring() accepts both "bold" and "bright" as aliases
- attributes_tostring() always outputs "bright" (never "bold")

References:
- tmux-3.0a/attributes.c:32-50 (attributes_fromstring table)
- tmux-3.0a/attributes.c:26-28 (attributes_tostring output)
- tmux-3.0a/options.c:126-127 (OPTIONS_IS_STYLE → style_tostring)
- tmux commit f03b6113 (tmux 3.2 style format change)
@tony tony force-pushed the improved-options branch from 8f7b0e4 to 1a579e2 Compare October 25, 2025 15:59
tony added 2 commits October 25, 2025 11:06
Format expansion in style options (e.g., fg=#{...}) was added in
tmux 3.2 (commit f03b6113). Earlier versions reject styles with
format syntax as invalid.

Verification from tmux source code:
- Examined cmd-set-option.c in tmux 3.2 (commit f03b6113)
- Found validation logic: styles WITH #{...} skip validation
- Styles WITHOUT #{...} are still validated
- In tmux <3.2, ALL styles are validated, rejecting #{...} syntax

From commit f03b6113 message:
"Any styles without a '#{' are still validated when they are set
but any with a '#{' are not."

From cmd-set-option.c after f03b6113:
```c
if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
    strstr(value, "#{") == NULL &&
    style_parse(&sy, &grid_default_cell, value) != 0) {
```

The test now only runs the format expansion check on tmux 3.2+.

References:
- tmux commit f03b6113 (style format expansion support)
- cmd-set-option.c validation logic with strstr check
Fixes test_wait_for_pane_content_exact_match_detailed failure on tmux 3.1b
where capture_pane() was called immediately after send_keys() before the
content had been flushed/rendered by tmux.

The race condition manifested as:
  AssertionError: assert 'UNIQUE_TEST_STRING_123' in ''

Adding a 0.1s delay after send_keys() allows tmux to flush the content
before capture_pane() is called, fixing the timing issue on older versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant