diff --git a/CHANGES b/CHANGES index da57c9cda..60ba16680 100644 --- a/CHANGES +++ b/CHANGES @@ -47,6 +47,28 @@ _Notes on the upcoming release will go here._ ### Breaking changes +#### Empty tmux fields read as `""` (#900) + +A field tmux reports as empty, such as a cleared +{attr}`~libtmux.Pane.pane_title`, is now the empty string, and `refresh()` +picks the change up. `None` now means only that tmux was not asked for the +field, for example one newer than the running tmux. Both cases previously read +as `None`. + +Before: + +```python +if pane.pane_title is None: + ... +``` + +After: + +```python +if not pane.pane_title: + ... +``` + #### `with Server()` no longer kills the server on exit (#728) Leaving a `with` block previously killed the {class}`~libtmux.Server`, so diff --git a/MIGRATION b/MIGRATION index 4cde1544b..75ce49c64 100644 --- a/MIGRATION +++ b/MIGRATION @@ -116,6 +116,26 @@ sections below for detailed migration examples and code samples. _Detailed migration steps for the next version will be posted here._ +### Empty tmux fields read as `""` (#900) + +Fields that tmux reports as empty on {class}`~libtmux.Session`, +{class}`~libtmux.Window`, {class}`~libtmux.Pane`, and {class}`~libtmux.Client` +(such as a cleared {attr}`~libtmux.Pane.pane_title` or an unset +{attr}`~libtmux.Session.session_group`) now hydrate as `""` on initial queries +and after `refresh()`. `None` is reserved for format tokens that the listing +command or running tmux version did not request. + +Replace `is None` emptiness checks with truthiness checks: + +```python +>>> _ = pane.set_title('') +>>> pane.refresh() +>>> pane.pane_title +'' +>>> not pane.pane_title +True +``` + ### Server context managers preserve the daemon (#728) Before libtmux 0.69.0, leaving a {class}`~libtmux.Server` context killed the diff --git a/src/libtmux/neo.py b/src/libtmux/neo.py index e3ef15ec7..645ad1a65 100644 --- a/src/libtmux/neo.py +++ b/src/libtmux/neo.py @@ -1013,7 +1013,8 @@ def parse_output( Returns ------- OutputRaw - A dict mapping field names to non-empty string values. + A dict mapping every requested field name to its value. A field tmux + reports as empty is ``""``; a field that was not requested is absent. Examples -------- @@ -1029,7 +1030,15 @@ def parse_output( ... ) >>> result['session_id'] '$1' - >>> 'pane_id' in result + + A requested field tmux left empty is an empty string: + + >>> result['session_name'] + '' + + A field the listing does not request is absent: + + >>> 'client_name' in result False """ formats, _ = get_output_format(list_cmd, tmux_version) @@ -1039,8 +1048,7 @@ def parse_output( if values and values[-1] == "": values = values[:-1] - formatter = dict(zip(formats, values, strict=True)) - return {k: v for k, v in formatter.items() if v} + return dict(zip(formats, values, strict=True)) def _split_records(stdout: list[str], field_count: int) -> list[str]: diff --git a/src/libtmux/pane.py b/src/libtmux/pane.py index 9b104fee8..9ce595203 100644 --- a/src/libtmux/pane.py +++ b/src/libtmux/pane.py @@ -67,7 +67,8 @@ class Pane( between linked tmux objects. **Reading it.** Every field is a tmux format token read when the pane was - listed or last refreshed, and is ``None`` when tmux gave no value. + listed or last refreshed: ``""`` when tmux reports an empty value, and + ``None`` when the listing did not request the token. :meth:`~libtmux.Pane.refresh` rewrites the fields in place and raises :exc:`~libtmux.exc.TmuxObjectDoesNotExist` when the pane has gone. The edge properties convert tmux's flags to bools. diff --git a/src/libtmux/session.py b/src/libtmux/session.py index 00518644b..71ef52724 100644 --- a/src/libtmux/session.py +++ b/src/libtmux/session.py @@ -62,9 +62,10 @@ class Session( Holds :class:`Window` objects. **Reading it.** Every field is a tmux format token read when the session was - listed or last refreshed, and is ``None`` when tmux gave no value. - :meth:`~libtmux.Session.refresh` rewrites the fields in place and returns - ``None``. + listed or last refreshed: ``""`` when tmux reports an empty value, and + ``None`` when the listing did not request the token. + :meth:`~libtmux.Session.refresh` rewrites the fields in place and raises + :exc:`~libtmux.exc.TmuxObjectDoesNotExist` when the session has gone. - Identity: :attr:`~libtmux.Session.session_id`, :attr:`~libtmux.Session.id` diff --git a/src/libtmux/window.py b/src/libtmux/window.py index a34d147c4..ddcd815ad 100644 --- a/src/libtmux/window.py +++ b/src/libtmux/window.py @@ -63,7 +63,8 @@ class Window( Holds :class:`Pane` objects. **Reading it.** Every field is a tmux format token read when the window was - listed or last refreshed, and is ``None`` when tmux gave no value. + listed or last refreshed: ``""`` when tmux reports an empty value, and + ``None`` when the listing did not request the token. :meth:`~libtmux.Window.refresh` rewrites the fields in place and raises :exc:`~libtmux.exc.TmuxObjectDoesNotExist` when the window has gone. diff --git a/tests/test_neo.py b/tests/test_neo.py index 1d9cf298f..f4bc4d159 100644 --- a/tests/test_neo.py +++ b/tests/test_neo.py @@ -27,6 +27,7 @@ _token_scope, get_output_format, ) +from libtmux.pane import Pane if t.TYPE_CHECKING: from libtmux.session import Session @@ -334,7 +335,7 @@ def test_split_records_handles_no_objects() -> None: def test_refresh_clears_a_field_tmux_emptied(session: Session) -> None: - """A field tmux reports as empty stops holding its earlier value.""" + """A field tmux reports as empty reads ``""`` after a refresh.""" pane = session.active_window.active_pane assert pane is not None @@ -344,4 +345,20 @@ def test_refresh_clears_a_field_tmux_emptied(session: Session) -> None: pane.cmd("select-pane", "-T", "") pane.refresh() - assert pane.pane_title is None + assert pane.pane_title == "" + + +def test_empty_field_is_an_empty_string(session: Session) -> None: + """A field tmux reports as empty is ``""``; ``None`` means it was not asked.""" + window = session.active_window + pane = window.active_pane + assert pane is not None + + pane.set_title("titled") + pane.set_title("") + pane.refresh() + assert pane.pane_title == "" + + fresh = Pane.from_pane_id(server=session.server, pane_id=str(pane.pane_id)) + assert fresh.pane_title == "" + assert fresh.session_name == session.session_name