Skip to content

feat: Allow passing sleep to commands #749

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

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

#### What's new

- #749: Sleep before command execution, via `sleep: false` (per-pane only)

```yaml
session_name: Should not execute
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
sleep: 2
```

This will pause 2 seconds before execution

- #747: Skip execution via `enter: false`

```yaml
Expand Down
27 changes: 27 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,33 @@ Omit sending {kbd}`enter` to key commands. Equivalent to

````

## Pause / delay command execution

```{versionadded} 1.10.0bX
`sleep: [seconds]` option
```

Omit sending {kbd}`enter` to key commands. Equivalent to
[`time.sleep()`](time.sleep) before [`send_keys(enter=False)`](libtmux.Pane.send_keys).

````{tab} YAML

```{literalinclude} ../examples/sleep.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../examples/sleep.json
:language: json

```

````

## Window Index

You can specify a window's index using the `window_index` property. Windows
Expand Down
13 changes: 13 additions & 0 deletions examples/sleep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"session_name": "Should sleep 3 seconds before sending pane commands",
"windows": [
{
"panes": [
{
"shell_command": "echo \"___$((1 + 3))___\"",
"sleep": 3
}
]
}
]
}
5 changes: 5 additions & 0 deletions examples/sleep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
session_name: Should sleep 3 seconds before sending pane commands
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
sleep: 3
68 changes: 68 additions & 0 deletions tests/test_workspacebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,71 @@ def test_load_workspace_enter(
assert output in captured_pane
else:
assert output not in captured_pane


@pytest.mark.parametrize(
"yaml,output",
[
[
textwrap.dedent(
"""
session_name: Should not execute
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
sleep: 2
"""
),
"___4___",
],
[
textwrap.dedent(
"""
session_name: Should not execute
windows:
- panes:
- shell_command:
- echo "___$((1 + 3))___"
sleep: 2
"""
),
"___4___",
],
],
ids=[
"pane_sleep_3_shortform",
"pane_sleep_3_longform",
],
)
def test_load_workspace_sleep(
tmp_path: pathlib.Path,
server: libtmux.Server,
monkeypatch: pytest.MonkeyPatch,
yaml,
output,
):
yaml_config = tmp_path / "simple.yaml"
yaml_config.write_text(
yaml,
encoding="utf-8",
)
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(str(yaml_config)).get()
sconfig = config.expand(sconfig)
sconfig = config.trickle(sconfig)
builder = WorkspaceBuilder(sconf=sconfig, server=server)
builder.build()

t = time.process_time()

time.sleep(1)
session = builder.session
pane = session.attached_pane

while (time.process_time() - t) * 1000 < 2:
captured_pane = "\n".join(pane.capture_pane())

assert output not in captured_pane
time.sleep(0.1)
captured_pane = "\n".join(pane.capture_pane())
assert output in captured_pane
7 changes: 7 additions & 0 deletions tmuxp/workspacebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""
import logging
import time

from libtmux.exc import TmuxSessionExists
from libtmux.pane import Pane
Expand Down Expand Up @@ -363,8 +364,14 @@ def get_pane_shell():
suppress = True

enter = pconf.get("enter", True)
sleep = pconf.get("sleep", None)

for cmd in pconf["shell_command"]:
enter = cmd.get("enter", enter)
sleep = cmd.get("sleep", sleep)

if sleep is not None:
time.sleep(sleep)

p.send_keys(cmd["cmd"], suppress_history=suppress, enter=enter)

Expand Down