Skip to content

docs(traversal) Add more doctests #567

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

Merged
merged 2 commits into from
Feb 15, 2025
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
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Add `TestServer` pytest fixture for creating temporary tmux servers (#565):
- Includes comprehensive test coverage and documentation
- Available in doctest namespace

### Documentation

- Fix links to the "Topics" section
- More docs for "Traversal" Topic (#567)

## libtmux 0.42.1 (2024-02-15)

### Bug fixes
Expand Down
148 changes: 113 additions & 35 deletions docs/topics/traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,159 @@ Terminal two, `python` or `ptpython` if you have it:
$ python
```

Import `libtmux`:
## Setup

First, create a test session:

```python
>>> session = server.new_session() # Create a test session using existing server
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Minor typo: Add "the" before "existing server".

The comment should read "# Create a test session using the existing server".

Suggested change
>>> session = server.new_session() # Create a test session using existing server
>>> session = server.new_session() # Create a test session using the existing server

```

## Server Level

View the server's representation:

```python
>>> server # doctest: +ELLIPSIS
Server(socket_name=...)
```

Get all sessions in the server:

```python
import libtmux
>>> server.sessions # doctest: +ELLIPSIS
[Session($... ...)]
```

Attach default tmux {class}`~libtmux.Server` to `t`:
Get all windows across all sessions:

```python
>>> import libtmux
>>> t = libtmux.Server();
>>> t
Server(socket_path=/tmp/tmux-.../default)
>>> server.windows # doctest: +ELLIPSIS
[Window(@... ..., Session($... ...))]
```

Get first session {class}`~libtmux.Session` to `session`:
Get all panes across all windows:

```python
>>> server.panes # doctest: +ELLIPSIS
[Pane(%... Window(@... ..., Session($... ...)))]
```

## Session Level

Get first session:

```python
>>> session = server.sessions[0]
>>> session
Session($1 ...)
>>> session # doctest: +ELLIPSIS
Session($... ...)
```

Get a list of sessions:
Get windows in a session:

```python
>>> server.sessions
[Session($1 ...), Session($0 ...)]
>>> session.windows # doctest: +ELLIPSIS
[Window(@... ..., Session($... ...))]
```

Iterate through sessions in a server:
Get active window and pane:

```python
>>> for sess in server.sessions:
... print(sess)
Session($1 ...)
Session($0 ...)
>>> session.active_window # doctest: +ELLIPSIS
Window(@... ..., Session($... ...))

>>> session.active_pane # doctest: +ELLIPSIS
Pane(%... Window(@... ..., Session($... ...)))
```

Grab a {class}`~libtmux.Window` from a session:
## Window Level

Get a window and inspect its properties:

```python
>>> session.windows[0]
Window(@1 ...:..., Session($1 ...))
>>> window = session.windows[0]
>>> window.window_index # doctest: +ELLIPSIS
'...'
```

Grab the currently focused window from session:
Access the window's parent session:

```python
>>> session.active_window
Window(@1 ...:..., Session($1 ...))
>>> window.session # doctest: +ELLIPSIS
Session($... ...)
>>> window.session.session_id == session.session_id
True
```

Grab the currently focused {class}`Pane` from session:
Get panes in a window:

```python
>>> session.active_pane
Pane(%1 Window(@1 ...:..., Session($1 ...)))
>>> window.panes # doctest: +ELLIPSIS
[Pane(%... Window(@... ..., Session($... ...)))]
```

Assign the attached {class}`~libtmux.Pane` to `p`:
Get active pane:

```python
>>> p = session.active_pane
>>> window.active_pane # doctest: +ELLIPSIS
Pane(%... Window(@... ..., Session($... ...)))
```

Access the window/server of a pane:
## Pane Level

Get a pane and traverse upwards:

```python
>>> p = session.active_pane
>>> p.window
Window(@1 ...:..., Session($1 ...))
>>> pane = window.panes[0]
>>> pane.window.window_id == window.window_id
True
>>> pane.session.session_id == session.session_id
True
>>> pane.server is server
True
```

## Filtering and Finding Objects

>>> p.server
Server(socket_name=libtmux_test...)
Find windows by index:

```python
>>> session.windows.filter(window_index=window.window_index) # doctest: +ELLIPSIS
[Window(@... ..., Session($... ...))]
```

Get a specific pane by ID:

```python
>>> window.panes.get(pane_id=pane.pane_id) # doctest: +ELLIPSIS
Pane(%... Window(@... ..., Session($... ...)))
```

## Checking Relationships

Check if objects are related:

```python
>>> window in session.windows
True
>>> pane in window.panes
True
>>> session in server.sessions
True
```

Check if a window is active:

```python
>>> window.window_id == session.active_window.window_id
True
```

Check if a pane is active:

```python
>>> pane.pane_id == window.active_pane.pane_id
True
```

[target]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS