Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions MIGRATION
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ sections below for detailed migration examples and code samples.
_Detailed migration steps for the next version will be posted here._
<!-- END PLACEHOLDER - ADD NEW MIGRATION ENTRIES BELOW THIS LINE -->

### 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
Expand Down
16 changes: 12 additions & 4 deletions src/libtmux/neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand All @@ -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)
Expand All @@ -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]:
Expand Down
3 changes: 2 additions & 1 deletion src/libtmux/pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion src/libtmux/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
21 changes: 19 additions & 2 deletions tests/test_neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
_token_scope,
get_output_format,
)
from libtmux.pane import Pane

if t.TYPE_CHECKING:
from libtmux.session import Session
Expand Down Expand Up @@ -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

Expand All @@ -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
Loading